@oscarpalmer/atoms 0.192.0 → 0.193.1

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 (130) hide show
  1. package/dist/index.d.mts +25 -23
  2. package/dist/index.mjs +30 -31
  3. package/dist/queue.d.mts +25 -23
  4. package/dist/queue.mjs +27 -28
  5. package/package.json +3 -4
  6. package/src/array/difference.ts +0 -73
  7. package/src/array/exists.ts +0 -93
  8. package/src/array/filter.ts +0 -182
  9. package/src/array/find.ts +0 -183
  10. package/src/array/first.ts +0 -196
  11. package/src/array/flatten.ts +0 -20
  12. package/src/array/from.ts +0 -129
  13. package/src/array/get.ts +0 -131
  14. package/src/array/group-by.ts +0 -369
  15. package/src/array/index.ts +0 -26
  16. package/src/array/insert.ts +0 -52
  17. package/src/array/intersection.ts +0 -72
  18. package/src/array/last.ts +0 -194
  19. package/src/array/match.ts +0 -436
  20. package/src/array/move.ts +0 -315
  21. package/src/array/partition.ts +0 -91
  22. package/src/array/push.ts +0 -23
  23. package/src/array/reverse.ts +0 -33
  24. package/src/array/select.ts +0 -260
  25. package/src/array/single.ts +0 -93
  26. package/src/array/slice.ts +0 -335
  27. package/src/array/sort.ts +0 -607
  28. package/src/array/splice.ts +0 -106
  29. package/src/array/swap.ts +0 -322
  30. package/src/array/to-map.ts +0 -371
  31. package/src/array/to-record.ts +0 -351
  32. package/src/array/to-set.ts +0 -80
  33. package/src/array/toggle.ts +0 -79
  34. package/src/array/union.ts +0 -72
  35. package/src/array/unique.ts +0 -67
  36. package/src/array/update.ts +0 -75
  37. package/src/beacon.ts +0 -378
  38. package/src/color/constants.ts +0 -137
  39. package/src/color/index.ts +0 -83
  40. package/src/color/instance.ts +0 -162
  41. package/src/color/misc/alpha.ts +0 -56
  42. package/src/color/misc/format.ts +0 -29
  43. package/src/color/misc/get.ts +0 -210
  44. package/src/color/misc/is.ts +0 -221
  45. package/src/color/misc/state.ts +0 -243
  46. package/src/color/models.ts +0 -389
  47. package/src/color/space/hex.ts +0 -184
  48. package/src/color/space/hsl.ts +0 -174
  49. package/src/color/space/hwb.ts +0 -153
  50. package/src/color/space/rgb.ts +0 -240
  51. package/src/function/assert.ts +0 -174
  52. package/src/function/index.ts +0 -2
  53. package/src/function/limit.ts +0 -83
  54. package/src/function/memoize.ts +0 -202
  55. package/src/function/once.ts +0 -200
  56. package/src/function/retry.ts +0 -175
  57. package/src/function/work.ts +0 -1007
  58. package/src/herald.ts +0 -216
  59. package/src/index.ts +0 -68
  60. package/src/internal/array/callbacks.ts +0 -46
  61. package/src/internal/array/chunk.ts +0 -51
  62. package/src/internal/array/compact.ts +0 -56
  63. package/src/internal/array/find.ts +0 -200
  64. package/src/internal/array/group.ts +0 -42
  65. package/src/internal/array/index-of.ts +0 -182
  66. package/src/internal/array/insert.ts +0 -80
  67. package/src/internal/array/overlap.ts +0 -35
  68. package/src/internal/array/sets.ts +0 -74
  69. package/src/internal/array/shuffle.ts +0 -33
  70. package/src/internal/array/update.ts +0 -52
  71. package/src/internal/function/misc.ts +0 -8
  72. package/src/internal/function/timer.ts +0 -168
  73. package/src/internal/is.ts +0 -244
  74. package/src/internal/math/aggregate.ts +0 -164
  75. package/src/internal/math/misc.ts +0 -28
  76. package/src/internal/number.ts +0 -129
  77. package/src/internal/random.ts +0 -49
  78. package/src/internal/result.ts +0 -92
  79. package/src/internal/sized.ts +0 -25
  80. package/src/internal/string.ts +0 -128
  81. package/src/internal/subscription.ts +0 -264
  82. package/src/internal/value/compare.ts +0 -170
  83. package/src/internal/value/equal.ts +0 -465
  84. package/src/internal/value/get.ts +0 -57
  85. package/src/internal/value/handlers.ts +0 -95
  86. package/src/internal/value/has.ts +0 -118
  87. package/src/internal/value/misc.ts +0 -125
  88. package/src/internal/value/partial.ts +0 -46
  89. package/src/internal/value/set.ts +0 -132
  90. package/src/is.ts +0 -182
  91. package/src/logger.ts +0 -230
  92. package/src/math.ts +0 -281
  93. package/src/models.ts +0 -286
  94. package/src/number.ts +0 -1
  95. package/src/promise/delay.ts +0 -62
  96. package/src/promise/helpers.ts +0 -96
  97. package/src/promise/index.ts +0 -300
  98. package/src/promise/misc.ts +0 -96
  99. package/src/promise/models.ts +0 -195
  100. package/src/promise/timed.ts +0 -91
  101. package/src/query.ts +0 -147
  102. package/src/queue.ts +0 -814
  103. package/src/random.ts +0 -125
  104. package/src/result/index.ts +0 -105
  105. package/src/result/match.ts +0 -124
  106. package/src/result/misc.ts +0 -129
  107. package/src/result/models.ts +0 -83
  108. package/src/result/work/flow.ts +0 -488
  109. package/src/result/work/pipe.ts +0 -496
  110. package/src/sized/map.ts +0 -110
  111. package/src/sized/set.ts +0 -107
  112. package/src/string/case.ts +0 -236
  113. package/src/string/fuzzy.ts +0 -444
  114. package/src/string/index.ts +0 -199
  115. package/src/string/match.ts +0 -80
  116. package/src/string/normalize.ts +0 -205
  117. package/src/string/template.ts +0 -161
  118. package/src/value/clone.ts +0 -319
  119. package/src/value/collection.ts +0 -63
  120. package/src/value/diff.ts +0 -232
  121. package/src/value/freeze.ts +0 -322
  122. package/src/value/handle.ts +0 -3
  123. package/src/value/index.ts +0 -5
  124. package/src/value/merge.ts +0 -278
  125. package/src/value/omit.ts +0 -20
  126. package/src/value/pick.ts +0 -20
  127. package/src/value/shake.ts +0 -45
  128. package/src/value/smush.ts +0 -89
  129. package/src/value/transform.ts +0 -153
  130. package/src/value/unsmush.ts +0 -84
