From 6d6b79ba587a4b8415411da7ab482a0e061db80e Mon Sep 17 00:00:00 2001 From: Alexander O'Mara Date: Mon, 16 Dec 2024 02:27:39 -0500 Subject: [PATCH] Improved at --- arr.test.ts | 2 +- arr.ts | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/arr.test.ts b/arr.test.ts index ba68082..0971136 100644 --- a/arr.test.ts +++ b/arr.test.ts @@ -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})`); diff --git a/arr.ts b/arr.ts index 43202f4..20a6585 100644 --- a/arr.ts +++ b/arr.ts @@ -148,9 +148,11 @@ export function array( } 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]; } }