From 657d135e6d173b4e60a6b003d004b38dd76d3b05 Mon Sep 17 00:00:00 2001 From: Anthony Fu Date: Wed, 28 Apr 2021 18:15:49 +0800 Subject: [PATCH] feat(array): range --- src/array.test.ts | 9 ++++++- src/array.ts | 64 ++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 68 insertions(+), 5 deletions(-) diff --git a/src/array.test.ts b/src/array.test.ts index cc7a37a..5103120 100644 --- a/src/array.test.ts +++ b/src/array.test.ts @@ -1,4 +1,4 @@ -import { flattenArrayable } from './array' +import { flattenArrayable, range } from './array' it('flattenArrayable', () => { expect(flattenArrayable()).toEqual([]) @@ -8,3 +8,10 @@ it('flattenArrayable', () => { expect(flattenArrayable([1, [1, 2]])).toEqual([1, 1, 2]) expect(flattenArrayable([1, [1, [2]]])).toEqual([1, 1, [2]]) }) + +it('range', () => { + expect(range(0)).toEqual([]) + expect(range(2)).toEqual([0, 1]) + expect(range(2, 5)).toEqual([2, 3, 4]) + expect(range(2, 10, 2)).toEqual([2, 4, 6, 8]) +}) diff --git a/src/array.ts b/src/array.ts index 967a0b3..4ca20a1 100644 --- a/src/array.ts +++ b/src/array.ts @@ -1,7 +1,9 @@ import { Arrayable, Nullable } from './types' /** - * Convert Arrayable to Array + * Convert `Arrayable` to `Array` + * + * @category Array */ export function toArray(array?: Nullable>): Array { array = array || [] @@ -11,7 +13,9 @@ export function toArray(array?: Nullable>): Array { } /** - * Convert Arrayable to Array + * Convert `Arrayable` to `Array` and flat it + * + * @category Array */ export function flattenArrayable(array?: Nullable>>): Array { return toArray(array).flat(1) as Array @@ -19,6 +23,8 @@ export function flattenArrayable(array?: Nullable>>): /** * Use rest arguments to merge arrays + * + * @category Array */ export function mergeArrayable(...args: Nullable>[]): Array { return args.flatMap(i => toArray(i)) @@ -27,6 +33,7 @@ export function mergeArrayable(...args: Nullable>[]): Array { /** * Divide an array into two parts by a filter function. * + * @category Array * @example const [odd, even] = partition([1, 2, 3, 4], i => i % 2 != 0) */ export function partition(array: readonly T[], filter: (i: T, idx: number, arr: readonly T[]) => any) { @@ -36,17 +43,34 @@ export function partition(array: readonly T[], filter: (i: T, idx: number, ar return [pass, fail] } +/** + * Unique an Array + * + * @category Array + */ export function uniq(array: readonly T[]): T[] { return Array.from(new Set(array)) } +/** + * Get last item + * + * @category Array + */ export function last(array: readonly []): undefined export function last(array: readonly T[]): T export function last(array: readonly T[]): T | undefined { return at(array, -1) } +/** + * Remove an item from Array + * + * @category Array + */ export function remove(array: T[], value: T) { + if (!array) + return false const index = array.indexOf(value) if (index >= 0) { array.splice(index, 1) @@ -55,6 +79,11 @@ export function remove(array: T[], value: T) { return false } +/** + * Get nth item of Array. Negative for backward + * + * @category Array + */ export function at(array: readonly [], index: number): undefined export function at(array: readonly T[], index: number): T export function at(array: readonly T[] | [], index: number): T | undefined { @@ -62,10 +91,37 @@ export function at(array: readonly T[] | [], index: number): T | undefined { if (!len) return undefined - index = index % len - if (index < 0) index += len return array[index] } + +/** + * Genrate a range array of numbers. The `stop` is exclusive. + * + * @category Array + */ +export function range(stop: number): number[] +export function range(start: number, stop: number, step?: number): number[] +export function range(...args: any): number[] { + let start: number, stop: number, step: number + + if (args.length === 1) { + start = 0 + step = 1; + ([stop] = args) + } + else { + ([start, stop, step = 1] = args) + } + + const arr: number[] = [] + let current = start + while (current < stop) { + arr.push(current) + current += step || 1 + } + + return arr +}