-
Notifications
You must be signed in to change notification settings - Fork 0
/
array-utils.ts
396 lines (345 loc) · 10.5 KB
/
array-utils.ts
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/**
*
* @example
* const array = [{name: "Michael", age: 23}, {name: "Joachim", age: 15}, {name: "Enrico", age: 15}, {name: "Monica", age: 59}]
* const conditions = {age: 23, name: "Monica"}
* where(array, conditions); // [{name: "Michael", age: 23}, {name: "Enrico", age: 15}, {name: "Monica", age: 59}]
*/
export function where<T extends Record<string, unknown>>(array: T[], condition: Partial<T>): T[] {
if (!Array.isArray(array)) {
return array;
}
if (!condition || typeof condition !== "object") {
return [];
}
const result: T[] = [];
const conditionEntries = Object.entries(condition);
array.forEach((e) => {
const add = conditionEntries.some((conditionEntry) => e[conditionEntry[0] as keyof T] === conditionEntry[1]);
if (add) {
result[result.length] = e;
}
});
return result;
}
/**
* @example
* compareArrays(["a", "b", "c"], ["a", "b", "c"]) => true
* compareArrays([{v: "a"}, {v: "b"}, {v: "c"}], [{v: "a"}, {v: "b"}, {v: "c"}]) => false
* compareArrays([{v: "a"}, {v: "b"}, {v: "c"}], [{v: "a"}, {v: "b"}, {v: "c"}], (a, b) => a.v === b.v) => true
*/
export function compareArrays<T>(
prev: T[],
act: T[],
comparator: (a: T, b: T) => boolean = (a, b) => a === b,
): boolean {
if (prev.length !== act.length) {
return false;
}
for (let i = 0; i < prev.length; i++) {
if (!comparator(prev[i], act[i])) {
return false;
}
}
return true;
}
/**
* @example
* groupByLast([{a: "a", b: "A"}, {a: "b", b: "B"}, {a: "c", b: "C"}, {a: "a", b: "D"}], "a") ==> {a: {a: "a", b: "D"}, b: {a: "b", b: "B"}, c: {a: "c", b: "C"}}
*/
export function groupByLast<T, S extends keyof T>(arr: T[], key: S): { [k in S]: T } {
return arr.reduce((acc, curr) => Object.assign({}, acc, {[curr[key] as any]: curr}), {}) as { [k in S]: T };
}
/**
*
* @example
* analyzeArrayChanges(["a", "b", "c"], ["a", "b", "c"]) ==> {toAdd: [], toRemove: []}
* analyzeArrayChanges(["a", "b", "c"], ["b", "c", "d"]) ==> {toAdd: ["d"], toRemove: ["a"]}
*/
export function analyzeArrayChanges<T>(
prev: T[],
act: T[],
comparator: (a: T, b: T) => boolean = (a, b) => a === b,
): { toAdd: T[]; toRemove: T[] } {
const existingPrevIndices: { [key: number]: true } = {};
const toRemove: T[] = [];
const toAdd: T[] = [];
act.forEach((e) => {
const prevIndex = prev.findIndex((item) => comparator(e, item));
if (prevIndex < 0) {
toAdd.push(e);
} else {
existingPrevIndices[prevIndex] = true;
}
});
prev.forEach((e, i) => {
if (i in existingPrevIndices) {
return;
}
toRemove.push(e);
});
return {toAdd, toRemove};
}
/**
* Return sub array from array
*
* @deprecated use {@link Array.prototype.slice} instead
* @param array - input array
* @param minIndex - start index
* @param maxIndex - end index
* @returns final array
*/
export function subArray<T = any>(array: T[], minIndex = 0, maxIndex = array.length - 1): T[] {
if (!Array.isArray(array)) {
return array;
}
const result: T[] = [];
const final = array.length < maxIndex ? array.length - 1 : maxIndex;
for (let i = minIndex; i <= final; i++) {
result[result.length] = array[i];
}
return result;
}
/**
* Function return maximal value from numeric array
*
* @param array - array of numbers
* @returns maximal number from array
* @deprecated use {@link Math.max} instead
*/
export function max(array: number[]): number {
if (!Array.isArray(array)) {
return array;
}
if (array.length === 0) {
return 0;
}
return array.reduce((a, b) => a > b ? a : b);
}
/**
* Function return minimal value from numeric array
*
* @param array - array of numbers
* @returns minimal number from array
* @deprecated use {@link Math.min} instead
*/
export function min(array: number[]): number {
if (!Array.isArray(array)) {
return array;
}
if (array.length === 0) {
return 0;
}
return array.reduce((a, b) => a < b ? a : b);
}
/**
* Function return total value of all elements in numeric array
*
* @example
* sum([1, 2, 3, 4, 5]) => 15
* @param array - array of numbers
* @returns summary of all numbers in array
*/
export function sum(array: number[]): number {
if (!Array.isArray(array)) {
return array;
}
if (array.length === 0) {
return 0;
}
return array.reduce((a, b) => a + b);
}
/**
* Function returns average of numeric array given as input
*
* @example
* avg([1, 2, 3, 4, 5]) => 3
* @param array - array of numbers
* @returns average of all numbers in array
*/
export function avg(array: number[]): number {
if (!Array.isArray(array)) {
return array;
}
if (array.length === 0) {
return 0;
}
return array.reduce((a, b) => a + b) / array.length;
}
/**
* Function join array by delimiter and append prefix and postfix
*
* @example
* join(["a", "b", "c", "d"], "") => abcd
* join(["a", "b", "c", "d"], "=") => a=b=c=d
* join(["a", "b", "c", "d"], "=", ">>", "<<") => >>a=b=c=d<<
* @param array - not empty array
* @param delimiter - character used for join elements in array
* @param prefix - string append at the beginning of final string
* @param postfix - string append at the end of final string
* @returns final string
*/
export function join<T>(array: T[], delimiter: string, prefix = "", postfix = ""): string {
if (!Array.isArray(array)) {
return prefix + array + postfix;
}
return prefix + array.join(delimiter) + postfix;
}
/**
* Method returns last element from array or null if array is empty. If argument is not array, method returns argument
*
* @example
* getLast([]) => undefined
* getLast(["a", "b"]) => b
* getLast([5, 6]) => 6
* @param array - not empty array
* @returns last value from array
*/
export function getLast<T>(array: T[]): T | undefined {
if (!Array.isArray(array)) {
return array;
}
return array[array.length - 1];
}
/**
* Method returns random element from array
*
* @param array - not empty array
* @returns random value from array
*/
export function getRandomItem<T = unknown>(array: T[]): T | null {
if (!Array.isArray(array)) {
return array;
}
if (array.length === 0) {
return null;
}
return array[Math.floor(Math.random() * array.length)];
}
export function getNRandom<T>(args: T[], count: number): T[] {
if (!Array.isArray(args)) {
return args;
}
if (args.length === 0 || count === 0) {
return [];
}
if (args.length <= count) {
return args;
}
if (Math.random() < 2) {
throw new Error("Not implemented because of infinity loop");
}
const result = new Set<T>();
while (result.size <= count) {
const randomItem = getRandomItem<T>(args);
if (randomItem) {
result.add(randomItem);
}
}
return Array.from<T>(result);
}
/**
* Method return copy of array with only distinct elements
*
* @example
* makeUnique([5, 5, 3, 2, 1, 4, 5, 4]) ==> [5, 3, 2, 1, 4]
* makeUnique(["5", "5", "3", "2", "1", "4", "5", "4"]) ==> ["5", "3", "2", "1", "4"]
* @param array - array with duplicate elements
* @returns unique array
*/
export function makeUnique<T>(array: T[]): T[] {
if (!Array.isArray(array)) {
return array;
}
return Array.from(new Set<T>(array));
}
export function createFilledArray<T>(length: number, provider: ((index: number) => T) | T): T[] {
if (typeof provider === "function") {
return new Array<T | null>(length).fill(null).map((_, i) => (provider as (index: number) => T)(i));
}
return new Array<T>(length).fill(provider);
}
/**
* Combine 2 array each other
*/
export function eachOther<T>(arr: T[], callback: (a: T, b: T) => void): void {
arr.forEach((e, i) => {
for (let j = i + 1; j < arr.length; j++) {
callback(e, arr[j]);
}
});
}
// tslint:disable:no-for-each-push
export function mergeArrays2<S, T, R>(arr1: S[], arr2: T[], callback: (item1: S, item2: T) => R): R[] {
const result: R[] = [];
arr1.forEach((item1) => {
arr2.forEach((item2) => {
result.push(callback(item1, item2));
});
});
return result;
}
export function mergeArrays3<S, T, U, R>(arr1: S[], arr2: T[], arr3: U[], callback: (item1: S, item2: T, item3: U) => R): R[] {
const result: R[] = [];
arr1.forEach((item1) => {
arr2.forEach((item2) => {
arr3.forEach((item3) => {
result.push(callback(item1, item2, item3));
});
});
});
return result;
}
export function findArrayDiff<T>(arrA: T[], arrB: T[], comparator: (a: T, b: T) => number, merger?: (a: T, b: T) => T): { same: T[]; missingInA: T[]; missingInB: T[] } {
const sortedArrayA = [...arrA].sort(comparator);
const sortedArrayB = [...arrB].sort(comparator);
const same: T[] = [];
const missingInA: T[] = [];
const missingInB: T[] = [];
let i = 0;
let j = 0;
while (i < sortedArrayA.length || j < sortedArrayB.length) {
if (i === sortedArrayA.length) {
missingInA.push(sortedArrayB[j]);
j++;
continue;
}
if (j === sortedArrayB.length) {
missingInB.push(sortedArrayA[i]);
i++;
continue;
}
const comparatorResult = comparator(sortedArrayA[i], sortedArrayB[j]);
if (comparatorResult === 0) {
same.push(merger ? merger(sortedArrayA[i], sortedArrayB[j]) : sortedArrayA[i]);
i++;
j++;
} else if (comparatorResult < 0) {
missingInB.push(sortedArrayA[i]);
i++;
} else if (comparatorResult > 0) {
missingInA.push(sortedArrayB[j]);
j++;
}
}
return {same, missingInA, missingInB};
}
function _rowLength<T>(arr: T | T[] | T[][], fn: (a: number, b: number) => number, len0: number): number {
if (Array.isArray(arr)) {
if (Array.isArray(arr[0])) {
let len = len0;
for (const item of arr) {
len = fn(len, (item as T[]).length);
}
return len;
}
return arr.length;
}
return 0;
}
export function maxRowLength<T>(arr: T | T[] | T[][]): number {
return _rowLength(arr, Math.max, 0);
}
export function minRowLength<T>(arr: T | T[] | T[][]): number {
return _rowLength(arr, Math.min, Infinity);
}