@oscarpalmer/atoms 0.201.0 → 0.203.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 (156) hide show
  1. package/dist/array/difference.d.mts +4 -5
  2. package/dist/array/exists.d.mts +5 -6
  3. package/dist/array/filter.d.mts +10 -11
  4. package/dist/array/find.d.mts +10 -11
  5. package/dist/array/first.d.mts +10 -11
  6. package/dist/array/flatten.d.mts +2 -3
  7. package/dist/array/from.d.mts +7 -8
  8. package/dist/array/group-by.d.mts +16 -185
  9. package/dist/array/insert.d.mts +3 -4
  10. package/dist/array/intersection.d.mts +4 -5
  11. package/dist/array/last.d.mts +10 -11
  12. package/dist/array/move.d.mts +9 -10
  13. package/dist/array/partition.d.mts +5 -6
  14. package/dist/array/push.d.mts +2 -3
  15. package/dist/array/reverse.d.mts +2 -3
  16. package/dist/array/select.d.mts +11 -12
  17. package/dist/array/single.d.mts +4 -5
  18. package/dist/array/slice.d.mts +12 -13
  19. package/dist/array/sort.d.mts +31 -32
  20. package/dist/array/sort.mjs +41 -33
  21. package/dist/array/splice.d.mts +5 -6
  22. package/dist/array/swap.d.mts +8 -9
  23. package/dist/array/to-map.d.mts +18 -175
  24. package/dist/array/to-record.d.mts +18 -175
  25. package/dist/array/to-set.d.mts +4 -5
  26. package/dist/array/toggle.d.mts +4 -5
  27. package/dist/array/union.d.mts +4 -5
  28. package/dist/array/unique.d.mts +4 -5
  29. package/dist/array/update.d.mts +4 -5
  30. package/dist/beacon.d.mts +8 -9
  31. package/dist/beacon.mjs +90 -65
  32. package/dist/color/constants.d.mts +14 -13
  33. package/dist/color/constants.mjs +9 -2
  34. package/dist/color/index.d.mts +2 -2
  35. package/dist/color/instance.d.mts +3 -3
  36. package/dist/color/instance.mjs +125 -61
  37. package/dist/color/misc/alpha.d.mts +4 -5
  38. package/dist/color/misc/format.d.mts +6 -7
  39. package/dist/color/misc/format.mjs +15 -12
  40. package/dist/color/misc/get.d.mts +14 -15
  41. package/dist/color/misc/is.d.mts +16 -17
  42. package/dist/color/misc/is.mjs +3 -4
  43. package/dist/color/misc/state.d.mts +8 -10
  44. package/dist/color/misc/state.mjs +22 -20
  45. package/dist/color/models.d.mts +25 -19
  46. package/dist/color/space/hex.d.mts +9 -10
  47. package/dist/color/space/hsl.d.mts +8 -9
  48. package/dist/color/space/hwb.d.mts +9 -10
  49. package/dist/color/space/rgb.d.mts +11 -12
  50. package/dist/function/limit.d.mts +9 -8
  51. package/dist/function/limit.mjs +5 -5
  52. package/dist/function/once.d.mts +26 -6
  53. package/dist/function/once.mjs +135 -76
  54. package/dist/function/retry.d.mts +5 -6
  55. package/dist/function/retry.mjs +3 -3
  56. package/dist/index.d.mts +244 -584
  57. package/dist/index.mjs +1191 -810
  58. package/dist/internal/abort.d.mts +3 -4
  59. package/dist/internal/array/callbacks.d.mts +4 -5
  60. package/dist/internal/array/chunk.d.mts +2 -3
  61. package/dist/internal/array/compact.d.mts +3 -4
  62. package/dist/internal/array/find.d.mts +10 -11
  63. package/dist/internal/array/get.d.mts +7 -8
  64. package/dist/internal/array/group.d.mts +2 -3
  65. package/dist/internal/array/index-of.d.mts +10 -11
  66. package/dist/internal/array/insert.d.mts +5 -6
  67. package/dist/internal/array/match.d.mts +17 -18
  68. package/dist/internal/array/overlap.d.mts +2 -3
  69. package/dist/internal/array/sets.d.mts +5 -6
  70. package/dist/internal/array/shuffle.d.mts +2 -3
  71. package/dist/internal/array/update.d.mts +2 -3
  72. package/dist/internal/defaults.d.mts +3 -4
  73. package/dist/internal/function/assert.d.mts +10 -11
  74. package/dist/internal/function/limit.d.mts +41 -0
  75. package/dist/internal/function/limit.mjs +110 -0
  76. package/dist/internal/function/memoize.d.mts +4 -5
  77. package/dist/internal/function/memoize.mjs +53 -31
  78. package/dist/internal/function/misc.d.mts +2 -3
  79. package/dist/internal/function/work.d.mts +53 -54
  80. package/dist/internal/herald.d.mts +8 -9
  81. package/dist/internal/herald.mjs +38 -26
  82. package/dist/internal/is.d.mts +19 -20
  83. package/dist/internal/math/aggregate.d.mts +14 -15
  84. package/dist/internal/math/misc.d.mts +3 -4
  85. package/dist/internal/number.d.mts +4 -5
  86. package/dist/internal/random.d.mts +3 -4
  87. package/dist/internal/result/misc.d.mts +29 -14
  88. package/dist/internal/result/misc.mjs +4 -0
  89. package/dist/internal/result/models.d.mts +9 -10
  90. package/dist/internal/sized/map.d.mts +2 -3
  91. package/dist/internal/sized/misc.d.mts +2 -3
  92. package/dist/internal/string/case.d.mts +9 -10
  93. package/dist/internal/string/misc.d.mts +8 -9
  94. package/dist/internal/subscription.d.mts +14 -15
  95. package/dist/internal/subscription.mjs +69 -57
  96. package/dist/internal/value/compare.d.mts +17 -9
  97. package/dist/internal/value/compare.mjs +7 -7
  98. package/dist/internal/value/equal.d.mts +34 -15
  99. package/dist/internal/value/equal.mjs +28 -13
  100. package/dist/internal/value/get.d.mts +3 -4
  101. package/dist/internal/value/handlers.d.mts +19 -9
  102. package/dist/internal/value/handlers.mjs +50 -50
  103. package/dist/internal/value/has.d.mts +6 -7
  104. package/dist/internal/value/misc.d.mts +6 -7
  105. package/dist/internal/value/partial.d.mts +3 -4
  106. package/dist/internal/value/set.d.mts +4 -5
  107. package/dist/is.d.mts +13 -13
  108. package/dist/logger.d.mts +5 -4
  109. package/dist/logger.mjs +61 -54
  110. package/dist/math.d.mts +20 -20
  111. package/dist/models.d.mts +58 -46
  112. package/dist/promise/constants.d.mts +14 -15
  113. package/dist/promise/delay.d.mts +3 -4
  114. package/dist/promise/delay.mjs +4 -4
  115. package/dist/promise/helpers.d.mts +7 -8
  116. package/dist/promise/index.d.mts +13 -14
  117. package/dist/promise/misc.d.mts +6 -6
  118. package/dist/promise/models.d.mts +16 -17
  119. package/dist/promise/timed.d.mts +4 -5
  120. package/dist/promise/timed.mjs +5 -5
  121. package/dist/query.d.mts +3 -4
  122. package/dist/queue.d.mts +14 -15
  123. package/dist/queue.mjs +185 -129
  124. package/dist/random.d.mts +8 -8
  125. package/dist/result/index.d.mts +8 -9
  126. package/dist/result/match.d.mts +6 -7
  127. package/dist/result/misc.d.mts +3 -3
  128. package/dist/result/work/flow.d.mts +28 -29
  129. package/dist/result/work/pipe.d.mts +24 -25
  130. package/dist/sized/set.d.mts +2 -3
  131. package/dist/string/fuzzy.d.mts +21 -12
  132. package/dist/string/fuzzy.mjs +77 -49
  133. package/dist/string/index.d.mts +8 -8
  134. package/dist/string/match.d.mts +4 -5
  135. package/dist/string/normalize.d.mts +8 -9
  136. package/dist/string/normalize.mjs +9 -4
  137. package/dist/string/template.d.mts +9 -9
  138. package/dist/string/template.mjs +11 -4
  139. package/dist/value/clone.d.mts +21 -13
  140. package/dist/value/clone.mjs +22 -20
  141. package/dist/value/collection.d.mts +4 -5
  142. package/dist/value/diff.d.mts +5 -6
  143. package/dist/value/freeze.d.mts +18 -19
  144. package/dist/value/freeze.mjs +1 -1
  145. package/dist/value/merge.d.mts +13 -14
  146. package/dist/value/merge.mjs +21 -5
  147. package/dist/value/omit.d.mts +2 -3
  148. package/dist/value/pick.d.mts +2 -3
  149. package/dist/value/shake.d.mts +3 -4
  150. package/dist/value/smush.d.mts +3 -4
  151. package/dist/value/transform.d.mts +15 -10
  152. package/dist/value/transform.mjs +12 -7
  153. package/dist/value/unsmush.d.mts +3 -4
  154. package/package.json +1 -1
  155. package/dist/internal/function/timer.d.mts +0 -10
  156. package/dist/internal/function/timer.mjs +0 -87
@@ -1,10 +1,20 @@
1
1
  import { isPlainObject } from "../internal/is.mjs";
2
2
  import { compare } from "../internal/value/compare.mjs";
3
3
  //#region src/array/sort.ts
4
+ const SORTER_PROPERTY = "$sorter";
5
+ function Sorter(sorters) {
6
+ Object.defineProperty(this, SORTER_SYMBOL, { value: sorters });
7
+ }
8
+ Object.defineProperties(Sorter.prototype, {
9
+ [SORTER_PROPERTY]: { value: true },
10
+ index: { value: getSortedArrayIndex },
11
+ is: { value: isSortedArray },
12
+ sort: { value: sortArray }
13
+ });
4
14
  function getComparisonSorter(callback, modifier) {
5
15
  return {
6
16
  modifier,
7
- compare: { simple: callback },
17
+ comparison: { simple: callback },
8
18
  get: false,
9
19
  identifier: callback.toString()
10
20
  };
@@ -13,15 +23,29 @@ function getComparisonValue(first, second, sorters, length) {
13
23
  for (let index = 0; index < length; index += 1) {
14
24
  const sorter = sorters[index];
15
25
  const values = [sorter.get ? sorter.value(first) : first, sorter.get ? sorter.value(second) : second];
16
- const comparison = (sorter.compare?.complex?.(first, values[0], second, values[1]) ?? sorter.compare?.simple?.(values[0], values[1]) ?? compare(values[0], values[1])) * sorter.modifier;
26
+ const comparison = (sorter.comparison?.complex?.(first, values[0], second, values[1]) ?? sorter.comparison?.simple?.(values[0], values[1]) ?? compare(values[0], values[1])) * sorter.modifier;
17
27
  if (comparison !== 0) return comparison;
18
28
  }
19
29
  return 0;
20
30
  }
21
- function getIndex(array, item, sorters) {
31
+ function getModifier(first, second) {
32
+ return modifiers[first === true || second === true ? SORT_DIRECTION_DESCENDING : SORT_DIRECTION_ASCENDING];
33
+ }
34
+ function getObjectSorter(obj, modifier) {
35
+ let sorter;
36
+ if (typeof obj.comparison === "function") sorter = getComparisonSorter(obj.comparison, modifier);
37
+ else if (typeof obj.key === "string") {
38
+ sorter = getValueSortHandlers(obj.key, modifier);
39
+ if (typeof obj.compare === "function") sorter.comparison = { complex: obj.compare };
40
+ } else if (typeof obj.value === "function") sorter = getValueSortHandlers(obj.value, modifier);
41
+ if (sorter != null && typeof obj.direction === "string") sorter.modifier = modifiers[obj.direction] ?? modifier;
42
+ return sorter;
43
+ }
44
+ function getSortedArrayIndex(array, item) {
22
45
  if (!Array.isArray(array)) return -1;
23
46
  const { length } = array;
24
47
  if (length === 0) return 0;
48
+ const sorters = Array.isArray(this) ? this : this[SORTER_SYMBOL];
25
49
  const sortersLength = sorters.length;
26
50
  if (getComparisonValue(item, array[0], sorters, sortersLength) < 0) return 0;
27
51
  if (getComparisonValue(item, array[length - 1], sorters, sortersLength) >= 0) return length;
@@ -34,36 +58,23 @@ function getIndex(array, item, sorters) {
34
58
  }
35
59
  return low;
36
60
  }
37
- function getModifier(first, second) {
38
- return modifiers[first === true || second === true ? SORT_DIRECTION_DESCENDING : SORT_DIRECTION_ASCENDING];
39
- }
40
- function getObjectSorter(obj, modifier) {
41
- let sorter;
42
- if (typeof obj.comparison === "function") sorter = getComparisonSorter(obj.comparison, modifier);
43
- else if (typeof obj.key === "string") {
44
- sorter = getValueSorter(obj.key, modifier);
45
- if (typeof obj.compare === "function") sorter.compare = { complex: obj.compare };
46
- } else if (typeof obj.value === "function") sorter = getValueSorter(obj.value, modifier);
47
- if (sorter != null && typeof obj.direction === "string") sorter.modifier = modifiers[obj.direction] ?? modifier;
48
- return sorter;
49
- }
50
61
  function getSortedIndex(array, item, first, second) {
51
- return getIndex(array, item, getSorters(first, getModifier(first, second)));
62
+ return getSortedArrayIndex.call(getSortHandlers(first, getModifier(first, second)), array, item);
52
63
  }
53
- function getSorter(value, modifier) {
64
+ function getSortHandler(value, modifier) {
54
65
  switch (true) {
55
66
  case typeof value === "function": return getComparisonSorter(value, modifier);
56
- case typeof value === "string": return getValueSorter(value, modifier);
67
+ case typeof value === "string": return getValueSortHandlers(value, modifier);
57
68
  case isPlainObject(value): return getObjectSorter(value, modifier);
58
69
  }
59
70
  }
60
- function getSorters(value, modifier) {
71
+ function getSortHandlers(value, modifier) {
61
72
  const array = Array.isArray(value) ? value : [value];
62
73
  const { length } = array;
63
74
  const sorters = [];
64
75
  for (let index = 0; index < length; index += 1) {
65
76
  const item = array[index];
66
- const sorter = getSorter(item, modifier);
77
+ const sorter = getSortHandler(item, modifier);
67
78
  if (sorter != null) sorters.push(sorter);
68
79
  }
69
80
  if (sorters.length === 0) return [{
@@ -73,7 +84,7 @@ function getSorters(value, modifier) {
73
84
  }];
74
85
  return sorters.filter((value, index, array) => array.findIndex((next) => next.identifier === value.identifier) === index);
75
86
  }
76
- function getValueSorter(value, modifier) {
87
+ function getValueSortHandlers(value, modifier) {
77
88
  const isFunction = typeof value === "function";
78
89
  return {
79
90
  modifier,
@@ -83,21 +94,16 @@ function getValueSorter(value, modifier) {
83
94
  };
84
95
  }
85
96
  function initializeSorter(first, second) {
86
- const sorters = getSorters(first, getModifier(first, second));
87
- const sorter = (array) => sortArray(array, sorters);
88
- Object.defineProperties(sorter, {
89
- index: { value: (array, item) => getIndex(array, item, sorters) },
90
- is: { value: (array) => isSortedArray(array, sorters) }
91
- });
92
- return sorter;
97
+ return new Sorter(getSortHandlers(first, getModifier(first, second)));
93
98
  }
94
99
  function isSorted(array, first, second) {
95
- return isSortedArray(array, getSorters(first, getModifier(first, second)));
100
+ return isSortedArray.call(getSortHandlers(first, getModifier(first, second)), array);
96
101
  }
97
- function isSortedArray(array, sorters) {
102
+ function isSortedArray(array) {
98
103
  if (!Array.isArray(array)) return false;
99
104
  const { length } = array;
100
105
  if (length < 2) return true;
106
+ const sorters = Array.isArray(this) ? this : this[SORTER_SYMBOL];
101
107
  const sortersLength = sorters.length;
102
108
  let offset = 0;
103
109
  if (length >= SORT_THRESHOLD) {
@@ -119,10 +125,11 @@ function isSortedArray(array, sorters) {
119
125
  return true;
120
126
  }
121
127
  function sort(array, first, second) {
122
- return sortArray(array, getSorters(first, getModifier(first, second)));
128
+ return sortArray.call(getSortHandlers(first, getModifier(first, second)), array);
123
129
  }
124
- function sortArray(array, sorters) {
130
+ function sortArray(array) {
125
131
  if (!Array.isArray(array)) return [];
132
+ const sorters = Array.isArray(this) ? this : this[SORTER_SYMBOL];
126
133
  const { length } = sorters;
127
134
  return array.length > 1 ? array.sort((first, second) => getComparisonValue(first, second, sorters, length)) : array;
128
135
  }
@@ -130,6 +137,7 @@ const SORT_PEEK_PERCENTAGE = 10;
130
137
  const SORT_THRESHOLD = 100;
131
138
  const SORT_DIRECTION_ASCENDING = "ascending";
132
139
  const SORT_DIRECTION_DESCENDING = "descending";
140
+ const SORTER_SYMBOL = Symbol(SORTER_PROPERTY);
133
141
  const modifiers = {
134
142
  [SORT_DIRECTION_ASCENDING]: 1,
135
143
  [SORT_DIRECTION_DESCENDING]: -1
@@ -20,7 +20,7 @@
20
20
  * ); // => [3, 4], array becomes [1, 2, 6, 7, 5]
21
21
  * ```
22
22
  */
23
- declare function splice<Item>(array: Item[], start: number, amount: number, added: Item[]): Item[];
23
+ export declare function splice<Item>(array: Item[], start: number, amount: number, added: Item[]): Item[];
24
24
  /**
25
25
  * Adds items into an array at a specific index
26
26
  *
@@ -40,7 +40,7 @@ declare function splice<Item>(array: Item[], start: number, amount: number, adde
40
40
  * ); // => [], array becomes [1, 2, 6, 7, 3, 4, 5]
41
41
  * ```
42
42
  */
43
- declare function splice<Item>(array: Item[], start: number, added: Item[]): Item[];
43
+ export declare function splice<Item>(array: Item[], start: number, added: Item[]): Item[];
44
44
  /**
45
45
  * Removes and returns _(up to)_ a specific amount of items from an array, starting from a specific index
46
46
  *
@@ -60,7 +60,7 @@ declare function splice<Item>(array: Item[], start: number, added: Item[]): Item
60
60
  * ); // => [3, 4], array becomes [1, 2, 5]
61
61
  * ```
62
62
  */
63
- declare function splice<Item>(array: Item[], start: number, amount: number): Item[];
63
+ export declare function splice<Item>(array: Item[], start: number, amount: number): Item[];
64
64
  /**
65
65
  * Removes and returns all items from an array starting from a specific index
66
66
  *
@@ -78,6 +78,5 @@ declare function splice<Item>(array: Item[], start: number, amount: number): Ite
78
78
  * ); // => [3, 4, 5], array becomes [1, 2]
79
79
  * ```
80
80
  */
81
- declare function splice<Item>(array: Item[], start: number): Item[];
82
- //#endregion
83
- export { splice };
81
+ export declare function splice<Item>(array: Item[], start: number): Item[];
82
+ //#endregion
@@ -33,7 +33,7 @@ import { PlainObject } from "../models.mjs";
33
33
  * // ]
34
34
  * ```
35
35
  */
36
- declare function swap<Item>(array: Item[], first: Item[], second: Item[], callback: (item: Item, index: number, array: Item[]) => unknown): Item[];
36
+ export declare function swap<Item>(array: Item[], first: Item[], second: Item[], callback: (item: Item, index: number, array: Item[]) => unknown): Item[];
37
37
  /**
38
38
  * Swap two smaller arrays within a larger array
39
39
  *
@@ -67,7 +67,7 @@ declare function swap<Item>(array: Item[], first: Item[], second: Item[], callba
67
67
  * // ]
68
68
  * ```
69
69
  */
70
- declare function swap<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], first: Item[], second: Item[], key: ItemKey): Item[];
70
+ export declare function swap<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], first: Item[], second: Item[], key: ItemKey): Item[];
71
71
  /**
72
72
  * Swap two smaller arrays within a larger array
73
73
  *
@@ -87,7 +87,7 @@ declare function swap<Item extends PlainObject, ItemKey extends keyof Item>(arra
87
87
  * ); // => [5, 6, 3, 4, 1, 2]
88
88
  * ```
89
89
  */
90
- declare function swap<Item>(array: Item[], first: Item[], second: Item[]): Item[];
90
+ export declare function swap<Item>(array: Item[], first: Item[], second: Item[]): Item[];
91
91
  /**
92
92
  * Swap two indiced items in an array
93
93
  *
@@ -117,7 +117,7 @@ declare function swap<Item>(array: Item[], first: Item[], second: Item[]): Item[
117
117
  * // ]
118
118
  * ```
119
119
  */
120
- declare function swap<Item>(array: Item[], first: Item, second: Item, callback: (item: Item, index: number, array: Item[]) => unknown): Item[];
120
+ export declare function swap<Item>(array: Item[], first: Item, second: Item, callback: (item: Item, index: number, array: Item[]) => unknown): Item[];
121
121
  /**
122
122
  * Swap two indiced items in an array
123
123
  *
@@ -147,7 +147,7 @@ declare function swap<Item>(array: Item[], first: Item, second: Item, callback:
147
147
  * // ]
148
148
  * ```
149
149
  */
150
- declare function swap<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], first: Item, second: Item, key: ItemKey): Item[];
150
+ export declare function swap<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], first: Item, second: Item, key: ItemKey): Item[];
151
151
  /**
152
152
  * Swap two indiced items in an array
153
153
  *
@@ -165,8 +165,8 @@ declare function swap<Item extends PlainObject, ItemKey extends keyof Item>(arra
165
165
  * ); // => [1, 5, 3, 4, 2, 6]
166
166
  * ```
167
167
  */
168
- declare function swap<Item>(array: Item[], first: Item, second: Item): Item[];
169
- declare namespace swap {
168
+ export declare function swap<Item>(array: Item[], first: Item, second: Item): Item[];
169
+ export declare namespace swap {
170
170
  var indices: typeof swapIndices;
171
171
  }
172
172
  /**
@@ -182,5 +182,4 @@ declare namespace swap {
182
182
  * @returns Original array with items swapped _(or unchanged if unable to swap)_
183
183
  */
184
184
  declare function swapIndices<Item>(array: Item[], first: number, second: number): Item[];
185
- //#endregion
186
- export { swap };
185
+ //#endregion
@@ -1,13 +1,13 @@
1
- import { Key, PlainObject } from "../models.mjs";
1
+ import { Key, KeyOrCallback, KeyOrCallbackKey, KeyOrCallbackValue } from "../models.mjs";
2
2
  //#region src/array/to-map.d.ts
3
3
  /**
4
- * Create a _Map_ from an array of items using callbacks
4
+ * Create a _Map_ from an array of items using a specific key
5
5
  *
6
6
  * If multiple items have the same key, the latest item's value will be used
7
7
  *
8
8
  * @param array Array to convert
9
- * @param key Callback to get an item's grouping key
10
- * @param value Callback to get an item's value
9
+ * @param key Callback or key to use for grouping
10
+ * @param value Callback or key to use for value, defaulting to the item itself
11
11
  * @returns _Map_ of keyed values
12
12
  *
13
13
  * @example
@@ -19,74 +19,14 @@ import { Key, PlainObject } from "../models.mjs";
19
19
  * ); // => Map { 10 => 3, 20 => 2 }
20
20
  * ```
21
21
  */
22
- declare function toMap<Item, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: KeyCallback, value: ValueCallback): Map<ReturnType<KeyCallback>, ReturnType<ValueCallback>>;
22
+ export declare function toMap<Item, KeyArg extends KeyOrCallback<Item, Key>, ValueArg extends KeyOrCallback<Item>>(array: Item[], key: KeyArg, value: ValueArg): Map<KeyOrCallbackKey<Item, KeyArg>, KeyOrCallbackValue<Item, ValueArg>>;
23
23
  /**
24
- * Create a _Map_ from an array of items using a callback and value
25
- *
26
- * If multiple items have the same key, the latest item's value will be used
27
- *
28
- * @param array Array to convert
29
- * @param key Callback to get an item's grouping key
30
- * @param value Key to use for value
31
- * @returns _Map_ of keyed values
32
- *
33
- * @example
34
- * ```typescript
35
- * toMap(
36
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
37
- * item => item.value,
38
- * 'id',
39
- * ); // => Map { 10 => 3, 20 => 2 }
40
- * ```
41
- */
42
- declare function toMap<Item extends PlainObject, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ItemValue extends keyof Item>(array: Item[], key: KeyCallback, value: ItemValue): Map<ReturnType<KeyCallback>, Item[ItemValue]>;
43
- /**
44
- * Create a _Map_ from an array of items using a key and callback
45
- *
46
- * If multiple items have the same key, the latest item's value will be used
47
- *
48
- * @param array Array to convert
49
- * @param key Key to use for grouping
50
- * @param value Callback to get an item's value
51
- * @returns _Map_ of keyed values
52
- *
53
- * @example
54
- * ```typescript
55
- * toMap(
56
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
57
- * 'value',
58
- * item => item.id,
59
- * ); // => Map { 10 => 3, 20 => 2 }
60
- * ```
61
- */
62
- declare function toMap<Item extends PlainObject, ItemKey extends keyof Item, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: ItemKey, value: ValueCallback): Map<Item[ItemKey], ReturnType<ValueCallback>>;
63
- /**
64
- * Create a _Map_ from an array of items using a key and value
65
- *
66
- * If multiple items have the same key, the latest item's value will be used
67
- *
68
- * @param array Array to convert
69
- * @param key Key to use for grouping
70
- * @param value Key to use for value
71
- * @returns _Map_ of keyed values
72
- *
73
- * @example
74
- * ```typescript
75
- * toMap(
76
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
77
- * 'value',
78
- * 'id',
79
- * ); // => Map { 10 => 3, 20 => 2 }
80
- * ```
81
- */
82
- declare function toMap<Item extends PlainObject, ItemKey extends keyof Item, ItemValue extends keyof Item>(array: Item[], key: ItemKey, value: ItemValue): Map<Item[ItemKey], Item[ItemValue]>;
83
- /**
84
- * Create a _Map_ from an array of items using a callback
24
+ * Create a _Map_ from an array of items using a specific key
85
25
  *
86
26
  * If multiple items have the same key, the latest item will be used
87
27
  *
88
28
  * @param array Array to convert
89
- * @param callback Callback to get an item's grouping key
29
+ * @param key Callback or key to use for grouping
90
30
  * @returns _Map_ of keyed items
91
31
  *
92
32
  * @example
@@ -97,25 +37,7 @@ declare function toMap<Item extends PlainObject, ItemKey extends keyof Item, Ite
97
37
  * ); // => Map { 10 => {id: 3, value: 10}, 20 => {id: 2, value: 20} }
98
38
  * ```
99
39
  */
100
- declare function toMap<Item, Callback extends (item: Item, index: number, array: Item[]) => Key>(array: Item[], callback: Callback): Map<ReturnType<Callback>, Item>;
101
- /**
102
- * Create a _Map_ from an array of items using a key
103
- *
104
- * If multiple items have the same key, the latest item will be used
105
- *
106
- * @param array Array to convert
107
- * @param key Key to use for grouping
108
- * @returns _Map_ of keyed items
109
- *
110
- * @example
111
- * ```typescript
112
- * toMap(
113
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
114
- * 'value',
115
- * ); // => Map { 10 => {id: 3, value: 10}, 20 => {id: 2, value: 20} }
116
- * ```
117
- */
118
- declare function toMap<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey): Map<Item[ItemKey], Item>;
40
+ export declare function toMap<Item, KeyArg extends KeyOrCallback<Item, Key>>(array: Item[], key: KeyArg): Map<KeyOrCallbackKey<Item, KeyArg>, Item>;
119
41
  /**
120
42
  * Create a _Map_ from an array of items _(using indices as keys)_
121
43
  *
@@ -129,18 +51,18 @@ declare function toMap<Item extends PlainObject, ItemKey extends keyof Item>(arr
129
51
  * ); // => Map { 0 => {id: 1, value: 10}, 1 => {id: 2, value: 20}, 2 => {id: 3, value: 10} }
130
52
  * ```
131
53
  */
132
- declare function toMap<Item>(array: Item[]): Map<number, Item>;
133
- declare namespace toMap {
54
+ export declare function toMap<Item>(array: Item[]): Map<number, Item>;
55
+ export declare namespace toMap {
134
56
  var arrays: typeof toMapArrays;
135
57
  }
136
58
  /**
137
- * Create a _Map_ from an array of items using callbacks, grouping values into arrays
59
+ * Create a _Map_ from an array of items using a specific key, grouping values into arrays
138
60
  *
139
61
  * _Available as `toMapArrays` and `toMap.arrays`_
140
62
  *
141
63
  * @param array Array to convert
142
- * @param key Callback to get an item's grouping key
143
- * @param value Callback to get an item's value
64
+ * @param key Callback or key to use for grouping
65
+ * @param value Callback or key to use for value, defaulting to the item itself
144
66
  * @returns _Map_ of keyed arrays of values
145
67
  *
146
68
  * @example
@@ -152,74 +74,14 @@ declare namespace toMap {
152
74
  * ); // => Map { 10 => [1, 3], 20 => [2] }
153
75
  * ```
154
76
  */
155
- declare function toMapArrays<Item, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: KeyCallback, value: ValueCallback): Map<ReturnType<KeyCallback>, Array<ReturnType<ValueCallback>>>;
77
+ export declare function toMapArrays<Item, KeyArg extends KeyOrCallback<Item, Key>, ValueArg extends KeyOrCallback<Item>>(array: Item[], key: KeyArg, value: ValueArg): Map<KeyOrCallbackKey<Item, KeyArg>, Array<KeyOrCallbackValue<Item, ValueArg>>>;
156
78
  /**
157
- * Create a _Map_ from an array of items using a callback and value, grouping values into arrays
79
+ * Create a _Map_ from an array of items using a specific key, grouping items into arrays
158
80
  *
159
81
  * _Available as `toMapArrays` and `toMap.arrays`_
160
82
  *
161
83
  * @param array Array to convert
162
- * @param key Callback to get an item's grouping key
163
- * @param value Key to use for value
164
- * @returns _Map_ of keyed arrays of values
165
- *
166
- * @example
167
- * ```typescript
168
- * toMapArrays(
169
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
170
- * item => item.value,
171
- * 'id',
172
- * ); // => Map { 10 => [1, 3], 20 => [2] }
173
- * ```
174
- */
175
- declare function toMapArrays<Item extends PlainObject, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ItemValue extends keyof Item>(array: Item[], key: KeyCallback, value: ItemValue): Map<ReturnType<KeyCallback>, Item[ItemValue][]>;
176
- /**
177
- * Create a _Map_ from an array of items using a key and callback, grouping values into arrays
178
- *
179
- * _Available as `toMapArrays` and `toMap.arrays`_
180
- *
181
- * @param array Array to convert
182
- * @param key Key to use for grouping
183
- * @param value Callback to get an item's value
184
- * @returns _Map_ of keyed arrays of values
185
- *
186
- * @example
187
- * ```typescript
188
- * toMapArrays(
189
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
190
- * 'value',
191
- * item => item.id,
192
- * ); // => Map { 10 => [1, 3], 20 => [2] }
193
- * ```
194
- */
195
- declare function toMapArrays<Item extends PlainObject, ItemKey extends keyof Item, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: ItemKey, value: ValueCallback): Map<Item[ItemKey], Array<ReturnType<ValueCallback>>>;
196
- /**
197
- * Create a _Map_ from an array of items using a key and value, grouping values into arrays
198
- *
199
- * _Available as `toMapArrays` and `toMap.arrays`_
200
- *
201
- * @param array Array to convert
202
- * @param key Key to use for grouping
203
- * @param value Key to use for value
204
- * @returns _Map_ of keyed arrays of values
205
- *
206
- * @example
207
- * ```typescript
208
- * toMapArrays(
209
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
210
- * 'value',
211
- * 'id',
212
- * ); // => Map { 10 => [1, 3], 20 => [2] }
213
- * ```
214
- */
215
- declare function toMapArrays<Item extends PlainObject, ItemKey extends keyof Item, ItemValue extends keyof Item>(array: Item[], key: ItemKey, value: ItemValue): Map<Item[ItemKey], Item[ItemValue][]>;
216
- /**
217
- * Create a _Map_ from an array of items using a callback, grouping items into arrays
218
- *
219
- * _Available as `toMapArrays` and `toMap.arrays`_
220
- *
221
- * @param array Array to convert
222
- * @param callback Callback to get an item's grouping key
84
+ * @param key Callback or key to use for grouping
223
85
  * @returns _Map_ of keyed arrays of items
224
86
  *
225
87
  * @example
@@ -230,24 +92,5 @@ declare function toMapArrays<Item extends PlainObject, ItemKey extends keyof Ite
230
92
  * ); // => Map { 10 => [{id: 1, value: 10}, {id: 3, value: 10}], 20 => [{id: 2, value: 20}] }
231
93
  * ```
232
94
  */
233
- declare function toMapArrays<Item, Callback extends (item: Item, index: number, array: Item[]) => Key>(array: Item[], callback: Callback): Map<ReturnType<Callback>, Item[]>;
234
- /**
235
- * Create a _Map_ from an array of items using a key, grouping items into arrays
236
- *
237
- * _Available as `toMapArrays` and `toMap.arrays`_
238
- *
239
- * @param array Array to convert
240
- * @param key Key to use for grouping
241
- * @returns _Map_ of keyed arrays of items
242
- *
243
- * @example
244
- * ```typescript
245
- * toMapArrays(
246
- * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
247
- * 'value',
248
- * ); // => Map { 10 => [{id: 1, value: 10}, {id: 3, value: 10}], 20 => [{id: 2, value: 20}] }
249
- * ```
250
- */
251
- declare function toMapArrays<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey): Map<Item[ItemKey], Item[]>;
252
- //#endregion
253
- export { toMap, toMapArrays };
95
+ export declare function toMapArrays<Item, KeyArg extends KeyOrCallback<Item, Key>>(array: Item[], key: KeyArg): Map<KeyOrCallbackKey<Item, KeyArg>, Item[]>;
96
+ //#endregion