@oscarpalmer/atoms 0.185.0 → 0.186.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.
Files changed (94) hide show
  1. package/dist/array/difference.d.mts +29 -0
  2. package/dist/array/exists.d.mts +35 -0
  3. package/dist/array/filter.d.mts +72 -2
  4. package/dist/array/find.d.mts +70 -0
  5. package/dist/array/first.d.mts +77 -2
  6. package/dist/array/flatten.d.mts +6 -0
  7. package/dist/array/flatten.mjs +6 -0
  8. package/dist/array/from.d.mts +36 -0
  9. package/dist/array/get.d.mts +21 -13
  10. package/dist/array/group-by.d.mts +142 -0
  11. package/dist/array/insert.d.mts +16 -0
  12. package/dist/array/intersection.d.mts +29 -0
  13. package/dist/array/last.d.mts +75 -2
  14. package/dist/array/match.d.mts +161 -32
  15. package/dist/array/move.d.mts +78 -8
  16. package/dist/array/move.mjs +10 -0
  17. package/dist/array/partition.d.mts +35 -0
  18. package/dist/array/push.d.mts +8 -0
  19. package/dist/array/push.mjs +8 -0
  20. package/dist/array/reverse.d.mts +1 -0
  21. package/dist/array/reverse.mjs +1 -0
  22. package/dist/array/select.d.mts +94 -8
  23. package/dist/array/single.d.mts +29 -0
  24. package/dist/array/slice.d.mts +106 -16
  25. package/dist/array/sort.d.mts +21 -0
  26. package/dist/array/splice.d.mts +48 -0
  27. package/dist/array/splice.mjs +2 -1
  28. package/dist/array/swap.d.mts +113 -8
  29. package/dist/array/swap.mjs +1 -0
  30. package/dist/array/to-map.d.mts +124 -0
  31. package/dist/array/to-record.d.mts +124 -0
  32. package/dist/array/to-set.d.mts +24 -0
  33. package/dist/array/toggle.d.mts +38 -3
  34. package/dist/array/union.d.mts +29 -0
  35. package/dist/array/unique.d.mts +24 -0
  36. package/dist/array/update.d.mts +38 -3
  37. package/dist/index.d.mts +1892 -135
  38. package/dist/index.mjs +64 -18
  39. package/dist/internal/array/chunk.d.mts +6 -0
  40. package/dist/internal/array/chunk.mjs +6 -0
  41. package/dist/internal/array/compact.d.mts +12 -0
  42. package/dist/internal/array/index-of.d.mts +70 -0
  43. package/dist/internal/math/aggregate.d.mts +29 -0
  44. package/dist/internal/value/get.d.mts +25 -3
  45. package/dist/internal/value/has.d.mts +4 -4
  46. package/dist/models.d.mts +14 -1
  47. package/dist/value/collection.d.mts +1 -1
  48. package/dist/value/merge.d.mts +28 -25
  49. package/dist/value/merge.mjs +29 -18
  50. package/dist/value/transform.d.mts +1 -1
  51. package/dist/value/unsmush.d.mts +1 -5
  52. package/package.json +5 -5
  53. package/src/array/difference.ts +29 -0
  54. package/src/array/exists.ts +35 -0
  55. package/src/array/filter.ts +72 -2
  56. package/src/array/find.ts +70 -0
  57. package/src/array/first.ts +77 -3
  58. package/src/array/flatten.ts +6 -0
  59. package/src/array/from.ts +36 -0
  60. package/src/array/get.ts +21 -15
  61. package/src/array/group-by.ts +142 -0
  62. package/src/array/insert.ts +16 -2
  63. package/src/array/intersection.ts +29 -0
  64. package/src/array/last.ts +75 -2
  65. package/src/array/match.ts +171 -42
  66. package/src/array/move.ts +82 -12
  67. package/src/array/partition.ts +35 -0
  68. package/src/array/push.ts +8 -2
  69. package/src/array/reverse.ts +1 -0
  70. package/src/array/select.ts +94 -13
  71. package/src/array/single.ts +29 -0
  72. package/src/array/slice.ts +114 -24
  73. package/src/array/sort.ts +21 -0
  74. package/src/array/splice.ts +52 -4
  75. package/src/array/swap.ts +117 -12
  76. package/src/array/to-map.ts +124 -0
  77. package/src/array/to-record.ts +124 -0
  78. package/src/array/to-set.ts +24 -0
  79. package/src/array/toggle.ts +38 -3
  80. package/src/array/union.ts +29 -0
  81. package/src/array/unique.ts +24 -0
  82. package/src/array/update.ts +38 -3
  83. package/src/internal/array/chunk.ts +6 -0
  84. package/src/internal/array/compact.ts +12 -0
  85. package/src/internal/array/index-of.ts +70 -0
  86. package/src/internal/math/aggregate.ts +29 -0
  87. package/src/internal/string.ts +0 -2
  88. package/src/internal/value/get.ts +25 -3
  89. package/src/internal/value/has.ts +4 -4
  90. package/src/models.ts +18 -0
  91. package/src/value/collection.ts +1 -1
  92. package/src/value/merge.ts +88 -66
  93. package/src/value/transform.ts +1 -1
  94. package/src/value/unsmush.ts +1 -10
