fix(Repeat): lastIndexOf returned size instead of size - 1#2227
Merged
jdeniau merged 1 commit intoJun 22, 2026
Merged
Conversation
Repeat#lastIndexOf returned this.size when the searched value matched,
but the last valid index of a Repeat of n elements is n - 1. For
example Repeat('x', 3).lastIndexOf('x') returned 3 instead of 2.
An empty Repeat (Repeat(value, 0)) also incorrectly reported the value
as present: both indexOf and lastIndexOf returned 0 even though the
collection has no elements. Both now return -1 when size is 0, matching
the documented contract and the behaviour of List/Range/Seq.
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.
Bug
Repeat#lastIndexOfreturnsthis.sizewhen the searched value matches, but the last valid index of aRepeatofnelements isn - 1.Every other indexed collection (
List,Range, genericSeq) and the nativeArrayreturnsize - 1; onlyRepeatwas off by one.While fixing this I noticed a related issue on empty repeats — both
indexOfandlastIndexOfreported the value as present even though the collection has no elements:lastIndexOf('missing')is documented to return-1when the value isn't found, and an empty collection contains nothing, so both should return-1.Fix
In
src/Repeat.js:lastIndexOfreturnsthis.size - 1(instead ofthis.size) on a match.indexOfandlastIndexOfguard onthis.size !== 0so an emptyRepeatreturns-1.The infinite case is unchanged:
Repeat(value)hassize === Infinity, sosize - 1is stillInfinity.Tests
Added regression tests to
__tests__/Repeat.tscovering the first/last index of a non-empty repeat and the empty-repeat case. They fail onmain(lastIndexOfreturns3instead of2; empty repeat returns0instead of-1) and pass with the fix. Full suite (798 tests) green, eslint + prettier clean.