@@ -1,260 +0,0 @@
1
- import {FIND_VALUES_ALL, findValues} from '../internal/array/find';
2
- import type {PlainObject} from '../models';
3
-
4
- // #region Functions
5
-
6
- /**
7
- * Get a filtered and mapped array of items
8
- *
9
- * @param array Array to search in
10
- * @param filterCallback Callback to get an item's value for matching
11
- * @param filterValue Value to match against
12
- * @param mapCallback Callback to map the matched items
13
- * @returns Filtered and mapped array of items
14
- *
15
- * @example
16
- * ```typescript
17
- * select(
18
- * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
19
- * item => item.id,
20
- * 2,
21
- * item => item.name,
22
- * ); // => ['Bob']
23
- * ```
24
- */
25
- export function select<
26
- Item,
27
- FilterCallback extends (item: Item, index: number, array: Item[]) => unknown,
28
- MapCallback extends (item: Item, index: number, array: Item[]) => unknown,
29
- >(
30
- array: Item[],
31
- filterCallback: FilterCallback,
32
- filterValue: ReturnType<FilterCallback>,
33
- mapCallback: MapCallback,
34
- ): Array<ReturnType<MapCallback>>;
35
-
36
- /**
37
- * Get a filtered and mapped array of items
38
- *
39
- * @param array Array to search in
40
- * @param filterCallback Callback to get an item's value for matching
41
- * @param filterValue Value to match against
42
- * @param mapKey Key to get an item's value for mapping
43
- * @returns Filtered and mapped array of items
44
- *
45
- * @example
46
- * ```typescript
47
- * select(
48
- * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
49
- * item => item.id,
50
- * 2,
51
- * 'name',
52
- * ); // => ['Bob']
53
- * ```
54
- */
55
- export function select<
56
- Item extends PlainObject,
57
- FilterCallback extends (item: Item, index: number, array: Item[]) => unknown,
58
- MapKey extends keyof Item,
59
- >(
60
- array: Item[],
61
- filterCallback: FilterCallback,
62
- filterValue: ReturnType<FilterCallback>,
63
- mapKey: MapKey,
64
- ): Array<Item[MapKey]>;
65
-
66
- /**
67
- * Get a filtered and mapped array of items
68
- *
69
- * @param array Array to search in
70
- * @param filterKey Key to get an item's value for matching
71
- * @param filterValue Value to match against
72
- * @param mapCallback Callback to map the matched items
73
- * @returns Filtered and mapped array of items
74
- *
75
- * @example
76
- * ```typescript
77
- * select(
78
- * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
79
- * 'id',
80
- * 2,
81
- * item => item.name,
82
- * ); // => ['Bob']
83
- * ```
84
- */
85
- export function select<
86
- Item extends PlainObject,
87
- ItemKey extends keyof Item,
88
- MapCallback extends (item: Item, index: number, array: Item[]) => unknown,
89
- >(
90
- array: Item[],
91
- filterKey: ItemKey,
92
- filterValue: Item[ItemKey],
93
- mapCallback: MapCallback,
94
- ): Array<ReturnType<MapCallback>>;
95
-
96
- /**
97
- * Get a filtered and mapped array of items
98
- *
99
- * @param array Array to search in
100
- * @param filterKey Key to get an item's value for matching
101
- * @param filterValue Value to match against
102
- * @param mapKey Key to get an item's value for mapping
103
- * @returns Filtered and mapped array of items
104
- *
105
- * @example
106
- * ```typescript
107
- * select(
108
- * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
109
- * 'id',
110
- * 2,
111
- * 'name',
112
- * ); // => ['Bob']
113
- * ```
114
- */
115
- export function select<
116
- Item extends PlainObject,
117
- ItemKey extends keyof Item,
118
- MapKey extends keyof Item,
119
- >(
120
- array: Item[],
121
- filterKey: ItemKey,
122
- filterValue: Item[ItemKey],
123
- mapKey: MapKey,
124
- ): Array<Item[MapKey]>;
125
-
126
- /**
127
- * Get a filtered and mapped array of items
128
- *
129
- * @param array Array to search in
130
- * @param filterCallback Filter callback to match items
131
- * @param mapCallback Callback to map the matched items
132
- * @returns Filtered and mapped array of items
133
- *
134
- * @example
135
- * ```typescript
136
- * select(
137
- * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
138
- * item => item.id === 2,
139
- * item => item.name,
140
- * ); // => ['Bob']
141
- * ```
142
- */
143
- export function select<
144
- Item,
145
- FilterCallback extends (item: Item, index: number, array: Item[]) => unknown,
146
- MapCallback extends (item: Item, index: number, array: Item[]) => unknown,
147
- >(
148
- array: Item[],
149
- filterCallback: FilterCallback,
150
- filterValue: ReturnType<FilterCallback>,
151
- mapCallback: MapCallback,
152
- ): Array<ReturnType<MapCallback>>;
153
-
154
- /**
155
- * Get a filtered and mapped array of items
156
- *
157
- * @param array Array to search in
158
- * @param filterCallback Filter callback to match items
159
- * @param mapKey Key to get an item's value for mapping
160
- * @returns Filtered and mapped array of items
161
- *
162
- * @example
163
- * ```typescript
164
- * select(
165
- * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
166
- * item => item.id,
167
- * 2,
168
- * 'name'
169
- * ); // => ['Bob']
170
- * ```
171
- */
172
- export function select<
173
- Item extends PlainObject,
174
- FilterCallback extends (item: Item, index: number, array: Item[]) => unknown,
175
- MapKey extends keyof Item,
176
- >(
177
- array: Item[],
178
- filterCallback: FilterCallback,
179
- filterValue: ReturnType<FilterCallback>,
180
- mapKey: MapKey,
181
- ): Array<Item[MapKey]>;
182
-
183
- /**
184
- * Get a filtered and mapped array of items
185
- *
186
- * @param array Array to search in
187
- * @param filter Filter callback to match items
188
- * @param map Callback to map the matched items
189
- * @returns Filtered and mapped array of items
190
- *
191
- * @example
192
- * ```typescript
193
- * select(
194
- * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
195
- * item => item.id === 2,
196
- * item => item.name,
197
- * ); // => ['Bob']
198
- * ```
199
- */
200
- export function select<
201
- Item,
202
- MapCallback extends (item: Item, index: number, array: Item[]) => unknown,
203
- >(
204
- array: Item[],
205
- filter: (item: Item, index: number, array: Item[]) => boolean,
206
- map: MapCallback,
207
- ): Array<ReturnType<MapCallback>>;
208
-
209
- /**
210
- * Get a filtered and mapped array of items
211
- *
212
- * @param array Array to search in
213
- * @param filter Filter callback to match items
214
- * @param map Key to get an item's value for mapping
215
- * @returns Filtered and mapped array of items
216
- *
217
- * @example
218
- * ```typescript
219
- * select(
220
- * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
221
- * item => item.id === 2,
222
- * 'name'
223
- * ); // => ['Bob']
224
- * ```
225
- */
226
- export function select<Item extends PlainObject, MapKey extends keyof Item>(
227
- array: Item[],
228
- filter: (item: Item, index: number, array: Item[]) => boolean,
229
- map: MapKey,
230
- ): Array<Item[MapKey]>;
231
-
232
- /**
233
- * Get a filtered and mapped array of items
234
- *
235
- * @param array Array to search in
236
- * @param item Item to match against
237
- * @param map Callback to map the matched items
238
- * @returns Filtered and mapped array of items
239
- *
240
- * @example
241
- * ```typescript
242
- * select(
243
- * [1, 2, 3, 2, 1],
244
- * 3,
245
- * value => value ** 2,
246
- * ); // => [9]
247
- * ```
248
- */
249
- export function select<
250
- Item,
251
- MapCallback extends (item: Item, index: number, array: Item[]) => unknown,
252
- >(array: Item[], item: Item, map: MapCallback): Array<ReturnType<MapCallback>>;
253
-
254
- export function select(array: unknown[], ...parameters: unknown[]): unknown[] {
255
- const mapper = parameters.pop();
256
-
257
- return findValues(FIND_VALUES_ALL, array, parameters, mapper).matched;
258
- }
259
-
260
- // #endregion
@@ -1,93 +0,0 @@
1
- import {FIND_VALUES_ALL, findValues} from '../internal/array/find';
2
- import type {PlainObject} from '../models';
3
-
4
- // #region Functions
5
-
6
- /**
7
- * Get the _only_ item matching the given value
8
- *
9
- * Throws an error if multiple items match the value
10
- *
11
- * @param array Array to search in
12
- * @param callback Callback to get an item's value for matching
13
- * @param value Value to match against
14
- * @returns Only item that matches the value, or `undefined` if no match is found
15
- *
16
- * @example
17
- * ```typescript
18
- * single(
19
- * [{id: 1}, {id: 2}, {id: 3}],
20
- * item => item.id,
21
- * 2
22
- * ); // => {id: 2}
23
- * ```
24
- */
25
- export function single<
26
- Item,
27
- Callback extends (item: Item, index: number, array: Item[]) => unknown,
28
- >(array: Item[], callback: Callback, value: ReturnType<Callback>): Item | undefined;
29
-
30
- /**
31
- * Get the _only_ item matching the given value by key
32
- *
33
- * Throws an error if multiple items match the value
34
- *
35
- * @param array Array to search in
36
- * @param key Key to get an item's value for matching
37
- * @param value Value to match against
38
- * @returns Only item that matches the value, or `undefined` if no match is found
39
- *
40
- * @example
41
- * ```typescript
42
- * single(
43
- * [{id: 1}, {id: 2}, {id: 3}],
44
- * 'id',
45
- * 2
46
- * ); // => {id: 2}
47
- * ```
48
- */
49
- export function single<Item extends PlainObject, ItemKey extends keyof Item>(
50
- array: Item[],
51
- key: ItemKey,
52
- value: Item[ItemKey],
53
- ): Item | undefined;
54
-
55
- /**
56
- * Get the _only_ item matching the filter
57
- *
58
- * Throws an error if multiple items match the filter
59
- *
60
- * @param array Array to search in
61
- * @param filter Filter callback to match items
62
- * @returns Only item that matches the filter, or `undefined` if no match is found
63
- *
64
- * @example
65
- * ```typescript
66
- * single(
67
- * [{id: 1}, {id: 2}, {id: 3}],
68
- * item => item.id === 2
69
- * ); // => {id: 2}
70
- * ```
71
- */
72
- export function single<Item>(
73
- array: Item[],
74
- filter: (item: Item, index: number, array: Item[]) => boolean,
75
- ): Item | undefined;
76
-
77
- export function single(array: unknown[], ...parameters: unknown[]): unknown {
78
- const {matched} = findValues(FIND_VALUES_ALL, array, parameters);
79
-
80
- if (matched.length > 1) {
81
- throw new Error(SINGLE_MESSAGE);
82
- }
83
-
84
- return matched[0];
85
- }
86
-
87
- // #endregion
88
-
89
- // #region Variables
90
-
91
- const SINGLE_MESSAGE = 'Multiple items were found';
92
-
93
- // #endregion
@@ -1,335 +0,0 @@
1
- import {getArrayCallbacks} from '../internal/array/callbacks';
2
- import type {PlainObject} from '../models';
3
-
4
- // #region Types
5
-
6
- type ExtractType = 'drop' | 'take';
7
-
8
- // #endregion
9
-
10
- // #region Functions
11
-
12
- /**
13
- * Drop items from the start of an array until they match a value
14
- *
15
- * @param array Original array
16
- * @param callback Callback to get an item's value for matching
17
- * @param value Value to match against
18
- * @returns New array with items dropped
19
- *
20
- * @example
21
- * ```typescript
22
- * drop(
23
- * [{id: 1}, {id: 2}, {id: 3}],
24
- * item => item.id,
25
- * 2,
26
- * ); // => [{id: 2}, {id: 3}]
27
- * ```
28
- */
29
- export function drop<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(
30
- array: Item[],
31
- callback: Callback,
32
- value: ReturnType<Callback>,
33
- ): Item[];
34
-
35
- /**
36
- * Drop items from the start of an array until they match a value
37
- *
38
- * @param array Original array
39
- * @param key Key to get an item's value for matching
40
- * @param value Value to match against
41
- * @returns New array with items dropped
42
- *
43
- * @example
44
- * ```typescript
45
- * drop(
46
- * [{id: 1}, {id: 2}, {id: 3}],
47
- * 'id',
48
- * 2,
49
- * ); // => [{id: 2}, {id: 3}]
50
- * ```
51
- */
52
- export function drop<Item extends PlainObject, ItemKey extends keyof Item>(
53
- array: Item[],
54
- key: ItemKey,
55
- value: Item[ItemKey],
56
- ): Item[];
57
-
58
- /**
59
- * Drop items from the start of an array while they match a filter
60
- *
61
- * @param array Original array
62
- * @param callback Filter callback to match items
63
- * @returns New array with items dropped
64
- *
65
- * @example
66
- * ```typescript
67
- * drop(
68
- * [{id: 1}, {id: 2}, {id: 3}],
69
- * item => item.id === 2,
70
- * ); // => [{id: 2}, {id: 3}]
71
- * ```
72
- */
73
- export function drop<Item extends PlainObject>(
74
- array: Item[],
75
- callback: (item: Item, index: number, array: Item[]) => boolean,
76
- ): Item[];
77
-
78
- /**
79
- * Drop a specified number of items, from the start if `>= 0`, or from the end if `< 0`
80
- *
81
- * @param array Original array
82
- * @param count Number of items to drop
83
- * @returns New array with items dropped
84
- *
85
- * @example
86
- * ```typescript
87
- * drop([1, 2, 3, 4, 5], 2); // => [3, 4, 5]
88
- * drop([1, 2, 3, 4, 5], -2); // => [1, 2, 3]
89
- * ```
90
- */
91
- export function drop(array: unknown[], count: number): unknown[];
92
-
93
- export function drop(array: unknown[], first?: unknown, second?: unknown): unknown[] {
94
- return extract(EXTRACT_DROP, array, first, second);
95
- }
96
-
97
- function extract(
98
- type: ExtractType,
99
- array: unknown[],
100
- first?: unknown,
101
- second?: unknown,
102
- ): unknown[] {
103
- if (!Array.isArray(array)) {
104
- return [];
105
- }
106
-
107
- const {length} = array;
108
-
109
- if (length === 0) {
110
- return [];
111
- }
112
-
113
- const isTake = type === EXTRACT_TAKE;
114
-
115
- if (typeof first === 'number') {
116
- if (Math.abs(first) >= length) {
117
- return isTake ? array.slice() : [];
118
- }
119
-
120
- if (first === 0) {
121
- return isTake ? [] : array.slice();
122
- }
123
-
124
- if (isTake) {
125
- return first >= 0 ? array.slice(0, first) : array.slice(array.length + first);
126
- }
127
-
128
- return first >= 0 ? array.slice(first) : array.slice(0, array.length + first);
129
- }
130
-
131
- const callbacks =
132
- second == null ? getArrayCallbacks(first) : getArrayCallbacks(undefined, undefined, first);
133
-
134
- const isBoolean = callbacks?.bool != null;
135
-
136
- if (callbacks?.bool == null && callbacks?.value == null) {
137
- return isTake ? array.slice() : [];
138
- }
139
-
140
- const extracted: unknown[] = [];
141
-
142
- let push = false;
143
-
144
- for (let index = 0; index < length; index += 1) {
145
- const item = array[index];
146
-
147
- const matches = isBoolean
148
- ? callbacks.bool!(item, index, array)
149
- : Object.is(callbacks!.value!(item, index, array), second);
150
-
151
- if (isTake) {
152
- if (isBoolean ? !matches : matches) {
153
- break;
154
- }
155
-
156
- extracted.push(item);
157
-
158
- continue;
159
- }
160
-
161
- if (push) {
162
- extracted.push(item);
163
- } else if (isBoolean ? !matches : matches) {
164
- push = true;
165
-
166
- if (isBoolean) {
167
- extracted.push(item);
168
- }
169
- }
170
- }
171
-
172
- return extracted;
173
- }
174
-
175
- /**
176
- * Slice an array _(returning a new array with a specified range of items)_
177
- *
178
- * @param array Original array
179
- * @param start Start index _(inclusive)_
180
- * @param end End index _(exclusive)_
181
- * @returns New array with sliced items
182
- *
183
- * @example
184
- * ```typescript
185
- * slice([1, 2, 3, 4, 5], 1, 4); // => [2, 3, 4]
186
- * ```
187
- */
188
- export function slice<Item>(array: Item[], start: number, end: number): Item[];
189
-
190
- /**
191
- * Slice an array _(returning a new array with a specified number of items, from the start)_
192
- *
193
- * @param array Original array
194
- * @param count Maximum size of the new array
195
- * @returns New array with sliced items
196
- *
197
- * @example
198
- * ```typescript
199
- * slice([1, 2, 3, 4, 5], 3); // => [1, 2, 3]
200
- * ```
201
- */
202
- export function slice<Item>(array: Item[], count: number): Item[];
203
-
204
- /**
205
- * Slice an array _(returning a new array with all items)_
206
- *
207
- * @param array Array to slice
208
- * @returns Sliced array
209
- *
210
- * @example
211
- * ```typescript
212
- * slice([1, 2, 3]); // => [1, 2, 3]
213
- * ```
214
- */
215
- export function slice<Item>(array: Item[]): Item[];
216
-
217
- export function slice(array: unknown[], first?: number, second?: number): unknown[] {
218
- if (!Array.isArray(array) || array.length === 0) {
219
- return [];
220
- }
221
-
222
- const firstIsNumber = typeof first === 'number';
223
- const secondIsNumber = typeof second === 'number';
224
-
225
- if (!firstIsNumber && !secondIsNumber) {
226
- return array.slice();
227
- }
228
-
229
- if (!secondIsNumber) {
230
- return first! >= 0 ? array.slice(0, first) : array.slice(array.length + first!);
231
- }
232
-
233
- if (!firstIsNumber) {
234
- return array.slice();
235
- }
236
-
237
- return first! >= 0
238
- ? array.slice(first!, second!)
239
- : array.slice(array.length + first!, array.length + second!);
240
- }
241
-
242
- /**
243
- * Take items from the start of an array until they match a value
244
- *
245
- * @param array Original array
246
- * @param callback Callback to get an item's value for matching
247
- * @param value Value to match against
248
- * @returns New array with taken items
249
- *
250
- * @example
251
- * ```typescript
252
- * take(
253
- * [{id: 1}, {id: 2}, {id: 3}],
254
- * item => item.id,
255
- * 2,
256
- * ); // => [{id: 1}]
257
- * ```
258
- */
259
- export function take<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(
260
- array: Item[],
261
- callback: Callback,
262
- value: ReturnType<Callback>,
263
- ): Item[];
264
-
265
- /**
266
- * Take items from the start of an array until they match a value
267
- *
268
- * @param array Original array
269
- * @param key Key to get an item's value for matching
270
- * @param value Value to match against
271
- * @returns New array with taken items
272
- *
273
- * @example
274
- * ```typescript
275
- * take(
276
- * [{id: 1}, {id: 2}, {id: 3}],
277
- * 'id',
278
- * 2,
279
- * ); // => [{id: 1}]
280
- * ```
281
- */
282
- export function take<Item extends PlainObject, ItemKey extends keyof Item>(
283
- array: Item[],
284
- key: ItemKey,
285
- value: Item[ItemKey],
286
- ): Item[];
287
-
288
- /**
289
- * Take items from the start of an array while they match a filter
290
- *
291
- * @param array Original array
292
- * @param callback Filter callback to match items
293
- * @returns New array with taken items
294
- *
295
- * @example
296
- * ```typescript
297
- * take(
298
- * [{id: 1}, {id: 2}, {id: 3}],
299
- * item => item.id === 2,
300
- * ); // => [{id: 1}]
301
- * ```
302
- */
303
- export function take<Item extends PlainObject>(
304
- array: Item[],
305
- callback: (item: Item, index: number, array: Item[]) => boolean,
306
- ): Item[];
307
-
308
- /**
309
- * Take a specified number of items, from the start if `>= 0`, or from the end if `< 0`
310
- *
311
- * @param array Original array
312
- * @param count Number of items to take
313
- * @returns New array with taken items
314
- *
315
- * @example
316
- * ```typescript
317
- * take([1, 2, 3, 4, 5], 2); // => [1, 2]
318
- * take([1, 2, 3, 4, 5], -2); // => [4, 5]
319
- * ```
320
- */
321
- export function take(array: unknown[], count: number): unknown[];
322
-
323
- export function take(array: unknown[], first?: unknown, second?: unknown): unknown[] {
324
- return extract(EXTRACT_TAKE, array, first, second);
325
- }
326
-
327
- // #endregion
328
-
329
- // #region Variables
330
-
331
- const EXTRACT_DROP: ExtractType = 'drop';
332
-
333
- const EXTRACT_TAKE: ExtractType = 'take';
334
-
335
- // #endregion