-
Notifications
You must be signed in to change notification settings - Fork 5
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
Conversation
It would be nice to implement https://github.com/tc39/proposal-slice-notation/blob/master/README.md instead |
Ok, we can merge it, when:
|
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) && |
There was a problem hiding this comment.
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)`], |
There was a problem hiding this comment.
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)`], |
There was a problem hiding this comment.
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`]
],
There was a problem hiding this comment.
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
foo.sort().slice(0, 5)
- get top 5 items