@netwo/devkit 0.0.0-stage → 1.1.0
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/LICENSE +21 -0
- package/README.md +45 -2
- package/fesm2022/netwo-devkit-angular-forms.mjs +2125 -0
- package/fesm2022/netwo-devkit-angular-forms.mjs.map +1 -0
- package/fesm2022/netwo-devkit-angular.mjs +3782 -0
- package/fesm2022/netwo-devkit-angular.mjs.map +1 -0
- package/fesm2022/netwo-devkit.mjs +1910 -0
- package/fesm2022/netwo-devkit.mjs.map +1 -0
- package/package.json +55 -3
- package/types/netwo-devkit-angular-forms.d.ts +516 -0
- package/types/netwo-devkit-angular.d.ts +1670 -0
- package/types/netwo-devkit.d.ts +781 -0
|
@@ -0,0 +1,1910 @@
|
|
|
1
|
+
import { PhoneNumberUtil, PhoneNumber, PhoneNumberFormat } from 'google-libphonenumber';
|
|
2
|
+
import { Duration, DateTime } from 'luxon';
|
|
3
|
+
import { takeUntil, merge, switchMap, throwError, NEVER, from, take, shareReplay, map, mergeMap, concatMap, forkJoin, toArray, isObservable, of, scheduled, withLatestFrom, tap, Observable, pipe, filter, identity as identity$1, delay, fromEvent, startWith, distinctUntilChanged, EMPTY, finalize, noop, Subject, timer, fromEventPattern } from 'rxjs';
|
|
4
|
+
|
|
5
|
+
function isNotUndefined(obj) {
|
|
6
|
+
return obj !== undefined;
|
|
7
|
+
}
|
|
8
|
+
function isUndefined(obj) {
|
|
9
|
+
return obj === undefined;
|
|
10
|
+
}
|
|
11
|
+
function isNotNull(obj) {
|
|
12
|
+
return obj !== null;
|
|
13
|
+
}
|
|
14
|
+
function isNull(obj) {
|
|
15
|
+
return obj === null;
|
|
16
|
+
}
|
|
17
|
+
function isNotNullable(obj) {
|
|
18
|
+
return isNotNull(obj) && isNotUndefined(obj);
|
|
19
|
+
}
|
|
20
|
+
function isNullable(obj) {
|
|
21
|
+
return isNull(obj) || isUndefined(obj);
|
|
22
|
+
}
|
|
23
|
+
function isObject(obj) {
|
|
24
|
+
return typeof obj === 'object';
|
|
25
|
+
}
|
|
26
|
+
function isError(obj) {
|
|
27
|
+
return obj instanceof Error;
|
|
28
|
+
}
|
|
29
|
+
function isString(obj) {
|
|
30
|
+
return typeof obj === 'string';
|
|
31
|
+
}
|
|
32
|
+
function isNumber(obj) {
|
|
33
|
+
return typeof obj === 'number';
|
|
34
|
+
}
|
|
35
|
+
function isArray(obj) {
|
|
36
|
+
return Array.isArray(obj);
|
|
37
|
+
}
|
|
38
|
+
function isDate(obj) {
|
|
39
|
+
return obj instanceof Date;
|
|
40
|
+
}
|
|
41
|
+
function isStringArray(value) {
|
|
42
|
+
return isArray(value) && value.every(isString);
|
|
43
|
+
}
|
|
44
|
+
function isNumberArray(value) {
|
|
45
|
+
return isArray(value) && value.every(isNumber);
|
|
46
|
+
}
|
|
47
|
+
function isBoolArray(value) {
|
|
48
|
+
return isArray(value) && value.every(isBoolean);
|
|
49
|
+
}
|
|
50
|
+
function isBoolean(obj) {
|
|
51
|
+
return typeof obj === 'boolean';
|
|
52
|
+
}
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
54
|
+
function isFn(obj) {
|
|
55
|
+
return typeof obj === 'function';
|
|
56
|
+
}
|
|
57
|
+
function isKey(key, target) {
|
|
58
|
+
return key === target;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function parseNumber(value) {
|
|
62
|
+
const num = Number(value);
|
|
63
|
+
return Number.isNaN(num) ? undefined : num;
|
|
64
|
+
}
|
|
65
|
+
function coerceNumber(value) {
|
|
66
|
+
return isString(value) ? parseNumber(value) : value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const FIRSTNAME = [
|
|
70
|
+
'firstname',
|
|
71
|
+
'firstName',
|
|
72
|
+
'Firstname',
|
|
73
|
+
'FirstName',
|
|
74
|
+
'first_name',
|
|
75
|
+
];
|
|
76
|
+
const LASTNAME = [
|
|
77
|
+
'lastname',
|
|
78
|
+
'lastName',
|
|
79
|
+
'Lastname',
|
|
80
|
+
'LastName',
|
|
81
|
+
'last_name',
|
|
82
|
+
];
|
|
83
|
+
function coerceUserName(input) {
|
|
84
|
+
const firstnameMatch = FIRSTNAME.find((prop) => !!input[prop]);
|
|
85
|
+
const lastnameMatch = LASTNAME.find((prop) => !!input[prop]);
|
|
86
|
+
const firstname = (firstnameMatch && input[firstnameMatch]) ?? '';
|
|
87
|
+
const lastname = (lastnameMatch && input[lastnameMatch]) ?? '';
|
|
88
|
+
return { firstname, lastname };
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
class CircularQueue {
|
|
92
|
+
get length() {
|
|
93
|
+
return this.storage.length;
|
|
94
|
+
}
|
|
95
|
+
constructor(capacity = Infinity) {
|
|
96
|
+
this.capacity = capacity;
|
|
97
|
+
this.storage = [];
|
|
98
|
+
}
|
|
99
|
+
enqueue(item) {
|
|
100
|
+
if (this.length === this.capacity) {
|
|
101
|
+
const data = this.dequeue();
|
|
102
|
+
this.storage.push(item);
|
|
103
|
+
return data;
|
|
104
|
+
}
|
|
105
|
+
this.storage.push(item);
|
|
106
|
+
return undefined;
|
|
107
|
+
}
|
|
108
|
+
dequeue() {
|
|
109
|
+
return this.storage.shift();
|
|
110
|
+
}
|
|
111
|
+
toArray() {
|
|
112
|
+
return [...this.storage];
|
|
113
|
+
}
|
|
114
|
+
remove(item) {
|
|
115
|
+
const index = this.storage.indexOf(item);
|
|
116
|
+
if (index >= 0) {
|
|
117
|
+
this.storage.splice(index, 1);
|
|
118
|
+
return true;
|
|
119
|
+
}
|
|
120
|
+
return false;
|
|
121
|
+
}
|
|
122
|
+
clear() {
|
|
123
|
+
const items = [...this.storage];
|
|
124
|
+
this.storage.length = 0;
|
|
125
|
+
return items;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let instance;
|
|
130
|
+
/**
|
|
131
|
+
* Lazily returns the shared `PhoneNumberUtil` instance.
|
|
132
|
+
*
|
|
133
|
+
* Never call `PhoneNumberUtil.getInstance()` at module level: that call runs as
|
|
134
|
+
* soon as the module loads, which stops bundlers from tree-shaking
|
|
135
|
+
* google-libphonenumber (~600 kB) out of apps that never use phone helpers.
|
|
136
|
+
*/
|
|
137
|
+
function getPhoneNumberUtil() {
|
|
138
|
+
return (instance ??= PhoneNumberUtil.getInstance());
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function deepEqual(a, b, ...moreConditions) {
|
|
142
|
+
if (a === b)
|
|
143
|
+
return true;
|
|
144
|
+
if (a && b && typeof a == 'object' && typeof b == 'object') {
|
|
145
|
+
if (a.constructor !== b.constructor)
|
|
146
|
+
return false;
|
|
147
|
+
if (Array.isArray(a) && Array.isArray(b)) {
|
|
148
|
+
if (a.length != b.length)
|
|
149
|
+
return false;
|
|
150
|
+
for (let i = a.length; i-- !== 0;)
|
|
151
|
+
if (!deepEqual(a[i], b[i], ...moreConditions))
|
|
152
|
+
return false;
|
|
153
|
+
return true;
|
|
154
|
+
}
|
|
155
|
+
if (a instanceof Map && b instanceof Map) {
|
|
156
|
+
if (a.size !== b.size)
|
|
157
|
+
return false;
|
|
158
|
+
for (const i of a.entries())
|
|
159
|
+
if (!b.has(i[0]))
|
|
160
|
+
return false;
|
|
161
|
+
for (const i of a.entries())
|
|
162
|
+
if (!deepEqual(i[1], b.get(i[0]), ...moreConditions))
|
|
163
|
+
return false;
|
|
164
|
+
return true;
|
|
165
|
+
}
|
|
166
|
+
if (a instanceof Set && b instanceof Set) {
|
|
167
|
+
if (a.size !== b.size)
|
|
168
|
+
return false;
|
|
169
|
+
for (const i of a.entries())
|
|
170
|
+
if (!b.has(i[0]))
|
|
171
|
+
return false;
|
|
172
|
+
return true;
|
|
173
|
+
}
|
|
174
|
+
if (ArrayBuffer.isView(a) && ArrayBuffer.isView(b)) {
|
|
175
|
+
if (a.byteLength != b.byteLength)
|
|
176
|
+
return false;
|
|
177
|
+
const a1 = new Int8Array(a.buffer);
|
|
178
|
+
const b1 = new Int8Array(b.buffer);
|
|
179
|
+
for (let i = 0; i != a.byteLength; i++) {
|
|
180
|
+
if (a1[i] != b1[i])
|
|
181
|
+
return false;
|
|
182
|
+
}
|
|
183
|
+
return true;
|
|
184
|
+
}
|
|
185
|
+
if (a.valueOf !== Object.prototype.valueOf)
|
|
186
|
+
return a.valueOf() === b.valueOf();
|
|
187
|
+
if (a.toString !== Object.prototype.toString)
|
|
188
|
+
return a.toString() === b.toString();
|
|
189
|
+
const keys = Object.keys(a);
|
|
190
|
+
if (keys.length !== Object.keys(b).length)
|
|
191
|
+
return false;
|
|
192
|
+
for (const key of keys)
|
|
193
|
+
if (!Object.prototype.hasOwnProperty.call(b, key))
|
|
194
|
+
return false;
|
|
195
|
+
for (const key of keys) {
|
|
196
|
+
if (!deepEqual(a[key], b[key], ...moreConditions))
|
|
197
|
+
return false;
|
|
198
|
+
}
|
|
199
|
+
return true;
|
|
200
|
+
}
|
|
201
|
+
if (moreConditions.some((fn) => fn(a, b))) {
|
|
202
|
+
return true;
|
|
203
|
+
}
|
|
204
|
+
// true if both NaN, false otherwise
|
|
205
|
+
return a !== a && b !== b;
|
|
206
|
+
}
|
|
207
|
+
function withDuration() {
|
|
208
|
+
return (a, b) => {
|
|
209
|
+
if (isString(a) && isString(b)) {
|
|
210
|
+
const d1 = Duration.fromISO(a);
|
|
211
|
+
const d2 = Duration.fromISO(b);
|
|
212
|
+
if (d1.isValid && d2.isValid) {
|
|
213
|
+
return d1.toMillis() === d2.toMillis();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
return false;
|
|
217
|
+
};
|
|
218
|
+
}
|
|
219
|
+
function withPhoneNumber() {
|
|
220
|
+
return (a, b) => {
|
|
221
|
+
if (a instanceof PhoneNumber && b instanceof PhoneNumber) {
|
|
222
|
+
return (getPhoneNumberUtil().isNumberMatch(a, b) ===
|
|
223
|
+
PhoneNumberUtil.MatchType.EXACT_MATCH);
|
|
224
|
+
}
|
|
225
|
+
return false;
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const identity = (value) => value;
|
|
230
|
+
const defaultStringify = (value) => String(value);
|
|
231
|
+
const alwaysFalse = (_value) => false;
|
|
232
|
+
const alwaysTrue = (_value) => true;
|
|
233
|
+
|
|
234
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
235
|
+
const ASC_COMPARER = (v1, v2) => v1 - v2;
|
|
236
|
+
const DESC_COMPARER = (v1, v2) => v2 - v1;
|
|
237
|
+
function ascComparer(into = identity) {
|
|
238
|
+
return (v1, v2) => ASC_COMPARER(into(v1), into(v2));
|
|
239
|
+
}
|
|
240
|
+
function descComparer(into = identity) {
|
|
241
|
+
return (v1, v2) => DESC_COMPARER(into(v1), into(v2));
|
|
242
|
+
}
|
|
243
|
+
function localeComparer(options = {}) {
|
|
244
|
+
const { stringify = defaultStringify, ...collatorOptions } = options;
|
|
245
|
+
return (v1, v2) => stringify(v1).localeCompare(stringify(v2), undefined, collatorOptions);
|
|
246
|
+
}
|
|
247
|
+
function composeComparer(c1, c2, ...more) {
|
|
248
|
+
return (v1, v2) => [c1, c2, ...more].reduce((res, cur) => res || cur(v1, v2), 0);
|
|
249
|
+
}
|
|
250
|
+
const NUM_COMPARER_MAP = {
|
|
251
|
+
asc: ascComparer,
|
|
252
|
+
desc: descComparer,
|
|
253
|
+
};
|
|
254
|
+
function compareUsingTemplate(orderedArr, option) {
|
|
255
|
+
const { into = identity, type = 'asc' } = option ?? {};
|
|
256
|
+
return NUM_COMPARER_MAP[type]((value) => orderedArr.indexOf(into(value)));
|
|
257
|
+
}
|
|
258
|
+
/**
|
|
259
|
+
* Default binary comparer to match equality of two elements
|
|
260
|
+
* ATTENTION: considers two empty arrays equal
|
|
261
|
+
*
|
|
262
|
+
* @param v1 first element
|
|
263
|
+
* @param v2 second element
|
|
264
|
+
*/
|
|
265
|
+
const STRICT_COMPARER = (v1, v2) => v1 === v2 || bothEmpty(v1, v2);
|
|
266
|
+
function bothEmpty(v1, v2) {
|
|
267
|
+
return Array.isArray(v1) && Array.isArray(v2) && !v1.length && !v2.length;
|
|
268
|
+
}
|
|
269
|
+
const DEEP_EQUAL_COMPARER = (v1, v2) => deepEqual(v1, v2);
|
|
270
|
+
function strictComparer(into = identity) {
|
|
271
|
+
return (v1, v2) => STRICT_COMPARER(into(v1), into(v2));
|
|
272
|
+
}
|
|
273
|
+
function deepEqualComparer(into = identity) {
|
|
274
|
+
return (v1, v2) => DEEP_EQUAL_COMPARER(into(v1), into(v2));
|
|
275
|
+
}
|
|
276
|
+
function stringifyComparer(into = identity) {
|
|
277
|
+
return strictComparer((value) => JSON.stringify(into(value)));
|
|
278
|
+
}
|
|
279
|
+
function wrapNullishComparer(comparer) {
|
|
280
|
+
return (v1, v2) => isNullable(v1) || isNullable(v2) ? v1 === v2 : comparer(v1, v2);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function resolveValueOrFn(valueOrFn, ...args) {
|
|
284
|
+
return isFn(valueOrFn) ? valueOrFn(...args) : valueOrFn;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
const nodeSortComparer = (nodes) => ascComparer((node) => nodes.findIndex((flatNode) => node.id === flatNode.id));
|
|
288
|
+
class FlatTree {
|
|
289
|
+
constructor(options, nodes = []) {
|
|
290
|
+
this.options = options;
|
|
291
|
+
this.nodes = [...nodes].sort(options?.sortComparer);
|
|
292
|
+
}
|
|
293
|
+
// Factory method to create a new FlatTree instance
|
|
294
|
+
static create(options) {
|
|
295
|
+
return new FlatTree(options);
|
|
296
|
+
}
|
|
297
|
+
// Creates a new FlatTree instance with specific options
|
|
298
|
+
with(options) {
|
|
299
|
+
return new FlatTree(options, this.nodes);
|
|
300
|
+
}
|
|
301
|
+
// Adds a new node to the tree
|
|
302
|
+
add(id, value, parentId) {
|
|
303
|
+
const parentNode = parentId ? this.get(parentId) : undefined;
|
|
304
|
+
// Check if the parent node exists
|
|
305
|
+
if (parentId && !parentNode) {
|
|
306
|
+
throw new Error(`Parent node with id ${parentId} does not exist`);
|
|
307
|
+
}
|
|
308
|
+
// Calculate the level of the new node based on its parent
|
|
309
|
+
const level = parentNode ? parentNode.level + 1 : 0;
|
|
310
|
+
const newNode = { id, value, level, parentId };
|
|
311
|
+
return new FlatTree(this.options, [...this.nodes, newNode]);
|
|
312
|
+
}
|
|
313
|
+
// Adds new nodes to the tree
|
|
314
|
+
addAll(values) {
|
|
315
|
+
return values.reduce((newTree, [id, value, parentId]) => newTree.add(id, value, parentId), this);
|
|
316
|
+
}
|
|
317
|
+
// Removes a node from the tree
|
|
318
|
+
remove(id, options) {
|
|
319
|
+
const { removeParentIfEmpty = false } = options ?? {};
|
|
320
|
+
const descendants = this.getDescendants(id);
|
|
321
|
+
const nodesToRemove = [
|
|
322
|
+
id,
|
|
323
|
+
...descendants.map((descendant) => descendant.id),
|
|
324
|
+
];
|
|
325
|
+
// Remove nodes from the tree
|
|
326
|
+
const newNodes = this.nodes.filter((node) => !nodesToRemove.includes(node.id));
|
|
327
|
+
let updatedTree = new FlatTree(this.options, newNodes);
|
|
328
|
+
// Optionally remove the parent if it's empty after the removal
|
|
329
|
+
if (removeParentIfEmpty) {
|
|
330
|
+
const parentNode = this.getParent(id);
|
|
331
|
+
if (parentNode && updatedTree.getChildren(parentNode.id).length === 0) {
|
|
332
|
+
updatedTree = updatedTree.remove(parentNode.id, options);
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return updatedTree;
|
|
336
|
+
}
|
|
337
|
+
// Removes nodes from the tree
|
|
338
|
+
removeAll(ids, options) {
|
|
339
|
+
return ids.reduce((newTree, id) => newTree.remove(id, options), this);
|
|
340
|
+
}
|
|
341
|
+
// Updates the value of a node
|
|
342
|
+
update(id, changes) {
|
|
343
|
+
// Update the value of the specified node
|
|
344
|
+
const updatedNodes = this.nodes.map((node) => node.id === id
|
|
345
|
+
? { ...node, value: resolveValueOrFn(changes, node.value) }
|
|
346
|
+
: node);
|
|
347
|
+
return new FlatTree(this.options, updatedNodes);
|
|
348
|
+
}
|
|
349
|
+
// Updates the value of nodes
|
|
350
|
+
updateAll(payload) {
|
|
351
|
+
return payload.reduce((newTree, [id, changes]) => newTree.update(id, changes), this);
|
|
352
|
+
}
|
|
353
|
+
// Gets a node by its id
|
|
354
|
+
get(id) {
|
|
355
|
+
return this.nodes.find((node) => node.id === id);
|
|
356
|
+
}
|
|
357
|
+
// Gets the parent node of a given node
|
|
358
|
+
getParent(id) {
|
|
359
|
+
const node = this.get(id);
|
|
360
|
+
return node?.parentId ? this.get(node.parentId) : undefined;
|
|
361
|
+
}
|
|
362
|
+
// Gets the children nodes of a given node
|
|
363
|
+
getChildren(id) {
|
|
364
|
+
const node = this.get(id);
|
|
365
|
+
return node ? this.nodes.filter((n) => n.parentId === node.id) : [];
|
|
366
|
+
}
|
|
367
|
+
// Gets the ancestors of a given node
|
|
368
|
+
getAscendants(id) {
|
|
369
|
+
const node = this.get(id);
|
|
370
|
+
const ascendants = [];
|
|
371
|
+
// Traverse up the hierarchy until reaching the root
|
|
372
|
+
let currentId = node ? node.id : null;
|
|
373
|
+
while (currentId !== null) {
|
|
374
|
+
const parentNode = this.getParent(currentId);
|
|
375
|
+
if (parentNode) {
|
|
376
|
+
ascendants.push(parentNode);
|
|
377
|
+
currentId = parentNode.id;
|
|
378
|
+
}
|
|
379
|
+
else {
|
|
380
|
+
currentId = null;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return ascendants.reverse();
|
|
384
|
+
}
|
|
385
|
+
// Gets the descendants of a given node
|
|
386
|
+
getDescendants(id) {
|
|
387
|
+
const node = this.get(id);
|
|
388
|
+
const descendants = [];
|
|
389
|
+
const stack = node ? [...this.getChildren(node.id)] : [];
|
|
390
|
+
// Traverse down the hierarchy using a stack
|
|
391
|
+
while (stack.length) {
|
|
392
|
+
const current = stack.pop();
|
|
393
|
+
if (current) {
|
|
394
|
+
descendants.push(current);
|
|
395
|
+
stack.push(...this.getChildren(current.id));
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
return descendants;
|
|
399
|
+
}
|
|
400
|
+
// Gets the siblings of a given node
|
|
401
|
+
getSiblings(id) {
|
|
402
|
+
const node = this.get(id);
|
|
403
|
+
if (!node) {
|
|
404
|
+
return [];
|
|
405
|
+
}
|
|
406
|
+
const siblings = node.parentId
|
|
407
|
+
? this.getChildren(node.parentId)
|
|
408
|
+
: this.getAtLevel(0);
|
|
409
|
+
return siblings.filter((n) => n.id !== id);
|
|
410
|
+
}
|
|
411
|
+
// Gets nodes at a specific level in the tree
|
|
412
|
+
getAtLevel(level) {
|
|
413
|
+
return this.nodes.filter((node) => node.level === level);
|
|
414
|
+
}
|
|
415
|
+
// Get all leaf nodes in the flat tree
|
|
416
|
+
// A leaf node is a node that does not have any children
|
|
417
|
+
getLeaf() {
|
|
418
|
+
// Filter nodes that are leaves
|
|
419
|
+
const leafNodes = this.nodes.filter((node) => !this.nodes.some((child) => child.parentId === node.id));
|
|
420
|
+
return leafNodes;
|
|
421
|
+
}
|
|
422
|
+
// Get the deepest leaf nodes in the flat tree
|
|
423
|
+
// A leaf node is a node that does not have any children
|
|
424
|
+
getDeepestLeaf() {
|
|
425
|
+
// Get all leaf nodes
|
|
426
|
+
const leafNodes = this.getLeaf();
|
|
427
|
+
// Find the maximum level among the leaf nodes
|
|
428
|
+
const maxLevel = Math.max(...leafNodes.map((node) => node.level));
|
|
429
|
+
// Filter leaf nodes that are at the maximum level to get the deepest leaf nodes
|
|
430
|
+
const deepestLeafNodes = leafNodes.filter((node) => node.level === maxLevel);
|
|
431
|
+
return deepestLeafNodes;
|
|
432
|
+
}
|
|
433
|
+
// Builds a hierarchy tree starting from the top node
|
|
434
|
+
buildHierarchyFromTop(id) {
|
|
435
|
+
const node = this.get(id);
|
|
436
|
+
if (!node) {
|
|
437
|
+
return undefined;
|
|
438
|
+
}
|
|
439
|
+
return {
|
|
440
|
+
id: node.id,
|
|
441
|
+
value: node.value,
|
|
442
|
+
level: node.level,
|
|
443
|
+
children: this.buildHierarchyChildren(node.id),
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
// Builds a hierarchy tree starting from the bottom nodes
|
|
447
|
+
buildHierarchyFromBottom(ids, options) {
|
|
448
|
+
const { untilLevel = 0 } = options ?? {};
|
|
449
|
+
const nodeDictionary = {};
|
|
450
|
+
// Recursive function to fill the dictionary and build the hierarchy
|
|
451
|
+
const fillDictionary = (id) => {
|
|
452
|
+
const found = nodeDictionary[id];
|
|
453
|
+
if (found) {
|
|
454
|
+
return found;
|
|
455
|
+
}
|
|
456
|
+
const flatNode = this.get(id);
|
|
457
|
+
if (!flatNode) {
|
|
458
|
+
throw new Error(`Node with id ${id} not found`);
|
|
459
|
+
}
|
|
460
|
+
const node = {
|
|
461
|
+
id: flatNode.id,
|
|
462
|
+
value: flatNode.value,
|
|
463
|
+
level: flatNode.level,
|
|
464
|
+
children: [],
|
|
465
|
+
};
|
|
466
|
+
nodeDictionary[id] = node;
|
|
467
|
+
if (flatNode.parentId) {
|
|
468
|
+
const parentNode = fillDictionary(flatNode.parentId);
|
|
469
|
+
parentNode.children.push(node);
|
|
470
|
+
}
|
|
471
|
+
return node;
|
|
472
|
+
};
|
|
473
|
+
ids.forEach((id) => fillDictionary(id));
|
|
474
|
+
// Filter root nodes based on the specified limit level
|
|
475
|
+
// Maintaining the order based on their original appearance in the flat structure
|
|
476
|
+
return Object.values(nodeDictionary)
|
|
477
|
+
.sort(nodeSortComparer(this.nodes))
|
|
478
|
+
.filter((node) => node.level === untilLevel);
|
|
479
|
+
}
|
|
480
|
+
// Builds a hierarchy tree for a specific node
|
|
481
|
+
buildHierarchyChildren(id) {
|
|
482
|
+
const chidren = this.getChildren(id);
|
|
483
|
+
return chidren.map((child) => ({
|
|
484
|
+
id: child.id,
|
|
485
|
+
value: child.value,
|
|
486
|
+
level: child.level,
|
|
487
|
+
children: this.buildHierarchyChildren(child.id),
|
|
488
|
+
}));
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
function assertNonNullable(value, label) {
|
|
493
|
+
if (value === null || value === undefined) {
|
|
494
|
+
throw new Error(`Assertion failed: ${label} must be defined here`);
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
function assertValid(condition, message) {
|
|
498
|
+
if (!condition) {
|
|
499
|
+
throw new Error(message);
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
class LazyWeakMap {
|
|
504
|
+
constructor(factory) {
|
|
505
|
+
this.factory = factory;
|
|
506
|
+
this.map = new WeakMap();
|
|
507
|
+
}
|
|
508
|
+
get(key) {
|
|
509
|
+
if (!this.map.has(key)) {
|
|
510
|
+
const value = this.factory(key);
|
|
511
|
+
this.map.set(key, value);
|
|
512
|
+
}
|
|
513
|
+
const value = this.map.get(key);
|
|
514
|
+
assertNonNullable(value, 'value');
|
|
515
|
+
return value;
|
|
516
|
+
}
|
|
517
|
+
delete(key) {
|
|
518
|
+
return this.map.delete(key);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
const DEFAULT_EMPTY_STRING_VALUE = '–'; // en dash, not simple dash
|
|
523
|
+
function ignoreAccent(value) {
|
|
524
|
+
return value.normalize('NFD').replace(/[\u0300-\u036f]/g, '');
|
|
525
|
+
}
|
|
526
|
+
function normalize(value) {
|
|
527
|
+
return ignoreAccent(value.toLowerCase().trim());
|
|
528
|
+
}
|
|
529
|
+
function stringChunks(value, size) {
|
|
530
|
+
return [...stringChunksGenerator(value, size)];
|
|
531
|
+
}
|
|
532
|
+
function* stringChunksGenerator(value, n) {
|
|
533
|
+
for (let i = 0; i < value.length; i += n) {
|
|
534
|
+
yield value.slice(i, i + n);
|
|
535
|
+
}
|
|
536
|
+
}
|
|
537
|
+
function joinNonEmpty(...args) {
|
|
538
|
+
const lastArg = args[args.length - 1];
|
|
539
|
+
const hasOptions = isObject(lastArg) && 'sep' in lastArg;
|
|
540
|
+
const separator = hasOptions ? lastArg.sep : '';
|
|
541
|
+
const values = hasOptions ? args.slice(0, -1) : args;
|
|
542
|
+
return values.filter((value) => value !== '').join(separator);
|
|
543
|
+
}
|
|
544
|
+
function isNonEmpty(value) {
|
|
545
|
+
return isNotNullable(value) && value.trim() !== '';
|
|
546
|
+
}
|
|
547
|
+
function isEmpty(value) {
|
|
548
|
+
return !isNonEmpty(value);
|
|
549
|
+
}
|
|
550
|
+
function toNonEmpty(value) {
|
|
551
|
+
return isNonEmpty(value) ? value : undefined;
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
function truncate(value, length = 10) {
|
|
555
|
+
if (value.length <= length) {
|
|
556
|
+
return value;
|
|
557
|
+
}
|
|
558
|
+
return `${value.slice(0, length)}...`;
|
|
559
|
+
}
|
|
560
|
+
function capitalize(value) {
|
|
561
|
+
return `${value.charAt(0).toUpperCase()}${value.slice(1)}`;
|
|
562
|
+
}
|
|
563
|
+
function strictTitleCase(value) {
|
|
564
|
+
return value
|
|
565
|
+
.split(' ')
|
|
566
|
+
.map((value) => capitalize(value.toLowerCase()))
|
|
567
|
+
.join(' ');
|
|
568
|
+
}
|
|
569
|
+
function initial(firstname, lastname) {
|
|
570
|
+
if (firstname.length < 1 || lastname.length < 2) {
|
|
571
|
+
return '';
|
|
572
|
+
}
|
|
573
|
+
const firstLetter = firstname.substring(0, 1).toUpperCase();
|
|
574
|
+
const lastLetter = lastname.substring(0, 1).toUpperCase();
|
|
575
|
+
return `${firstLetter}${lastLetter}`;
|
|
576
|
+
}
|
|
577
|
+
function toCamelCase(value) {
|
|
578
|
+
return value.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
579
|
+
}
|
|
580
|
+
|
|
581
|
+
function formatAddress(address) {
|
|
582
|
+
if (!address)
|
|
583
|
+
return DEFAULT_EMPTY_STRING_VALUE;
|
|
584
|
+
const fullStreet = [
|
|
585
|
+
address.streetNumber,
|
|
586
|
+
address.street && strictTitleCase(address.street),
|
|
587
|
+
]
|
|
588
|
+
.filter((s) => s !== undefined)
|
|
589
|
+
.join(' ')
|
|
590
|
+
.trim();
|
|
591
|
+
const localization = [address.zipCode, address.city]
|
|
592
|
+
.filter((s) => s !== undefined)
|
|
593
|
+
.join(' ')
|
|
594
|
+
.trim();
|
|
595
|
+
const fullAddress = [fullStreet, localization]
|
|
596
|
+
.filter((s) => s !== '')
|
|
597
|
+
.join(', ');
|
|
598
|
+
if (fullAddress === '')
|
|
599
|
+
return DEFAULT_EMPTY_STRING_VALUE;
|
|
600
|
+
return fullAddress;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function formatFileSize(value) {
|
|
604
|
+
if (value === 0) {
|
|
605
|
+
return '0 o';
|
|
606
|
+
}
|
|
607
|
+
const e = Math.floor(Math.log(value) / Math.log(1024));
|
|
608
|
+
const unit = ' KMGTP'.charAt(e);
|
|
609
|
+
const fractionDigits = computeFractionDigits(unit);
|
|
610
|
+
// RTFM : 1024 -- > '1 Ko' , 1024000 --> '1 Mo'
|
|
611
|
+
return `${parseFloat((value / Math.pow(1024, e)).toFixed(fractionDigits))} ${unit}o`;
|
|
612
|
+
}
|
|
613
|
+
function computeFractionDigits(unit) {
|
|
614
|
+
switch (unit) {
|
|
615
|
+
case 'P':
|
|
616
|
+
case 'T':
|
|
617
|
+
case 'G':
|
|
618
|
+
return 1;
|
|
619
|
+
case 'M':
|
|
620
|
+
return 2;
|
|
621
|
+
default:
|
|
622
|
+
return 0;
|
|
623
|
+
}
|
|
624
|
+
}
|
|
625
|
+
|
|
626
|
+
const localFrenchPhoneNumberRegExp = /* @__PURE__ */ RegExp('^0[0-9]{9}$');
|
|
627
|
+
function parseFrenchPhoneNumber(value) {
|
|
628
|
+
const cleanedValue = value.replaceAll(' ', '');
|
|
629
|
+
return localFrenchPhoneNumberRegExp.test(cleanedValue) ? cleanedValue : null;
|
|
630
|
+
}
|
|
631
|
+
function formatFrenchPhoneNumber(value) {
|
|
632
|
+
if (!value) {
|
|
633
|
+
return undefined;
|
|
634
|
+
}
|
|
635
|
+
const phoneNumber = getPhoneNumberUtil().parse(value, 'FR');
|
|
636
|
+
const formattedPhoneNumber = getPhoneNumberUtil().format(phoneNumber, PhoneNumberFormat.NATIONAL);
|
|
637
|
+
if (formattedPhoneNumber === '000000000') {
|
|
638
|
+
return undefined;
|
|
639
|
+
}
|
|
640
|
+
return formattedPhoneNumber;
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
const URL_REGEXP = /https?:\/\/[^\s"'<>]+/gu;
|
|
644
|
+
function linkify(value, options = {}) {
|
|
645
|
+
const { target = '_blank', classList = ['underline'], text = identity, } = options;
|
|
646
|
+
const intoText = isString(text) ? () => text : text;
|
|
647
|
+
return value.replaceAll(URL_REGEXP, (url) => `<a target="${target}" class="${classList.join(' ')}" href="${encodeURI(url)}">${intoText(url)}</a>`);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
function num2digit(n) {
|
|
651
|
+
return `00${n}`.slice(-2);
|
|
652
|
+
}
|
|
653
|
+
|
|
654
|
+
const replaceFrom = (value, mask, ...startIndexes) => startIndexes.reduce((acc, start) => acc.substring(0, start) + mask + acc.substring(start + mask.length), value);
|
|
655
|
+
function formatPhoneNumber(value) {
|
|
656
|
+
let isAnonymized = false;
|
|
657
|
+
let phoneNumber;
|
|
658
|
+
if (isString(value)) {
|
|
659
|
+
isAnonymized = value.substring(4, 10) === 'XXXXXX';
|
|
660
|
+
const phoneNumberString = isAnonymized
|
|
661
|
+
? replaceFrom(value, '000000', 4)
|
|
662
|
+
: value;
|
|
663
|
+
try {
|
|
664
|
+
phoneNumber = getPhoneNumberUtil().parse(phoneNumberString, 'FR');
|
|
665
|
+
}
|
|
666
|
+
catch (error) {
|
|
667
|
+
console.error(error);
|
|
668
|
+
return undefined;
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
else {
|
|
672
|
+
phoneNumber = value;
|
|
673
|
+
}
|
|
674
|
+
let formatted = getPhoneNumberUtil().format(phoneNumber, PhoneNumberFormat.NATIONAL);
|
|
675
|
+
if (isAnonymized) {
|
|
676
|
+
formatted = replaceFrom(formatted, 'XX', 3, 6, 9);
|
|
677
|
+
}
|
|
678
|
+
return formatted === '000000000' ? undefined : formatted;
|
|
679
|
+
}
|
|
680
|
+
|
|
681
|
+
const BYTES_UNITS = {
|
|
682
|
+
en: ['B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB'],
|
|
683
|
+
fr: ['o', 'ko', 'Mo', 'Go', 'To', 'Po', 'Eo', 'Zo', 'Yo'],
|
|
684
|
+
};
|
|
685
|
+
const BIT_UNITS = [
|
|
686
|
+
'b',
|
|
687
|
+
'kbit',
|
|
688
|
+
'Mbit',
|
|
689
|
+
'Gbit',
|
|
690
|
+
'Tbit',
|
|
691
|
+
'Pbit',
|
|
692
|
+
'Ebit',
|
|
693
|
+
'Zbit',
|
|
694
|
+
'Ybit',
|
|
695
|
+
];
|
|
696
|
+
/*
|
|
697
|
+
Formats the given number using `Number#toLocaleString`.
|
|
698
|
+
- If locale is a string, the value is expected to be a locale-key (for example: `de`).
|
|
699
|
+
- If locale is true, the system default locale is used for translation.
|
|
700
|
+
- If no value for locale is specified, the number is returned unmodified.
|
|
701
|
+
*/
|
|
702
|
+
const toLocaleString = (value, locales, options) => {
|
|
703
|
+
const result = (() => {
|
|
704
|
+
if (locales !== undefined || options !== undefined) {
|
|
705
|
+
return value.toLocaleString(undefined, options);
|
|
706
|
+
}
|
|
707
|
+
else {
|
|
708
|
+
return String(value);
|
|
709
|
+
}
|
|
710
|
+
})();
|
|
711
|
+
return result;
|
|
712
|
+
};
|
|
713
|
+
function prettyBytes(number, options = {}) {
|
|
714
|
+
if (!Number.isFinite(number)) {
|
|
715
|
+
throw new TypeError(`Expected a finite number, got ${typeof number}: ${number}`);
|
|
716
|
+
}
|
|
717
|
+
options = {
|
|
718
|
+
bits: false,
|
|
719
|
+
binary: false,
|
|
720
|
+
space: true,
|
|
721
|
+
...options,
|
|
722
|
+
};
|
|
723
|
+
const locale = options.locale ?? 'fr';
|
|
724
|
+
const UNITS = options.bits ? BIT_UNITS : (BYTES_UNITS[locale] ?? []);
|
|
725
|
+
const separator = options.space ? ' ' : '';
|
|
726
|
+
if (options.signed && number === 0) {
|
|
727
|
+
return ` 0${separator}${UNITS[0]}`;
|
|
728
|
+
}
|
|
729
|
+
const isNegative = number < 0;
|
|
730
|
+
const prefix = isNegative ? '-' : options.signed ? '+' : '';
|
|
731
|
+
if (isNegative) {
|
|
732
|
+
number = -number;
|
|
733
|
+
}
|
|
734
|
+
let localeOptions;
|
|
735
|
+
if (options.minimumFractionDigits !== undefined) {
|
|
736
|
+
localeOptions = { minimumFractionDigits: options.minimumFractionDigits };
|
|
737
|
+
}
|
|
738
|
+
if (options.maximumFractionDigits !== undefined) {
|
|
739
|
+
localeOptions = {
|
|
740
|
+
maximumFractionDigits: options.maximumFractionDigits,
|
|
741
|
+
...localeOptions,
|
|
742
|
+
};
|
|
743
|
+
}
|
|
744
|
+
if (number < 1) {
|
|
745
|
+
const numberString = toLocaleString(number, options.locale, localeOptions);
|
|
746
|
+
return prefix + numberString + separator + (UNITS[0] ?? '');
|
|
747
|
+
}
|
|
748
|
+
const exponent = Math.min(Math.floor(options.binary
|
|
749
|
+
? Math.log(number) / Math.log(1024)
|
|
750
|
+
: Math.log10(number) / 3), UNITS.length - 1);
|
|
751
|
+
number /= (options.binary ? 1024 : 1000) ** exponent;
|
|
752
|
+
if (!localeOptions) {
|
|
753
|
+
number = +number.toPrecision(3);
|
|
754
|
+
}
|
|
755
|
+
const numberString = toLocaleString(number, options.locale, localeOptions);
|
|
756
|
+
const unit = UNITS[exponent] ?? '';
|
|
757
|
+
return prefix + numberString + separator + unit;
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
/**
|
|
761
|
+
* Formats a duration in seconds to a human-readable string in the format "1j 2h 45 min"
|
|
762
|
+
* @param durationInSeconds The duration in seconds to format
|
|
763
|
+
* @returns A formatted string representation of the duration, or "0 min" if duration is 0
|
|
764
|
+
*
|
|
765
|
+
* @example
|
|
766
|
+
* formatDuration(3665) // "1h 1 min"
|
|
767
|
+
* formatDuration(86400) // "1j"
|
|
768
|
+
* formatDuration(7380) // "2h 3 min"
|
|
769
|
+
* formatDuration(0) // "0 min"
|
|
770
|
+
*/
|
|
771
|
+
function formatDuration(durationInSeconds) {
|
|
772
|
+
const duration = Duration.fromObject({ seconds: durationInSeconds });
|
|
773
|
+
const days = Math.floor(duration.as('days'));
|
|
774
|
+
const hours = Math.floor(duration.minus({ days }).as('hours'));
|
|
775
|
+
const minutes = Math.floor(duration.minus({ days, hours }).as('minutes'));
|
|
776
|
+
let durationStr = '';
|
|
777
|
+
if (days > 0) {
|
|
778
|
+
durationStr += `${days}j `;
|
|
779
|
+
}
|
|
780
|
+
if (hours > 0) {
|
|
781
|
+
durationStr += `${hours}h `;
|
|
782
|
+
}
|
|
783
|
+
if (minutes > 0) {
|
|
784
|
+
durationStr += `${minutes} min`;
|
|
785
|
+
}
|
|
786
|
+
return durationStr.trim() || '0 min';
|
|
787
|
+
}
|
|
788
|
+
/**
|
|
789
|
+
* Formats a period between two dates without repeating the date if start day and en day are the same
|
|
790
|
+
*
|
|
791
|
+
* @param startDate The start date of the period
|
|
792
|
+
* @param endDate The end date of the period
|
|
793
|
+
* @returns A formatted string representation of the period in French format
|
|
794
|
+
*
|
|
795
|
+
* @example
|
|
796
|
+
* // Different dates
|
|
797
|
+
* formatPeriod(
|
|
798
|
+
* new Date('2024-01-15T09:30:00'),
|
|
799
|
+
* new Date('2024-01-16T17:00:00')
|
|
800
|
+
* )
|
|
801
|
+
* // Returns: "15/01/2024 09:30 - 16/01/2024 17h"
|
|
802
|
+
*
|
|
803
|
+
* @example
|
|
804
|
+
* // Same date
|
|
805
|
+
* formatPeriod(
|
|
806
|
+
* new Date('2024-01-15T09:30:00'),
|
|
807
|
+
* new Date('2024-01-15T17:00:00')
|
|
808
|
+
* )
|
|
809
|
+
* // Returns: "15/01/2024 09:30-17h"
|
|
810
|
+
*
|
|
811
|
+
* @example
|
|
812
|
+
* // Round hours (no minutes)
|
|
813
|
+
* formatPeriod(
|
|
814
|
+
* new Date('2024-01-15T09:00:00'),
|
|
815
|
+
* new Date('2024-01-15T17:00:00')
|
|
816
|
+
* )
|
|
817
|
+
* // Returns: "15/01/2024 09h-17h"
|
|
818
|
+
*/
|
|
819
|
+
function formatPeriod(startDate, endDate) {
|
|
820
|
+
const startDateTime = DateTime.fromJSDate(startDate);
|
|
821
|
+
const endDateTime = DateTime.fromJSDate(endDate);
|
|
822
|
+
const startDateStr = startDateTime.toFormat('dd/MM/yyyy');
|
|
823
|
+
const endDateStr = endDateTime.toFormat('dd/MM/yyyy');
|
|
824
|
+
// Format time, hiding minutes when they are 00 (round hours)
|
|
825
|
+
const formatTime = (dateTime) => {
|
|
826
|
+
return dateTime.minute === 0
|
|
827
|
+
? dateTime.toFormat('HH') + 'h'
|
|
828
|
+
: dateTime.toFormat('HH:mm');
|
|
829
|
+
};
|
|
830
|
+
const startTimeStr = formatTime(startDateTime);
|
|
831
|
+
const endTimeStr = formatTime(endDateTime);
|
|
832
|
+
if (startDateStr === endDateStr) {
|
|
833
|
+
return `${startDateStr} ${startTimeStr}-${endTimeStr}`;
|
|
834
|
+
}
|
|
835
|
+
else {
|
|
836
|
+
return `${startDateStr} ${startTimeStr} - ${endDateStr} ${endTimeStr}`;
|
|
837
|
+
}
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
function formatUserInitial(value) {
|
|
841
|
+
const { firstname, lastname } = coerceUserName(value);
|
|
842
|
+
return initial(firstname, lastname);
|
|
843
|
+
}
|
|
844
|
+
|
|
845
|
+
function formatUserName(value) {
|
|
846
|
+
const { firstname, lastname } = coerceUserName(value);
|
|
847
|
+
return [strictTitleCase(lastname), strictTitleCase(firstname)]
|
|
848
|
+
.join(' ')
|
|
849
|
+
.trim();
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
class ActionRefError extends Error {
|
|
853
|
+
constructor(payload) {
|
|
854
|
+
super(payload.message);
|
|
855
|
+
this.source = payload.source;
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
class ActionRef {
|
|
859
|
+
constructor(sucess$, failure$, cancelled$) {
|
|
860
|
+
this.sucess$ = sucess$;
|
|
861
|
+
this.failure$ = failure$;
|
|
862
|
+
this.cancelled$ = cancelled$;
|
|
863
|
+
this.afterSuccess$ = this.sucess$.pipe(takeUntil(merge(this.failure$, this.cancelled$)));
|
|
864
|
+
this.afterFailure$ = this.failure$.pipe(takeUntil(merge(this.sucess$, this.cancelled$)));
|
|
865
|
+
this.afterCancelled$ = this.cancelled$.pipe(takeUntil(merge(this.sucess$, this.failure$)));
|
|
866
|
+
this.afterEvent$ = merge(this.sucess$, this.failure$.pipe(switchMap((source) => throwError(() => new ActionRefError({ source }))))).pipe(takeUntil(this.cancelled$));
|
|
867
|
+
}
|
|
868
|
+
static of(task, events) {
|
|
869
|
+
setTimeout(task);
|
|
870
|
+
return new ActionRef(ActionRef.resolveInput(events.success), ActionRef.resolveInput(events.failure), ActionRef.resolveInput(events.cancelled ?? NEVER));
|
|
871
|
+
}
|
|
872
|
+
static resolveInput(input) {
|
|
873
|
+
return from(input).pipe(take(1), shareReplay(1));
|
|
874
|
+
}
|
|
875
|
+
}
|
|
876
|
+
|
|
877
|
+
function assertNonEmpty(array) {
|
|
878
|
+
if (array[0] === undefined) {
|
|
879
|
+
throw Error('Expected a non empty array');
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
function mapNonEmptyArray(nonEmptyArray, fn) {
|
|
883
|
+
return nonEmptyArray.map((value, index, array) => fn(value, index, array));
|
|
884
|
+
}
|
|
885
|
+
function insertAt(arr, index, ...items) {
|
|
886
|
+
return [...arr.slice(0, index), ...items, ...arr.slice(index, arr.length)];
|
|
887
|
+
}
|
|
888
|
+
/**
|
|
889
|
+
* Slices an array into chunks of specified size.
|
|
890
|
+
* @param items The array to be sliced.
|
|
891
|
+
* @param chunkSize The size of each chunk.
|
|
892
|
+
* @returns An array of arrays, each containing elements of the original array sliced into chunks.
|
|
893
|
+
*/
|
|
894
|
+
function sliceIntoChunks(items, chunkSize) {
|
|
895
|
+
const res = [];
|
|
896
|
+
for (let i = 0; i < items.length; i += chunkSize) {
|
|
897
|
+
const chunk = items.slice(i, i + chunkSize);
|
|
898
|
+
res.push(chunk);
|
|
899
|
+
}
|
|
900
|
+
return res;
|
|
901
|
+
}
|
|
902
|
+
function groupBy(items, keyGetter) {
|
|
903
|
+
return items.reduce((res, item) => {
|
|
904
|
+
const key = keyGetter(item);
|
|
905
|
+
const oldValue = res.get(key) ?? [];
|
|
906
|
+
return res.set(key, oldValue.concat(item));
|
|
907
|
+
}, new Map());
|
|
908
|
+
}
|
|
909
|
+
function intersect(arr1, arr2, fn = defaultStringify) {
|
|
910
|
+
const _arr1 = [...arr1];
|
|
911
|
+
const _arr2 = [...arr2].map((item) => fn(item));
|
|
912
|
+
return _arr1.reduce((result, v) => (_arr2.includes(fn(v)) ? [...result, v] : result), []);
|
|
913
|
+
}
|
|
914
|
+
function diff(arr1, arr2, comparer = strictComparer()) {
|
|
915
|
+
return arr1.filter((item1) => !arr2.some((item2) => comparer(item1, item2)));
|
|
916
|
+
}
|
|
917
|
+
function distinct(arr, comparer = strictComparer()) {
|
|
918
|
+
return arr.filter((x, i, arr) => arr.findIndex((other) => comparer(x, other)) === i);
|
|
919
|
+
}
|
|
920
|
+
function dropLast(arr) {
|
|
921
|
+
return arr.slice(0, -1);
|
|
922
|
+
}
|
|
923
|
+
function safeCastFromStringArr(target, values) {
|
|
924
|
+
return isNotNullable(target) && values.some((item) => item === target)
|
|
925
|
+
? target
|
|
926
|
+
: undefined;
|
|
927
|
+
}
|
|
928
|
+
function getCenteredSlice(arr, currentIndex, size) {
|
|
929
|
+
const half = Math.floor(size / 2);
|
|
930
|
+
let start = currentIndex - half;
|
|
931
|
+
if (size % 2 === 0) {
|
|
932
|
+
start += 1;
|
|
933
|
+
}
|
|
934
|
+
if (start < 0) {
|
|
935
|
+
start = 0;
|
|
936
|
+
}
|
|
937
|
+
let end = start + size;
|
|
938
|
+
if (end > arr.length) {
|
|
939
|
+
end = arr.length;
|
|
940
|
+
start = Math.max(0, end - size);
|
|
941
|
+
}
|
|
942
|
+
return arr.slice(start, end);
|
|
943
|
+
}
|
|
944
|
+
function completeDiff(oldArray, newArray, comparer = strictComparer()) {
|
|
945
|
+
const added = [];
|
|
946
|
+
const removed = [];
|
|
947
|
+
const unchanged = [];
|
|
948
|
+
const remainingOld = [...oldArray];
|
|
949
|
+
for (const newItem of newArray) {
|
|
950
|
+
const index = remainingOld.findIndex((oldItem) => comparer(newItem, oldItem));
|
|
951
|
+
if (index !== -1) {
|
|
952
|
+
unchanged.push(newItem);
|
|
953
|
+
remainingOld.splice(index, 1);
|
|
954
|
+
}
|
|
955
|
+
else {
|
|
956
|
+
added.push(newItem);
|
|
957
|
+
}
|
|
958
|
+
}
|
|
959
|
+
removed.push(...remainingOld);
|
|
960
|
+
return { added, removed, unchanged };
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
function arrayConf(conf) {
|
|
964
|
+
const { selectId: _selectId, sortComparer } = conf;
|
|
965
|
+
function selectId(entity) {
|
|
966
|
+
const id = _selectId?.(entity) ?? entity.id;
|
|
967
|
+
assertNonNullable(id, `id`);
|
|
968
|
+
assertValid(isString(id) || isNumber(id), `id must return a string or number, but got: ${typeof id}`);
|
|
969
|
+
return id;
|
|
970
|
+
}
|
|
971
|
+
return {
|
|
972
|
+
selectId,
|
|
973
|
+
sortComparer,
|
|
974
|
+
};
|
|
975
|
+
}
|
|
976
|
+
/**
|
|
977
|
+
* Adds an entity to the collection.
|
|
978
|
+
* If the entity collection has an entity with the same ID, it is not overridden.
|
|
979
|
+
*/
|
|
980
|
+
function addOne(array, entity, conf) {
|
|
981
|
+
const { selectId, sortComparer } = conf;
|
|
982
|
+
const id = selectId(entity);
|
|
983
|
+
const result = array.some((item) => selectId(item) === id)
|
|
984
|
+
? array
|
|
985
|
+
: [...array, entity];
|
|
986
|
+
return sort(result, sortComparer);
|
|
987
|
+
}
|
|
988
|
+
/**
|
|
989
|
+
* Adds multiple entities to the collection.
|
|
990
|
+
* If the entity collection has entities with the same IDs, they are not overridden .
|
|
991
|
+
*/
|
|
992
|
+
function addMany(array, entities, conf) {
|
|
993
|
+
const { selectId, sortComparer } = conf;
|
|
994
|
+
const existingIds = new Set(array.map((item) => selectId(item)));
|
|
995
|
+
const newEntities = entities.filter((item) => !existingIds.has(selectId(item)));
|
|
996
|
+
const result = newEntities.length === 0 ? array : [...array, ...newEntities];
|
|
997
|
+
return sort(result, sortComparer);
|
|
998
|
+
}
|
|
999
|
+
/**
|
|
1000
|
+
* Adds or replaces an entity in the collection.
|
|
1001
|
+
*/
|
|
1002
|
+
function setOne(array, entity, conf) {
|
|
1003
|
+
const { selectId, sortComparer } = conf;
|
|
1004
|
+
const id = selectId(entity);
|
|
1005
|
+
const index = array.findIndex((item) => selectId(item) === id);
|
|
1006
|
+
if (index === -1)
|
|
1007
|
+
return [...array, entity];
|
|
1008
|
+
const result = array.map((item, i) => (i === index ? entity : item));
|
|
1009
|
+
return sort(result, sortComparer);
|
|
1010
|
+
}
|
|
1011
|
+
/**
|
|
1012
|
+
* Adds or replaces multiple entities in the collection.
|
|
1013
|
+
*/
|
|
1014
|
+
function setMany(array, entities, conf) {
|
|
1015
|
+
const { selectId, sortComparer } = conf;
|
|
1016
|
+
const map = new Map(array.map((item) => [selectId(item), item]));
|
|
1017
|
+
for (const entity of entities) {
|
|
1018
|
+
map.set(selectId(entity), entity);
|
|
1019
|
+
}
|
|
1020
|
+
const result = Array.from(map.values());
|
|
1021
|
+
return sort(result, sortComparer);
|
|
1022
|
+
}
|
|
1023
|
+
/**
|
|
1024
|
+
* Removes an entity from the collection by ID.
|
|
1025
|
+
*/
|
|
1026
|
+
function removeOne(array, idOrPredicate, conf) {
|
|
1027
|
+
const { selectId, sortComparer } = conf;
|
|
1028
|
+
const result = (() => {
|
|
1029
|
+
if (isFn(idOrPredicate)) {
|
|
1030
|
+
return array.filter((item) => !idOrPredicate(item));
|
|
1031
|
+
}
|
|
1032
|
+
return array.filter((item) => selectId(item) !== idOrPredicate);
|
|
1033
|
+
})();
|
|
1034
|
+
return sort(result, sortComparer);
|
|
1035
|
+
}
|
|
1036
|
+
/**
|
|
1037
|
+
* Removes multiple entities from the collection by IDs or predicate.
|
|
1038
|
+
*/
|
|
1039
|
+
function removeMany(array, idsOrPredicate, conf) {
|
|
1040
|
+
const { selectId, sortComparer } = conf;
|
|
1041
|
+
const result = (() => {
|
|
1042
|
+
if (isFn(idsOrPredicate)) {
|
|
1043
|
+
return array.filter((item) => !idsOrPredicate(item));
|
|
1044
|
+
}
|
|
1045
|
+
const idsSet = new Set(idsOrPredicate);
|
|
1046
|
+
return array.filter((item) => !idsSet.has(selectId(item)));
|
|
1047
|
+
})();
|
|
1048
|
+
return sort(result, sortComparer);
|
|
1049
|
+
}
|
|
1050
|
+
/**
|
|
1051
|
+
* Updates an entity in the collection by ID.
|
|
1052
|
+
* Supports partial updates.
|
|
1053
|
+
*/
|
|
1054
|
+
function updateOne(array, update, conf) {
|
|
1055
|
+
const { selectId, sortComparer } = conf;
|
|
1056
|
+
const result = array.map((item) => selectId(item) === update.id
|
|
1057
|
+
? { ...item, ...resolveValueOrFn(update.changes, item) }
|
|
1058
|
+
: item);
|
|
1059
|
+
return sort(result, sortComparer);
|
|
1060
|
+
}
|
|
1061
|
+
/**
|
|
1062
|
+
* Updates multiple entities in the collection by IDs or predicate.
|
|
1063
|
+
* Supports partial updates.
|
|
1064
|
+
*/
|
|
1065
|
+
function updateMany(array, updates, conf) {
|
|
1066
|
+
const { selectId, sortComparer } = conf;
|
|
1067
|
+
const updatesMap = new Map(updates.map((item) => [item.id, item.changes]));
|
|
1068
|
+
const result = array.map((item) => {
|
|
1069
|
+
const id = selectId(item);
|
|
1070
|
+
const changes = updatesMap.get(id);
|
|
1071
|
+
return changes ? { ...item, ...resolveValueOrFn(changes, item) } : item;
|
|
1072
|
+
});
|
|
1073
|
+
return sort(result, sortComparer);
|
|
1074
|
+
}
|
|
1075
|
+
function sort(array, sortComparer) {
|
|
1076
|
+
return sortComparer ? [...array].sort(sortComparer) : array;
|
|
1077
|
+
}
|
|
1078
|
+
|
|
1079
|
+
const KILO_OCTET = 1024;
|
|
1080
|
+
const MEGA_OCTET = KILO_OCTET * KILO_OCTET;
|
|
1081
|
+
const GIGA_OCTET = MEGA_OCTET * KILO_OCTET;
|
|
1082
|
+
const TERA_OCTET = GIGA_OCTET * KILO_OCTET;
|
|
1083
|
+
// Turn a number to a data volume with an inferrable unit
|
|
1084
|
+
function asDataVolume(n) {
|
|
1085
|
+
return n;
|
|
1086
|
+
}
|
|
1087
|
+
function koToOctet(n) {
|
|
1088
|
+
return asDataVolume(Math.round(n * KILO_OCTET));
|
|
1089
|
+
}
|
|
1090
|
+
function moToOctet(n) {
|
|
1091
|
+
return asDataVolume(Math.round(n * MEGA_OCTET));
|
|
1092
|
+
}
|
|
1093
|
+
function goToOctet(n) {
|
|
1094
|
+
return asDataVolume(Math.round(n * GIGA_OCTET));
|
|
1095
|
+
}
|
|
1096
|
+
function toToOctet(n) {
|
|
1097
|
+
return asDataVolume(Math.round(n * TERA_OCTET));
|
|
1098
|
+
}
|
|
1099
|
+
function toKo(n) {
|
|
1100
|
+
return asDataVolume(Number((n / KILO_OCTET).toFixed(1)));
|
|
1101
|
+
}
|
|
1102
|
+
function toMo(n) {
|
|
1103
|
+
return asDataVolume(Number((n / MEGA_OCTET).toFixed(1)));
|
|
1104
|
+
}
|
|
1105
|
+
function toGo(n) {
|
|
1106
|
+
return asDataVolume(Number((n / GIGA_OCTET).toFixed(1)));
|
|
1107
|
+
}
|
|
1108
|
+
function toTo(n) {
|
|
1109
|
+
return asDataVolume(Number((n / TERA_OCTET).toFixed(1)));
|
|
1110
|
+
}
|
|
1111
|
+
function toReadableVolume(data) {
|
|
1112
|
+
const isNumber = typeof data === 'number';
|
|
1113
|
+
const comparedVolume = isNumber ? undefined : data.comparedVolume;
|
|
1114
|
+
const volume = isNumber ? data : data.volume;
|
|
1115
|
+
if (volume >= TERA_OCTET) {
|
|
1116
|
+
return {
|
|
1117
|
+
volume: toTo(volume),
|
|
1118
|
+
comparedVolume: comparedVolume !== undefined ? toTo(comparedVolume) : undefined,
|
|
1119
|
+
unit: 'To',
|
|
1120
|
+
};
|
|
1121
|
+
}
|
|
1122
|
+
else if (volume >= GIGA_OCTET) {
|
|
1123
|
+
return {
|
|
1124
|
+
volume: toGo(volume),
|
|
1125
|
+
comparedVolume: comparedVolume !== undefined ? toGo(comparedVolume) : undefined,
|
|
1126
|
+
unit: 'Go',
|
|
1127
|
+
};
|
|
1128
|
+
}
|
|
1129
|
+
else if (volume >= MEGA_OCTET) {
|
|
1130
|
+
return {
|
|
1131
|
+
volume: toMo(volume),
|
|
1132
|
+
comparedVolume: comparedVolume !== undefined ? toMo(comparedVolume) : undefined,
|
|
1133
|
+
unit: 'Mo',
|
|
1134
|
+
};
|
|
1135
|
+
}
|
|
1136
|
+
else if (volume >= KILO_OCTET) {
|
|
1137
|
+
return {
|
|
1138
|
+
volume: toKo(volume),
|
|
1139
|
+
comparedVolume: comparedVolume !== undefined ? toKo(comparedVolume) : undefined,
|
|
1140
|
+
unit: 'Ko',
|
|
1141
|
+
};
|
|
1142
|
+
}
|
|
1143
|
+
return { volume: volume, comparedVolume, unit: 'Octet' };
|
|
1144
|
+
}
|
|
1145
|
+
|
|
1146
|
+
function isDateInRange(range, target = new Date(), useTime = false) {
|
|
1147
|
+
const [start, end] = range;
|
|
1148
|
+
const targetDateTime = useTime
|
|
1149
|
+
? DateTime.fromJSDate(target)
|
|
1150
|
+
: DateTime.fromJSDate(target)
|
|
1151
|
+
.set({ hour: 0, minute: 0, second: 0, millisecond: 0 })
|
|
1152
|
+
.plus({ day: 1 })
|
|
1153
|
+
.minus({ seconds: 1 });
|
|
1154
|
+
return ((!start || DateTime.fromJSDate(start) <= targetDateTime) &&
|
|
1155
|
+
(!end || DateTime.fromJSDate(end) >= targetDateTime));
|
|
1156
|
+
}
|
|
1157
|
+
function clampDate(date, min, max) {
|
|
1158
|
+
let timestamp = date.getTime();
|
|
1159
|
+
if (min)
|
|
1160
|
+
timestamp = Math.max(timestamp, min.getTime());
|
|
1161
|
+
if (max)
|
|
1162
|
+
timestamp = Math.min(timestamp, max.getTime());
|
|
1163
|
+
return new Date(timestamp);
|
|
1164
|
+
}
|
|
1165
|
+
function isBefore(date, dateToCompareWith) {
|
|
1166
|
+
return DateTime.fromJSDate(date) < DateTime.fromJSDate(dateToCompareWith);
|
|
1167
|
+
}
|
|
1168
|
+
function isBeforeNow(date) {
|
|
1169
|
+
return isBefore(date, new Date());
|
|
1170
|
+
}
|
|
1171
|
+
const isWorkingDay = (date, holidays, includeSaturday = false) => {
|
|
1172
|
+
const weekendDays = includeSaturday ? [7] : [6, 7];
|
|
1173
|
+
return (!weekendDays.includes(date.weekday) &&
|
|
1174
|
+
!holidays.some((holiday) => date.hasSame(holiday, 'day')));
|
|
1175
|
+
};
|
|
1176
|
+
const addWorkingDays = (date, numberOfWorkingDays, holidays, includeSaturday = false) => {
|
|
1177
|
+
while (numberOfWorkingDays > 0) {
|
|
1178
|
+
date = date.plus({ days: 1 });
|
|
1179
|
+
if (isWorkingDay(date, holidays, includeSaturday)) {
|
|
1180
|
+
numberOfWorkingDays--;
|
|
1181
|
+
}
|
|
1182
|
+
}
|
|
1183
|
+
return date;
|
|
1184
|
+
};
|
|
1185
|
+
|
|
1186
|
+
function getDeepest(root, deepSelector) {
|
|
1187
|
+
let current = root;
|
|
1188
|
+
let next;
|
|
1189
|
+
while ((next = deepSelector(current))) {
|
|
1190
|
+
current = next;
|
|
1191
|
+
}
|
|
1192
|
+
return current;
|
|
1193
|
+
}
|
|
1194
|
+
|
|
1195
|
+
const EMAIL_REGEX = /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,}$/i;
|
|
1196
|
+
function isEmail(value) {
|
|
1197
|
+
return EMAIL_REGEX.test(value);
|
|
1198
|
+
}
|
|
1199
|
+
|
|
1200
|
+
function mapFileToBase64(file) {
|
|
1201
|
+
return new Promise((resolve, reject) => {
|
|
1202
|
+
const reader = new FileReader();
|
|
1203
|
+
reader.readAsDataURL(file);
|
|
1204
|
+
reader.onload = () => isString(reader.result)
|
|
1205
|
+
? resolve(reader.result)
|
|
1206
|
+
: reject(new Error('Unable to convert to base64'));
|
|
1207
|
+
reader.onerror = reject;
|
|
1208
|
+
});
|
|
1209
|
+
}
|
|
1210
|
+
function isBase64Image(data) {
|
|
1211
|
+
return data.split(';')[1]?.startsWith('base64') ?? false;
|
|
1212
|
+
}
|
|
1213
|
+
|
|
1214
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
1215
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1216
|
+
function makeSafe(fn, errHandler) {
|
|
1217
|
+
return (...args) => {
|
|
1218
|
+
try {
|
|
1219
|
+
return fn(...args);
|
|
1220
|
+
}
|
|
1221
|
+
catch (err) {
|
|
1222
|
+
errHandler?.(err, ...args);
|
|
1223
|
+
return undefined;
|
|
1224
|
+
}
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
1227
|
+
function withDefault(fn, defaultValue) {
|
|
1228
|
+
return (...args) => fn(...args) ?? defaultValue;
|
|
1229
|
+
}
|
|
1230
|
+
function createDebouncedFn(fn, delay = 0) {
|
|
1231
|
+
let timer;
|
|
1232
|
+
const debounced = (...args) => {
|
|
1233
|
+
if (timer)
|
|
1234
|
+
clearTimeout(timer);
|
|
1235
|
+
timer = setTimeout(() => fn(...args), delay);
|
|
1236
|
+
};
|
|
1237
|
+
debounced.cancel = () => {
|
|
1238
|
+
if (timer) {
|
|
1239
|
+
clearTimeout(timer);
|
|
1240
|
+
timer = undefined;
|
|
1241
|
+
}
|
|
1242
|
+
};
|
|
1243
|
+
return debounced;
|
|
1244
|
+
}
|
|
1245
|
+
|
|
1246
|
+
const IPV4_OCTET_PATTERN = '(25[0-5]|2[0-4]\\d|1?\\d?\\d)';
|
|
1247
|
+
const IPV4_PATTERN = /* @__PURE__ */ new RegExp(`^${IPV4_OCTET_PATTERN}(\\.${IPV4_OCTET_PATTERN}){3}$`);
|
|
1248
|
+
const IPV4_CIDR_PATTERN = /* @__PURE__ */ new RegExp(`^${IPV4_OCTET_PATTERN}(\\.${IPV4_OCTET_PATTERN}){3}\\/(3[0-2]|[12]?\\d)$`);
|
|
1249
|
+
function isValidIpv4Address(value) {
|
|
1250
|
+
return IPV4_PATTERN.test(value);
|
|
1251
|
+
}
|
|
1252
|
+
function isValidIpv4Cidr(value) {
|
|
1253
|
+
return IPV4_CIDR_PATTERN.test(value);
|
|
1254
|
+
}
|
|
1255
|
+
function ipv4ToInt(ip) {
|
|
1256
|
+
return (ip
|
|
1257
|
+
.split('.')
|
|
1258
|
+
.map(Number)
|
|
1259
|
+
.reduce((acc, n) => (acc << 8) + n, 0) >>> 0);
|
|
1260
|
+
}
|
|
1261
|
+
function intToIpv4(value) {
|
|
1262
|
+
return [
|
|
1263
|
+
value >>> 24,
|
|
1264
|
+
(value >>> 16) & 255,
|
|
1265
|
+
(value >>> 8) & 255,
|
|
1266
|
+
value & 255,
|
|
1267
|
+
].join('.');
|
|
1268
|
+
}
|
|
1269
|
+
function getIpv4HostAddress(networkAddress, offset) {
|
|
1270
|
+
assertValid(isValidIpv4Address(networkAddress), 'networkAddress');
|
|
1271
|
+
const base = ipv4ToInt(networkAddress);
|
|
1272
|
+
const value = (base + offset) >>> 0;
|
|
1273
|
+
return intToIpv4(value);
|
|
1274
|
+
}
|
|
1275
|
+
function parseIpv4Cidr(cidr) {
|
|
1276
|
+
assertValid(isValidIpv4Cidr(cidr), 'cidr');
|
|
1277
|
+
const [ip, pref] = cidr.split('/');
|
|
1278
|
+
const prefix = Number(pref);
|
|
1279
|
+
assertNonNullable(ip, 'ip');
|
|
1280
|
+
const ipInt = ipv4ToInt(ip);
|
|
1281
|
+
const mask = prefix === 0 ? 0 : (0xffffffff << (32 - prefix)) >>> 0;
|
|
1282
|
+
const network = (ipInt & mask) >>> 0;
|
|
1283
|
+
return { network, ipInt, prefix };
|
|
1284
|
+
}
|
|
1285
|
+
function cidrIsNetworkAddress(cidr) {
|
|
1286
|
+
const { network, ipInt } = parseIpv4Cidr(cidr);
|
|
1287
|
+
return network === ipInt;
|
|
1288
|
+
}
|
|
1289
|
+
function ipv4BelongsToCidr(ipAddress, cidr) {
|
|
1290
|
+
if (!isValidIpv4Address(ipAddress) || !isValidIpv4Cidr(cidr))
|
|
1291
|
+
return false;
|
|
1292
|
+
const { network, prefix } = parseIpv4Cidr(cidr);
|
|
1293
|
+
const mask = prefix === 0 ? 0 : (0xffffffff << (32 - prefix)) >>> 0;
|
|
1294
|
+
return (ipv4ToInt(ipAddress) & mask) >>> 0 === network;
|
|
1295
|
+
}
|
|
1296
|
+
function getIpv4NetworkAddressFromCidr(cidr) {
|
|
1297
|
+
const { network, prefix } = parseIpv4Cidr(cidr);
|
|
1298
|
+
const ipv4 = intToIpv4(network);
|
|
1299
|
+
return `${ipv4}/${prefix}`;
|
|
1300
|
+
}
|
|
1301
|
+
function cidrComparer(aCidr, bCidr) {
|
|
1302
|
+
const aIp = parseIpv4Cidr(aCidr);
|
|
1303
|
+
const bIp = parseIpv4Cidr(bCidr);
|
|
1304
|
+
if (aIp.network !== bIp.network)
|
|
1305
|
+
return aIp.network - bIp.network;
|
|
1306
|
+
if (aIp.prefix !== bIp.prefix)
|
|
1307
|
+
return aIp.prefix - bIp.prefix;
|
|
1308
|
+
return aIp.ipInt - bIp.ipInt;
|
|
1309
|
+
}
|
|
1310
|
+
|
|
1311
|
+
function getIterationMeta(item, source) {
|
|
1312
|
+
const index = source.indexOf(item);
|
|
1313
|
+
const parity = index % 2;
|
|
1314
|
+
return {
|
|
1315
|
+
even: parity === 0,
|
|
1316
|
+
first: source[0] === item,
|
|
1317
|
+
index,
|
|
1318
|
+
last: source[source.length - 1] === item,
|
|
1319
|
+
odd: parity !== 0,
|
|
1320
|
+
};
|
|
1321
|
+
}
|
|
1322
|
+
|
|
1323
|
+
function luhn(value) {
|
|
1324
|
+
// Accept only digits, dashes or spaces
|
|
1325
|
+
if (/[^0-9-\s]+/.test(value))
|
|
1326
|
+
return false;
|
|
1327
|
+
const cleanValue = value.replace(/\D/g, '');
|
|
1328
|
+
const result = cleanValue
|
|
1329
|
+
.split('')
|
|
1330
|
+
.reverse()
|
|
1331
|
+
.reduce((previous, char) => {
|
|
1332
|
+
const digit = parseInt(char, 10);
|
|
1333
|
+
if (previous.is_odd) {
|
|
1334
|
+
return { sum: previous.sum + digit, is_odd: !previous.is_odd };
|
|
1335
|
+
}
|
|
1336
|
+
else {
|
|
1337
|
+
const sum = () => {
|
|
1338
|
+
const x = 2 * digit;
|
|
1339
|
+
if (x > 9) {
|
|
1340
|
+
return x - 9;
|
|
1341
|
+
}
|
|
1342
|
+
else
|
|
1343
|
+
return x;
|
|
1344
|
+
};
|
|
1345
|
+
return {
|
|
1346
|
+
sum: previous.sum + sum(),
|
|
1347
|
+
is_odd: !previous.is_odd,
|
|
1348
|
+
};
|
|
1349
|
+
}
|
|
1350
|
+
}, { sum: 0, is_odd: true }).sum;
|
|
1351
|
+
return result % 10 == 0;
|
|
1352
|
+
}
|
|
1353
|
+
|
|
1354
|
+
function mapValues(map, mapper) {
|
|
1355
|
+
return new Map(Array.from(map.entries(), ([key, value]) => [key, mapper(value, key)]));
|
|
1356
|
+
}
|
|
1357
|
+
|
|
1358
|
+
/**
|
|
1359
|
+
* Default handler for matching stringified version of an item and a search query
|
|
1360
|
+
* @param item arbitrary element to match with a string
|
|
1361
|
+
* @param matchValue search query
|
|
1362
|
+
* @param stringify handler to turn item into a string
|
|
1363
|
+
*/
|
|
1364
|
+
const defaultMatcher = (item, matchValue, stringify = defaultStringify) => normalize(stringify(item)).includes(normalize(matchValue));
|
|
1365
|
+
|
|
1366
|
+
function clamp(value, min, max) {
|
|
1367
|
+
return Math.min(Math.max(value, min), max);
|
|
1368
|
+
}
|
|
1369
|
+
function ensureSafeNumber(value) {
|
|
1370
|
+
return value === Infinity ? Number.MAX_SAFE_INTEGER : value;
|
|
1371
|
+
}
|
|
1372
|
+
function percentage(value, total) {
|
|
1373
|
+
if (total <= 0)
|
|
1374
|
+
return 0;
|
|
1375
|
+
const result = (value / total) * 100;
|
|
1376
|
+
return clamp(Math.floor(result), 0, 100);
|
|
1377
|
+
}
|
|
1378
|
+
function separateIntegerDecimal(value) {
|
|
1379
|
+
const integerPart = Math.floor(value);
|
|
1380
|
+
const decimalPart = value - integerPart;
|
|
1381
|
+
return {
|
|
1382
|
+
integerPart: integerPart,
|
|
1383
|
+
decimalPart: decimalPart,
|
|
1384
|
+
};
|
|
1385
|
+
}
|
|
1386
|
+
|
|
1387
|
+
function setAtPath(obj, path, value) {
|
|
1388
|
+
const pathArray = path.split('.');
|
|
1389
|
+
return setAtPathByArray(obj, pathArray, value);
|
|
1390
|
+
}
|
|
1391
|
+
function setAtPathByArray(obj, path, value) {
|
|
1392
|
+
if (path.length === 0) {
|
|
1393
|
+
return value;
|
|
1394
|
+
}
|
|
1395
|
+
const [currentKey, ...restPath] = path;
|
|
1396
|
+
return {
|
|
1397
|
+
...obj,
|
|
1398
|
+
[currentKey]: setAtPathByArray(obj[currentKey], restPath, value),
|
|
1399
|
+
};
|
|
1400
|
+
}
|
|
1401
|
+
function deleteImmutable(obj, key) {
|
|
1402
|
+
const { [key]: _deleted, ...remainingObj } = obj;
|
|
1403
|
+
return remainingObj;
|
|
1404
|
+
}
|
|
1405
|
+
function mergeWith(base, ...objs) {
|
|
1406
|
+
const keys = Object.keys(base);
|
|
1407
|
+
return keys.reduce((res, key) => ({
|
|
1408
|
+
...res,
|
|
1409
|
+
[key]: objs.reduce((entry, obj) => obj[key] ?? entry, base[key]),
|
|
1410
|
+
}), {});
|
|
1411
|
+
}
|
|
1412
|
+
function diffWith(o1, o2, comparer) {
|
|
1413
|
+
const keys = Object.keys(o1);
|
|
1414
|
+
return keys.reduce((res, key) => {
|
|
1415
|
+
if (comparer(o1[key], o2[key])) {
|
|
1416
|
+
return res;
|
|
1417
|
+
}
|
|
1418
|
+
return {
|
|
1419
|
+
...res,
|
|
1420
|
+
[key]: o2[key],
|
|
1421
|
+
};
|
|
1422
|
+
}, {});
|
|
1423
|
+
}
|
|
1424
|
+
function compareKeysWith(o1, o2, comparer) {
|
|
1425
|
+
const keys = Object.keys(o1);
|
|
1426
|
+
return keys.reduce((res, key) => ({
|
|
1427
|
+
...res,
|
|
1428
|
+
[key]: comparer(o1[key], o2[key]),
|
|
1429
|
+
}), {});
|
|
1430
|
+
}
|
|
1431
|
+
function entries(obj) {
|
|
1432
|
+
return Object.entries(obj);
|
|
1433
|
+
}
|
|
1434
|
+
function hasNonNullValues(obj) {
|
|
1435
|
+
return Object.values(obj).some(isNotNullable);
|
|
1436
|
+
}
|
|
1437
|
+
function zipToObject(values, keys, mapper) {
|
|
1438
|
+
return values.reduce((result, cur, index) => {
|
|
1439
|
+
const name = keys[index];
|
|
1440
|
+
if (isNullable(name))
|
|
1441
|
+
return result;
|
|
1442
|
+
return {
|
|
1443
|
+
...result,
|
|
1444
|
+
[name]: mapper(cur),
|
|
1445
|
+
};
|
|
1446
|
+
}, {});
|
|
1447
|
+
}
|
|
1448
|
+
|
|
1449
|
+
function errorMessage(verb) {
|
|
1450
|
+
return (_, phone, regionCode) => {
|
|
1451
|
+
const formatted = isString(phone)
|
|
1452
|
+
? phone
|
|
1453
|
+
: getPhoneNumberUtil().format(phone, PhoneNumberFormat.E164);
|
|
1454
|
+
return `Failed to ${verb} phone number "${formatted}" for the region "${regionCode ?? 'XX'}"`;
|
|
1455
|
+
};
|
|
1456
|
+
}
|
|
1457
|
+
const parseError = /* @__PURE__ */ errorMessage('parse');
|
|
1458
|
+
const formatError = /* @__PURE__ */ errorMessage('format');
|
|
1459
|
+
const parse = (phone, regionCode) => {
|
|
1460
|
+
const parsed = getPhoneNumberUtil().parse(phone, regionCode);
|
|
1461
|
+
// TODO FCA should be activated but in local env or with the mocks, many phone numbers are invalid. We shall use isDevMode ???
|
|
1462
|
+
// const validator = regionCode
|
|
1463
|
+
// ? () =>
|
|
1464
|
+
// phoneUtil.isValidNumberForRegion.apply(phoneUtil, [parsed, regionCode])
|
|
1465
|
+
// : () => phoneUtil.isValidNumber.apply(phoneUtil, [parsed]);
|
|
1466
|
+
// if (!validator()) {
|
|
1467
|
+
// throw new Error(parseError(phone, regionCode));
|
|
1468
|
+
// }
|
|
1469
|
+
return parsed;
|
|
1470
|
+
};
|
|
1471
|
+
function coerce(input) {
|
|
1472
|
+
return isString(input) ? parse(input) : input;
|
|
1473
|
+
}
|
|
1474
|
+
|
|
1475
|
+
// this whole module (and google-libphonenumber with it) when `phoneNumber` is unused.
|
|
1476
|
+
function formatter(format) {
|
|
1477
|
+
return (phone, regionCode) => {
|
|
1478
|
+
const parsed = isString(phone) ? parse(phone, regionCode) : phone;
|
|
1479
|
+
return (phone && getPhoneNumberUtil().format(parsed, PhoneNumberFormat[format]));
|
|
1480
|
+
};
|
|
1481
|
+
}
|
|
1482
|
+
const formatE164 = /* @__PURE__ */ formatter('E164');
|
|
1483
|
+
const formatInternational = /* @__PURE__ */ formatter('INTERNATIONAL');
|
|
1484
|
+
const formatNational = /* @__PURE__ */ formatter('NATIONAL');
|
|
1485
|
+
const safePhoneNumber = /* @__PURE__ */ Object.assign(
|
|
1486
|
+
/* @__PURE__ */ makeSafe(parse, parseError), {
|
|
1487
|
+
coerce: /* @__PURE__ */ makeSafe(coerce, parseError),
|
|
1488
|
+
e164: /* @__PURE__ */ makeSafe(formatE164, formatError),
|
|
1489
|
+
international: /* @__PURE__ */ makeSafe(formatInternational, formatError),
|
|
1490
|
+
national: /* @__PURE__ */ makeSafe(formatNational, formatError),
|
|
1491
|
+
});
|
|
1492
|
+
const phoneNumber = /* @__PURE__ */ Object.assign(parse, {
|
|
1493
|
+
coerce,
|
|
1494
|
+
e164: formatE164,
|
|
1495
|
+
international: formatInternational,
|
|
1496
|
+
national: formatNational,
|
|
1497
|
+
safe: safePhoneNumber,
|
|
1498
|
+
});
|
|
1499
|
+
|
|
1500
|
+
class Deferred extends Promise {
|
|
1501
|
+
constructor() {
|
|
1502
|
+
let _resolve;
|
|
1503
|
+
let _reject;
|
|
1504
|
+
super((resolve, reject) => {
|
|
1505
|
+
_resolve = resolve;
|
|
1506
|
+
_reject = reject;
|
|
1507
|
+
});
|
|
1508
|
+
this.resolve = _resolve;
|
|
1509
|
+
this.reject = _reject;
|
|
1510
|
+
}
|
|
1511
|
+
asPromise() {
|
|
1512
|
+
return this;
|
|
1513
|
+
}
|
|
1514
|
+
}
|
|
1515
|
+
function isPromise(value) {
|
|
1516
|
+
return value instanceof Promise;
|
|
1517
|
+
}
|
|
1518
|
+
function isPromiseFulfilled(promise) {
|
|
1519
|
+
return promise.status === 'fulfilled';
|
|
1520
|
+
}
|
|
1521
|
+
function isPromiseRejected(promise) {
|
|
1522
|
+
return promise.status === 'rejected';
|
|
1523
|
+
}
|
|
1524
|
+
function waitTime(ms, signal) {
|
|
1525
|
+
return new Promise((resolve, reject) => {
|
|
1526
|
+
const timeoutId = setTimeout(() => resolve(), ms);
|
|
1527
|
+
signal?.addEventListener('abort', () => {
|
|
1528
|
+
clearTimeout(timeoutId);
|
|
1529
|
+
reject(new Error('Operation aborted'));
|
|
1530
|
+
}, { once: true });
|
|
1531
|
+
});
|
|
1532
|
+
}
|
|
1533
|
+
|
|
1534
|
+
function resolveRealmConfig(config, hostname) {
|
|
1535
|
+
if (isString(config)) {
|
|
1536
|
+
return config;
|
|
1537
|
+
}
|
|
1538
|
+
const realm = config.find((realmConf) => realmConf.hostname === hostname)?.realm;
|
|
1539
|
+
assertNonNullable(realm, 'realm');
|
|
1540
|
+
return realm;
|
|
1541
|
+
}
|
|
1542
|
+
|
|
1543
|
+
function removeFrontSlash(url) {
|
|
1544
|
+
if (url.startsWith('/')) {
|
|
1545
|
+
return url.slice(1);
|
|
1546
|
+
}
|
|
1547
|
+
return url;
|
|
1548
|
+
}
|
|
1549
|
+
function removeTrailingSlash(url) {
|
|
1550
|
+
if (url.endsWith('/')) {
|
|
1551
|
+
return url.slice(0, -1);
|
|
1552
|
+
}
|
|
1553
|
+
return url;
|
|
1554
|
+
}
|
|
1555
|
+
|
|
1556
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
1557
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1558
|
+
function reduceOfArr(fn, initialValue) {
|
|
1559
|
+
return (source) => source.pipe(initialValue
|
|
1560
|
+
? map((input) => input.reduce((prev, cur, index, array) => fn(prev, cur, index, array), initialValue()))
|
|
1561
|
+
: map((input) => input.reduce((prev, cur, index, array) => fn(prev, cur, index, array))));
|
|
1562
|
+
}
|
|
1563
|
+
|
|
1564
|
+
function reduceAndOfBoolArr() {
|
|
1565
|
+
return (source) => source.pipe(reduceOfArr((prev, cur) => prev && cur, () => true));
|
|
1566
|
+
}
|
|
1567
|
+
|
|
1568
|
+
function reduceOrOfBoolArr() {
|
|
1569
|
+
return (source) => source.pipe(reduceOfArr((prev, cur) => prev || cur, () => false));
|
|
1570
|
+
}
|
|
1571
|
+
|
|
1572
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1573
|
+
function flatMapOfArr(fn, thisArg) {
|
|
1574
|
+
return (source) => source.pipe(map((input) => input.flatMap((value, index, array) => fn(value, index, array), thisArg)));
|
|
1575
|
+
}
|
|
1576
|
+
|
|
1577
|
+
/* eslint-disable @typescript-eslint/no-unsafe-return */
|
|
1578
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1579
|
+
/**
|
|
1580
|
+
* Operator function to map each array element into an observable sequence,
|
|
1581
|
+
* emitting results in chunks based on the specified chunk size.
|
|
1582
|
+
* @param project A function to apply to each element of the source array.
|
|
1583
|
+
* @param conf Optional conf
|
|
1584
|
+
* @returns An operator function that maps input arrays to observable sequences,
|
|
1585
|
+
* emitting results in chunks based on the specified chunk size.
|
|
1586
|
+
*/
|
|
1587
|
+
function chunksMap(project, conf = {}) {
|
|
1588
|
+
const { chunkSize = Infinity } = conf;
|
|
1589
|
+
return mergeMap((input) => from(sliceIntoChunks(input, chunkSize)).pipe(
|
|
1590
|
+
// Concatenate each chunk's observables
|
|
1591
|
+
concatMap((chunk, index) => chunk.length
|
|
1592
|
+
? forkJoin(chunk.map((value) => project(value, index)))
|
|
1593
|
+
: []), toArray(),
|
|
1594
|
+
// Flatten the resulting array of arrays into a single array
|
|
1595
|
+
flatMapOfArr((arr) => arr)));
|
|
1596
|
+
}
|
|
1597
|
+
|
|
1598
|
+
function resolveValueOrObs(valueOrObs, scheduler) {
|
|
1599
|
+
const ob$ = isObservable(valueOrObs) ? valueOrObs : of(valueOrObs);
|
|
1600
|
+
return scheduler ? scheduled(ob$, scheduler) : ob$;
|
|
1601
|
+
}
|
|
1602
|
+
|
|
1603
|
+
function concatOfArr(external) {
|
|
1604
|
+
const external$ = resolveValueOrObs(external);
|
|
1605
|
+
return (source) => source.pipe(withLatestFrom(external$, (input, items) => [...input].concat(items)));
|
|
1606
|
+
}
|
|
1607
|
+
|
|
1608
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1609
|
+
function everyOfArr(fn, thisArg) {
|
|
1610
|
+
return (source) => source.pipe(map((input) => input.every((value, index, array) => fn(value, index, array), thisArg)));
|
|
1611
|
+
}
|
|
1612
|
+
|
|
1613
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1614
|
+
function filterOfArr(fn, thisArg) {
|
|
1615
|
+
return (source) => source.pipe(map((input) => input.filter((value, index, array) => fn(value, index, array), thisArg)));
|
|
1616
|
+
}
|
|
1617
|
+
|
|
1618
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1619
|
+
function findOfArr(fn, thisArg) {
|
|
1620
|
+
return (source) => source.pipe(map((input) => input.find((value, index, array) => fn(value, index, array), thisArg)));
|
|
1621
|
+
}
|
|
1622
|
+
|
|
1623
|
+
function firstOfArr(defaultValue) {
|
|
1624
|
+
return (source) => source.pipe(map(([firstInput]) => firstInput ?? defaultValue));
|
|
1625
|
+
}
|
|
1626
|
+
|
|
1627
|
+
function groupByOfArr(keyGetter) {
|
|
1628
|
+
return (source) => source.pipe(map((input) => groupBy(input, keyGetter)));
|
|
1629
|
+
}
|
|
1630
|
+
|
|
1631
|
+
function includesOfArr(searchElement, fromIndex) {
|
|
1632
|
+
return (source) => source.pipe(map((input) => input.includes(searchElement, fromIndex)));
|
|
1633
|
+
}
|
|
1634
|
+
|
|
1635
|
+
function indexOfArr(searchElement, fromIndex) {
|
|
1636
|
+
return (source) => source.pipe(map((input) => input.indexOf(searchElement, fromIndex)));
|
|
1637
|
+
}
|
|
1638
|
+
|
|
1639
|
+
function joinOfArr(separator) {
|
|
1640
|
+
return (source) => source.pipe(map((input) => input.join(separator)));
|
|
1641
|
+
}
|
|
1642
|
+
|
|
1643
|
+
function lastOfArr(defaultValue) {
|
|
1644
|
+
return (source) => source.pipe(map((array) => array[array.length - 1] ?? defaultValue));
|
|
1645
|
+
}
|
|
1646
|
+
|
|
1647
|
+
function lastIndexOfArr(searchElement, fromIndex) {
|
|
1648
|
+
return (source) => source.pipe(map((input) => input.lastIndexOf(searchElement, fromIndex ?? input.length - 1)));
|
|
1649
|
+
}
|
|
1650
|
+
|
|
1651
|
+
function lengthOfArr() {
|
|
1652
|
+
return (source) => source.pipe(map((input) => input.length));
|
|
1653
|
+
}
|
|
1654
|
+
|
|
1655
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1656
|
+
function mapOfArr(fn, thisArg) {
|
|
1657
|
+
return (source) => source.pipe(map((input) => input.map((value, index, array) => fn(value, index, array), thisArg)));
|
|
1658
|
+
}
|
|
1659
|
+
|
|
1660
|
+
function operationOfArr(operand) {
|
|
1661
|
+
return (source) => source.pipe(mergeMap((arr) => arr.length
|
|
1662
|
+
? of(arr).pipe(reduceOfArr((prev, cur) => operand(prev, cur)))
|
|
1663
|
+
: of(0)));
|
|
1664
|
+
}
|
|
1665
|
+
|
|
1666
|
+
function subOfArr() {
|
|
1667
|
+
return (source) => source.pipe(operationOfArr((prev, cur) => prev - cur));
|
|
1668
|
+
}
|
|
1669
|
+
|
|
1670
|
+
function sumOfArr() {
|
|
1671
|
+
return (source) => source.pipe(operationOfArr((prev, cur) => prev + cur));
|
|
1672
|
+
}
|
|
1673
|
+
|
|
1674
|
+
function reverseOfArr() {
|
|
1675
|
+
return (source) => source.pipe(map((input) => [...input].reverse()));
|
|
1676
|
+
}
|
|
1677
|
+
|
|
1678
|
+
function sliceOfArr(start, end) {
|
|
1679
|
+
return (source) => source.pipe(map((input) => input.slice(start, end)));
|
|
1680
|
+
}
|
|
1681
|
+
|
|
1682
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1683
|
+
function someOfArr(fn, thisArg) {
|
|
1684
|
+
return (source) => source.pipe(map((input) => input.some((value, index, array) => fn(value, index, array), thisArg)));
|
|
1685
|
+
}
|
|
1686
|
+
|
|
1687
|
+
function sortOfArr(fn) {
|
|
1688
|
+
return (source) => source.pipe(map((input) => [...input].sort(fn)));
|
|
1689
|
+
}
|
|
1690
|
+
|
|
1691
|
+
function switchMapOfArr(fn) {
|
|
1692
|
+
return (source) => source.pipe(switchMap((arr) => forkJoin(arr.map((item, index) => fn(item, index)))));
|
|
1693
|
+
}
|
|
1694
|
+
|
|
1695
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1696
|
+
function tapOfArr(fn, thisArg) {
|
|
1697
|
+
return (source) => source.pipe(tap((input) => input.forEach((value, index, array) => fn(value, index, array), thisArg)));
|
|
1698
|
+
}
|
|
1699
|
+
|
|
1700
|
+
const ONCE = /* @__PURE__ */ new Observable((subscriber) => {
|
|
1701
|
+
subscriber.next();
|
|
1702
|
+
subscriber.complete();
|
|
1703
|
+
});
|
|
1704
|
+
|
|
1705
|
+
function mapToVoid() {
|
|
1706
|
+
return (source) => source.pipe(mergeMap(() => ONCE));
|
|
1707
|
+
}
|
|
1708
|
+
|
|
1709
|
+
function filterMasked(predicate) {
|
|
1710
|
+
return pipe(filter(predicate), mapToVoid());
|
|
1711
|
+
}
|
|
1712
|
+
|
|
1713
|
+
/**
|
|
1714
|
+
* filter the falsy value
|
|
1715
|
+
*/
|
|
1716
|
+
function filterFalsy() {
|
|
1717
|
+
return filterMasked((value) => !value);
|
|
1718
|
+
}
|
|
1719
|
+
|
|
1720
|
+
/**
|
|
1721
|
+
* filter the truthy value
|
|
1722
|
+
*/
|
|
1723
|
+
function filterTruthy() {
|
|
1724
|
+
return filterMasked((value) => value);
|
|
1725
|
+
}
|
|
1726
|
+
|
|
1727
|
+
function delayOrNoop(time) {
|
|
1728
|
+
return time > 0 ? delay(time) : identity$1;
|
|
1729
|
+
}
|
|
1730
|
+
|
|
1731
|
+
function documentVisibilityState(document) {
|
|
1732
|
+
return fromEvent(document, 'visibilitychange').pipe(startWith(document.visibilityState), map(() => document.visibilityState), distinctUntilChanged());
|
|
1733
|
+
}
|
|
1734
|
+
|
|
1735
|
+
function exhaustMapBy(selector, project) {
|
|
1736
|
+
const active = new Set();
|
|
1737
|
+
return (source) => source.pipe(mergeMap((value, index) => {
|
|
1738
|
+
const selected = selector(value);
|
|
1739
|
+
if (active.has(selected)) {
|
|
1740
|
+
return EMPTY;
|
|
1741
|
+
}
|
|
1742
|
+
active.add(selected);
|
|
1743
|
+
return from(project(value, index)).pipe(finalize(() => active.delete(selected)));
|
|
1744
|
+
}));
|
|
1745
|
+
}
|
|
1746
|
+
|
|
1747
|
+
/** Fetches and aggregates every page returned by a paginated source. */
|
|
1748
|
+
function fetchAllPaginatedElements(source, options = {}) {
|
|
1749
|
+
const { pageSize = 100, chunkSize = 5, onEvent = noop } = options;
|
|
1750
|
+
const destroy$ = new Subject();
|
|
1751
|
+
const abort = () => destroy$.next();
|
|
1752
|
+
const notify = (event) => onEvent(event, abort);
|
|
1753
|
+
const intoPagination = (page) => ({ page, pageSize });
|
|
1754
|
+
const firstPagination = intoPagination(1);
|
|
1755
|
+
return from(source(firstPagination, 0)).pipe(mergeMap((page) => {
|
|
1756
|
+
const { items, total } = page;
|
|
1757
|
+
notify({ page, pagination: firstPagination, chunkIndex: 0 });
|
|
1758
|
+
const totalPages = Math.ceil(total / pageSize);
|
|
1759
|
+
const paginations = Array.from({ length: totalPages - 1 }, (_, index) => intoPagination(index + 2));
|
|
1760
|
+
return of(paginations).pipe(chunksMap((pagination, index, chunkIndex = index + 1) => from(source(pagination, chunkIndex)).pipe(tap((page) => notify({ page, pagination, chunkIndex }))), { chunkSize }), reduceOfArr((acc, { items }) => acc.concat(items), () => items));
|
|
1761
|
+
}), takeUntil(destroy$), finalize(() => destroy$.complete()));
|
|
1762
|
+
}
|
|
1763
|
+
|
|
1764
|
+
function filterNotNullable() {
|
|
1765
|
+
return (source) => source.pipe(filter(isNotNullable));
|
|
1766
|
+
}
|
|
1767
|
+
|
|
1768
|
+
function filterNullable() {
|
|
1769
|
+
return (source) => source.pipe(filter(isNullable));
|
|
1770
|
+
}
|
|
1771
|
+
|
|
1772
|
+
function inactivity(dueTime, document) {
|
|
1773
|
+
return merge(fromEvent(document, 'mousemove'), fromEvent(document, 'keydown'), fromEvent(document, 'scroll'), fromEvent(document, 'touchstart')).pipe(switchMap(() => timer(dueTime).pipe(mapToVoid())));
|
|
1774
|
+
}
|
|
1775
|
+
|
|
1776
|
+
function minShowTime(minTrueDelay, // delay before emitting true
|
|
1777
|
+
minTrueVisible) {
|
|
1778
|
+
let lastTrueTime = 0;
|
|
1779
|
+
let hasEmittedTrue = false;
|
|
1780
|
+
return (source) => source.pipe(switchMap((value) => {
|
|
1781
|
+
if (value) {
|
|
1782
|
+
// Case TRUE:
|
|
1783
|
+
// - wait at least minTrueDelay before emitting true
|
|
1784
|
+
// - reset lastTrueTime when true is emitted
|
|
1785
|
+
return timer(minTrueDelay).pipe(map(() => {
|
|
1786
|
+
lastTrueTime = Date.now();
|
|
1787
|
+
hasEmittedTrue = true;
|
|
1788
|
+
return true;
|
|
1789
|
+
}));
|
|
1790
|
+
}
|
|
1791
|
+
else {
|
|
1792
|
+
if (hasEmittedTrue) {
|
|
1793
|
+
// Case FALSE after a true was already emitted:
|
|
1794
|
+
// - calculate elapsed time since last true
|
|
1795
|
+
// - if less than minTrueVisible, wait the remaining time
|
|
1796
|
+
const elapsed = Date.now() - lastTrueTime;
|
|
1797
|
+
const wait = Math.max(minTrueVisible - elapsed, 0);
|
|
1798
|
+
return timer(wait).pipe(map(() => {
|
|
1799
|
+
hasEmittedTrue = false;
|
|
1800
|
+
return false;
|
|
1801
|
+
}));
|
|
1802
|
+
}
|
|
1803
|
+
else {
|
|
1804
|
+
// Case FALSE without a previous true:
|
|
1805
|
+
// - emit immediately
|
|
1806
|
+
return of(false);
|
|
1807
|
+
}
|
|
1808
|
+
}
|
|
1809
|
+
}));
|
|
1810
|
+
}
|
|
1811
|
+
|
|
1812
|
+
function maxOfNum(max$) {
|
|
1813
|
+
return (source) => source.pipe(withLatestFrom(resolveValueOrObs(max$)), map(([input, max]) => Math.max(input, max)));
|
|
1814
|
+
}
|
|
1815
|
+
|
|
1816
|
+
function observeMutation(element, options) {
|
|
1817
|
+
return fromEventPattern((handler) => {
|
|
1818
|
+
const observer = new MutationObserver(handler);
|
|
1819
|
+
observer.observe(element, options);
|
|
1820
|
+
return () => observer.disconnect();
|
|
1821
|
+
});
|
|
1822
|
+
}
|
|
1823
|
+
|
|
1824
|
+
function observeResize(element, options) {
|
|
1825
|
+
return fromEventPattern((handler) => {
|
|
1826
|
+
const observer = new ResizeObserver(handler);
|
|
1827
|
+
observer.observe(element, options);
|
|
1828
|
+
return () => observer.disconnect();
|
|
1829
|
+
});
|
|
1830
|
+
}
|
|
1831
|
+
|
|
1832
|
+
function switchMapBy(selector, project) {
|
|
1833
|
+
return (source) => source.pipe(mergeMap((value, index) => from(project(value, index)).pipe(takeUntil(source.pipe(filter((input) => selector(input) === selector(value)))))));
|
|
1834
|
+
}
|
|
1835
|
+
|
|
1836
|
+
function switchMapConcurrent(project, conf) {
|
|
1837
|
+
const { abort$, concurrent = Infinity, onEmpty } = conf ?? {};
|
|
1838
|
+
return (source) => new Observable((subscriber) => {
|
|
1839
|
+
const queue = new CircularQueue(concurrent);
|
|
1840
|
+
const cancelSub = abort$?.().subscribe(() => {
|
|
1841
|
+
queue.clear().forEach((sub) => sub.unsubscribe());
|
|
1842
|
+
});
|
|
1843
|
+
const outerSub = source.subscribe({
|
|
1844
|
+
next: (value) => {
|
|
1845
|
+
const inner$ = project(value);
|
|
1846
|
+
// eslint-disable-next-line rxjs/no-nested-subscribe
|
|
1847
|
+
const innerSub = inner$.subscribe({
|
|
1848
|
+
next: (val) => subscriber.next(val),
|
|
1849
|
+
error: (err) => subscriber.error(err),
|
|
1850
|
+
complete: () => {
|
|
1851
|
+
queue.remove(innerSub);
|
|
1852
|
+
if (queue.length === 0)
|
|
1853
|
+
onEmpty?.();
|
|
1854
|
+
},
|
|
1855
|
+
});
|
|
1856
|
+
const removedSub = queue.enqueue(innerSub);
|
|
1857
|
+
removedSub?.unsubscribe();
|
|
1858
|
+
},
|
|
1859
|
+
error: (err) => subscriber.error(err),
|
|
1860
|
+
complete: () => {
|
|
1861
|
+
const checkComplete = () => {
|
|
1862
|
+
if (queue.length === 0)
|
|
1863
|
+
subscriber.complete();
|
|
1864
|
+
};
|
|
1865
|
+
queue.toArray().forEach((sub) => sub.add(checkComplete));
|
|
1866
|
+
checkComplete();
|
|
1867
|
+
},
|
|
1868
|
+
});
|
|
1869
|
+
return () => {
|
|
1870
|
+
outerSub.unsubscribe();
|
|
1871
|
+
cancelSub?.unsubscribe();
|
|
1872
|
+
queue.clear().forEach((sub) => sub.unsubscribe());
|
|
1873
|
+
};
|
|
1874
|
+
});
|
|
1875
|
+
}
|
|
1876
|
+
|
|
1877
|
+
function throwIfNot(predicate, errorFactory) {
|
|
1878
|
+
return (source) => source.pipe(mergeMap((input) => {
|
|
1879
|
+
if (predicate(input)) {
|
|
1880
|
+
return of(input);
|
|
1881
|
+
}
|
|
1882
|
+
return throwError(() => errorFactory?.() ?? new Error('An unexpected error occurred.'));
|
|
1883
|
+
}));
|
|
1884
|
+
}
|
|
1885
|
+
|
|
1886
|
+
function throwIfNullable(errorFactory) {
|
|
1887
|
+
return (source) => source.pipe(throwIfNot(Boolean, errorFactory));
|
|
1888
|
+
}
|
|
1889
|
+
|
|
1890
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
1891
|
+
|
|
1892
|
+
//Thanks https://github.com/Microsoft/TypeScript/issues/4895#issuecomment-401067935
|
|
1893
|
+
//This allow to define Opaque types, hence defining easily newtypes
|
|
1894
|
+
|
|
1895
|
+
function resolveValueOrArr(prop) {
|
|
1896
|
+
return isArray(prop) ? prop : [prop];
|
|
1897
|
+
}
|
|
1898
|
+
|
|
1899
|
+
function resolveValueOrPromise(valueOrPromise) {
|
|
1900
|
+
return isPromise(valueOrPromise)
|
|
1901
|
+
? valueOrPromise
|
|
1902
|
+
: Promise.resolve(valueOrPromise);
|
|
1903
|
+
}
|
|
1904
|
+
|
|
1905
|
+
/**
|
|
1906
|
+
* Generated bundle index. Do not edit.
|
|
1907
|
+
*/
|
|
1908
|
+
|
|
1909
|
+
export { ActionRef, ActionRefError, CircularQueue, DEFAULT_EMPTY_STRING_VALUE, Deferred, FlatTree, LazyWeakMap, ONCE, addMany, addOne, addWorkingDays, alwaysFalse, alwaysTrue, arrayConf, asDataVolume, ascComparer, assertNonEmpty, assertNonNullable, assertValid, capitalize, chunksMap, cidrComparer, cidrIsNetworkAddress, clamp, clampDate, coerceNumber, coerceUserName, compareKeysWith, compareUsingTemplate, completeDiff, composeComparer, concatOfArr, createDebouncedFn, deepEqual, deepEqualComparer, defaultMatcher, defaultStringify, delayOrNoop, deleteImmutable, descComparer, diff, diffWith, distinct, documentVisibilityState, dropLast, ensureSafeNumber, entries, everyOfArr, exhaustMapBy, fetchAllPaginatedElements, filterFalsy, filterMasked, filterNotNullable, filterNullable, filterOfArr, filterTruthy, findOfArr, firstOfArr, flatMapOfArr, formatAddress, formatDuration, formatFileSize, formatFrenchPhoneNumber, formatPeriod, formatPhoneNumber, formatUserInitial, formatUserName, getCenteredSlice, getDeepest, getIpv4HostAddress, getIpv4NetworkAddressFromCidr, getIterationMeta, getPhoneNumberUtil, goToOctet, groupBy, groupByOfArr, hasNonNullValues, identity, ignoreAccent, inactivity, includesOfArr, indexOfArr, initial, insertAt, intersect, ipv4BelongsToCidr, isArray, isBase64Image, isBefore, isBeforeNow, isBoolArray, isBoolean, isDate, isDateInRange, isEmail, isEmpty, isError, isFn, isKey, isNonEmpty, isNotNull, isNotNullable, isNotUndefined, isNull, isNullable, isNumber, isNumberArray, isObject, isPromise, isPromiseFulfilled, isPromiseRejected, isString, isStringArray, isUndefined, isWorkingDay, joinNonEmpty, joinOfArr, koToOctet, lastIndexOfArr, lastOfArr, lengthOfArr, linkify, localeComparer, luhn, makeSafe, mapFileToBase64, mapNonEmptyArray, mapOfArr, mapToVoid, mapValues, maxOfNum, mergeWith, minShowTime, moToOctet, normalize, num2digit, observeMutation, observeResize, operationOfArr, parseFrenchPhoneNumber, parseNumber, percentage, phoneNumber, prettyBytes, reduceAndOfBoolArr, reduceOfArr, reduceOrOfBoolArr, removeFrontSlash, removeMany, removeOne, removeTrailingSlash, resolveRealmConfig, resolveValueOrArr, resolveValueOrFn, resolveValueOrObs, resolveValueOrPromise, reverseOfArr, safeCastFromStringArr, separateIntegerDecimal, setAtPath, setMany, setOne, sliceIntoChunks, sliceOfArr, someOfArr, sortOfArr, strictComparer, strictTitleCase, stringChunks, stringifyComparer, subOfArr, sumOfArr, switchMapBy, switchMapConcurrent, switchMapOfArr, tapOfArr, throwIfNot, throwIfNullable, toCamelCase, toGo, toKo, toMo, toNonEmpty, toReadableVolume, toTo, toToOctet, truncate, updateMany, updateOne, waitTime, withDefault, withDuration, withPhoneNumber, wrapNullishComparer, zipToObject };
|
|
1910
|
+
//# sourceMappingURL=netwo-devkit.mjs.map
|