fix(slice): explicit zero end no longer returns the whole array#265
Open
spokodev wants to merge 1 commit into
Open
fix(slice): explicit zero end no longer returns the whole array#265spokodev wants to merge 1 commit into
spokodev wants to merge 1 commit into
Conversation
A slice with an explicit `0` end (e.g. `$[:0]`, `$[2:0]`) returned the entire array instead of an empty result. `end` was computed with `(parts[1] && Number.parseInt(parts[1])) || len`, so a parsed end of `0` is falsy and got replaced by the array length. Treat an omitted end (empty string) as the length and parse any explicit value, including 0.
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.
Problem
A slice with an explicit
0end returns the entire array instead of an empty result:For comparison,
$[:1]→[0]and$[2:1]→[]are already correct — only an explicit0end is mishandled. Per the Python slice semantics this library documents,arr[:0]is[].Cause
In
_slicethe end index is computed as:When
parts[1]is'0',Number.parseInt('0')is0, which is falsy, so|| lenreplaces the legitimate index0with the array length.The slice loc is gated upstream by
/^(-?\d*):(-?\d*):?(\d*)$/, soparts[1]is always either an empty string (end omitted) or a valid integer string. The fix defaults an omitted (empty) end tolenand parses any explicit value, including0:startandstepare intentionally left unchanged:start's falsy default is already0, andstep's|| 1is a deliberate guard against an infinitei += 0loop.This is the same
0-is-falsy class as the@propertyfix in #245.Verification
test/test.slice.js($[:0],$[2:0],$[0:0]→[]); fails before, passes after.start:end:stepshape on arrays of length 0–7 (4,608 cases): 264 divergences before → 0 after. All 264 wereend == 0cases; the fix introduces no new divergence.