-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
array.js
184 lines (168 loc) · 3.63 KB
/
array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
* Utility module to work with Arrays.
*
* @module array
*/
import * as set from './set.js'
/**
* Return the last element of an array. The element must exist
*
* @template L
* @param {ArrayLike<L>} arr
* @return {L}
*/
export const last = arr => arr[arr.length - 1]
/**
* @template C
* @return {Array<C>}
*/
export const create = () => /** @type {Array<C>} */ ([])
/**
* @template D
* @param {Array<D>} a
* @return {Array<D>}
*/
export const copy = a => /** @type {Array<D>} */ (a.slice())
/**
* Append elements from src to dest
*
* @template M
* @param {Array<M>} dest
* @param {Array<M>} src
*/
export const appendTo = (dest, src) => {
for (let i = 0; i < src.length; i++) {
dest.push(src[i])
}
}
/**
* Transforms something array-like to an actual Array.
*
* @function
* @template T
* @param {ArrayLike<T>|Iterable<T>} arraylike
* @return {T}
*/
export const from = Array.from
/**
* True iff condition holds on every element in the Array.
*
* @function
* @template ITEM
* @template {ArrayLike<ITEM>} ARR
*
* @param {ARR} arr
* @param {function(ITEM, number, ARR):boolean} f
* @return {boolean}
*/
export const every = (arr, f) => {
for (let i = 0; i < arr.length; i++) {
if (!f(arr[i], i, arr)) {
return false
}
}
return true
}
/**
* True iff condition holds on some element in the Array.
*
* @function
* @template S
* @template {ArrayLike<S>} ARR
* @param {ARR} arr
* @param {function(S, number, ARR):boolean} f
* @return {boolean}
*/
export const some = (arr, f) => {
for (let i = 0; i < arr.length; i++) {
if (f(arr[i], i, arr)) {
return true
}
}
return false
}
/**
* @template ELEM
*
* @param {ArrayLike<ELEM>} a
* @param {ArrayLike<ELEM>} b
* @return {boolean}
*/
export const equalFlat = (a, b) => a.length === b.length && every(a, (item, index) => item === b[index])
/**
* @template ELEM
* @param {Array<Array<ELEM>>} arr
* @return {Array<ELEM>}
*/
export const flatten = arr => fold(arr, /** @type {Array<ELEM>} */ ([]), (acc, val) => acc.concat(val))
/**
* @template T
* @param {number} len
* @param {function(number, Array<T>):T} f
* @return {Array<T>}
*/
export const unfold = (len, f) => {
const array = new Array(len)
for (let i = 0; i < len; i++) {
array[i] = f(i, array)
}
return array
}
/**
* @template T
* @template RESULT
* @param {Array<T>} arr
* @param {RESULT} seed
* @param {function(RESULT, T, number):RESULT} folder
*/
export const fold = (arr, seed, folder) => arr.reduce(folder, seed)
export const isArray = Array.isArray
/**
* @template T
* @param {Array<T>} arr
* @return {Array<T>}
*/
export const unique = arr => from(set.from(arr))
/**
* @template T
* @template M
* @param {ArrayLike<T>} arr
* @param {function(T):M} mapper
* @return {Array<T>}
*/
export const uniqueBy = (arr, mapper) => {
/**
* @type {Set<M>}
*/
const happened = set.create()
/**
* @type {Array<T>}
*/
const result = []
for (let i = 0; i < arr.length; i++) {
const el = arr[i]
const mapped = mapper(el)
if (!happened.has(mapped)) {
happened.add(mapped)
result.push(el)
}
}
return result
}
/**
* @template {ArrayLike<any>} ARR
* @template {function(ARR extends ArrayLike<infer T> ? T : never, number, ARR):any} MAPPER
* @param {ARR} arr
* @param {MAPPER} mapper
* @return {Array<MAPPER extends function(...any): infer M ? M : never>}
*/
export const map = (arr, mapper) => {
/**
* @type {Array<any>}
*/
const res = Array(arr.length)
for (let i = 0; i < arr.length; i++) {
res[i] = mapper(/** @type {any} */ (arr[i]), i, /** @type {any} */ (arr))
}
return /** @type {any} */ (res)
}