Skip to content

Commit

Permalink
Improved at
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexanderOMara committed Dec 16, 2024
1 parent d84d76f commit 6d6b79b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion arr.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ Deno.test('array: at', () => {
[-1, -2, -3, -4, -5, -6, -7, -8, -9, -10],
[Infinity, -Infinity, NaN, Number.EPSILON, -Number.EPSILON],
[1.1, 0.5, -1.1, -0.5, -0],
['1', ''],
['1', '', 'invalid'],
].flat() as number[]
) {
assertStrictEquals(four.at(index), jsar.at(index), `at(${index})`);
Expand Down
8 changes: 5 additions & 3 deletions arr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,11 @@ export function array<T extends Type>(
}

public at(i: number): T | undefined {
i ||= 0;
i -= i % 1;
if ((i < 0 ? i += length : i) >= 0 && i < length) {
i = +i || 0;
if (
((i -= i % 1) < 0 ? i += length : i) >= 0 &&
i < length
) {
return this[i];
}
}
Expand Down

0 comments on commit 6d6b79b

Please sign in to comment.