@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.
@@ -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
- 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
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
- return [...Array(max + 1).keys()].slice(min)
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
- 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
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
- 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
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
- 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
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
- let i = theArray.indexOf(elementToRemove)
76
- if (i !== -1) {
77
- theArray.splice(i, 1)
78
- return true
79
- } else {
80
- return false
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
- const found = []
88
- for (let element of theArray) {
89
- if (finder(element)) {
90
- found.push(element)
91
- }
92
- }
93
- return found
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
- const index = theArray.findIndex(finder)
102
- if (index !== -1) {
103
- theArray.splice(index, 1)
104
- return true
105
- } else {
106
- return false
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
- var removedElement = false
111
- for (let i = theArray.length - 1; i >= startIndex; i--) {
112
- if (finder(theArray[i])) {
113
- theArray.splice(i, 1)
114
- removedElement = true
115
- }
116
- }
117
- return removedElement
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
- let i = theArray.indexOf(oldElement)
121
- if (i !== -1) {
122
- theArray.splice(i, 1, newElement)
123
- return true
124
- } else {
125
- return false
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
- const resultList = []
133
- for (const item of array) {
134
- const resultItem = mapper(item)
135
- if (resultItem != null) {
136
- resultList.push(resultItem)
137
- }
138
- }
139
- return resultList
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
- return downcast(array.filter(item => item != null))
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
- return theArray[theArray.length - 1]
153
+ return theArray[theArray.length - 1];
151
154
  }
152
155
  export function isEmpty(array) {
153
- return array.length === 0
156
+ return array.length === 0;
154
157
  }
155
158
  export function lastThrow(array) {
156
- if (isEmpty(array)) {
157
- throw new RangeError("Array is empty")
158
- }
159
- return neverNull(last(array))
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
- if (isEmpty(array)) {
163
- throw new RangeError("Array is empty")
164
- }
165
- return array[0]
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
- return array[0] || null
171
+ return array[0] || null;
169
172
  }
170
173
  export function findLast(array, predicate) {
171
- const index = findLastIndex(array, predicate)
172
- if (index !== -1) {
173
- return array[index]
174
- }
175
- return null
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
- for (let i = array.length - 1; i >= 0; i--) {
179
- if (predicate(array[i])) {
180
- return i
181
- }
182
- }
183
- return -1
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
- return theArray.indexOf(elementToCheck) !== -1
189
+ return theArray.indexOf(elementToCheck) !== -1;
187
190
  }
188
191
  export function addAll(array, elements) {
189
- array.push(...elements)
192
+ array.push(...elements);
190
193
  }
191
194
  export function removeAll(array, elements) {
192
- elements.forEach(element => {
193
- remove(array, element)
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
- const map = new Map()
201
- for (let el of iterable) {
202
- const key = discriminator(el)
203
- getFromMap(map, key, () => new Set()).add(mapper(el))
204
- }
205
- return map
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
- const map = new Map()
218
- for (let el of iterable) {
219
- const key = discriminator(el)
220
- getFromMap(map, key, () => []).push(mapper(el))
221
- }
222
- return map
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
- return groupByAndMap(iterable, discriminator, identity)
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
- if (chunkSize < 1) {
242
- return []
243
- }
244
- let chunkNum = 0
245
- const chunks = []
246
- let end
247
- do {
248
- let start = chunkNum * chunkSize
249
- end = start + chunkSize
250
- chunks[chunkNum] = array.slice(start, end)
251
- chunkNum++
252
- } while (end < array.length)
253
- return chunks
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
- return arrays.flat()
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
- const result = []
266
- for (const item of array) {
267
- const mapped = mapper(item)
268
- result.push(...mapped)
269
- }
270
- return result
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
- let i = 0
283
- while (i < array.length) {
284
- const compareResult = comparator(array[i], element)
285
- // We need to check for replacement for each element that is equal or we might miss it
286
- if (compareResult === 0 && replaceIf(element, array[i])) {
287
- array.splice(i, 1, element)
288
- return
289
- } else if (compareResult <= 0) {
290
- // We continue searching until the last suitable position
291
- i++
292
- } else {
293
- break
294
- }
295
- }
296
- // This also handles empty array
297
- array.splice(i, 0, element)
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
- const zipped = []
301
- for (let i = 0; i < Math.min(arr1.length, arr2.length); i++) {
302
- zipped.push([arr1[i], arr2[i]])
303
- }
304
- return zipped
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
- const deduplicated = []
308
- arr.forEach(a => {
309
- const isDuplicate = deduplicated.some(b => comp(a, b))
310
- if (!isDuplicate) {
311
- deduplicated.push(a)
312
- }
313
- })
314
- return deduplicated
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
- var m = 0
332
- var n = ar.length - 1
333
- while (m <= n) {
334
- var k = (n + m) >> 1
335
- var cmp = compare_fn(el, ar[k])
336
- if (cmp > 0) {
337
- m = k + 1
338
- } else if (cmp < 0) {
339
- n = k - 1
340
- } else {
341
- return k
342
- }
343
- }
344
- return -m - 1
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
- if (array.length === 0) {
348
- return 0
349
- } else {
350
- return array.length - 1
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
- return new Set(...iterables.map(iterable => Array.from(iterable)))
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
- return array1.filter(element1 => !array2.some(element2 => compare(element1, element2)))
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
- const diff = new Set()
376
- for (const el of set1) {
377
- if (!set2.has(el)) {
378
- diff.add(el)
379
- }
380
- }
381
- for (const el of set2) {
382
- if (!set1.has(el)) {
383
- diff.add(el)
384
- }
385
- }
386
- return diff
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
- const left = []
395
- const right = []
396
- for (let item of array) {
397
- if (predicate(item)) {
398
- left.push(item)
399
- } else {
400
- right.push(item)
401
- }
402
- }
403
- return [left, right]
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
- const mask = await Promise.all(array.map(predicate))
413
- const [left, right] = partition(zip(mask, array), item => item[0])
414
- return [left.map(i => i[1]), right.map(i => i[1])]
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
- return numberRange(0, n - 1).map(factory)
429
+ return numberRange(0, n - 1).map(factory);
421
430
  }
@@ -1,25 +1,23 @@
1
1
  declare type StatePending<T> = {
2
- status: "pending"
3
- }
2
+ status: "pending";
3
+ promise: Promise<T>;
4
+ };
4
5
  declare type StateComplete<T> = {
5
- status: "complete"
6
- result: T
7
- }
6
+ status: "complete";
7
+ result: T;
8
+ };
8
9
  declare type StateFailure = {
9
- status: "failure"
10
- error: any
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> extends Promise<T> {
19
- private _state
20
- constructor(promiseOrCallback: PromiseCallback<T> | Promise<T>)
21
- state(): Readonly<AsyncResultState<T>>
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 {};