@web-ts-toolkit/utils 0.2.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/index.mjs ADDED
@@ -0,0 +1,718 @@
1
+ // src/addLeadingSlash.ts
2
+ function addLeadingSlash(value) {
3
+ return value.startsWith("/") ? value : `/${value}`;
4
+ }
5
+
6
+ // src/assign.ts
7
+ function assign(target, ...sources) {
8
+ return Object.assign(target, ...sources);
9
+ }
10
+
11
+ // src/castArray.ts
12
+ function castArray(value) {
13
+ return Array.isArray(value) ? value : [value];
14
+ }
15
+
16
+ // src/_internal.ts
17
+ var pathPattern = /[^.[\]]+|\[(?:([^"'[\]]+)|["']([^"']+)["'])\]/g;
18
+ function isObject(value) {
19
+ return value !== null && (typeof value === "object" || typeof value === "function");
20
+ }
21
+ function isPlainObject(value) {
22
+ if (value === null || typeof value !== "object") {
23
+ return false;
24
+ }
25
+ const prototype = Object.getPrototypeOf(value);
26
+ return prototype === null || prototype === Object.prototype;
27
+ }
28
+ function normalizePathPart(part) {
29
+ if (typeof part === "number") {
30
+ return part;
31
+ }
32
+ return /^\d+$/.test(part) ? Number(part) : part;
33
+ }
34
+ function toPath(path) {
35
+ if (Array.isArray(path)) {
36
+ return path.map(normalizePathPart);
37
+ }
38
+ const input = String(path);
39
+ if (input.length === 0) {
40
+ return [];
41
+ }
42
+ const result = [];
43
+ input.replace(pathPattern, (_, bare, quoted) => {
44
+ const token = bare ?? quoted ?? _;
45
+ result.push(normalizePathPart(token));
46
+ return "";
47
+ });
48
+ return result.length > 0 ? result : [input];
49
+ }
50
+ function hasPath(value, path) {
51
+ const parts = toPath(path);
52
+ if (parts.length === 0) {
53
+ return true;
54
+ }
55
+ let current = value;
56
+ for (let index = 0; index < parts.length; index++) {
57
+ if (current === null || current === void 0) {
58
+ return false;
59
+ }
60
+ const key = parts[index];
61
+ if (!(key in Object(current))) {
62
+ return false;
63
+ }
64
+ current = current[key];
65
+ }
66
+ return true;
67
+ }
68
+ function getPath(value, path, defaultValue) {
69
+ const parts = toPath(path);
70
+ let current = value;
71
+ for (let index = 0; index < parts.length; index++) {
72
+ if (current === null || current === void 0) {
73
+ return defaultValue;
74
+ }
75
+ current = current[parts[index]];
76
+ }
77
+ return current === void 0 ? defaultValue : current;
78
+ }
79
+ function setPath(target, path, value) {
80
+ if (!isObject(target)) {
81
+ return target;
82
+ }
83
+ const parts = toPath(path);
84
+ if (parts.length === 0) {
85
+ return target;
86
+ }
87
+ let current = target;
88
+ for (let index = 0; index < parts.length - 1; index++) {
89
+ const key = parts[index];
90
+ const nextKey = parts[index + 1];
91
+ const existing = current[key];
92
+ if (!isObject(existing)) {
93
+ current[key] = typeof nextKey === "number" ? [] : {};
94
+ }
95
+ current = current[key];
96
+ }
97
+ current[parts[parts.length - 1]] = value;
98
+ return target;
99
+ }
100
+ function deletePath(target, path) {
101
+ if (!isObject(target)) {
102
+ return;
103
+ }
104
+ const parts = toPath(path);
105
+ if (parts.length === 0) {
106
+ return;
107
+ }
108
+ let current = target;
109
+ for (let index = 0; index < parts.length - 1; index++) {
110
+ const key = parts[index];
111
+ const nextValue = current[key];
112
+ if (!isObject(nextValue)) {
113
+ return;
114
+ }
115
+ current = nextValue;
116
+ }
117
+ delete current[parts[parts.length - 1]];
118
+ }
119
+ function sameValueZero(left, right) {
120
+ return left === right || left !== left && right !== right;
121
+ }
122
+ function arrayIncludes(array, value) {
123
+ for (let index = 0; index < array.length; index++) {
124
+ if (sameValueZero(array[index], value)) {
125
+ return true;
126
+ }
127
+ }
128
+ return false;
129
+ }
130
+ function baseUniq(array) {
131
+ const result = [];
132
+ for (let index = 0; index < array.length; index++) {
133
+ if (!arrayIncludes(result, array[index])) {
134
+ result.push(array[index]);
135
+ }
136
+ }
137
+ return result;
138
+ }
139
+ function identity(value) {
140
+ return value;
141
+ }
142
+ function getIteratee(iteratee) {
143
+ if (typeof iteratee === "function") {
144
+ return iteratee;
145
+ }
146
+ if (iteratee === void 0) {
147
+ return identity;
148
+ }
149
+ return (value) => getPath(value, iteratee);
150
+ }
151
+ function compareAscending(left, right) {
152
+ if (sameValueZero(left, right)) {
153
+ return 0;
154
+ }
155
+ if (left === void 0) {
156
+ return 1;
157
+ }
158
+ if (right === void 0) {
159
+ return -1;
160
+ }
161
+ if (left === null) {
162
+ return 1;
163
+ }
164
+ if (right === null) {
165
+ return -1;
166
+ }
167
+ if (left > right) {
168
+ return 1;
169
+ }
170
+ if (left < right) {
171
+ return -1;
172
+ }
173
+ const leftString = String(left);
174
+ const rightString = String(right);
175
+ if (leftString === rightString) {
176
+ return 0;
177
+ }
178
+ return leftString > rightString ? 1 : -1;
179
+ }
180
+ function cloneValue(value) {
181
+ if (typeof structuredClone === "function") {
182
+ try {
183
+ return structuredClone(value);
184
+ } catch {
185
+ }
186
+ }
187
+ if (Array.isArray(value)) {
188
+ return value.map((item) => cloneValue(item));
189
+ }
190
+ if (value instanceof Date) {
191
+ return new Date(value.getTime());
192
+ }
193
+ if (value instanceof RegExp) {
194
+ return new RegExp(value.source, value.flags);
195
+ }
196
+ if (isPlainObject(value)) {
197
+ const result = {};
198
+ const keys2 = Object.keys(value);
199
+ for (let index = 0; index < keys2.length; index++) {
200
+ const key = keys2[index];
201
+ result[key] = cloneValue(value[key]);
202
+ }
203
+ return result;
204
+ }
205
+ return value;
206
+ }
207
+ function deepEqual(left, right) {
208
+ if (sameValueZero(left, right)) {
209
+ return true;
210
+ }
211
+ if (left instanceof Date && right instanceof Date) {
212
+ return left.getTime() === right.getTime();
213
+ }
214
+ if (left instanceof RegExp && right instanceof RegExp) {
215
+ return left.source === right.source && left.flags === right.flags;
216
+ }
217
+ if (Array.isArray(left) || Array.isArray(right)) {
218
+ if (!Array.isArray(left) || !Array.isArray(right) || left.length !== right.length) {
219
+ return false;
220
+ }
221
+ for (let index = 0; index < left.length; index++) {
222
+ if (!deepEqual(left[index], right[index])) {
223
+ return false;
224
+ }
225
+ }
226
+ return true;
227
+ }
228
+ if (!isObject(left) || !isObject(right)) {
229
+ return false;
230
+ }
231
+ const leftKeys = Object.keys(left);
232
+ const rightKeys = Object.keys(right);
233
+ if (leftKeys.length !== rightKeys.length) {
234
+ return false;
235
+ }
236
+ for (let index = 0; index < leftKeys.length; index++) {
237
+ const key = leftKeys[index];
238
+ if (!(key in right) || !deepEqual(left[key], right[key])) {
239
+ return false;
240
+ }
241
+ }
242
+ return true;
243
+ }
244
+ function partialMatch(object, source) {
245
+ if (sameValueZero(object, source)) {
246
+ return true;
247
+ }
248
+ if (Array.isArray(source)) {
249
+ if (!Array.isArray(object) || object.length < source.length) {
250
+ return false;
251
+ }
252
+ for (let index = 0; index < source.length; index++) {
253
+ if (!partialMatch(object[index], source[index])) {
254
+ return false;
255
+ }
256
+ }
257
+ return true;
258
+ }
259
+ if (isPlainObject(source)) {
260
+ if (!isObject(object)) {
261
+ return false;
262
+ }
263
+ const keys2 = Object.keys(source);
264
+ for (let index = 0; index < keys2.length; index++) {
265
+ const key = keys2[index];
266
+ if (!partialMatch(object[key], source[key])) {
267
+ return false;
268
+ }
269
+ }
270
+ return true;
271
+ }
272
+ return deepEqual(object, source);
273
+ }
274
+
275
+ // src/cloneDeep.ts
276
+ function cloneDeep(value) {
277
+ return cloneValue(value);
278
+ }
279
+
280
+ // src/compact.ts
281
+ function compact(array) {
282
+ if (!Array.isArray(array)) {
283
+ return [];
284
+ }
285
+ return array.filter(Boolean);
286
+ }
287
+
288
+ // src/difference.ts
289
+ function difference(array, ...values) {
290
+ if (!Array.isArray(array)) {
291
+ return [];
292
+ }
293
+ const excluded = values.flatMap((value) => Array.isArray(value) ? value : []);
294
+ return array.filter((item) => !arrayIncludes(excluded, item));
295
+ }
296
+
297
+ // src/filter.ts
298
+ function filter(collection, predicate) {
299
+ if (!collection) {
300
+ return [];
301
+ }
302
+ const result = [];
303
+ if (Array.isArray(collection)) {
304
+ for (let index = 0; index < collection.length; index++) {
305
+ const value = collection[index];
306
+ if (predicate(value, index)) {
307
+ result.push(value);
308
+ }
309
+ }
310
+ return result;
311
+ }
312
+ const keys2 = Object.keys(collection);
313
+ for (let index = 0; index < keys2.length; index++) {
314
+ const key = keys2[index];
315
+ const value = collection[key];
316
+ if (predicate(value, key)) {
317
+ result.push(value);
318
+ }
319
+ }
320
+ return result;
321
+ }
322
+
323
+ // src/find.ts
324
+ function find(collection, predicate) {
325
+ if (!collection) {
326
+ return void 0;
327
+ }
328
+ if (Array.isArray(collection)) {
329
+ for (let index = 0; index < collection.length; index++) {
330
+ const value = collection[index];
331
+ if (predicate(value, index)) {
332
+ return value;
333
+ }
334
+ }
335
+ return void 0;
336
+ }
337
+ const keys2 = Object.keys(collection);
338
+ for (let index = 0; index < keys2.length; index++) {
339
+ const key = keys2[index];
340
+ const value = collection[key];
341
+ if (predicate(value, key)) {
342
+ return value;
343
+ }
344
+ }
345
+ return void 0;
346
+ }
347
+
348
+ // src/flatten.ts
349
+ function flatten(array) {
350
+ if (!Array.isArray(array)) {
351
+ return [];
352
+ }
353
+ const result = [];
354
+ for (let index = 0; index < array.length; index++) {
355
+ const value = array[index];
356
+ if (Array.isArray(value)) {
357
+ result.push(...value);
358
+ } else {
359
+ result.push(value);
360
+ }
361
+ }
362
+ return result;
363
+ }
364
+
365
+ // src/flattenDeep.ts
366
+ function flattenDeep(array) {
367
+ if (!Array.isArray(array)) {
368
+ return [];
369
+ }
370
+ const result = [];
371
+ for (let index = 0; index < array.length; index++) {
372
+ const value = array[index];
373
+ if (Array.isArray(value)) {
374
+ result.push(...flattenDeep(value));
375
+ } else {
376
+ result.push(value);
377
+ }
378
+ }
379
+ return result;
380
+ }
381
+
382
+ // src/forEach.ts
383
+ function forEach(collection, iteratee) {
384
+ if (!collection) {
385
+ return collection;
386
+ }
387
+ const callback = iteratee;
388
+ if (Array.isArray(collection)) {
389
+ for (let index = 0; index < collection.length; index++) {
390
+ if (callback(collection[index], index, collection) === false) {
391
+ break;
392
+ }
393
+ }
394
+ return collection;
395
+ }
396
+ const keys2 = Object.keys(collection);
397
+ for (let index = 0; index < keys2.length; index++) {
398
+ const key = keys2[index];
399
+ if (callback(collection[key], key, collection) === false) {
400
+ break;
401
+ }
402
+ }
403
+ return collection;
404
+ }
405
+
406
+ // src/get.ts
407
+ function get(object, path, defaultValue) {
408
+ return getPath(object, path, defaultValue);
409
+ }
410
+
411
+ // src/intersection.ts
412
+ function intersection(...arrays) {
413
+ const [first = [], ...rest] = arrays;
414
+ if (!Array.isArray(first)) {
415
+ return [];
416
+ }
417
+ const uniqueValues = baseUniq(first);
418
+ return uniqueValues.filter((value) => rest.every((array) => Array.isArray(array) && arrayIncludes(array, value)));
419
+ }
420
+
421
+ // src/intersectionBy.ts
422
+ function intersectionBy(...args) {
423
+ const arrays = [...args];
424
+ const last = arrays[arrays.length - 1];
425
+ const iteratee = typeof last === "function" || typeof last === "string" || typeof last === "number" ? arrays.pop() : void 0;
426
+ const callback = getIteratee(iteratee);
427
+ const sources = arrays.filter(Array.isArray);
428
+ if (sources.length === 0) {
429
+ return [];
430
+ }
431
+ const [first, ...rest] = sources;
432
+ const result = [];
433
+ const seen = [];
434
+ for (let index = 0; index < first.length; index++) {
435
+ const item = first[index];
436
+ const computed = callback(item);
437
+ if (seen.some((entry) => sameValueZero(entry, computed))) {
438
+ continue;
439
+ }
440
+ if (rest.every(
441
+ (array) => baseUniq(array.map((value) => callback(value))).some((entry) => sameValueZero(entry, computed))
442
+ )) {
443
+ seen.push(computed);
444
+ result.push(item);
445
+ }
446
+ }
447
+ return result;
448
+ }
449
+
450
+ // src/isArray.ts
451
+ function isArray(value) {
452
+ return Array.isArray(value);
453
+ }
454
+
455
+ // src/isBoolean.ts
456
+ function isBoolean(value) {
457
+ return value === true || value === false || value instanceof Boolean;
458
+ }
459
+
460
+ // src/isEmpty.ts
461
+ function isEmpty(value) {
462
+ if (value == null) {
463
+ return true;
464
+ }
465
+ if (Array.isArray(value) || typeof value === "string") {
466
+ return value.length === 0;
467
+ }
468
+ if (value instanceof Map || value instanceof Set) {
469
+ return value.size === 0;
470
+ }
471
+ if (isObject(value)) {
472
+ return Object.keys(value).length === 0;
473
+ }
474
+ return false;
475
+ }
476
+
477
+ // src/isEqual.ts
478
+ function isEqual(left, right) {
479
+ return deepEqual(left, right);
480
+ }
481
+
482
+ // src/isFunction.ts
483
+ function isFunction(value) {
484
+ return typeof value === "function";
485
+ }
486
+
487
+ // src/isMatch.ts
488
+ function isMatch(object, source) {
489
+ return partialMatch(object, source);
490
+ }
491
+
492
+ // src/isNaN.ts
493
+ function isNaNValue(value) {
494
+ if (value instanceof Number) {
495
+ return Number.isNaN(value.valueOf());
496
+ }
497
+ return typeof value === "number" && Number.isNaN(value);
498
+ }
499
+
500
+ // src/isNil.ts
501
+ function isNil(value) {
502
+ return value == null;
503
+ }
504
+
505
+ // src/isNumber.ts
506
+ function isNumber(value) {
507
+ return typeof value === "number" || value instanceof Number;
508
+ }
509
+
510
+ // src/isObject.ts
511
+ function isObject2(value) {
512
+ return isObject(value);
513
+ }
514
+
515
+ // src/isPlainObject.ts
516
+ function isPlainObject2(value) {
517
+ return isPlainObject(value);
518
+ }
519
+
520
+ // src/isPromise.ts
521
+ function isPromise(value) {
522
+ return !!value && isFunction(value.then);
523
+ }
524
+
525
+ // src/isString.ts
526
+ function isString(value) {
527
+ return typeof value === "string" || value instanceof String;
528
+ }
529
+
530
+ // src/isUndefined.ts
531
+ function isUndefined(value) {
532
+ return value === void 0;
533
+ }
534
+
535
+ // src/keys.ts
536
+ function keys(value) {
537
+ if (value == null) {
538
+ return [];
539
+ }
540
+ return Object.keys(Object(value));
541
+ }
542
+
543
+ // src/map.ts
544
+ function map(collection, iteratee) {
545
+ if (!collection) {
546
+ return [];
547
+ }
548
+ const callback = getIteratee(iteratee);
549
+ const result = [];
550
+ if (Array.isArray(collection)) {
551
+ for (let index = 0; index < collection.length; index++) {
552
+ result.push(callback(collection[index], index, collection));
553
+ }
554
+ return result;
555
+ }
556
+ const keys2 = Object.keys(collection);
557
+ for (let index = 0; index < keys2.length; index++) {
558
+ const key = keys2[index];
559
+ result.push(callback(collection[key], key, collection));
560
+ }
561
+ return result;
562
+ }
563
+
564
+ // src/noop.ts
565
+ function noop() {
566
+ }
567
+
568
+ // src/omit.ts
569
+ function omit(object, paths) {
570
+ if (!isObject(object)) {
571
+ return {};
572
+ }
573
+ const result = cloneDeep(object);
574
+ const pathList = Array.isArray(paths) ? paths : [paths];
575
+ for (let index = 0; index < pathList.length; index++) {
576
+ deletePath(result, pathList[index]);
577
+ }
578
+ return result;
579
+ }
580
+
581
+ // src/orderBy.ts
582
+ function orderBy(collection, iteratees = [], orders = []) {
583
+ if (!Array.isArray(collection)) {
584
+ return [];
585
+ }
586
+ const callbacks = (iteratees.length > 0 ? iteratees : [void 0]).map((iteratee) => getIteratee(iteratee));
587
+ return [...collection].map((value, index) => ({ value, index })).sort((left, right) => {
588
+ for (let index = 0; index < callbacks.length; index++) {
589
+ const callback = callbacks[index];
590
+ const comparison = compareAscending(callback(left.value), callback(right.value));
591
+ if (comparison !== 0) {
592
+ return orders[index] === "desc" ? comparison * -1 : comparison;
593
+ }
594
+ }
595
+ return left.index - right.index;
596
+ }).map((entry) => entry.value);
597
+ }
598
+
599
+ // src/padEnd.ts
600
+ function padEnd(value, length = 0, chars = " ") {
601
+ return String(value).padEnd(length, chars);
602
+ }
603
+
604
+ // src/pick.ts
605
+ function pick(object, paths) {
606
+ if (!isObject(object)) {
607
+ return {};
608
+ }
609
+ const result = {};
610
+ const pathList = Array.isArray(paths) ? paths : [paths];
611
+ for (let index = 0; index < pathList.length; index++) {
612
+ const path = pathList[index];
613
+ if (!hasPath(object, path)) {
614
+ continue;
615
+ }
616
+ setPath(result, path, getPath(object, path));
617
+ }
618
+ return result;
619
+ }
620
+
621
+ // src/reduce.ts
622
+ function reduce(collection, iteratee, accumulator) {
623
+ const hasAccumulator = arguments.length >= 3;
624
+ const entries = [];
625
+ const callback = iteratee;
626
+ if (Array.isArray(collection)) {
627
+ for (let index = 0; index < collection.length; index++) {
628
+ entries.push([index, collection[index]]);
629
+ }
630
+ } else if (collection) {
631
+ const keys2 = Object.keys(collection);
632
+ for (let index = 0; index < keys2.length; index++) {
633
+ const key = keys2[index];
634
+ entries.push([key, collection[key]]);
635
+ }
636
+ }
637
+ if (!hasAccumulator) {
638
+ if (entries.length === 0) {
639
+ throw new TypeError("Reduce of empty collection with no initial value");
640
+ }
641
+ accumulator = entries[0][1];
642
+ entries.shift();
643
+ }
644
+ let result = accumulator;
645
+ for (let index = 0; index < entries.length; index++) {
646
+ const [key, value] = entries[index];
647
+ result = callback(result, value, key, collection);
648
+ }
649
+ return result;
650
+ }
651
+
652
+ // src/set.ts
653
+ function set(object, path, value) {
654
+ return setPath(object, path, value);
655
+ }
656
+
657
+ // src/toStringRecord.ts
658
+ function toStringRecord(value) {
659
+ if (!isPlainObject2(value)) {
660
+ return void 0;
661
+ }
662
+ const result = {};
663
+ const entries = Object.entries(value);
664
+ for (let index = 0; index < entries.length; index++) {
665
+ const [key, entryValue] = entries[index];
666
+ result[key] = String(entryValue);
667
+ }
668
+ return entries.length > 0 ? result : void 0;
669
+ }
670
+
671
+ // src/uniq.ts
672
+ function uniq(array) {
673
+ if (!Array.isArray(array)) {
674
+ return [];
675
+ }
676
+ return baseUniq(array);
677
+ }
678
+ export {
679
+ addLeadingSlash,
680
+ assign,
681
+ castArray,
682
+ cloneDeep,
683
+ compact,
684
+ difference,
685
+ filter,
686
+ find,
687
+ flatten,
688
+ flattenDeep,
689
+ forEach,
690
+ get,
691
+ intersection,
692
+ intersectionBy,
693
+ isArray,
694
+ isBoolean,
695
+ isEmpty,
696
+ isEqual,
697
+ isFunction,
698
+ isMatch,
699
+ isNaNValue as isNaN,
700
+ isNil,
701
+ isNumber,
702
+ isObject2 as isObject,
703
+ isPlainObject2 as isPlainObject,
704
+ isPromise,
705
+ isString,
706
+ isUndefined,
707
+ keys,
708
+ map,
709
+ noop,
710
+ omit,
711
+ orderBy,
712
+ padEnd,
713
+ pick,
714
+ reduce,
715
+ set,
716
+ toStringRecord,
717
+ uniq
718
+ };