@hkdigital/lib-sveltekit 0.0.41 → 0.0.44

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.
@@ -0,0 +1,171 @@
1
+ /**
2
+ * Convert a value to an array
3
+ * - Converts an Arguments object to plain JS array
4
+ * - If the value is undefined or null, an empty array is returned
5
+ * - A primitive value is "wrapped" in an array
6
+ *
7
+ * @param {mixed} value - Item to convert
8
+ *
9
+ * @param {number} [start]
10
+ * Index of the array to start extraction.
11
+ * (the first element of the array is at index 0)
12
+ *
13
+ * @param {number} [end]
14
+ * Index of the array where the extraction should end
15
+ *
16
+ * --
17
+ *
18
+ * @returns {Array} array
19
+ *
20
+ * --
21
+ *
22
+ * @example
23
+ * toArray( [ 1, 2, 3, 4, 5 ], 2, 4 ) returns [ 3, 4 ]
24
+ */
25
+ export function toArray(value: mixed, start?: number, end?: number): any[];
26
+ /**
27
+ * Convert an async iterator to an array
28
+ * - If no async iterator is passed, the value will be processed by `from()`
29
+ *
30
+ * @param {AsyncIterator|mixed} value
31
+ * Async iterator or value to convert to array
32
+ *
33
+ * @returns {Array} list of items returned by the iterator
34
+ */
35
+ export function toArrayAsync(value: AsyncIterator<any, any, any> | mixed): any[];
36
+ /**
37
+ * Convert a path string to an array path
38
+ * - The path string will be spit at the `pathSeparator` token
39
+ * - If the supplied path is already an array, the original array will
40
+ * be returned
41
+ *
42
+ * @param {string|string[]} path
43
+ * String or array path (e.g. "some.path.to")
44
+ *
45
+ * @param {string} [pathSeparator=PATH_SEPARATOR]
46
+ * A custom path separator to use instead of the default "."
47
+ *
48
+ * @returns {string[]} array path (e.g. ["some", "path", "to"])
49
+ */
50
+ export function toArrayPath(path: string | string[], pathSeparator?: string): string[];
51
+ /**
52
+ * Loop over the supplied array and call the callback for every element
53
+ * - The callback will receive the current element of the array as
54
+ * first argument
55
+ * - Via [additionalArguments], additional arguments may be supplied that
56
+ * will be passed to the callback function
57
+ *
58
+ * @param {array} arr - The array to loop
59
+ * @param {function} callback - Callback to call for every element
60
+ * @param {array} additionalArguments The additional arguments
61
+ */
62
+ export function loop(arr: any[], callback: Function, additionalArguments: any[]): void;
63
+ /**
64
+ * Get a list of all values from the items in the array at the
65
+ * specified path
66
+ *
67
+ * @param {object[]} items
68
+ *
69
+ * @param {object} [options]
70
+ *
71
+ * @param {boolean} [options.outputAsSet=false]
72
+ * Output a Set instead of an array
73
+ *
74
+ * @param {string} [options.pathSeparator=PATH_SEPARATOR]
75
+ * A custom path separator to use instead of the default "."
76
+ *
77
+ *
78
+ *
79
+ * @returns {mixed[]} values
80
+ */
81
+ export function pathValues(items: object[], path: any, options?: {
82
+ outputAsSet?: boolean;
83
+ pathSeparator?: string;
84
+ }): mixed[];
85
+ /**
86
+ * Sort function that sorts a list of objects by values encountered at the
87
+ * specified key values of the object.
88
+ * - Sorts array inline (no new array is returned)
89
+ * - This method is faster than `sortByPathValue` since the value lookup in the
90
+ * items can be done faster
91
+ *
92
+ * @param {Object[]} items - List of items to sort
93
+ *
94
+ * @param {string} key
95
+ * Object key to use for getting the values in the items to compare.
96
+ *
97
+ * @param {function} [compareFn=smallestFirst]
98
+ * Function to use to compare values. See `compare.js`.
99
+ */
100
+ export function sortByKeyValue(items: any[], key: string, compareFn?: Function): void;
101
+ /**
102
+ * Sort function that sorts a list of objects by values encountered at the
103
+ * specified key values of the object.
104
+ * - Sorts array inline (no new array is returned)
105
+ * - This method is faster than `sortByPathValue` since the value lookup in the
106
+ * items can be done faster
107
+ *
108
+ * @param {Object[]} items - List of items to sort
109
+ *
110
+ * @param {string} key
111
+ * Object key to use for getting the values in the items to compare.
112
+ *
113
+ * @param {function} [compareFn=smallestFirst]
114
+ * Function to use to compare values. See `compare.js`.
115
+ */
116
+ export function sortByKeyValueReversed(items: any[], key: string, compareFn?: Function): void;
117
+ /**
118
+ * Sort function that sorts a list of objects by values encountered at the
119
+ * specified object path.
120
+ * - Sorts array inline (no new array is returned)
121
+ *
122
+ * @param {Object[]} items - List of items to sort
123
+ *
124
+ * @param {string[]|string} path
125
+ * Path to use for getting the values in the items to compare.
126
+ * If a string path has been supplied, the default path separator
127
+ * (PATH_SEPARATOR) is assumed. Use `toArrayPath` to convert paths with
128
+ * custom path separators.
129
+ *
130
+ * @param {function} [compareFn=smallestFirst]
131
+ * Function to use to compare values. See `compare.js`.
132
+ */
133
+ export function sortByPathValue(items: any[], path: string[] | string, compareFn?: Function): void;
134
+ /**
135
+ * Find the first item in the list of objects that matches the selector
136
+ * - All items in the supplied array must be objects
137
+ *
138
+ * @param {object[]} arr
139
+ * @param {object|null} selector
140
+ *
141
+ * @returns {object|null} first matched item
142
+ */
143
+ export function findFirst(arr: object[], selector: object | null): object | null;
144
+ /**
145
+ * Returns all items from the list of items that match the selector
146
+ * - All items in the supplied array must be objects
147
+ *
148
+ * @param {object[]} arr
149
+ * @param {object|null} selector
150
+ *
151
+ * @returns {object[]} matching items
152
+ */
153
+ export function findAll(arr: object[], selector: object | null): object[];
154
+ /**
155
+ * Convert array to an object using a list of keys for each index
156
+ *
157
+ * @param {array} arr
158
+ * @param {string[]} keys
159
+ *
160
+ * @returns {object}
161
+ */
162
+ export function arrayToObject(arr: any[], keys: string[]): object;
163
+ export { PATH_SEPARATOR } from "../object/index.js";
164
+ import { smallestFirst } from '../compare/index.js';
165
+ import { largestFirst } from '../compare/index.js';
166
+ export const arraySlice: (start?: number, end?: number) => any[];
167
+ export const arrayConcat: {
168
+ (...items: ConcatArray<any>[]): any[];
169
+ (...items: any[]): any[];
170
+ };
171
+ export { smallestFirst, largestFirst };
@@ -0,0 +1,432 @@
1
+ /* ------------------------------------------------------------------ Imports */
2
+
3
+ import * as expect from '../expect/index.js';
4
+
5
+ import { smallestFirst, largestFirst } from '../compare/index.js';
6
+
7
+ import { objectGet, PATH_SEPARATOR } from '../object/index.js';
8
+
9
+ import Selector from '../../classes/data/Selector.js';
10
+
11
+ /* ---------------------------------------------------------------- Internals */
12
+
13
+ const arraySlice = Array.prototype.slice;
14
+ const arrayConcat = Array.prototype.concat;
15
+
16
+ /* ------------------------------------------------------------------ Exports */
17
+
18
+ export { PATH_SEPARATOR } from '../object/index.js';
19
+
20
+ export { smallestFirst, largestFirst };
21
+
22
+ export { arraySlice, arrayConcat };
23
+
24
+ // -----------------------------------------------------------------------------
25
+
26
+ /**
27
+ * Convert a value to an array
28
+ * - Converts an Arguments object to plain JS array
29
+ * - If the value is undefined or null, an empty array is returned
30
+ * - A primitive value is "wrapped" in an array
31
+ *
32
+ * @param {mixed} value - Item to convert
33
+ *
34
+ * @param {number} [start]
35
+ * Index of the array to start extraction.
36
+ * (the first element of the array is at index 0)
37
+ *
38
+ * @param {number} [end]
39
+ * Index of the array where the extraction should end
40
+ *
41
+ * --
42
+ *
43
+ * @returns {Array} array
44
+ *
45
+ * --
46
+ *
47
+ * @example
48
+ * toArray( [ 1, 2, 3, 4, 5 ], 2, 4 ) returns [ 3, 4 ]
49
+ */
50
+ export function toArray(value, start, end) {
51
+ if (Array.isArray(value)) {
52
+ //
53
+ // Do not clone input value if it is already an array
54
+ //
55
+ // @note this behaviour differs from Array.from
56
+ //
57
+ if (undefined === start) {
58
+ return value;
59
+ }
60
+
61
+ return value.slice(start, end);
62
+ }
63
+
64
+ if (undefined === value || null === value) {
65
+ //
66
+ // Return an empty array if value is undefined or null
67
+ //
68
+ // ==> this behaviour differs from Array.from() <==
69
+ //
70
+ return [];
71
+ } else if (!(value instanceof Object)) {
72
+ //
73
+ // Wrap (non-object) in array (e.g. a string)
74
+ //
75
+ // @note this behaviour differs from Array.from
76
+ //
77
+ return [value];
78
+ }
79
+
80
+ if (undefined === start) {
81
+ //
82
+ // Construct a new array from the value
83
+ //
84
+ return Array.from(value);
85
+ } else {
86
+ return arraySlice(value, start, end);
87
+ }
88
+ }
89
+
90
+ // -----------------------------------------------------------------------------
91
+
92
+ /**
93
+ * Convert an async iterator to an array
94
+ * - If no async iterator is passed, the value will be processed by `from()`
95
+ *
96
+ * @param {AsyncIterator|mixed} value
97
+ * Async iterator or value to convert to array
98
+ *
99
+ * @returns {Array} list of items returned by the iterator
100
+ */
101
+ export async function toArrayAsync(value) {
102
+ if (value instanceof Object && typeof value[Symbol.asyncIterator] === 'function') {
103
+ // value is an async iterator
104
+
105
+ const arr = [];
106
+
107
+ for await (const item of value) {
108
+ arr.push(item);
109
+ }
110
+
111
+ return arr;
112
+ } else {
113
+ // value is not an async iterator
114
+
115
+ return toArray(value);
116
+ }
117
+ }
118
+
119
+ // -----------------------------------------------------------------------------
120
+
121
+ /**
122
+ * Convert a path string to an array path
123
+ * - The path string will be spit at the `pathSeparator` token
124
+ * - If the supplied path is already an array, the original array will
125
+ * be returned
126
+ *
127
+ * @param {string|string[]} path
128
+ * String or array path (e.g. "some.path.to")
129
+ *
130
+ * @param {string} [pathSeparator=PATH_SEPARATOR]
131
+ * A custom path separator to use instead of the default "."
132
+ *
133
+ * @returns {string[]} array path (e.g. ["some", "path", "to"])
134
+ */
135
+ export function toArrayPath(path, pathSeparator = PATH_SEPARATOR) {
136
+ if (typeof path === 'string') {
137
+ return path.split(pathSeparator);
138
+ } else if (Array.isArray(path)) {
139
+ // path is already an array
140
+ return path;
141
+ } else {
142
+ throw new Error('Missing or invalid parameter [path] (expected string or array)');
143
+ }
144
+ }
145
+
146
+ // -----------------------------------------------------------------------------
147
+
148
+ /**
149
+ * Loop over the supplied array and call the callback for every element
150
+ * - The callback will receive the current element of the array as
151
+ * first argument
152
+ * - Via [additionalArguments], additional arguments may be supplied that
153
+ * will be passed to the callback function
154
+ *
155
+ * @param {array} arr - The array to loop
156
+ * @param {function} callback - Callback to call for every element
157
+ * @param {array} additionalArguments The additional arguments
158
+ */
159
+ export function loop(arr, callback, additionalArguments) {
160
+ expect.function(callback);
161
+
162
+ if (!arr) {
163
+ return;
164
+ }
165
+
166
+ expect.array(arr);
167
+
168
+ if (!arr.length) {
169
+ // Nothing to do
170
+ return;
171
+ }
172
+
173
+ // >> CASE A: no additional arguments
174
+
175
+ if (!additionalArguments) {
176
+ for (let j = 0, n = arr.length; j < n; j = j + 1) {
177
+ callback(arr[j]);
178
+ }
179
+
180
+ return;
181
+ }
182
+
183
+ // >> CASE B: additional arguments
184
+
185
+ expect.arrayLike(additionalArguments);
186
+
187
+ const args = [null, ...additionalArguments];
188
+
189
+ for (let j = 0, n = arr.length; j < n; j = j + 1) {
190
+ args[0] = arr[j];
191
+ callback(...args);
192
+ }
193
+ }
194
+
195
+ // -----------------------------------------------------------------------------
196
+
197
+ /**
198
+ * Get a list of all values from the items in the array at the
199
+ * specified path
200
+ *
201
+ * @param {object[]} items
202
+ *
203
+ * @param {object} [options]
204
+ *
205
+ * @param {boolean} [options.outputAsSet=false]
206
+ * Output a Set instead of an array
207
+ *
208
+ * @param {string} [options.pathSeparator=PATH_SEPARATOR]
209
+ * A custom path separator to use instead of the default "."
210
+ *
211
+ *
212
+ *
213
+ * @returns {mixed[]} values
214
+ */
215
+ export function pathValues(items, path, options = {}) {
216
+ // == Process parameters
217
+
218
+ expect.array(items);
219
+
220
+ const { outputAsSet = false, pathSeparator = PATH_SEPARATOR } = options;
221
+
222
+ if (typeof path === 'string') {
223
+ path = toArrayPath(path, pathSeparator);
224
+ } else {
225
+ expect.stringArray(path);
226
+ }
227
+
228
+ // >> CASE A: Output as plain Array
229
+
230
+ if (!outputAsSet) {
231
+ const values = [];
232
+
233
+ for (let j = 0, n = items.length; j < n; j = j + 1) {
234
+ const item = items[j];
235
+
236
+ expect.object(item);
237
+
238
+ values.push(objectGet(item, path));
239
+ }
240
+
241
+ return values;
242
+ }
243
+
244
+ // >> CASE B: Output as Set
245
+
246
+ const values = new Set();
247
+
248
+ for (let j = 0, n = items.length; j < n; j = j + 1) {
249
+ const item = items[j];
250
+
251
+ expect.object(item);
252
+
253
+ values.add(objectGet(item, path));
254
+ }
255
+
256
+ return values;
257
+ }
258
+
259
+ // -----------------------------------------------------------------------------
260
+
261
+ /**
262
+ * Sort function that sorts a list of objects by values encountered at the
263
+ * specified key values of the object.
264
+ * - Sorts array inline (no new array is returned)
265
+ * - This method is faster than `sortByPathValue` since the value lookup in the
266
+ * items can be done faster
267
+ *
268
+ * @param {Object[]} items - List of items to sort
269
+ *
270
+ * @param {string} key
271
+ * Object key to use for getting the values in the items to compare.
272
+ *
273
+ * @param {function} [compareFn=smallestFirst]
274
+ * Function to use to compare values. See `compare.js`.
275
+ */
276
+ export function sortByKeyValue(items, key, compareFn = smallestFirst) {
277
+ expect.function(compareFn);
278
+
279
+ expect.array(items);
280
+
281
+ expect.string(key);
282
+
283
+ items.sort((itemA, itemB) => {
284
+ return compareFn(itemA[key], itemB[key]);
285
+ });
286
+ }
287
+
288
+ // -----------------------------------------------------------------------------
289
+
290
+ /**
291
+ * Sort function that sorts a list of objects by values encountered at the
292
+ * specified key values of the object.
293
+ * - Sorts array inline (no new array is returned)
294
+ * - This method is faster than `sortByPathValue` since the value lookup in the
295
+ * items can be done faster
296
+ *
297
+ * @param {Object[]} items - List of items to sort
298
+ *
299
+ * @param {string} key
300
+ * Object key to use for getting the values in the items to compare.
301
+ *
302
+ * @param {function} [compareFn=smallestFirst]
303
+ * Function to use to compare values. See `compare.js`.
304
+ */
305
+ export function sortByKeyValueReversed(items, key, compareFn = largestFirst) {
306
+ expect.function(compareFn);
307
+
308
+ expect.array(items);
309
+
310
+ expect.string(key);
311
+
312
+ items.sort((itemA, itemB) => {
313
+ return compareFn(itemA[key], itemB[key]);
314
+ });
315
+ }
316
+
317
+ // -----------------------------------------------------------------------------
318
+
319
+ /**
320
+ * Sort function that sorts a list of objects by values encountered at the
321
+ * specified object path.
322
+ * - Sorts array inline (no new array is returned)
323
+ *
324
+ * @param {Object[]} items - List of items to sort
325
+ *
326
+ * @param {string[]|string} path
327
+ * Path to use for getting the values in the items to compare.
328
+ * If a string path has been supplied, the default path separator
329
+ * (PATH_SEPARATOR) is assumed. Use `toArrayPath` to convert paths with
330
+ * custom path separators.
331
+ *
332
+ * @param {function} [compareFn=smallestFirst]
333
+ * Function to use to compare values. See `compare.js`.
334
+ */
335
+ export function sortByPathValue(items, path, compareFn = smallestFirst) {
336
+ expect.function(compareFn);
337
+
338
+ expect.array(items);
339
+
340
+ if (typeof path === 'string') {
341
+ path = toArrayPath(path);
342
+ } else {
343
+ expect.stringArray(path);
344
+ }
345
+
346
+ const cache = new Map();
347
+
348
+ items.sort((itemA, itemB) => {
349
+ let valueA = cache.get(itemA);
350
+
351
+ if (undefined === valueA) {
352
+ valueA = objectGet(itemA, path);
353
+
354
+ if (undefined !== valueA) {
355
+ cache.set(itemA, valueA);
356
+ }
357
+ }
358
+
359
+ let valueB = cache.get(itemB);
360
+
361
+ if (undefined === valueB) {
362
+ valueB = objectGet(itemB, path);
363
+
364
+ if (undefined !== valueB) {
365
+ cache.set(itemB, valueB);
366
+ }
367
+ }
368
+
369
+ return compareFn(valueA, valueB);
370
+ });
371
+
372
+ cache.clear();
373
+ }
374
+
375
+ // -----------------------------------------------------------------------------
376
+
377
+ /**
378
+ * Find the first item in the list of objects that matches the selector
379
+ * - All items in the supplied array must be objects
380
+ *
381
+ * @param {object[]} arr
382
+ * @param {object|null} selector
383
+ *
384
+ * @returns {object|null} first matched item
385
+ */
386
+ export function findFirst(arr, selector) {
387
+ const selectorObj = new Selector(selector);
388
+
389
+ return selectorObj.findFirst(arr);
390
+ }
391
+
392
+ // -----------------------------------------------------------------------------
393
+
394
+ /**
395
+ * Returns all items from the list of items that match the selector
396
+ * - All items in the supplied array must be objects
397
+ *
398
+ * @param {object[]} arr
399
+ * @param {object|null} selector
400
+ *
401
+ * @returns {object[]} matching items
402
+ */
403
+ export function findAll(arr, selector) {
404
+ const selectorObj = new Selector(selector);
405
+
406
+ return selectorObj.findAll(arr);
407
+ }
408
+
409
+ // -----------------------------------------------------------------------------
410
+
411
+ /**
412
+ * Convert array to an object using a list of keys for each index
413
+ *
414
+ * @param {array} arr
415
+ * @param {string[]} keys
416
+ *
417
+ * @returns {object}
418
+ */
419
+ export function arrayToObject(arr, keys) {
420
+ expect.array(arr);
421
+ expect.array(keys);
422
+
423
+ const obj = {};
424
+
425
+ const n = Math.min(arr.length, keys.length);
426
+
427
+ for (let j = 0; j < n; j = j + 1) {
428
+ obj[keys[j]] = arr[j];
429
+ }
430
+
431
+ return obj;
432
+ }
@@ -0,0 +1,68 @@
1
+ /**
2
+ * Check if the values of two variables should be considered the same
3
+ *
4
+ * @param {*} value1 - First value for comparison
5
+ * @param {*} value2 - Second value for comparison
6
+ *
7
+ * @return {boolean} true if the two values can be considered the same
8
+ */
9
+ export function equals(value1: any, value2: any, _pendingComparisons: any): boolean;
10
+ /**
11
+ * Returns true if x is greater than y
12
+ *
13
+ * @param {*} x - First value
14
+ * @param {*} y - Second value
15
+ */
16
+ export function isGreaterThan(x: any, y: any): boolean;
17
+ /**
18
+ * Returns true if x is less than y
19
+ *
20
+ * @param {*} x - First value
21
+ * @param {*} y - Second value
22
+ */
23
+ export function isLessThan(x: any, y: any): boolean;
24
+ /**
25
+ * Compare function that can be used for sorting smallest values first
26
+ * - undefined values are placed at the ...???FIXME???... of the sorted array
27
+ *
28
+ * @param {*} x - First value
29
+ * @param {*} y - Second value
30
+ *
31
+ * @returns {number} 0 = keep original order,
32
+ * 1 = sort x after y,
33
+ * -1 = sort x before y
34
+ */
35
+ export function smallestFirst(x: any, y: any): number;
36
+ /**
37
+ * Compare function that can be used for sorting largest values first
38
+ * - undefined values are placed at the ...???FIXME???... of the sorted array
39
+ *
40
+ * @param {*} x - First value
41
+ * @param {*} y - Second value
42
+ *
43
+ * @returns {number} 0 = keep original order,
44
+ * 1 = sort x after y,
45
+ * -1 = sort x before y
46
+ */
47
+ export function largestFirst(x: any, y: any): number;
48
+ /**
49
+ * Comparator that can be used for sorting using an object path
50
+ *
51
+ * @param {function} compareFn - Function to use to compare the values
52
+ *
53
+ * @param {*} a - First value
54
+ * @param {*} b - Second value
55
+ *
56
+ * @param {string|string[]} path - Object path
57
+ */
58
+ export function compareUsingPath(compareFn: Function, a: any, b: any, path: string | string[]): any;
59
+ /**
60
+ * Comparator that can be used for sorting using an object key
61
+ *
62
+ * @param {function} compareFn - Function to use to compare the values
63
+ * @param {string|string[]} path - Object path
64
+ *
65
+ * @param {*} x - First value
66
+ * @param {*} y - Second value
67
+ */
68
+ export function compareUsingKey(compareFn: Function, key: any, a: any, b: any): any;