Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add slice method and slice notation #11

Merged
merged 8 commits into from
Dec 17, 2019
Merged

Add slice method and slice notation #11

merged 8 commits into from
Dec 17, 2019

Conversation

smelukov
Copy link
Member

@smelukov smelukov commented Aug 5, 2019

foo.sort().slice(0, 5) - get top 5 items

@lahmatiy
Copy link
Member

lahmatiy commented Aug 5, 2019

It would be nice to implement https://github.com/tc39/proposal-slice-notation/blob/master/README.md instead

@lahmatiy
Copy link
Member

lahmatiy commented Aug 5, 2019

Ok, we can merge it, when:

  • method can be applied to string values
  • method covered by tests

@smelukov smelukov changed the title Add slice method Add slice method and slice notation Aug 5, 2019
support array like values for slice
src/utils.js Outdated
@@ -24,9 +24,16 @@ function isRegExp(value) {
return toString.call(value) === '[object RegExp]';
}

function isArrayLike(value) {
return value &&
hasOwnProperty.call(value, 0) &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So empty string or array is not array like?

src/parser.js Outdated
['[ e : e : e ]', code`fn.slice(current, $2, $4, $6)`],
['[ : e ]', code`fn.slice(current, 0, $3, 1)`],
['[ : e : e ]', code`fn.slice(current, 0, $3, $5)`],
['[ e : ]', code`fn.slice(current, $2, current.length, 1)`],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My suggestion is to pass undefined instead of current.length and convert undefined to current.length inside slice() when it's appropriate.

src/parser.js Outdated
@@ -255,7 +255,16 @@ const grammar = {
['. SYMBOL ( arguments )', code`method.$2(/*@2*/current, $4)`],
['.. SYMBOL', code`fn.recursive(/*@2*/current, "$2")`],
['..( block )', code`fn.recursive(current, current => { $2 })`],
['.[ block ]', code`fn.filter(current, current => { $2 })`]
['.[ block ]', code`fn.filter(current, current => { $2 })`],
['[ e : e ]', code`fn.slice(current, $2, $4, 1)`],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try to collapse grammar, may be like that:

            ['[ sliceNotationComponent ]', code`fn.slice(current, 0, $2)`],
            ['[ sliceNotationComponent sliceNotationComponent ]', code`fn.slice(current, 0, $2, $3)`],
            ['[ e sliceNotationComponent ]', code`fn.slice(current, $2, $3)`],
            ['[ e sliceNotationComponent sliceNotationComponent ]', code`fn.slice(current, $2, $3, $4)`]
        ],

        sliceNotationComponent: [
            [':', code`undefined`],
            [': e', code`$2`]
        ],

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One more try

        queryRoot: [
            ...
            ['[ sliceNotation ]', code`fn.slice(current, $2)`]
        ],

        relativePath: [
            ...
            ['query [ sliceNotation ]', code`fn.slice($1, $3)`]
        ],

        sliceNotation: [
            ['sliceNotationComponent', code`0, $1`],
            ['sliceNotationComponent sliceNotationComponent', code`0, $1, $2`],
            ['e sliceNotationComponent', code`$1, $2`],
            ['e sliceNotationComponent sliceNotationComponent', code`$1, $2, $3`]
        ],

        sliceNotationComponent: [
            [':', code`undefined`],
            [': e', code`$2`]
        ],

sliceNotation can be changed to include [] into its syntaxes

@lahmatiy lahmatiy merged commit 347913f into master Dec 17, 2019
@lahmatiy lahmatiy deleted the smelukov-patch-1 branch December 24, 2019 22:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants