@oscarpalmer/atoms 0.74.1 → 0.76.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 (282) hide show
  1. package/dist/js/array/chunk.cjs +18 -0
  2. package/dist/js/array/{chunk.mjs → chunk.js} +2 -2
  3. package/dist/js/array/compact.cjs +6 -0
  4. package/dist/js/array/{compact.mjs → compact.js} +0 -1
  5. package/dist/js/array/count.cjs +14 -0
  6. package/dist/js/array/count.js +14 -0
  7. package/dist/js/array/exists.cjs +14 -0
  8. package/dist/js/array/exists.js +14 -0
  9. package/dist/js/array/filter.cjs +14 -0
  10. package/dist/js/array/filter.js +14 -0
  11. package/dist/js/array/find.cjs +14 -0
  12. package/dist/js/array/find.js +14 -0
  13. package/dist/js/array/group-by.cjs +35 -0
  14. package/dist/js/array/group-by.js +35 -0
  15. package/dist/js/array/index-of.cjs +14 -0
  16. package/dist/js/array/index-of.js +14 -0
  17. package/dist/js/array/index.cjs +40 -0
  18. package/dist/js/array/index.js +34 -381
  19. package/dist/js/array/insert.cjs +31 -0
  20. package/dist/js/array/insert.js +31 -0
  21. package/dist/js/array/models.cjs +1 -0
  22. package/dist/js/array/models.js +1 -0
  23. package/dist/js/array/shuffle.cjs +13 -0
  24. package/dist/js/array/{shuffle.mjs → shuffle.js} +2 -3
  25. package/dist/js/array/sort.cjs +53 -0
  26. package/dist/js/array/{sort.mjs → sort.js} +13 -9
  27. package/dist/js/array/splice.cjs +14 -0
  28. package/dist/js/array/splice.js +14 -0
  29. package/dist/js/array/to-map.cjs +27 -0
  30. package/dist/js/array/to-map.js +27 -0
  31. package/dist/js/array/to-record.cjs +12 -0
  32. package/dist/js/array/to-record.js +12 -0
  33. package/dist/js/array/unique.cjs +7 -0
  34. package/dist/js/array/unique.js +7 -0
  35. package/dist/js/colour/base.cjs +18 -0
  36. package/dist/js/colour/{base.mjs → base.js} +4 -3
  37. package/dist/js/colour/functions.cjs +96 -0
  38. package/dist/js/colour/{functions.mjs → functions.js} +18 -19
  39. package/dist/js/colour/hex.cjs +56 -0
  40. package/dist/js/colour/{hex.mjs → hex.js} +34 -15
  41. package/dist/js/colour/hsl.cjs +85 -0
  42. package/dist/js/colour/{hsl.mjs → hsl.js} +40 -12
  43. package/dist/js/colour/index.cjs +29 -0
  44. package/dist/js/colour/index.js +14 -272
  45. package/dist/js/colour/is.cjs +27 -0
  46. package/dist/js/colour/{is.mjs → is.js} +7 -6
  47. package/dist/js/colour/rgb.cjs +87 -0
  48. package/dist/js/colour/{rgb.mjs → rgb.js} +43 -12
  49. package/dist/js/{emitter.mjs → emitter.cjs} +88 -56
  50. package/dist/js/emitter.js +85 -56
  51. package/dist/js/{function.mjs → function.cjs} +66 -46
  52. package/dist/js/function.js +61 -48
  53. package/dist/js/index.cjs +136 -0
  54. package/dist/js/index.js +133 -1724
  55. package/dist/js/internal/array/callbacks.cjs +23 -0
  56. package/dist/js/internal/array/callbacks.js +23 -0
  57. package/dist/js/internal/array/find.cjs +51 -0
  58. package/dist/js/internal/array/find.js +51 -0
  59. package/dist/js/internal/value/handle.cjs +21 -0
  60. package/dist/js/internal/{value-handle.mjs → value/handle.js} +0 -1
  61. package/dist/js/{is.mjs → is.cjs} +17 -18
  62. package/dist/js/is.js +15 -27
  63. package/dist/js/logger.cjs +108 -0
  64. package/dist/js/logger.js +44 -8
  65. package/dist/js/{math.mjs → math.cjs} +7 -8
  66. package/dist/js/math.js +4 -5
  67. package/dist/js/models.cjs +1 -0
  68. package/dist/js/models.js +1 -0
  69. package/dist/js/{number.mjs → number.cjs} +8 -8
  70. package/dist/js/number.js +5 -5
  71. package/dist/js/{query.mjs → query.cjs} +18 -18
  72. package/dist/js/query.js +17 -86
  73. package/dist/js/{queue.mjs → queue.cjs} +12 -13
  74. package/dist/js/queue.js +9 -10
  75. package/dist/js/{random.mjs → random.cjs} +14 -21
  76. package/dist/js/random.js +9 -26
  77. package/dist/js/{sized.mjs → sized.cjs} +68 -33
  78. package/dist/js/sized.js +66 -38
  79. package/dist/js/string/case.cjs +41 -0
  80. package/dist/js/string/{case.mjs → case.js} +13 -9
  81. package/dist/js/string/index.cjs +59 -0
  82. package/dist/js/string/index.js +30 -100
  83. package/dist/js/string/template.cjs +21 -0
  84. package/dist/js/string/{template.mjs → template.js} +4 -5
  85. package/dist/js/{touch.mjs → touch.cjs} +4 -7
  86. package/dist/js/touch.js +3 -5
  87. package/dist/js/value/clone.cjs +76 -0
  88. package/dist/js/value/{clone.mjs → clone.js} +5 -6
  89. package/dist/js/value/compare.cjs +59 -0
  90. package/dist/js/value/{compare.mjs → compare.js} +4 -6
  91. package/dist/js/value/diff.cjs +75 -0
  92. package/dist/js/value/{diff.mjs → diff.js} +13 -11
  93. package/dist/js/value/equal.cjs +106 -0
  94. package/dist/js/value/{equal.mjs → equal.js} +13 -8
  95. package/dist/js/value/get.cjs +21 -0
  96. package/dist/js/value/{get.mjs → get.js} +9 -4
  97. package/dist/js/value/index.cjs +30 -0
  98. package/dist/js/value/index.js +19 -510
  99. package/dist/js/value/merge.cjs +35 -0
  100. package/dist/js/value/{merge.mjs → merge.js} +7 -6
  101. package/dist/js/value/set.cjs +25 -0
  102. package/dist/js/value/{set.mjs → set.js} +3 -4
  103. package/dist/js/value/smush.cjs +26 -0
  104. package/dist/js/value/{smush.mjs → smush.js} +3 -4
  105. package/dist/js/value/unsmush.cjs +39 -0
  106. package/dist/js/value/{unsmush.mjs → unsmush.js} +10 -7
  107. package/package.json +50 -47
  108. package/src/js/array/chunk.ts +6 -5
  109. package/src/js/array/compact.ts +8 -10
  110. package/src/js/array/count.ts +39 -19
  111. package/src/js/array/exists.ts +39 -15
  112. package/src/js/array/filter.ts +42 -20
  113. package/src/js/array/find.ts +45 -20
  114. package/src/js/array/group-by.ts +158 -27
  115. package/src/js/array/index-of.ts +40 -18
  116. package/src/js/array/index.ts +10 -9
  117. package/src/js/array/insert.ts +28 -14
  118. package/src/js/array/models.ts +18 -15
  119. package/src/js/array/shuffle.ts +3 -3
  120. package/src/js/array/sort.ts +32 -34
  121. package/src/js/array/splice.ts +22 -26
  122. package/src/js/array/to-map.ts +133 -45
  123. package/src/js/array/to-record.ts +137 -39
  124. package/src/js/array/unique.ts +20 -15
  125. package/src/js/colour/base.ts +3 -3
  126. package/src/js/colour/functions.ts +4 -6
  127. package/src/js/colour/hex.ts +5 -5
  128. package/src/js/colour/hsl.ts +18 -12
  129. package/src/js/colour/index.ts +1 -1
  130. package/src/js/colour/is.ts +3 -3
  131. package/src/js/colour/rgb.ts +17 -11
  132. package/src/js/emitter.ts +12 -9
  133. package/src/js/function.ts +17 -17
  134. package/src/js/index.ts +16 -16
  135. package/src/js/internal/array/callbacks.ts +33 -0
  136. package/src/js/internal/{array-find.ts → array/find.ts} +23 -24
  137. package/src/js/internal/{value-handle.ts → value/handle.ts} +1 -1
  138. package/src/js/is.ts +2 -2
  139. package/src/js/logger.ts +14 -14
  140. package/src/js/math.ts +1 -1
  141. package/src/js/models.ts +5 -5
  142. package/src/js/number.ts +2 -2
  143. package/src/js/query.ts +6 -10
  144. package/src/js/queue.ts +1 -1
  145. package/src/js/random.ts +9 -19
  146. package/src/js/sized.ts +145 -145
  147. package/src/js/string/case.ts +1 -1
  148. package/src/js/string/index.ts +13 -7
  149. package/src/js/string/template.ts +6 -3
  150. package/src/js/value/clone.ts +3 -7
  151. package/src/js/value/compare.ts +3 -5
  152. package/src/js/value/diff.ts +4 -4
  153. package/src/js/value/equal.ts +2 -2
  154. package/src/js/value/get.ts +3 -3
  155. package/src/js/value/index.ts +2 -2
  156. package/src/js/value/merge.ts +3 -3
  157. package/src/js/value/set.ts +3 -4
  158. package/src/js/value/smush.ts +4 -4
  159. package/src/js/value/unsmush.ts +4 -4
  160. package/types/array/chunk.d.cts +8 -0
  161. package/types/array/chunk.d.ts +2 -2
  162. package/types/array/compact.d.cts +12 -0
  163. package/types/array/compact.d.ts +4 -4
  164. package/types/array/count.d.cts +24 -0
  165. package/types/array/count.d.ts +13 -7
  166. package/types/array/exists.d.cts +26 -0
  167. package/types/array/exists.d.ts +11 -3
  168. package/types/array/filter.d.cts +26 -0
  169. package/types/array/filter.d.ts +14 -6
  170. package/types/array/find.d.cts +26 -0
  171. package/types/array/find.d.ts +14 -6
  172. package/types/array/group-by.d.cts +58 -0
  173. package/types/array/group-by.d.ts +49 -5
  174. package/types/array/index-of.d.cts +26 -0
  175. package/types/array/index-of.d.ts +14 -6
  176. package/types/array/index.d.cts +239 -95
  177. package/types/array/index.d.ts +7 -6
  178. package/types/array/insert.d.cts +16 -0
  179. package/types/array/insert.d.ts +10 -5
  180. package/types/array/models.d.cts +14 -11
  181. package/types/array/models.d.ts +14 -12
  182. package/types/array/shuffle.d.cts +8 -0
  183. package/types/array/shuffle.d.ts +2 -2
  184. package/types/array/sort.d.cts +24 -0
  185. package/types/array/sort.d.ts +10 -10
  186. package/types/array/splice.d.cts +22 -0
  187. package/types/array/splice.d.ts +4 -4
  188. package/types/array/to-map.d.cts +61 -0
  189. package/types/array/to-map.d.ts +42 -20
  190. package/types/array/to-record.d.cts +61 -0
  191. package/types/array/to-record.d.ts +42 -20
  192. package/types/array/unique.d.cts +21 -0
  193. package/types/array/unique.d.ts +10 -6
  194. package/types/colour/base.d.cts +153 -0
  195. package/types/colour/base.d.ts +2 -2
  196. package/types/colour/functions.d.cts +173 -0
  197. package/types/colour/functions.d.ts +3 -3
  198. package/types/colour/hex.d.cts +157 -0
  199. package/types/colour/hex.d.ts +4 -4
  200. package/types/colour/hsl.d.cts +157 -0
  201. package/types/colour/hsl.d.ts +16 -10
  202. package/types/colour/index.d.cts +28 -16
  203. package/types/colour/index.d.ts +1 -1
  204. package/types/colour/is.d.cts +170 -0
  205. package/types/colour/is.d.ts +3 -3
  206. package/types/colour/rgb.d.cts +157 -0
  207. package/types/colour/rgb.d.ts +15 -9
  208. package/types/emitter.d.cts +11 -8
  209. package/types/emitter.d.ts +11 -8
  210. package/types/function.d.cts +10 -10
  211. package/types/function.d.ts +11 -11
  212. package/types/index.d.cts +1120 -736
  213. package/types/index.d.ts +16 -16
  214. package/types/internal/array/callbacks.d.cts +11 -0
  215. package/types/internal/array/callbacks.d.ts +2 -0
  216. package/types/internal/array/find.d.cts +7 -0
  217. package/types/internal/array/find.d.ts +3 -0
  218. package/types/internal/value/handle.d.cts +37 -0
  219. package/types/internal/{value-handle.d.ts → value/handle.d.ts} +1 -1
  220. package/types/is.d.ts +1 -1
  221. package/types/logger.d.cts +13 -21
  222. package/types/logger.d.ts +13 -21
  223. package/types/math.d.cts +1 -1
  224. package/types/math.d.ts +1 -1
  225. package/types/models.d.cts +4 -4
  226. package/types/models.d.ts +1 -4
  227. package/types/number.d.cts +2 -2
  228. package/types/number.d.ts +2 -2
  229. package/types/query.d.cts +2 -2
  230. package/types/query.d.ts +3 -3
  231. package/types/queue.d.cts +1 -1
  232. package/types/queue.d.ts +1 -1
  233. package/types/random.d.cts +8 -12
  234. package/types/random.d.ts +8 -12
  235. package/types/sized.d.cts +6 -6
  236. package/types/sized.d.ts +6 -6
  237. package/types/string/case.d.cts +28 -0
  238. package/types/string/index.d.cts +30 -27
  239. package/types/string/index.d.ts +3 -3
  240. package/types/string/template.d.cts +50 -0
  241. package/types/string/template.d.ts +4 -1
  242. package/types/value/clone.d.cts +8 -0
  243. package/types/value/clone.d.ts +4 -2
  244. package/types/value/compare.d.cts +8 -0
  245. package/types/value/diff.d.cts +25 -0
  246. package/types/value/equal.d.cts +12 -0
  247. package/types/value/get.d.cts +1319 -0
  248. package/types/value/get.d.ts +1 -1
  249. package/types/value/index.d.cts +8 -6
  250. package/types/value/index.d.ts +2 -2
  251. package/types/value/merge.d.cts +72 -0
  252. package/types/value/merge.d.ts +2 -2
  253. package/types/value/set.d.cts +1011 -0
  254. package/types/value/set.d.ts +1 -1
  255. package/types/value/smush.d.cts +1368 -0
  256. package/types/value/smush.d.ts +2 -2
  257. package/types/value/unsmush.d.cts +141 -0
  258. package/types/value/unsmush.d.ts +2 -2
  259. package/dist/js/array/count.mjs +0 -8
  260. package/dist/js/array/exists.mjs +0 -8
  261. package/dist/js/array/filter.mjs +0 -8
  262. package/dist/js/array/find.mjs +0 -8
  263. package/dist/js/array/group-by.mjs +0 -33
  264. package/dist/js/array/index-of.mjs +0 -8
  265. package/dist/js/array/index.mjs +0 -34
  266. package/dist/js/array/insert.mjs +0 -22
  267. package/dist/js/array/models.mjs +0 -0
  268. package/dist/js/array/splice.mjs +0 -9
  269. package/dist/js/array/to-map.mjs +0 -27
  270. package/dist/js/array/to-record.mjs +0 -8
  271. package/dist/js/array/unique.mjs +0 -8
  272. package/dist/js/colour/index.mjs +0 -30
  273. package/dist/js/index.mjs +0 -17
  274. package/dist/js/internal/array-callbacks.mjs +0 -19
  275. package/dist/js/internal/array-find.mjs +0 -51
  276. package/dist/js/logger.mjs +0 -69
  277. package/dist/js/models.mjs +0 -0
  278. package/dist/js/string/index.mjs +0 -44
  279. package/dist/js/value/index.mjs +0 -23
  280. package/src/js/internal/array-callbacks.ts +0 -28
  281. package/types/internal/array-callbacks.d.ts +0 -2
  282. package/types/internal/array-find.d.ts +0 -4
@@ -1,29 +1,54 @@
1
- import {findValue} from '../internal/array-find';
2
- import type {Key} from '../models';
3
- import type {BooleanCallback, KeyCallback} from './models';
1
+ import type {BooleanCallback, KeyCallback} from '~/array/models';
2
+ import {findValue} from '~/internal/array/find';
4
3
 
5
4
  /**
6
- * Returns the first item matching `value`, or `undefined` if no match is found
5
+ * Get the first item matching `value` _(or `undefined` if no match is found)_
7
6
  */
8
- export function find<Model, Value>(
9
- array: Model[],
10
- value: Value | BooleanCallback<Model>,
11
- ): Model | undefined;
7
+ export function find<Item>(array: Item[], value: Item): Item | undefined;
12
8
 
13
9
  /**
14
- * - Returns the first matching item, or `undefined` if no match is found
10
+ * Get the first item matching `value` _(or `undefined` if no match is found)_
11
+ */
12
+ export function find<Item>(
13
+ array: Item[],
14
+ matches: BooleanCallback<Item>,
15
+ ): Item | undefined;
16
+
17
+ /**
18
+ * - Get the first matching item _(or `undefined` if no match is found)_
19
+ * - Use `key` to find a comparison value to match with `value`
20
+ */
21
+ export function find<Item, Key extends keyof Item>(
22
+ array: Item[],
23
+ key: Key,
24
+ value: Item[Key],
25
+ ): Item | undefined;
26
+
27
+ /**
28
+ * - Get the first matching item _(or `undefined` if no match is found)_
15
29
  * - Use `key` to find a comparison value to match with `value`
16
30
  */
17
- export function find<Model, Value = Model>(
18
- array: Model[],
19
- value: Value,
20
- key: Key | KeyCallback<Model>,
21
- ): Model | undefined;
31
+ export function find<Item, Key extends KeyCallback<Item>>(
32
+ array: Item[],
33
+ key: Key,
34
+ value: ReturnType<Key>,
35
+ ): Item | undefined;
36
+
37
+ export function find<Item>(
38
+ array: unknown[],
39
+ ...parameters: unknown[]
40
+ ): Item | undefined {
41
+ const {length} = parameters;
22
42
 
23
- export function find<Model, Value = Model>(
24
- array: Model[],
25
- value: Value | BooleanCallback<Model>,
26
- key?: Key | KeyCallback<Model>,
27
- ): Model | undefined {
28
- return findValue('value', array, value, key) as Model | undefined;
43
+ return findValue(
44
+ 'value',
45
+ array,
46
+ length === 1 && typeof parameters[0] === 'function'
47
+ ? parameters[0]
48
+ : undefined,
49
+ length === 2 ? parameters[0] : undefined,
50
+ length === 1 && typeof parameters[0] !== 'function'
51
+ ? parameters[0]
52
+ : parameters[1],
53
+ ) as Item | undefined;
29
54
  }
@@ -1,47 +1,178 @@
1
- import {getCallbacks} from '../internal/array-callbacks';
2
- import type {Key} from '../models';
3
- import type {KeyCallback} from './models';
1
+ import type {KeyCallback, ValueCallback} from '~/array/models';
2
+ import {getCallbacks} from '~/internal/array/callbacks';
3
+ import type {Key, KeyedValue, PlainObject} from '~/models';
4
4
 
5
5
  /**
6
- * Groups an array of items using a key or callback
6
+ * Create a record from an array of items using a specific key
7
7
  */
8
- export function groupBy<Value>(
9
- array: Value[],
10
- key: Key | KeyCallback<Value>,
11
- ): Record<Key, Value[]> {
12
- return groupValues(array, key, true, false) as never;
8
+ export function groupBy<Item, Key extends keyof Item>(
9
+ array: Item[],
10
+ key: Key,
11
+ ): Record<KeyedValue<Item, Key>, Item>;
12
+
13
+ /**
14
+ * Create a record from an array of items using a specific key, and grouping them into arrays
15
+ */
16
+ export function groupBy<Item, Key extends keyof Item>(
17
+ array: Item[],
18
+ key: Key,
19
+ arrays: true,
20
+ ): Record<KeyedValue<Item, Key>, Item[]>;
21
+
22
+ /**
23
+ * Create a record from an array of items using a specific key
24
+ */
25
+ export function groupBy<Item, Key extends KeyCallback<Item>>(
26
+ array: Item[],
27
+ key: Key,
28
+ ): Record<ReturnType<Key>, Item>;
29
+
30
+ /**
31
+ * Create a record from an array of items using a specific key, and grouping them into arrays
32
+ */
33
+ export function groupBy<Item, Key extends KeyCallback<Item>>(
34
+ array: Item[],
35
+ key: Key,
36
+ arrays: true,
37
+ ): Record<ReturnType<Key>, Item[]>;
38
+
39
+ /**
40
+ * Create a record from an array of items using a specific key and value
41
+ */
42
+ export function groupBy<Item, Key extends keyof Item, Value extends keyof Item>(
43
+ array: Item[],
44
+ key: Key,
45
+ value: Value,
46
+ ): Record<KeyedValue<Item, Key>, KeyedValue<Item, Value>>;
47
+
48
+ /**
49
+ * Create a record from an array of items using a specific key and value, and grouping them into arrays
50
+ */
51
+ export function groupBy<Item, Key extends keyof Item, Value extends keyof Item>(
52
+ array: Item[],
53
+ key: Key,
54
+ value: Value,
55
+ arrays: true,
56
+ ): Record<KeyedValue<Item, Key>, Array<KeyedValue<Item, Value>>>;
57
+
58
+ /**
59
+ * Create a record from an array of items using a specific key and value
60
+ */
61
+ export function groupBy<
62
+ Item,
63
+ Key extends keyof Item,
64
+ Value extends ValueCallback<Item>,
65
+ >(
66
+ array: Item[],
67
+ key: Key,
68
+ value: Value,
69
+ ): Record<KeyedValue<Item, Key>, ReturnType<Value>>;
70
+
71
+ /**
72
+ * Create a record from an array of items using a specific key and value, and grouping them into arrays
73
+ */
74
+ export function groupBy<
75
+ Item,
76
+ Key extends keyof Item,
77
+ Value extends ValueCallback<Item>,
78
+ >(
79
+ array: Item[],
80
+ key: Key,
81
+ value: Value,
82
+ arrays: true,
83
+ ): Record<KeyedValue<Item, Key>, Array<ReturnType<Value>>>;
84
+
85
+ /**
86
+ * Create a record from an array of items using a specific key and value
87
+ */
88
+ export function groupBy<
89
+ Item,
90
+ Key extends KeyCallback<Item>,
91
+ Value extends keyof Item,
92
+ >(
93
+ array: Item[],
94
+ key: Key,
95
+ value: Value,
96
+ ): Record<ReturnType<Key>, KeyedValue<Item, Value>>;
97
+
98
+ /**
99
+ * Create a record from an array of items using a specific key and value, and grouping them into arrays
100
+ */
101
+ export function groupBy<
102
+ Item,
103
+ Key extends KeyCallback<Item>,
104
+ Value extends keyof Item,
105
+ >(
106
+ array: Item[],
107
+ key: Key,
108
+ value: Value,
109
+ arrays: true,
110
+ ): Record<ReturnType<Key>, Array<KeyedValue<Item, Value>>>;
111
+
112
+ /**
113
+ * Create a record from an array of items using a specific key and value
114
+ */
115
+ export function groupBy<
116
+ Item,
117
+ Key extends KeyCallback<Item>,
118
+ Value extends ValueCallback<Item>,
119
+ >(
120
+ array: Item[],
121
+ key: Key,
122
+ value: Value,
123
+ ): Record<ReturnType<Key>, ReturnType<Value>>;
124
+
125
+ /**
126
+ * Create a record from an array of items using a specific key and value, and grouping them into arrays
127
+ */
128
+ export function groupBy<
129
+ Item,
130
+ Key extends KeyCallback<Item>,
131
+ Value extends ValueCallback<Item>,
132
+ >(
133
+ array: Item[],
134
+ key: Key,
135
+ value: Value,
136
+ arrays: true,
137
+ ): Record<ReturnType<Key>, Array<ReturnType<Value>>>;
138
+
139
+ export function groupBy(
140
+ array: unknown[],
141
+ first?: boolean,
142
+ second?: unknown,
143
+ third?: boolean,
144
+ ): PlainObject {
145
+ return groupValues(
146
+ array,
147
+ first,
148
+ second,
149
+ first === true || second === true || third === true,
150
+ ) as never;
13
151
  }
14
152
 
15
- export function groupValues<Value>(
16
- array: Value[],
17
- key: Key | KeyCallback<Value>,
153
+ export function groupValues(
154
+ array: unknown[],
155
+ key: unknown,
156
+ value: unknown,
18
157
  arrays: boolean,
19
- indicable: boolean,
20
158
  ): Record<Key, unknown> {
21
- const callbacks = getCallbacks(undefined, key);
22
- const hasCallback = typeof callbacks?.key === 'function';
23
-
24
- if (!hasCallback && !indicable) {
25
- return {};
26
- }
27
-
159
+ const callbacks = getCallbacks(undefined, key, value);
28
160
  const record: Record<Key, unknown> = {};
29
161
  const {length} = array;
30
162
 
31
163
  for (let index = 0; index < length; index += 1) {
32
- const value = array[index];
164
+ const item = array[index];
33
165
 
34
- const key = hasCallback
35
- ? callbacks?.key?.(value, index, array) ?? index
36
- : index;
166
+ const key = callbacks?.key?.(item, index, array) ?? index;
167
+ const value = callbacks?.value?.(item, index, array) ?? item;
37
168
 
38
169
  if (arrays) {
39
170
  const existing = record[key];
40
171
 
41
- if (Array.isArray(existing)) {
42
- existing.push(value);
43
- } else {
172
+ if (existing == null) {
44
173
  record[key] = [value];
174
+ } else {
175
+ (existing as unknown[]).push(value);
45
176
  }
46
177
  } else {
47
178
  record[key] = value;
@@ -1,29 +1,51 @@
1
- import {findValue} from '../internal/array-find';
2
- import type {Key} from '../models';
3
- import type {BooleanCallback, KeyCallback} from './models';
1
+ import type {BooleanCallback, KeyCallback} from '~/array/models';
2
+ import {findValue} from '~/internal/array/find';
4
3
 
5
4
  /**
6
- * Returns the index for the first item matching `value`, or `-1` if no match is found
5
+ * Get the index for the first item matching `value` _(or `-1` if no match is found)_
7
6
  */
8
- export function indexOf<Model, Value>(
9
- array: Model[],
10
- value: Value | BooleanCallback<Model>,
7
+ export function indexOf<Item>(array: Item[], value: Item): number;
8
+
9
+ /**
10
+ * Get the index for the first item matching `value` _(or `-1` if no match is found)_
11
+ */
12
+ export function indexOf<Item>(
13
+ array: Item[],
14
+ matches: BooleanCallback<Item>,
11
15
  ): number;
12
16
 
13
17
  /**
14
- * - Returns the index for the first matching item, or `-1` if no match is found
18
+ * - Get the index for the first matching item _(or `-1` if no match is found)_
15
19
  * - Use `key` to find a comparison value to match with `value`
16
20
  */
17
- export function indexOf<Model, Value = Model>(
18
- array: Model[],
19
- value: Value,
20
- key: Key | KeyCallback<Model>,
21
+ export function indexOf<Item, Key extends keyof Item>(
22
+ array: Item[],
23
+ key: Key,
24
+ value: Item[Key],
21
25
  ): number;
22
26
 
23
- export function indexOf<Model, Value = Model>(
24
- array: Model[],
25
- value: Value | BooleanCallback<Model>,
26
- key?: Key | KeyCallback<Model>,
27
- ): number {
28
- return findValue('index', array, value, key) as number;
27
+ /**
28
+ * - Get the index for the first matching item _(or `-1` if no match is found)_
29
+ * - Use `key` to find a comparison value to match with `value`
30
+ */
31
+ export function indexOf<Item, Key extends KeyCallback<Item>>(
32
+ array: Item[],
33
+ key: Key,
34
+ value: ReturnType<Key>,
35
+ ): number;
36
+
37
+ export function indexOf(array: unknown[], ...parameters: unknown[]): number {
38
+ const {length} = parameters;
39
+
40
+ return findValue(
41
+ 'index',
42
+ array,
43
+ length === 1 && typeof parameters[0] === 'function'
44
+ ? parameters[0]
45
+ : undefined,
46
+ length === 2 ? parameters[0] : undefined,
47
+ length === 1 && typeof parameters[0] !== 'function'
48
+ ? parameters[0]
49
+ : parameters[1],
50
+ ) as number;
29
51
  }
@@ -1,19 +1,20 @@
1
- import type {NestedArrayType} from '../models';
2
- import {insertValues} from './insert';
1
+ import {insertValues} from '~/array/insert';
2
+ import type {NestedArrayType} from '~/models';
3
3
 
4
4
  /**
5
- * Flattens an array _(using native `flat` and maximum depth)_
5
+ * Flatten an array _(using native `flat` and maximum depth)_
6
6
  */
7
- export function flatten<Value>(array: Value[]): NestedArrayType<Value>[] {
8
- return array.flat(Number.POSITIVE_INFINITY) as NestedArrayType<Value>[];
7
+ export function flatten<Item>(array: Item[]): NestedArrayType<Item>[] {
8
+ return array.flat(Number.POSITIVE_INFINITY) as NestedArrayType<Item>[];
9
9
  }
10
10
 
11
11
  /**
12
- * - Pushes values to the end of an array, returning the new length
13
- * - Uses chunking to avoid stack overflow
12
+ * - Push values to the end of an array
13
+ * - Returns the new length
14
+ * - _(Uses chunking to avoid stack overflow)_
14
15
  */
15
- export function push<Value>(array: Value[], values: Value[]): number {
16
- return insertValues('push', array, values, array.length, 0) as number;
16
+ export function push<Item>(array: Item[], pushed: Item[]): number {
17
+ return insertValues('push', array, pushed, array.length, 0) as number;
17
18
  }
18
19
 
19
20
  export * from './chunk';
@@ -1,30 +1,44 @@
1
- import {chunk} from './chunk';
2
- import type {InsertType} from './models';
1
+ import {chunk} from '~/array/chunk';
2
+ import type {InsertType} from '~/array/models';
3
3
 
4
4
  /**
5
- * - Inserts values into an array at a specified index
6
- * - Uses chunking to avoid stack overflow
5
+ * - Insert values into an array _(at the end)_
6
+ * - _(Uses chunking to avoid stack overflow_)
7
7
  */
8
- export function insert<Value>(
9
- array: Value[],
10
- index: number,
11
- values: Value[],
8
+ export function insert<Item>(array: Item[], items: Item[]): void;
9
+
10
+ /**
11
+ * - Insert values into an array at a specified index
12
+ * - _(Uses chunking to avoid stack overflow_)
13
+ */
14
+ export function insert<Item>(array: Item[], index: number, items: Item[]): void;
15
+
16
+ export function insert(
17
+ array: unknown[],
18
+ first: unknown,
19
+ second?: unknown[],
12
20
  ): void {
13
- insertValues('splice', array, values, index, 0);
21
+ insertValues(
22
+ 'splice',
23
+ array,
24
+ Array.isArray(first) ? first : (second ?? []),
25
+ typeof first === 'number' ? first : array.length,
26
+ 0,
27
+ );
14
28
  }
15
29
 
16
- export function insertValues<Value>(
30
+ export function insertValues<Item>(
17
31
  type: InsertType,
18
- array: Value[],
19
- values: Value[],
32
+ array: Item[],
33
+ items: Item[],
20
34
  start: number,
21
35
  deleteCount: number,
22
36
  ): unknown {
23
- const chunked = chunk(values);
37
+ const chunked = chunk(items);
24
38
  const lastIndex = chunked.length - 1;
25
39
 
26
40
  let index = Number(chunked.length);
27
- let returned: Value[] | undefined;
41
+ let returned: Item[] | undefined;
28
42
 
29
43
  while (--index >= 0) {
30
44
  const result = array.splice(
@@ -1,32 +1,35 @@
1
- import type {Key} from '../models';
1
+ import type {GenericCallback, Key} from '~/models';
2
2
 
3
- export type ArrayCallback<Value, Returned> = (
4
- value: Value,
3
+ export type ArrayCallback<Item, Value> = (
4
+ item: Item,
5
5
  index: number,
6
- array: Value[],
7
- ) => Returned;
6
+ array: Item[],
7
+ ) => Value;
8
8
 
9
- export type BooleanCallback<Value> = ArrayCallback<Value, boolean>;
9
+ export type BooleanCallback<Item> = ArrayCallback<Item, boolean>;
10
10
 
11
- export type Callbacks<Value> = {
12
- bool?: BooleanCallback<Value>;
13
- key?: KeyCallback<Value>;
11
+ export type Callbacks = {
12
+ bool?: GenericCallback;
13
+ key?: GenericCallback;
14
+ value?: GenericCallback;
14
15
  };
15
16
 
16
17
  export type FindType = 'index' | 'value';
17
18
 
18
19
  export type InsertType = 'push' | 'splice';
19
20
 
20
- export type KeyCallback<Value> = ArrayCallback<Value, Key>;
21
+ export type KeyCallback<Item> = ArrayCallback<Item, Key>;
21
22
 
22
- export type SortKey<Value> = {
23
+ export type SortKey<Item> = {
23
24
  direction: 'asc' | 'desc';
24
- value: Key | SortKeyCallback<Value>;
25
+ value: Key | SortKeyCallback<Item>;
25
26
  };
26
27
 
27
- export type SortKeyCallback<Value> = (value: Value) => Key;
28
+ export type SortKeyCallback<Item> = (item: Item) => unknown;
28
29
 
29
- export type SortKeyWithCallback<Value> = {
30
- callback: SortKeyCallback<Value>;
30
+ export type SortKeyWithCallback<Item> = {
31
+ callback: SortKeyCallback<Item>;
31
32
  direction: 'asc' | 'desc';
32
33
  };
34
+
35
+ export type ValueCallback<Item> = ArrayCallback<Item, unknown>;
@@ -1,9 +1,9 @@
1
- import {getRandomInteger} from '../random';
1
+ import {getRandomInteger} from '~/random';
2
2
 
3
3
  /**
4
- * Shuffles an array
4
+ * Shuffle an array
5
5
  */
6
- export function shuffle<Value>(array: Value[]): Value[] {
6
+ export function shuffle<Item>(array: Item[]): Item[] {
7
7
  const shuffled = array.slice();
8
8
  const {length} = shuffled;
9
9
 
@@ -1,43 +1,42 @@
1
- import {isKey} from '../is';
2
- import type {Key, PlainObject} from '../models';
3
- import {compare} from '../value/compare';
4
- import type {SortKey, SortKeyCallback, SortKeyWithCallback} from './models';
1
+ import type {
2
+ SortKey,
3
+ SortKeyCallback,
4
+ SortKeyWithCallback,
5
+ } from '~/array/models';
6
+ import {isKey} from '~/is';
7
+ import type {Key, PlainObject} from '~/models';
8
+ import {compare} from '~/value/compare';
5
9
 
6
10
  /**
7
- * Sorts an array of items _(ascending by default)_
11
+ * Sort an array of items _(defaults to ascending)_
8
12
  */
9
- export function sort<Value>(array: Value[], descending?: boolean): Value[];
13
+ export function sort<Item>(array: Item[], descending?: boolean): Item[];
10
14
 
11
15
  /**
12
- * - Sorts an array of items, using a `key` to sort by a specific value
13
- * - Ascending by default, but can be changed by setting `descending` to `true`, or using a `SortKey`
16
+ * - Sort an array of items, using a `key` to sort by a specific value
17
+ * - Defaults to ascending, but can be changed by setting `descending` to `true`, or using a `SortKey`
14
18
  */
15
- export function sort<Value>(
16
- array: Value[],
17
- key: Key | SortKey<Value> | SortKeyCallback<Value>,
19
+ export function sort<Item>(
20
+ array: Item[],
21
+ key: Key | SortKey<Item> | SortKeyCallback<Item>,
18
22
  descending?: boolean,
19
- ): Value[];
23
+ ): Item[];
20
24
 
21
25
  /**
22
- * - Sorts an array of items, using multiple `keys` to sort by specific values
23
- * - Ascending by default, but can be changed by setting `descending` to `true`, or using `SortKey`
26
+ * - Sort an array of items, using multiple `keys` to sort by specific values
27
+ * - Defaults to ascending, but can be changed by setting `descending` to `true`, or using `SortKey`
24
28
  */
25
- export function sort<Value>(
26
- array: Value[],
27
- keys: Array<Key | SortKey<Value> | SortKeyCallback<Value>>,
29
+ export function sort<Item>(
30
+ array: Item[],
31
+ keys: Array<Key | SortKey<Item> | SortKeyCallback<Item>>,
28
32
  descending?: boolean,
29
- ): Value[];
30
-
31
- export function sort<Value>(
32
- array: Value[],
33
- first?:
34
- | boolean
35
- | Key
36
- | SortKey<Value>
37
- | SortKeyCallback<Value>
38
- | Array<Key | SortKey<Value> | SortKeyCallback<Value>>,
39
- second?: boolean,
40
- ): Value[] {
33
+ ): Item[];
34
+
35
+ export function sort(
36
+ array: unknown[],
37
+ first?: unknown,
38
+ second?: unknown,
39
+ ): unknown[] {
41
40
  if (array.length < 2) {
42
41
  return array;
43
42
  }
@@ -52,23 +51,22 @@ export function sort<Value>(
52
51
 
53
52
  const keys = (Array.isArray(first) ? first : [first])
54
53
  .map(key => {
55
- const returned: SortKeyWithCallback<Value> = {
54
+ const returned: SortKeyWithCallback<unknown> = {
56
55
  direction,
57
56
  callback: undefined as never,
58
57
  };
59
58
 
60
59
  if (isKey(key)) {
61
- returned.callback = (value: Value) =>
62
- (value as PlainObject)[key] as never;
60
+ returned.callback = value => (value as PlainObject)[key] as never;
63
61
  } else if (typeof key === 'function') {
64
62
  returned.callback = key;
65
63
  } else if (typeof key?.value === 'function' || isKey(key?.value)) {
66
64
  returned.direction = key?.direction ?? direction;
65
+
67
66
  returned.callback =
68
67
  typeof key.value === 'function'
69
68
  ? key.value
70
- : (value: Value) =>
71
- (value as PlainObject)[key.value as Key] as never;
69
+ : value => (value as PlainObject)[key.value as Key] as never;
72
70
  }
73
71
 
74
72
  return returned;