Repository navigation
feat(slice): support negative steps like [::-1] - #288
Merged
Merged
Conversation
The slice selector only handled a positive step: the check that routes [start:end:step] to _slice allowed only digits after the second colon, so [::-1] never reached it, and a step of 0 was silently turned into 1. Slices now follow RFC 9535 section 2.3.4.2.2. A negative step walks backwards, with the last item as the default start and the first item as the default end, and a step of 0 selects nothing. Positive steps behave as before. Fixes JSONPath-Plus#176
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #176.
$[::-1]and any other slice with a negative step returned nothing. Two things were in the way. The regex that routes[start:end:step]to the slice code only allowed digits for the step, so[::-1]was never treated as a slice. And_sliceonly walked forwards. A step of0was also silently turned into1, so$[1:2:0]returned item 1.Slices now follow RFC 9535 section 2.3.4.2.2:
0selects nothing.I found it by running the official JSONPath compliance test suite for RFC 9535 against the library. Every one of the 14 failing slice cases in it was a negative step or a zero step, and all of them pass now (the other slice cases already did). The rest of the suite fails mostly on things this library does differently on purpose (
?@.awithout parentheses, the function extensions, and so on), so I left those alone.Tests: 16 new ones in
test.slice.js, the 14 compliance cases (13 negative steps, plus one that was already fine), reversing objects withresultType: 'path', and the zero step. 15 of them fail without the change and all pass with it.Checks: eslint,
tscand the c8 coverage check (100%) pass locally. On my machine (Windows) the twoclitests fail both with and without this change, becausebinisn't found by the shell they spawn, so I'm relying on CI for those. I didn't run the rollup build.