package/dist/index.mjs CHANGED
@@ -143,9 +143,15 @@ function groupArraysBy(array, first, second) {
143
143
  //#region src/internal/array/chunk.ts
144
144
  /**
145
145
  * Chunk an array into smaller arrays
146
+ *
146
147
  * @param array Array to chunk
147
148
  * @param size Size of each chunk _(minimum is `1`, maximum is `5000`; defaults to `5000`)_
148
149
  * @returns Array of arrays
150
+ *
151
+ * @example
152
+ * ```typescript
153
+ * chunk([1, 2, 3, 4, 5], 2); // => [[1, 2], [3, 4], [5]]
154
+ * ```
149
155
  */
150
156
  function chunk(array, size) {
151
157
  if (!Array.isArray(array)) return [];
@@ -441,8 +447,14 @@ function findLast(array, ...parameters) {
441
447
  //#region src/array/flatten.ts
442
448
  /**
443
449
  * Flatten an array _(using native `flat` and maximum depth)_
450
+ *
444
451
  * @param array Array to flatten
445
452
  * @returns Flattened array
453
+ *
454
+ * @example
455
+ * ```typescript
456
+ * flatten([1, [2, [3, 4], 5], 6]); // => [1, 2, 3, 4, 5, 6]
457
+ * ```
446
458
  */
447
459
  function flatten(array) {
448
460
  return Array.isArray(array) ? array.flat(Number.POSITIVE_INFINITY) : [];
@@ -584,9 +596,17 @@ const starts = new Set([COMPARISON_START, COMPARISON_SAME]);
584
596
  //#region src/array/push.ts
585
597
  /**
586
598
  * Push items into an array _(at the end)_
599
+ *
600
+ * _(Uses chunking to avoid call stack size being exceeded)_
601
+ *
587
602
  * @param array Original array
588
603
  * @param pushed Pushed items
589
604
  * @returns New length of the array
605
+ *
606
+ * @example
607
+ * ```typescript
608
+ * push([1, 2, 3], [4, 5]); // => 5 (new length); array becomes [1, 2, 3, 4, 5]
609
+ * ```
590
610
  */
591
611
  function push(array, pushed) {
592
612
  return insertValues(INSERT_TYPE_PUSH, array, pushed, array.length, 0);
@@ -595,6 +615,7 @@ function push(array, pushed) {
595
615
  //#region src/array/reverse.ts
596
616
  /**
597
617
  * Reverse the order of items in an array
618
+ *
598
619
  * @param array Array to reverse
599
620
  * @returns Reversed array
600
621
  */
@@ -678,7 +699,8 @@ const EXTRACT_TAKE = "take";
678
699
  //#endregion
679
700
  //#region src/array/splice.ts
680
701
  function splice(array, start, deleteCountOrItems, items) {
681
- return insertValues(INSERT_TYPE_SPLICE, array, typeof deleteCountOrItems === "number" ? items : deleteCountOrItems, start, typeof deleteCountOrItems === "number" ? deleteCountOrItems : 0);
702
+ const deleteCountIsNumber = typeof deleteCountOrItems === "number";
703
+ return insertValues(INSERT_TYPE_SPLICE, array, deleteCountIsNumber ? items : deleteCountOrItems, start, deleteCountIsNumber ? deleteCountOrItems : 0);
682
704
  }
683
705
  //#endregion
684
706
  //#region src/array/to-set.ts
@@ -795,10 +817,20 @@ move.toIndex = moveToIndex;
795
817
  * If the from index is out of bounds, the array will be returned unchanged
796
818
  *
797
819
  * Available as `moveIndices` and `move.indices`
820
+ *
798
821
  * @param array Array to move within
799
822
  * @param from Index to move from
800
823
  * @param to Index to move to
801
824
  * @returns Original array with item moved _(or unchanged if unable to move)_
825
+ *
826
+ * @example
827
+ * ```typescript
828
+ * const array = [1, 2, 3, 4];
829
+ *
830
+ * moveIndices(array, 0, 2); // => [2, 3, 1, 4]
831
+ * moveIndices(array, -1, 0); // => [4, 2, 3, 1]
832
+ * moveIndices(array, 5, 1); // => [4, 2, 3, 1] (unchanged)
833
+ * ```
802
834
  */
803
835
  function moveIndices(array, from, to) {
804
836
  if (!Array.isArray(array)) return [];
@@ -1240,6 +1272,7 @@ function swapArrays(array, from, to, key) {
1240
1272
  * If either index is out of bounds, the array will be returned unchanged
1241
1273
  *
1242
1274
  * Available as `swapIndices` and `swap.indices`
1275
+ *
1243
1276
  * @param array Array of items to swap
1244
1277
  * @param first First index _(can be negative to count from the end)_
1245
1278
  * @param second Second index _(can be negative to count from the end)_
@@ -1980,6 +2013,7 @@ const ONCE_MESSAGE_EXPECTATION = "Once expected a function";
1980
2013
  * An error thrown when a retry fails
1981
2014
  */
1982
2015
  var RetryError = class extends Error {
2016
+ original;
1983
2017
  constructor(message, original) {
1984
2018
  super(message);
1985
2019
  this.original = original;
@@ -3513,7 +3547,8 @@ function unsmush(value) {
3513
3547
  //#endregion
3514
3548
  //#region src/value/merge.ts
3515
3549
  /**
3516
- * Assign values from multiple arrays or objects to the first one
3550
+ * Assign values from one or more objects to the first one
3551
+ *
3517
3552
  * @param to Value to assign to
3518
3553
  * @param from Values to assign
3519
3554
  * @param options Assigning options
@@ -3522,7 +3557,7 @@ function unsmush(value) {
3522
3557
  function assign(to, from, options) {
3523
3558
  const actual = getMergeOptions(options);
3524
3559
  actual.assignValues = true;
3525
- return mergeValues([to, ...from], actual, true);
3560
+ return mergeValues([to, ...from], actual);
3526
3561
  }
3527
3562
  assign.initialize = initializeAssigner;
3528
3563
  function getMergeOptions(options) {
@@ -3543,59 +3578,70 @@ function getReplaceableObjects(value) {
3543
3578
  const items = (Array.isArray(value) ? value : [value]).filter((item) => typeof item === "string" || item instanceof RegExp);
3544
3579
  if (items.length > 0) return (name) => items.some((item) => typeof item === "string" ? item === name : item.test(name));
3545
3580
  }
3546
- function handleMerge(values, options) {
3547
- return !Array.isArray(values) || values.length === 0 ? {} : mergeValues(values, options, true);
3548
- }
3549
3581
  /**
3550
3582
  * Create an assigner with predefined options
3551
3583
  *
3552
3584
  * Available as `initializeAssigner` and `assign.initialize`
3585
+ *
3553
3586
  * @param options Assigning options
3554
3587
  * @returns Assigner function
3555
3588
  */
3556
3589
  function initializeAssigner(options) {
3557
3590
  const actual = getMergeOptions(options);
3558
3591
  actual.assignValues = true;
3559
- return ((to, from) => mergeValues([to, ...from], actual, true));
3592
+ return ((to, from) => mergeValues([to, ...from], actual));
3560
3593
  }
3561
3594
  /**
3562
3595
  * Create a merger with predefined options
3563
3596
  *
3564
3597
  * Available as `initializeMerger` and `merge.initialize`
3598
+ *
3565
3599
  * @param options Merging options
3566
3600
  * @returns Merger function
3567
3601
  */
3568
3602
  function initializeMerger(options) {
3569
3603
  const actual = getMergeOptions(options);
3570
- return (values) => handleMerge(values, actual);
3604
+ return ((values) => mergeValues(values, actual));
3571
3605
  }
3606
+ /**
3607
+ * Merge multiple arrays or objects into a single one
3608
+ *
3609
+ * @param values Values to merge
3610
+ * @param options Merging options
3611
+ * @returns Merged value
3612
+ */
3572
3613
  function merge(values, options) {
3573
- return handleMerge(values, getMergeOptions(options));
3614
+ return mergeValues(values, getMergeOptions(options));
3574
3615
  }
3575
3616
  merge.initialize = initializeMerger;
3576
- function mergeObjects(values, options, prefix) {
3617
+ function mergeObjects(values, options, destination, prefix) {
3577
3618
  const { length } = values;
3578
- const isArray = values.every(Array.isArray);
3579
- const merged = options.assignValues ? values[0] : isArray ? [] : {};
3580
- for (let outerIndex = 0; outerIndex < length; outerIndex += 1) {
3619
+ const isArray = Array.isArray(destination ?? values[0]);
3620
+ const merged = destination ?? (isArray ? [] : {});
3621
+ const offset = destination == null ? 0 : 1;
3622
+ for (let outerIndex = offset; outerIndex < length; outerIndex += 1) {
3581
3623
  const item = values[outerIndex];
3582
3624
  const keys = Object.keys(item);
3583
3625
  const size = keys.length;
3584
3626
  for (let innerIndex = 0; innerIndex < size; innerIndex += 1) {
3585
3627
  const key = keys[innerIndex];
3586
- const full = join([prefix, key], ".");
3587
3628
  const next = item[key];
3588
3629
  const previous = merged[key];
3589
3630
  if (next == null && (options.skipNullableAny || isArray && options.skipNullableInArrays)) continue;
3590
- if (isArrayOrPlainObject(next) && isArrayOrPlainObject(previous) && !(options.replaceableObjects?.(full) ?? false)) merged[key] = mergeValues([previous, next], options, false, full);
3631
+ const full = options.replaceableObjects == null ? void 0 : prefix == null ? key : `${prefix}.${key}`;
3632
+ if (isArrayOrPlainObject(next) && isArrayOrPlainObject(previous) && !(options.replaceableObjects?.(full) ?? false)) merged[key] = mergeObjects([previous, next], options, destination == null ? void 0 : merged[key], full);
3591
3633
  else merged[key] = next;
3592
3634
  }
3593
3635
  }
3594
3636
  return merged;
3595
3637
  }
3596
- function mergeValues(values, options, validate, prefix) {
3597
- const actual = validate ? values.filter(isArrayOrPlainObject) : values;
3598
- return actual.length > 1 ? mergeObjects(actual, options, prefix) : actual[0] ?? {};
3638
+ function mergeValues(values, options, prefix) {
3639
+ if (!Array.isArray(values)) return {};
3640
+ const actual = values.filter(isArrayOrPlainObject);
3641
+ if (actual.length === 0) return {};
3642
+ if (options.assignValues && actual.length === 2 && !Array.isArray(actual[0]) && Object.keys(actual[0]).length === 0) return Object.assign(actual[0], actual[1]);
3643
+ if (actual.length > 1) return mergeObjects(actual, options, options.assignValues ? actual[0] : void 0, prefix);
3644
+ return options.assignValues ? actual[0] : Array.isArray(actual[0]) ? actual[0].slice() : { ...actual[0] };
3599
3645
  }
3600
3646
  //#endregion
3601
3647
  //#region src/value/transform.ts
@@ -1,9 +1,15 @@
1
1
  //#region src/internal/array/chunk.d.ts
2
2
  /**
3
3
  * Chunk an array into smaller arrays
4
+ *
4
5
  * @param array Array to chunk
5
6
  * @param size Size of each chunk _(minimum is `1`, maximum is `5000`; defaults to `5000`)_
6
7
  * @returns Array of arrays
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * chunk([1, 2, 3, 4, 5], 2); // => [[1, 2], [3, 4], [5]]
12
+ * ```
7
13
  */
8
14
  declare function chunk<Item>(array: Item[], size?: number): Item[][];
9
15
  //#endregion
@@ -1,9 +1,15 @@
1
1
  //#region src/internal/array/chunk.ts
2
2
  /**
3
3
  * Chunk an array into smaller arrays
4
+ *
4
5
  * @param array Array to chunk
5
6
  * @param size Size of each chunk _(minimum is `1`, maximum is `5000`; defaults to `5000`)_
6
7
  * @returns Array of arrays
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * chunk([1, 2, 3, 4, 5], 2); // => [[1, 2], [3, 4], [5]]
12
+ * ```
7
13
  */
8
14
  function chunk(array, size) {
9
15
  if (!Array.isArray(array)) return [];
@@ -1,15 +1,27 @@
1
1
  //#region src/internal/array/compact.d.ts
2
2
  /**
3
3
  * Compact an array _(removing all false-y values)_
4
+ *
4
5
  * @param array Array to compact
5
6
  * @param strict True to remove all false-y values
6
7
  * @returns Compacted array
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * compact([0, 1, '', 'hello', false, true, null, undefined]); // => [1, 'hello', true]
12
+ * ```
7
13
  */
8
14
  declare function compact<Item>(array: Item[], strict: true): Exclude<Item, 0 | '' | false | null | undefined>[];
9
15
  /**
10
16
  * Compact an array _(removing all `null` and `undefined` values)_
17
+ *
11
18
  * @param array Array to compact
12
19
  * @returns Compacted array
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * compact([0, 1, '', 'hello', false, true, null, undefined]); // => [0, 1, '', 'hello', false, true]
24
+ * ```
13
25
  */
14
26
  declare function compact<Item>(array: Item[]): Exclude<Item, null | undefined>[];
15
27
  //#endregion
@@ -3,32 +3,67 @@ import { PlainObject } from "../../models.mjs";
3
3
  //#region src/internal/array/index-of.d.ts
4
4
  /**
5
5
  * Get the index of the first matching item by callback
6
+ *
6
7
  * @param array Array to search in
7
8
  * @param callback Callback to get an item's value
8
9
  * @param value Value to match against
9
10
  * @returns Index of the first matching item, or `-1` if no match is found
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * indexOf(
15
+ * [{id: 1}, {id: 2}, {id: 3}],
16
+ * item => item.id,
17
+ * 2,
18
+ * ); // => 1
19
+ * ```
10
20
  */
11
21
  declare function indexOf<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): number;
12
22
  /**
13
23
  * Get the index of the first matching item by key
24
+ *
14
25
  * @param array Array to search in
15
26
  * @param key Key to match items by
16
27
  * @param value Value to match against
17
28
  * @returns Index of the first matching item, or `-1` if no match is found
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * indexOf(
33
+ * [{id: 1}, {id: 2}, {id: 3}],
34
+ * 'id',
35
+ * 2,
36
+ * ); // => 1
37
+ * ```
18
38
  */
19
39
  declare function indexOf<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): number;
20
40
  /**
21
41
  * Get the index of the first item matching the filter
42
+ *
22
43
  * @param array Array to search in
23
44
  * @param filter Filter callback to match items
24
45
  * @returns Index of the first matching item, or `-1` if no match is found
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * indexOf(
50
+ * [{id: 1}, {id: 2}, {id: 3}],
51
+ * item => item.id === 2,
52
+ * ); // => 1
53
+ * ```
25
54
  */
26
55
  declare function indexOf<Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean): number;
27
56
  /**
28
57
  * Get the index of the first item matching the given item
58
+ *
29
59
  * @param array Array to search in
30
60
  * @param item Item to match against
31
61
  * @returns Index of the first matching item, or `-1` if no match is found
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * indexOf([1, 2, 3, 4, 5], 3); // => 2
66
+ * ```
32
67
  */
33
68
  declare function indexOf<Item>(array: Item[], item: Item): number;
34
69
  declare namespace indexOf {
@@ -38,38 +73,73 @@ declare namespace indexOf {
38
73
  * Get the index of the last matching item by callback
39
74
  *
40
75
  * Available as `lastIndexOf` and `indexOf.last`
76
+ *
41
77
  * @param array Array to search in
42
78
  * @param callback Callback to get an item's value
43
79
  * @param value Value to match against
44
80
  * @returns Index of the last matching item, or `-1` if no match is found
81
+ *
82
+ * @example
83
+ * ```typescript
84
+ * lastIndexOf(
85
+ * [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
86
+ * item => item.id,
87
+ * 2,
88
+ * ); // => 3
89
+ * ```
45
90
  */
46
91
  declare function lastIndexOf<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): number;
47
92
  /**
48
93
  * Get the index of the last matching item by key
49
94
  *
50
95
  * Available as `lastIndexOf` and `indexOf.last`
96
+ *
51
97
  * @param array Array to search in
52
98
  * @param key Key to match items by
53
99
  * @param value Value to match against
54
100
  * @returns Index of the last matching item, or `-1` if no match is found
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * lastIndexOf(
105
+ * [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
106
+ * 'id',
107
+ * 2,
108
+ * ); // => 3
109
+ * ```
55
110
  */
56
111
  declare function lastIndexOf<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): number;
57
112
  /**
58
113
  * Get the index of the last item matching the filter
59
114
  *
60
115
  * Available as `lastIndexOf` and `indexOf.last`
116
+ *
61
117
  * @param array Array to search in
62
118
  * @param filter Filter callback to match items
63
119
  * @returns Index of the last matching item, or `-1` if no match is found
120
+ *
121
+ * @example
122
+ * ```typescript
123
+ * lastIndexOf(
124
+ * [{id: 1}, {id: 2}, {id: 3}, {id: 2}],
125
+ * item => item.id === 2,
126
+ * ); // => 3
127
+ * ```
64
128
  */
65
129
  declare function lastIndexOf<Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean): number;
66
130
  /**
67
131
  * Get the index of the last item matching the given item
68
132
  *
69
133
  * Available as `lastIndexOf` and `indexOf.last`
134
+ *
70
135
  * @param array Array to search in
71
136
  * @param item Item to match against
72
137
  * @returns Index of the last matching item, or `-1` if no match is found
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * lastIndexOf([1, 2, 3, 2, 1], 2); // => 3
142
+ * ```
73
143
  */
74
144
  declare function lastIndexOf<Item>(array: Item[], item: Item): number;
75
145
  //#endregion
@@ -11,6 +11,17 @@ declare function aggregate(type: AggregationType, array: unknown[], key: unknown
11
11
  declare function getAggregateCallback(key: unknown): Function | undefined;
12
12
  /**
13
13
  * Get the maximum value from a list of items
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * max(
18
+ * [{id: 1, value: 10}, {id: 2, value: 20}],
19
+ * item => item.value,
20
+ * ); // 20
21
+ *
22
+ * max([], item => item.value); // Number.NaN
23
+ * ```
24
+ *
14
25
  * @param items List of items
15
26
  * @param callback Callback to get an item's value
16
27
  * @returns Maximum value, or `NaN` if no maximum can be found
@@ -18,6 +29,17 @@ declare function getAggregateCallback(key: unknown): Function | undefined;
18
29
  declare function max<Item>(items: Item[], callback: (item: Item, index: number, array: Item[]) => number): number;
19
30
  /**
20
31
  * Get the maximum value from a list of items
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * max(
36
+ * [{id: 1, value: 10}, {id: 2, value: 20}],
37
+ * 'value',
38
+ * ); // 20
39
+ *
40
+ * max([], 'value'); // Number.NaN
41
+ * ```
42
+ *
21
43
  * @param items List of items
22
44
  * @param key Key to use for value
23
45
  * @returns Maximum value, or `NaN` if no maximum can be found
@@ -25,6 +47,13 @@ declare function max<Item>(items: Item[], callback: (item: Item, index: number,
25
47
  declare function max<Item extends PlainObject, ItemKey extends keyof NumericalValues<Item>>(items: Item[], key: ItemKey): number;
26
48
  /**
27
49
  * Get the maximum value from a list of numbers
50
+ *
51
+ * @example
52
+ * ```typescript
53
+ * max([10, 20]); // 20
54
+ * max([]); // Number.NaN
55
+ * ```
56
+ *
28
57
  * @param values List of numbers
29
58
  * @returns Maximum value, or `NaN` if no maximum can be found
30
59
  */
@@ -3,16 +3,38 @@ import { NestedKeys, NestedValue, PlainObject } from "../../models.mjs";
3
3
  //#region src/internal/value/get.d.ts
4
4
  /**
5
5
  * Get the value from an object using a known path
6
+ *
7
+ * @example
8
+ * ```typescript
9
+ * const data = {foo: {bar: {baz: 42}}};
10
+ *
11
+ * getValue(data, 'foo'); // {bar: {baz: 42}}
12
+ * getValue(data, 'foo.bar'); // {baz: 42}
13
+ * getValue(data, 'foo.bar.baz'); // 42
14
+ * getValue(data, 'foo.nope'); // undefined
15
+ * ```
16
+ *
6
17
  * @param data Object to get value from
7
- * @param path Path for value, e.g., `foo.bar.baz`
18
+ * @param path Path for value
8
19
  * @returns Found value, or `undefined`
9
20
  */
10
21
  declare function getValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): NestedValue<Data, Path>;
11
22
  /**
12
23
  * Get the value from an object using an unknown path
24
+ *
25
+ * @example
26
+ * ```typescript
27
+ * const data = {foo: {bar: {baz: 42}}};
28
+ *
29
+ * getValue(data, 'foo'); // {bar: {baz: 42}}
30
+ * getValue(data, 'foo.bar'); // {baz: 42}
31
+ * getValue(data, 'Foo.Bar.Baz', true); // 42
32
+ * getValue(data, 'foo.nope'); // undefined
33
+ * ```
34
+ *
13
35
  * @param data Object to get value from
14
- * @param path Path for value, e.g., `foo.bar.baz`
15
- * @param ignoreCase If `true`, the path matching is case-insensitive
36
+ * @param path Path for value
37
+ * @param ignoreCase If `true`, path matching is case-insensitive
16
38
  * @returns Found value, or `undefined`
17
39
  */
18
40
  declare function getValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): unknown;
@@ -6,7 +6,7 @@ import { Result } from "../../result/models.mjs";
6
6
  * Check if a nested property is defined in an object
7
7
  * @param data Object to check in
8
8
  * @param path Path for property
9
- * @return `true` if the property exists, `false` otherwise
9
+ * @returns `true` if the property exists, `false` otherwise
10
10
  */
11
11
  declare function hasValue<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path): boolean;
12
12
  /**
@@ -14,7 +14,7 @@ declare function hasValue<Data extends PlainObject, Path extends NestedKeys<Data
14
14
  * @param data Object to check in
15
15
  * @param path Path for property
16
16
  * @param ignoreCase If `true`, the path matching is case-insensitive
17
- * @return `true` if the property exists, `false` otherwise
17
+ * @returns `true` if the property exists, `false` otherwise
18
18
  */
19
19
  declare function hasValue<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): boolean;
20
20
  declare namespace hasValue {
@@ -27,7 +27,7 @@ declare namespace hasValue {
27
27
  * @param data Object to check in
28
28
  * @param path Path for property
29
29
  * @param ignoreCase If `true`, the path matching is case-insensitive
30
- * @return Result object
30
+ * @returns Result object
31
31
  */
32
32
  declare function hasValueResult<Data extends PlainObject, Path extends NestedKeys<Data>>(data: Data, path: Path, ignoreCase?: boolean): Result<NestedValue<Data, ToString<Path>>, string>;
33
33
  /**
@@ -37,7 +37,7 @@ declare function hasValueResult<Data extends PlainObject, Path extends NestedKey
37
37
  * @param data Object to check in
38
38
  * @param path Path for property
39
39
  * @param ignoreCase If `true`, the path matching is case-insensitive
40
- * @return Result object
40
+ * @returns Result object
41
41
  */
42
42
  declare function hasValueResult<Data extends PlainObject>(data: Data, path: string, ignoreCase?: boolean): Result<unknown, string>;
43
43
  //#endregion
package/dist/models.d.mts CHANGED
@@ -143,5 +143,18 @@ type ToString<Value> = Value extends string | number ? `${Value}` : never;
143
143
  * A union type of all typed arrays
144
144
  */
145
145
  type TypedArray = Int8Array | Uint8Array | Uint8ClampedArray | Int16Array | Uint16Array | Int32Array | Uint32Array | Float32Array | Float64Array | BigInt64Array | BigUint64Array;
146
+ /**
147
+ * Converts a union type to an intersection type
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * type A = {a: string};
152
+ * type B = {b: number};
153
+ * type C = UnionToIntersection<A | B>; // {a: string} & {b: number}
154
+ * ```
155
+ *
156
+ * Thanks, type-fest!
157
+ */
158
+ type UnionToIntersection<Union> = (Union extends unknown ? (distributedUnion: Union) => void : never) extends ((mergedIntersection: infer Intersection) => void) ? Intersection & Union : never;
146
159
  //#endregion
147
- export { ArrayOrPlainObject, AsyncCancelableCallback, BuiltIns, CancelableCallback, Constructor, EventPosition, GenericAsyncCallback, GenericCallback, Key, KeyedValue, NestedArray, NestedKeys, NestedPartial, NestedValue, NestedValues, NumericalKeys, NumericalValues, OnceAsyncCallback, OnceCallback, PlainObject, Primitive, RequiredKeys, Simplify, ToString, TypedArray };
160
+ export { ArrayOrPlainObject, AsyncCancelableCallback, BuiltIns, CancelableCallback, Constructor, EventPosition, GenericAsyncCallback, GenericCallback, Key, KeyedValue, NestedArray, NestedKeys, NestedPartial, NestedValue, NestedValues, NumericalKeys, NumericalValues, OnceAsyncCallback, OnceCallback, PlainObject, Primitive, RequiredKeys, Simplify, ToString, TypedArray, UnionToIntersection };
@@ -11,7 +11,7 @@ declare function inMap<Value>(map: Map<unknown, Value>, value: Value): boolean;
11
11
  * @param map Map to check in
12
12
  * @param value Value to check for
13
13
  * @param key To return the key for the value
14
- * @return The key for the value if it exists, otherwise `undefined`
14
+ * @returns The key for the value if it exists, otherwise `undefined`
15
15
  */
16
16
  declare function inMap<Key, Value>(map: Map<Key, Value>, value: Value, key: true): Key;
17
17
  /**
@@ -1,17 +1,20 @@
1
- import { ArrayOrPlainObject, NestedPartial } from "../models.mjs";
1
+ import { ArrayOrPlainObject, NestedPartial, PlainObject, UnionToIntersection } from "../models.mjs";
2
2
 
3
3
  //#region src/value/merge.d.ts
4
4
  /**
5
5
  * Options for assigning values
6
6
  */
7
7
  type AssignOptions = Omit<MergeOptions, 'assignValues'>;
8
- /**
9
- * Assign values from multiple arrays or objects to the first one
10
- * @param to Value to assign to
11
- * @param from Values to assign
12
- * @returns Assigned value
13
- */
14
- type Assigner<Model extends ArrayOrPlainObject = ArrayOrPlainObject> = (to: NestedPartial<Model>, from: NestedPartial<Model>[]) => Model;
8
+ type Assigner = {
9
+ /**
10
+ * Assign values from one or more objects to the first one
11
+ *
12
+ * @param to Value to assign to
13
+ * @param from Values to assign
14
+ * @returns Assigned value
15
+ */
16
+ <To extends PlainObject, From extends PlainObject[]>(to: To, from: [...From]): To & UnionToIntersection<From[number]>;
17
+ };
15
18
  /**
16
19
  * Options for merging values
17
20
  */
@@ -45,20 +48,24 @@ type MergeOptions = {
45
48
  */
46
49
  skipNullableInArrays?: boolean;
47
50
  };
51
+ type Merger = {
52
+ /**
53
+ * Merge multiple arrays or objects into a single one
54
+ *
55
+ * @param values Values to merge
56
+ * @returns Merged value
57
+ */
58
+ <Values extends ArrayOrPlainObject[]>(values: NestedPartial<Values[number]>[]): UnionToIntersection<Values[number]>;
59
+ };
48
60
  /**
49
- * Merge multiple arrays or objects into a single one
50
- * @param values Values to merge
51
- * @returns Merged value
52
- */
53
- type Merger<Model extends ArrayOrPlainObject = ArrayOrPlainObject> = (values: NestedPartial<Model>[]) => Model;
54
- /**
55
- * Assign values from multiple arrays or objects to the first one
61
+ * Assign values from one or more objects to the first one
62
+ *
56
63
  * @param to Value to assign to
57
64
  * @param from Values to assign
58
65
  * @param options Assigning options
59
66
  * @returns Assigned value
60
67
  */
61
- declare function assign<Model extends ArrayOrPlainObject>(to: NestedPartial<Model>, from: NestedPartial<Model>[], options?: AssignOptions): Model;
68
+ declare function assign<To extends PlainObject, From extends PlainObject[]>(to: To, from: [...From], options?: AssignOptions): To & UnionToIntersection<From[number]>;
62
69
  declare namespace assign {
63
70
  var initialize: typeof initializeAssigner;
64
71
  }
@@ -66,32 +73,28 @@ declare namespace assign {
66
73
  * Create an assigner with predefined options
67
74
  *
68
75
  * Available as `initializeAssigner` and `assign.initialize`
76
+ *
69
77
  * @param options Assigning options
70
78
  * @returns Assigner function
71
79
  */
72
- declare function initializeAssigner<Model extends ArrayOrPlainObject>(options?: AssignOptions): Assigner<Model>;
80
+ declare function initializeAssigner(options?: AssignOptions): Assigner;
73
81
  /**
74
82
  * Create a merger with predefined options
75
83
  *
76
84
  * Available as `initializeMerger` and `merge.initialize`
85
+ *
77
86
  * @param options Merging options
78
87
  * @returns Merger function
79
88
  */
80
89
  declare function initializeMerger(options?: MergeOptions): Merger;
81
90
  /**
82
91
  * Merge multiple arrays or objects into a single one
92
+ *
83
93
  * @param values Values to merge
84
94
  * @param options Merging options
85
95
  * @returns Merged value
86
96
  */
87
- declare function merge<Model extends ArrayOrPlainObject>(values: NestedPartial<Model>[], options?: MergeOptions): Model;
88
- /**
89
- * Merge multiple arrays or objects into a single one
90
- * @param values Values to merge
91
- * @param options Merging options
92
- * @returns Merged value
93
- */
94
- declare function merge(values: NestedPartial<ArrayOrPlainObject>[], options?: MergeOptions): ArrayOrPlainObject;
97
+ declare function merge<Values extends ArrayOrPlainObject[]>(values: [...Values], options?: MergeOptions): UnionToIntersection<Values[number]>;
95
98
  declare namespace merge {
96
99
  var initialize: typeof initializeMerger;
97
100
  }