@tutao/tutanota-utils 3.94.0 → 3.94.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ArrayUtils.d.ts +39 -44
- package/dist/ArrayUtils.js +229 -220
- package/dist/AsyncResult.d.ts +15 -17
- package/dist/AsyncResult.js +21 -33
- package/dist/CollectionUtils.d.ts +1 -1
- package/dist/CollectionUtils.js +1 -1
- package/dist/DateUtils.d.ts +18 -18
- package/dist/DateUtils.js +38 -40
- package/dist/Encoding.d.ts +25 -25
- package/dist/Encoding.js +181 -175
- package/dist/LazyLoaded.d.ts +32 -32
- package/dist/LazyLoaded.js +66 -65
- package/dist/MapUtils.d.ts +4 -4
- package/dist/MapUtils.js +25 -24
- package/dist/MathUtils.d.ts +2 -2
- package/dist/MathUtils.js +2 -2
- package/dist/PromiseMap.d.ts +4 -8
- package/dist/PromiseMap.js +50 -49
- package/dist/PromiseUtils.d.ts +16 -19
- package/dist/PromiseUtils.js +76 -72
- package/dist/SortedArray.d.ts +11 -11
- package/dist/SortedArray.js +30 -30
- package/dist/StringUtils.d.ts +14 -14
- package/dist/StringUtils.js +36 -31
- package/dist/TypeRef.d.ts +11 -11
- package/dist/TypeRef.js +15 -15
- package/dist/Utils.d.ts +53 -59
- package/dist/Utils.js +227 -207
- package/dist/index.d.ts +19 -145
- package/dist/index.js +14 -140
- package/dist/tsbuildinfo +1 -1
- package/package.json +3 -2
package/dist/ArrayUtils.js
CHANGED
|
@@ -1,20 +1,20 @@
|
|
|
1
|
-
import { downcast, identity, neverNull } from "./Utils.js"
|
|
2
|
-
import { getFromMap } from "./MapUtils.js"
|
|
1
|
+
import { downcast, identity, neverNull } from "./Utils.js";
|
|
2
|
+
import { getFromMap } from "./MapUtils.js";
|
|
3
3
|
export function concat(...arrays) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
4
|
+
let length = arrays.reduce((previous, current) => previous + current.length, 0);
|
|
5
|
+
let result = new Uint8Array(length);
|
|
6
|
+
let index = 0;
|
|
7
|
+
arrays.forEach(array => {
|
|
8
|
+
result.set(array, index);
|
|
9
|
+
index += array.length;
|
|
10
|
+
});
|
|
11
|
+
return result;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
14
14
|
* Create an array filled with the numbers min..max (inclusive)
|
|
15
15
|
*/
|
|
16
16
|
export function numberRange(min, max) {
|
|
17
|
-
|
|
17
|
+
return [...Array(max + 1).keys()].slice(min);
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
20
|
* Compares two arrays for equality based on ===.
|
|
@@ -25,18 +25,18 @@ export function numberRange(min, max) {
|
|
|
25
25
|
* It is valid to compare Uint8Array to Array<T>, don't restrict it to be one type
|
|
26
26
|
*/
|
|
27
27
|
export function arrayEquals(a1, a2) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
28
|
+
if (a1 === a2) {
|
|
29
|
+
return true;
|
|
30
|
+
}
|
|
31
|
+
if (a1.length === a2.length) {
|
|
32
|
+
for (let i = 0; i < a1.length; i++) {
|
|
33
|
+
if (a1[i] !== a2[i]) {
|
|
34
|
+
return false;
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
return true;
|
|
38
|
+
}
|
|
39
|
+
return false;
|
|
40
40
|
}
|
|
41
41
|
/**
|
|
42
42
|
* Compares two arrays for equality based on a predicate
|
|
@@ -46,24 +46,24 @@ export function arrayEquals(a1, a2) {
|
|
|
46
46
|
* @returns {boolean}
|
|
47
47
|
*/
|
|
48
48
|
export function arrayEqualsWithPredicate(a1, a2, predicate) {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
49
|
+
if (a1.length === a2.length) {
|
|
50
|
+
for (let i = 0; i < a1.length; i++) {
|
|
51
|
+
if (!predicate(a1[i], a2[i])) {
|
|
52
|
+
return false;
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
return false;
|
|
58
58
|
}
|
|
59
59
|
export function arrayHash(array) {
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
60
|
+
let hash = 0;
|
|
61
|
+
hash |= 0;
|
|
62
|
+
for (let i = 0; i < array.length; i++) {
|
|
63
|
+
hash = (hash << 5) - hash + array[i];
|
|
64
|
+
hash |= 0; // Convert to 32bit integer
|
|
65
|
+
}
|
|
66
|
+
return hash;
|
|
67
67
|
}
|
|
68
68
|
/**
|
|
69
69
|
* Remove the element from theArray if it is contained in the array.
|
|
@@ -72,25 +72,26 @@ export function arrayHash(array) {
|
|
|
72
72
|
* @return True if the element was removed, false otherwise.
|
|
73
73
|
*/
|
|
74
74
|
export function remove(theArray, elementToRemove) {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
75
|
+
let i = theArray.indexOf(elementToRemove);
|
|
76
|
+
if (i !== -1) {
|
|
77
|
+
theArray.splice(i, 1);
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
82
83
|
}
|
|
83
84
|
/**
|
|
84
85
|
* Find all items in an array that pass the given predicate
|
|
85
86
|
*/
|
|
86
87
|
export function findAll(theArray, finder) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
88
|
+
const found = [];
|
|
89
|
+
for (let element of theArray) {
|
|
90
|
+
if (finder(element)) {
|
|
91
|
+
found.push(element);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return found;
|
|
94
95
|
}
|
|
95
96
|
/**
|
|
96
97
|
* @param theArray
|
|
@@ -98,48 +99,50 @@ export function findAll(theArray, finder) {
|
|
|
98
99
|
* @return {boolean} if the element was found
|
|
99
100
|
*/
|
|
100
101
|
export function findAndRemove(theArray, finder) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
102
|
+
const index = theArray.findIndex(finder);
|
|
103
|
+
if (index !== -1) {
|
|
104
|
+
theArray.splice(index, 1);
|
|
105
|
+
return true;
|
|
106
|
+
}
|
|
107
|
+
else {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
108
110
|
}
|
|
109
111
|
export function findAllAndRemove(theArray, finder, startIndex = 0) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
112
|
+
var removedElement = false;
|
|
113
|
+
for (let i = theArray.length - 1; i >= startIndex; i--) {
|
|
114
|
+
if (finder(theArray[i])) {
|
|
115
|
+
theArray.splice(i, 1);
|
|
116
|
+
removedElement = true;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return removedElement;
|
|
118
120
|
}
|
|
119
121
|
export function replace(theArray, oldElement, newElement) {
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
122
|
+
let i = theArray.indexOf(oldElement);
|
|
123
|
+
if (i !== -1) {
|
|
124
|
+
theArray.splice(i, 1, newElement);
|
|
125
|
+
return true;
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
return false;
|
|
129
|
+
}
|
|
127
130
|
}
|
|
128
131
|
/**
|
|
129
132
|
* Same as filterMap in some languages. Apply mapper and then only include non-nullable items.
|
|
130
133
|
*/
|
|
131
134
|
export function mapAndFilterNull(array, mapper) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
135
|
+
const resultList = [];
|
|
136
|
+
for (const item of array) {
|
|
137
|
+
const resultItem = mapper(item);
|
|
138
|
+
if (resultItem != null) {
|
|
139
|
+
resultList.push(resultItem);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
return resultList;
|
|
140
143
|
}
|
|
141
144
|
export function filterNull(array) {
|
|
142
|
-
|
|
145
|
+
return downcast(array.filter(item => item != null));
|
|
143
146
|
}
|
|
144
147
|
/**
|
|
145
148
|
* Provides the last element of the given array.
|
|
@@ -147,62 +150,62 @@ export function filterNull(array) {
|
|
|
147
150
|
* @return The last element of the array.
|
|
148
151
|
*/
|
|
149
152
|
export function last(theArray) {
|
|
150
|
-
|
|
153
|
+
return theArray[theArray.length - 1];
|
|
151
154
|
}
|
|
152
155
|
export function isEmpty(array) {
|
|
153
|
-
|
|
156
|
+
return array.length === 0;
|
|
154
157
|
}
|
|
155
158
|
export function lastThrow(array) {
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
159
|
+
if (isEmpty(array)) {
|
|
160
|
+
throw new RangeError("Array is empty");
|
|
161
|
+
}
|
|
162
|
+
return neverNull(last(array));
|
|
160
163
|
}
|
|
161
164
|
export function firstThrow(array) {
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
165
|
+
if (isEmpty(array)) {
|
|
166
|
+
throw new RangeError("Array is empty");
|
|
167
|
+
}
|
|
168
|
+
return array[0];
|
|
166
169
|
}
|
|
167
170
|
export function first(array) {
|
|
168
|
-
|
|
171
|
+
return array[0] || null;
|
|
169
172
|
}
|
|
170
173
|
export function findLast(array, predicate) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
174
|
+
const index = findLastIndex(array, predicate);
|
|
175
|
+
if (index !== -1) {
|
|
176
|
+
return array[index];
|
|
177
|
+
}
|
|
178
|
+
return null;
|
|
176
179
|
}
|
|
177
180
|
export function findLastIndex(array, predicate) {
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
181
|
+
for (let i = array.length - 1; i >= 0; i--) {
|
|
182
|
+
if (predicate(array[i])) {
|
|
183
|
+
return i;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return -1;
|
|
184
187
|
}
|
|
185
188
|
export function contains(theArray, elementToCheck) {
|
|
186
|
-
|
|
189
|
+
return theArray.indexOf(elementToCheck) !== -1;
|
|
187
190
|
}
|
|
188
191
|
export function addAll(array, elements) {
|
|
189
|
-
|
|
192
|
+
array.push(...elements);
|
|
190
193
|
}
|
|
191
194
|
export function removeAll(array, elements) {
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
+
elements.forEach(element => {
|
|
196
|
+
remove(array, element);
|
|
197
|
+
});
|
|
195
198
|
}
|
|
196
199
|
/**
|
|
197
200
|
* Group an array based on the given discriminator, but each group will have only unique items
|
|
198
201
|
*/
|
|
199
202
|
export function groupByAndMapUniquely(iterable, discriminator, mapper) {
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
203
|
+
const map = new Map();
|
|
204
|
+
for (let el of iterable) {
|
|
205
|
+
const key = discriminator(el);
|
|
206
|
+
getFromMap(map, key, () => new Set()).add(mapper(el));
|
|
207
|
+
}
|
|
208
|
+
return map;
|
|
206
209
|
}
|
|
207
210
|
/**
|
|
208
211
|
* convert an Array of T's into a Map of Arrays of E's by
|
|
@@ -214,12 +217,12 @@ export function groupByAndMapUniquely(iterable, discriminator, mapper) {
|
|
|
214
217
|
* @returns {Map<R, Array<E>>}
|
|
215
218
|
*/
|
|
216
219
|
export function groupByAndMap(iterable, discriminator, mapper) {
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
220
|
+
const map = new Map();
|
|
221
|
+
for (let el of iterable) {
|
|
222
|
+
const key = discriminator(el);
|
|
223
|
+
getFromMap(map, key, () => []).push(mapper(el));
|
|
224
|
+
}
|
|
225
|
+
return map;
|
|
223
226
|
}
|
|
224
227
|
/**
|
|
225
228
|
* Group array elements based on keys produced by a discriminator
|
|
@@ -228,7 +231,7 @@ export function groupByAndMap(iterable, discriminator, mapper) {
|
|
|
228
231
|
* @returns {NodeJS.Global.Map<R, Array<T>>}
|
|
229
232
|
*/
|
|
230
233
|
export function groupBy(iterable, discriminator) {
|
|
231
|
-
|
|
234
|
+
return groupByAndMap(iterable, discriminator, identity);
|
|
232
235
|
}
|
|
233
236
|
/**
|
|
234
237
|
* split an array into chunks of a given size.
|
|
@@ -238,22 +241,22 @@ export function groupBy(iterable, discriminator) {
|
|
|
238
241
|
* @returns {Array<Array<T>>}
|
|
239
242
|
*/
|
|
240
243
|
export function splitInChunks(chunkSize, array) {
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
244
|
+
if (chunkSize < 1) {
|
|
245
|
+
return [];
|
|
246
|
+
}
|
|
247
|
+
let chunkNum = 0;
|
|
248
|
+
const chunks = [];
|
|
249
|
+
let end;
|
|
250
|
+
do {
|
|
251
|
+
let start = chunkNum * chunkSize;
|
|
252
|
+
end = start + chunkSize;
|
|
253
|
+
chunks[chunkNum] = array.slice(start, end);
|
|
254
|
+
chunkNum++;
|
|
255
|
+
} while (end < array.length);
|
|
256
|
+
return chunks;
|
|
254
257
|
}
|
|
255
258
|
export function flat(arrays) {
|
|
256
|
-
|
|
259
|
+
return arrays.flat();
|
|
257
260
|
}
|
|
258
261
|
/**
|
|
259
262
|
* Maps an array into a nested array and then flattens it
|
|
@@ -262,12 +265,12 @@ export function flat(arrays) {
|
|
|
262
265
|
* @returns {T|*[]}
|
|
263
266
|
*/
|
|
264
267
|
export function flatMap(array, mapper) {
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
268
|
+
const result = [];
|
|
269
|
+
for (const item of array) {
|
|
270
|
+
const mapped = mapper(item);
|
|
271
|
+
result.push(...mapped);
|
|
272
|
+
}
|
|
273
|
+
return result;
|
|
271
274
|
}
|
|
272
275
|
/**
|
|
273
276
|
* Inserts element into the sorted array. Will find <b>the last</b> matching position.
|
|
@@ -279,39 +282,41 @@ export function flatMap(array, mapper) {
|
|
|
279
282
|
* @param replaceIf identity comparison for replacement
|
|
280
283
|
*/
|
|
281
284
|
export function insertIntoSortedArray(element, array, comparator, replaceIf = () => false) {
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
285
|
+
let i = 0;
|
|
286
|
+
while (i < array.length) {
|
|
287
|
+
const compareResult = comparator(array[i], element);
|
|
288
|
+
// We need to check for replacement for each element that is equal or we might miss it
|
|
289
|
+
if (compareResult === 0 && replaceIf(element, array[i])) {
|
|
290
|
+
array.splice(i, 1, element);
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
else if (compareResult <= 0) {
|
|
294
|
+
// We continue searching until the last suitable position
|
|
295
|
+
i++;
|
|
296
|
+
}
|
|
297
|
+
else {
|
|
298
|
+
break;
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
// This also handles empty array
|
|
302
|
+
array.splice(i, 0, element);
|
|
298
303
|
}
|
|
299
304
|
export function zip(arr1, arr2) {
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
+
const zipped = [];
|
|
306
|
+
for (let i = 0; i < Math.min(arr1.length, arr2.length); i++) {
|
|
307
|
+
zipped.push([arr1[i], arr2[i]]);
|
|
308
|
+
}
|
|
309
|
+
return zipped;
|
|
305
310
|
}
|
|
306
311
|
export function deduplicate(arr, comp = (a, b) => a === b) {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
312
|
+
const deduplicated = [];
|
|
313
|
+
arr.forEach(a => {
|
|
314
|
+
const isDuplicate = deduplicated.some(b => comp(a, b));
|
|
315
|
+
if (!isDuplicate) {
|
|
316
|
+
deduplicated.push(a);
|
|
317
|
+
}
|
|
318
|
+
});
|
|
319
|
+
return deduplicated;
|
|
315
320
|
}
|
|
316
321
|
/**
|
|
317
322
|
* http://jsfiddle.net/aryzhov/pkfst550/
|
|
@@ -328,33 +333,36 @@ export function deduplicate(arr, comp = (a, b) => a === b) {
|
|
|
328
333
|
* the returned value can be the index of any one of the equal elements.
|
|
329
334
|
*/
|
|
330
335
|
export function binarySearch(ar, el, compare_fn) {
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
336
|
+
var m = 0;
|
|
337
|
+
var n = ar.length - 1;
|
|
338
|
+
while (m <= n) {
|
|
339
|
+
var k = (n + m) >> 1;
|
|
340
|
+
var cmp = compare_fn(el, ar[k]);
|
|
341
|
+
if (cmp > 0) {
|
|
342
|
+
m = k + 1;
|
|
343
|
+
}
|
|
344
|
+
else if (cmp < 0) {
|
|
345
|
+
n = k - 1;
|
|
346
|
+
}
|
|
347
|
+
else {
|
|
348
|
+
return k;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
return -m - 1;
|
|
345
352
|
}
|
|
346
353
|
export function lastIndex(array) {
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
354
|
+
if (array.length === 0) {
|
|
355
|
+
return 0;
|
|
356
|
+
}
|
|
357
|
+
else {
|
|
358
|
+
return array.length - 1;
|
|
359
|
+
}
|
|
352
360
|
}
|
|
353
361
|
/**
|
|
354
362
|
* All of the elements in all of the arguments combined, and deduplicated
|
|
355
363
|
*/
|
|
356
364
|
export function union(...iterables) {
|
|
357
|
-
|
|
365
|
+
return new Set(...iterables.map(iterable => Array.from(iterable)));
|
|
358
366
|
}
|
|
359
367
|
/**
|
|
360
368
|
* return a new array containing every item from array1 that isn't in array2
|
|
@@ -364,7 +372,7 @@ export function union(...iterables) {
|
|
|
364
372
|
* @returns {Array<$NonMaybeType<T>>|Array<T>}
|
|
365
373
|
*/
|
|
366
374
|
export function difference(array1, array2, compare = (a, b) => a === b) {
|
|
367
|
-
|
|
375
|
+
return array1.filter(element1 => !array2.some(element2 => compare(element1, element2)));
|
|
368
376
|
}
|
|
369
377
|
/**
|
|
370
378
|
* Returns a set with elements that are *not* in both sets.
|
|
@@ -372,18 +380,18 @@ export function difference(array1, array2, compare = (a, b) => a === b) {
|
|
|
372
380
|
* {a, b, c} △ {b, c, d} == {a, d}
|
|
373
381
|
*/
|
|
374
382
|
export function symmetricDifference(set1, set2) {
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
383
|
+
const diff = new Set();
|
|
384
|
+
for (const el of set1) {
|
|
385
|
+
if (!set2.has(el)) {
|
|
386
|
+
diff.add(el);
|
|
387
|
+
}
|
|
388
|
+
}
|
|
389
|
+
for (const el of set2) {
|
|
390
|
+
if (!set1.has(el)) {
|
|
391
|
+
diff.add(el);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
return diff;
|
|
387
395
|
}
|
|
388
396
|
/**
|
|
389
397
|
* Splits an array into two based on a predicate, where elements that match the predicate go into the left side
|
|
@@ -391,16 +399,17 @@ export function symmetricDifference(set1, set2) {
|
|
|
391
399
|
* @param predicate
|
|
392
400
|
*/
|
|
393
401
|
export function partition(array, predicate) {
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
402
|
+
const left = [];
|
|
403
|
+
const right = [];
|
|
404
|
+
for (let item of array) {
|
|
405
|
+
if (predicate(item)) {
|
|
406
|
+
left.push(item);
|
|
407
|
+
}
|
|
408
|
+
else {
|
|
409
|
+
right.push(item);
|
|
410
|
+
}
|
|
411
|
+
}
|
|
412
|
+
return [left, right];
|
|
404
413
|
}
|
|
405
414
|
/**
|
|
406
415
|
* like partition(), but async
|
|
@@ -409,13 +418,13 @@ export function partition(array, predicate) {
|
|
|
409
418
|
* @param predicate
|
|
410
419
|
*/
|
|
411
420
|
export async function partitionAsync(array, predicate) {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
421
|
+
const mask = await Promise.all(array.map(predicate));
|
|
422
|
+
const [left, right] = partition(zip(mask, array), item => item[0]);
|
|
423
|
+
return [left.map(i => i[1]), right.map(i => i[1])];
|
|
415
424
|
}
|
|
416
425
|
/**
|
|
417
426
|
* Create an array with n elements by calling the provided factory
|
|
418
427
|
*/
|
|
419
428
|
export function arrayOf(n, factory) {
|
|
420
|
-
|
|
429
|
+
return numberRange(0, n - 1).map(factory);
|
|
421
430
|
}
|
package/dist/AsyncResult.d.ts
CHANGED
|
@@ -1,25 +1,23 @@
|
|
|
1
1
|
declare type StatePending<T> = {
|
|
2
|
-
|
|
3
|
-
|
|
2
|
+
status: "pending";
|
|
3
|
+
promise: Promise<T>;
|
|
4
|
+
};
|
|
4
5
|
declare type StateComplete<T> = {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
}
|
|
6
|
+
status: "complete";
|
|
7
|
+
result: T;
|
|
8
|
+
};
|
|
8
9
|
declare type StateFailure = {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
}
|
|
12
|
-
declare type AsyncResultState<T> = StatePending<T> | StateComplete<T> | StateFailure
|
|
13
|
-
declare type PromiseCallback<T> = (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void
|
|
10
|
+
status: "failure";
|
|
11
|
+
error: any;
|
|
12
|
+
};
|
|
13
|
+
declare type AsyncResultState<T> = StatePending<T> | StateComplete<T> | StateFailure;
|
|
14
14
|
/**
|
|
15
15
|
* Represents a resource that is either not ready, ready, or error
|
|
16
16
|
* Sort of fills a similar role to LazyLoaded, usage is more verbose but also more typesafe. maybe this should be reconciled.
|
|
17
17
|
*/
|
|
18
|
-
export declare class AsyncResult<T>
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
result(): T | null
|
|
23
|
-
static completed<T>(value: T): AsyncResult<T>
|
|
18
|
+
export declare class AsyncResult<T> {
|
|
19
|
+
_state: AsyncResultState<T>;
|
|
20
|
+
constructor(promise: Promise<T>);
|
|
21
|
+
state(): Readonly<AsyncResultState<T>>;
|
|
24
22
|
}
|
|
25
|
-
export {}
|
|
23
|
+
export {};
|