Skip to content

Commit 657d135

Browse files
committed
feat(array): range
1 parent c8f1bb5 commit 657d135

File tree

2 files changed

+68
-5
lines changed

2 files changed

+68
-5
lines changed

src/array.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { flattenArrayable } from './array'
1+
import { flattenArrayable, range } from './array'
22

33
it('flattenArrayable', () => {
44
expect(flattenArrayable()).toEqual([])
@@ -8,3 +8,10 @@ it('flattenArrayable', () => {
88
expect(flattenArrayable([1, [1, 2]])).toEqual([1, 1, 2])
99
expect(flattenArrayable([1, [1, [2]]])).toEqual([1, 1, [2]])
1010
})
11+
12+
it('range', () => {
13+
expect(range(0)).toEqual([])
14+
expect(range(2)).toEqual([0, 1])
15+
expect(range(2, 5)).toEqual([2, 3, 4])
16+
expect(range(2, 10, 2)).toEqual([2, 4, 6, 8])
17+
})

src/array.ts

Lines changed: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { Arrayable, Nullable } from './types'
22

33
/**
4-
* Convert Arrayable<T> to Array<T>
4+
* Convert `Arrayable<T>` to `Array<T>`
5+
*
6+
* @category Array
57
*/
68
export function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
79
array = array || []
@@ -11,14 +13,18 @@ export function toArray<T>(array?: Nullable<Arrayable<T>>): Array<T> {
1113
}
1214

1315
/**
14-
* Convert Arrayable<T> to Array<T>
16+
* Convert `Arrayable<T>` to `Array<T>` and flat it
17+
*
18+
* @category Array
1519
*/
1620
export function flattenArrayable<T>(array?: Nullable<Arrayable<T | Array<T>>>): Array<T> {
1721
return toArray(array).flat(1) as Array<T>
1822
}
1923

2024
/**
2125
* Use rest arguments to merge arrays
26+
*
27+
* @category Array
2228
*/
2329
export function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T> {
2430
return args.flatMap(i => toArray(i))
@@ -27,6 +33,7 @@ export function mergeArrayable<T>(...args: Nullable<Arrayable<T>>[]): Array<T> {
2733
/**
2834
* Divide an array into two parts by a filter function.
2935
*
36+
* @category Array
3037
* @example const [odd, even] = partition([1, 2, 3, 4], i => i % 2 != 0)
3138
*/
3239
export function partition<T>(array: readonly T[], filter: (i: T, idx: number, arr: readonly T[]) => any) {
@@ -36,17 +43,34 @@ export function partition<T>(array: readonly T[], filter: (i: T, idx: number, ar
3643
return [pass, fail]
3744
}
3845

46+
/**
47+
* Unique an Array
48+
*
49+
* @category Array
50+
*/
3951
export function uniq<T>(array: readonly T[]): T[] {
4052
return Array.from(new Set(array))
4153
}
4254

55+
/**
56+
* Get last item
57+
*
58+
* @category Array
59+
*/
4360
export function last(array: readonly []): undefined
4461
export function last<T>(array: readonly T[]): T
4562
export function last<T>(array: readonly T[]): T | undefined {
4663
return at(array, -1)
4764
}
4865

66+
/**
67+
* Remove an item from Array
68+
*
69+
* @category Array
70+
*/
4971
export function remove<T>(array: T[], value: T) {
72+
if (!array)
73+
return false
5074
const index = array.indexOf(value)
5175
if (index >= 0) {
5276
array.splice(index, 1)
@@ -55,17 +79,49 @@ export function remove<T>(array: T[], value: T) {
5579
return false
5680
}
5781

82+
/**
83+
* Get nth item of Array. Negative for backward
84+
*
85+
* @category Array
86+
*/
5887
export function at(array: readonly [], index: number): undefined
5988
export function at<T>(array: readonly T[], index: number): T
6089
export function at<T>(array: readonly T[] | [], index: number): T | undefined {
6190
const len = array.length
6291
if (!len)
6392
return undefined
6493

65-
index = index % len
66-
6794
if (index < 0)
6895
index += len
6996

7097
return array[index]
7198
}
99+
100+
/**
101+
* Genrate a range array of numbers. The `stop` is exclusive.
102+
*
103+
* @category Array
104+
*/
105+
export function range(stop: number): number[]
106+
export function range(start: number, stop: number, step?: number): number[]
107+
export function range(...args: any): number[] {
108+
let start: number, stop: number, step: number
109+
110+
if (args.length === 1) {
111+
start = 0
112+
step = 1;
113+
([stop] = args)
114+
}
115+
else {
116+
([start, stop, step = 1] = args)
117+
}
118+
119+
const arr: number[] = []
120+
let current = start
121+
while (current < stop) {
122+
arr.push(current)
123+
current += step || 1
124+
}
125+
126+
return arr
127+
}

0 commit comments

Comments
 (0)