Skip to content

Commit ba7115d

Browse files
authored
Merge pull request #190 from sylvainpolletvillard/arrayschema-with-negative-indexes
fix ArraySchema#with with negative indexes
2 parents 0b5efbb + 09383d0 commit ba7115d

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/types/custom/ArraySchema.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ export class ArraySchema<V = any> implements Array<V>, Collection<number, V> {
722722
//
723723
with(index: number, value: V): ArraySchema<V> {
724724
const copy = this.items.slice();
725+
// Allow negative indexing from the end
726+
if (index < 0) index += this.length;
725727
copy[index] = value;
726728
return new ArraySchema(...copy);
727729
}

test/ArraySchema.test.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1840,6 +1840,13 @@ describe("ArraySchema Tests", () => {
18401840
assert.strictEqual(undefined, arr.at(5));
18411841
assert.strictEqual(undefined, arr.at(-6));
18421842
});
1843+
1844+
it("#with()", () => {
1845+
const arr = new ArraySchema<number>(1, 2, 3, 4, 5);
1846+
assert.deepStrictEqual([1, 6, 3, 4, 5], arr.with(1, 6).toJSON());
1847+
assert.deepStrictEqual([1, 2, 3, 4, 7], arr.with(-1, 7).toJSON());
1848+
assert.deepStrictEqual([1, 2, 3, 8, 5], arr.with(-2, 8).toJSON());
1849+
});
18431850
})
18441851

18451852
describe("ArraySchema <-> Array type interchangability", () => {

0 commit comments

Comments
 (0)