From ed52261d4fc8141525d6507ff153bbd7917623f7 Mon Sep 17 00:00:00 2001 From: Enzo Innocenzi Date: Thu, 12 Oct 2023 12:56:57 +0200 Subject: [PATCH] feat(range): support negative steps --- src/array.test.ts | 1 + src/array.ts | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/array.test.ts b/src/array.test.ts index f716c33..9ebc32a 100644 --- a/src/array.test.ts +++ b/src/array.test.ts @@ -30,6 +30,7 @@ it('range', () => { expect(range(2)).toEqual([0, 1]) expect(range(2, 5)).toEqual([2, 3, 4]) expect(range(2, 10, 2)).toEqual([2, 4, 6, 8]) + expect(range(3, 0, -1)).toEqual([3, 2, 1]) }) it('partition', () => { diff --git a/src/array.ts b/src/array.ts index 612ce18..ed49d22 100644 --- a/src/array.ts +++ b/src/array.ts @@ -154,7 +154,7 @@ export function range(...args: any): number[] { const arr: number[] = [] let current = start - while (current < stop) { + while (step > 0 ? (current < stop) : (current > stop)) { arr.push(current) current += step || 1 }