@oscarpalmer/atoms 0.185.0 → 0.186.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 (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 +27 -7
  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 +1898 -129
  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 +27 -8
  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
@@ -3,46 +3,100 @@ import { PlainObject } from "../models.mjs";
3
3
  //#region src/array/select.d.ts
4
4
  /**
5
5
  * Get a filtered and mapped array of items
6
+ *
6
7
  * @param array Array to search in
7
8
  * @param filterCallback Callback to get an item's value for matching
8
9
  * @param filterValue Value to match against
9
10
  * @param mapCallback Callback to map the matched items
10
11
  * @returns Filtered and mapped array of items
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * select(
16
+ * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
17
+ * item => item.id,
18
+ * 2,
19
+ * item => item.name,
20
+ * ); // => ['Bob']
21
+ * ```
11
22
  */
12
23
  declare function select<Item, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, mapCallback: MapCallback): Array<ReturnType<MapCallback>>;
13
24
  /**
14
25
  * Get a filtered and mapped array of items
26
+ *
15
27
  * @param array Array to search in
16
28
  * @param filterCallback Callback to get an item's value for matching
17
29
  * @param filterValue Value to match against
18
30
  * @param mapKey Key to get an item's value for mapping
19
31
  * @returns Filtered and mapped array of items
32
+ *
33
+ * @example
34
+ * ```typescript
35
+ * select(
36
+ * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
37
+ * item => item.id,
38
+ * 2,
39
+ * 'name',
40
+ * ); // => ['Bob']
41
+ * ```
20
42
  */
21
43
  declare function select<Item extends PlainObject, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, MapKey extends keyof Item>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, mapKey: MapKey): Array<Item[MapKey]>;
22
44
  /**
23
45
  * Get a filtered and mapped array of items
46
+ *
24
47
  * @param array Array to search in
25
48
  * @param filterKey Key to get an item's value for matching
26
49
  * @param filterValue Value to match against
27
50
  * @param mapCallback Callback to map the matched items
28
51
  * @returns Filtered and mapped array of items
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * select(
56
+ * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
57
+ * 'id',
58
+ * 2,
59
+ * item => item.name,
60
+ * ); // => ['Bob']
61
+ * ```
29
62
  */
30
63
  declare function select<Item extends PlainObject, ItemKey extends keyof Item, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filterKey: ItemKey, filterValue: Item[ItemKey], mapCallback: MapCallback): Array<ReturnType<MapCallback>>;
31
64
  /**
32
65
  * Get a filtered and mapped array of items
66
+ *
33
67
  * @param array Array to search in
34
68
  * @param filterKey Key to get an item's value for matching
35
69
  * @param filterValue Value to match against
36
70
  * @param mapKey Key to get an item's value for mapping
37
71
  * @returns Filtered and mapped array of items
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * select(
76
+ * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
77
+ * 'id',
78
+ * 2,
79
+ * 'name',
80
+ * ); // => ['Bob']
81
+ * ```
38
82
  */
39
83
  declare function select<Item extends PlainObject, ItemKey extends keyof Item, MapKey extends keyof Item>(array: Item[], filterKey: ItemKey, filterValue: Item[ItemKey], mapKey: MapKey): Array<Item[MapKey]>;
40
84
  /**
41
85
  * Get a filtered and mapped array of items
86
+ *
42
87
  * @param array Array to search in
43
88
  * @param filterCallback Filter callback to match items
44
89
  * @param mapCallback Callback to map the matched items
45
90
  * @returns Filtered and mapped array of items
91
+ *
92
+ * @example
93
+ * ```typescript
94
+ * select(
95
+ * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
96
+ * item => item.id === 2,
97
+ * item => item.name,
98
+ * ); // => ['Bob']
99
+ * ```
46
100
  */
47
101
  declare function select<Item, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, mapCallback: MapCallback): Array<ReturnType<MapCallback>>;
48
102
  /**
@@ -51,39 +105,71 @@ declare function select<Item, FilterCallback extends (item: Item, index: number,
51
105
  * @param filterCallback Filter callback to match items
52
106
  * @param mapKey Key to get an item's value for mapping
53
107
  * @returns Filtered and mapped array of items
108
+ *
109
+ * @example
110
+ * ```typescript
111
+ * select(
112
+ * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
113
+ * item => item.id,
114
+ * 2,
115
+ * 'name'
116
+ * ); // => ['Bob']
117
+ * ```
54
118
  */
55
119
  declare function select<Item extends PlainObject, FilterCallback extends (item: Item, index: number, array: Item[]) => unknown, MapKey extends keyof Item>(array: Item[], filterCallback: FilterCallback, filterValue: ReturnType<FilterCallback>, mapKey: MapKey): Array<Item[MapKey]>;
56
120
  /**
57
121
  * Get a filtered and mapped array of items
122
+ *
58
123
  * @param array Array to search in
59
124
  * @param filter Filter callback to match items
60
125
  * @param map Callback to map the matched items
61
126
  * @returns Filtered and mapped array of items
127
+ *
128
+ * @example
129
+ * ```typescript
130
+ * select(
131
+ * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
132
+ * item => item.id === 2,
133
+ * item => item.name,
134
+ * ); // => ['Bob']
135
+ * ```
62
136
  */
63
137
  declare function select<Item, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, map: MapCallback): Array<ReturnType<MapCallback>>;
64
138
  /**
65
139
  * Get a filtered and mapped array of items
140
+ *
66
141
  * @param array Array to search in
67
142
  * @param filter Filter callback to match items
68
143
  * @param map Key to get an item's value for mapping
69
144
  * @returns Filtered and mapped array of items
145
+ *
146
+ * @example
147
+ * ```typescript
148
+ * select(
149
+ * [{id: 1, name: 'Alice'}, {id: 2, name: 'Bob'}, {id: 3, name: 'Charlie'}],
150
+ * item => item.id === 2,
151
+ * 'name'
152
+ * ); // => ['Bob']
153
+ * ```
70
154
  */
71
155
  declare function select<Item extends PlainObject, MapKey extends keyof Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean, map: MapKey): Array<Item[MapKey]>;
72
156
  /**
73
157
  * Get a filtered and mapped array of items
158
+ *
74
159
  * @param array Array to search in
75
160
  * @param item Item to match against
76
161
  * @param map Callback to map the matched items
77
162
  * @returns Filtered and mapped array of items
163
+ *
164
+ * @example
165
+ * ```typescript
166
+ * select(
167
+ * [1, 2, 3, 2, 1],
168
+ * 3,
169
+ * value => value ** 2,
170
+ * ); // => [9]
171
+ * ```
78
172
  */
79
173
  declare function select<Item, MapCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], item: Item, map: MapCallback): Array<ReturnType<MapCallback>>;
80
- /**
81
- * Get a filtered and mapped array of items
82
- * @param array Array to search in
83
- * @param item Item to match against
84
- * @param map Key to get an item's value for mapping
85
- * @returns Filtered and mapped array of items
86
- */
87
- declare function select<Item extends PlainObject, MapKey extends keyof Item>(array: Item[], item: Item, map: MapKey): Array<Item[MapKey]>;
88
174
  //#endregion
89
175
  export { select };
@@ -5,29 +5,58 @@ import { PlainObject } from "../models.mjs";
5
5
  * Get the _only_ item matching the given value
6
6
  *
7
7
  * Throws an error if multiple items match the value
8
+ *
8
9
  * @param array Array to search in
9
10
  * @param callback Callback to get an item's value for matching
10
11
  * @param value Value to match against
11
12
  * @returns Only item that matches the value, or `undefined` if no match is found
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * single(
17
+ * [{id: 1}, {id: 2}, {id: 3}],
18
+ * item => item.id,
19
+ * 2
20
+ * ); // => {id: 2}
21
+ * ```
12
22
  */
13
23
  declare function single<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): Item | undefined;
14
24
  /**
15
25
  * Get the _only_ item matching the given value by key
16
26
  *
17
27
  * Throws an error if multiple items match the value
28
+ *
18
29
  * @param array Array to search in
19
30
  * @param key Key to get an item's value for matching
20
31
  * @param value Value to match against
21
32
  * @returns Only item that matches the value, or `undefined` if no match is found
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * single(
37
+ * [{id: 1}, {id: 2}, {id: 3}],
38
+ * 'id',
39
+ * 2
40
+ * ); // => {id: 2}
41
+ * ```
22
42
  */
23
43
  declare function single<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): Item | undefined;
24
44
  /**
25
45
  * Get the _only_ item matching the filter
26
46
  *
27
47
  * Throws an error if multiple items match the filter
48
+ *
28
49
  * @param array Array to search in
29
50
  * @param filter Filter callback to match items
30
51
  * @returns Only item that matches the filter, or `undefined` if no match is found
52
+ *
53
+ * @example
54
+ * ```typescript
55
+ * single(
56
+ * [{id: 1}, {id: 2}, {id: 3}],
57
+ * item => item.id === 2
58
+ * ); // => {id: 2}
59
+ * ```
31
60
  */
32
61
  declare function single<Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean): Item | undefined;
33
62
  //#endregion
@@ -3,83 +3,173 @@ import { PlainObject } from "../models.mjs";
3
3
  //#region src/array/slice.d.ts
4
4
  /**
5
5
  * Drop items from the start of an array until they match a value
6
+ *
6
7
  * @param array Original array
7
- * @param key Key to get an item's value for matching
8
+ * @param callback Callback to get an item's value for matching
8
9
  * @param value Value to match against
9
10
  * @returns New array with items dropped
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * drop(
15
+ * [{id: 1}, {id: 2}, {id: 3}],
16
+ * item => item.id,
17
+ * 2,
18
+ * ); // => [{id: 2}, {id: 3}]
19
+ * ```
10
20
  */
11
- declare function drop<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): Item[];
21
+ declare function drop<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): Item[];
12
22
  /**
13
23
  * Drop items from the start of an array until they match a value
24
+ *
14
25
  * @param array Original array
15
- * @param callback Callback to get an item's value for matching
26
+ * @param key Key to get an item's value for matching
16
27
  * @param value Value to match against
17
- * @return New array with items dropped
28
+ * @returns New array with items dropped
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * drop(
33
+ * [{id: 1}, {id: 2}, {id: 3}],
34
+ * 'id',
35
+ * 2,
36
+ * ); // => [{id: 2}, {id: 3}]
37
+ * ```
18
38
  */
19
- declare function drop<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): Item[];
39
+ declare function drop<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): Item[];
20
40
  /**
21
41
  * Drop items from the start of an array while they match a filter
42
+ *
22
43
  * @param array Original array
23
44
  * @param callback Filter callback to match items
24
- * @return New array with items dropped
45
+ * @returns New array with items dropped
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * drop(
50
+ * [{id: 1}, {id: 2}, {id: 3}],
51
+ * item => item.id === 2,
52
+ * ); // => [{id: 2}, {id: 3}]
53
+ * ```
25
54
  */
26
55
  declare function drop<Item extends PlainObject>(array: Item[], callback: (item: Item, index: number, array: Item[]) => boolean): Item[];
27
56
  /**
28
57
  * Drop a specified number of items, from the start if `>= 0`, or from the end if `< 0`
58
+ *
29
59
  * @param array Original array
30
60
  * @param count Number of items to drop
31
61
  * @returns New array with items dropped
62
+ *
63
+ * @example
64
+ * ```typescript
65
+ * drop([1, 2, 3, 4, 5], 2); // => [3, 4, 5]
66
+ * drop([1, 2, 3, 4, 5], -2); // => [1, 2, 3]
67
+ * ```
32
68
  */
33
69
  declare function drop(array: unknown[], count: number): unknown[];
34
70
  /**
35
71
  * Slice an array, returning a new array with a specified range of items
72
+ *
36
73
  * @param array Original array
37
74
  * @param start Start index _(inclusive)_
38
75
  * @param end End index _(exclusive)_
39
- * @return New array with sliced items
76
+ * @returns New array with sliced items
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * slice([1, 2, 3, 4, 5], 1, 4); // => [2, 3, 4]
81
+ * ```
40
82
  */
41
83
  declare function slice<Item>(array: Item[], start: number, end: number): Item[];
42
84
  /**
43
- * Slice an array, returning a new array with a specified number of items
85
+ * Slice an array, returning a new array with a specified number of items _(from the start)_
86
+ *
44
87
  * @param array Original array
45
- * @param count Maximum sixe of the new array
46
- * @return New array with sliced items
88
+ * @param count Maximum size of the new array
89
+ * @returns New array with sliced items
90
+ *
91
+ * @example
92
+ * ```typescript
93
+ * slice([1, 2, 3, 4, 5], 3); // => [1, 2, 3]
94
+ * ```
47
95
  */
48
96
  declare function slice<Item>(array: Item[], count: number): Item[];
49
97
  /**
50
98
  * Slice an array
99
+ *
51
100
  * @param array Array to slice
52
101
  * @returns Sliced array
102
+ *
103
+ * @example
104
+ * ```typescript
105
+ * slice([1, 2, 3]); // => [1, 2, 3]
106
+ * ```
53
107
  */
54
108
  declare function slice<Item>(array: Item[]): Item[];
55
109
  /**
56
110
  * Take items from the start of an array until they match a value
111
+ *
57
112
  * @param array Original array
58
- * @param key Key to get an item's value for matching
113
+ * @param callback Callback to get an item's value for matching
59
114
  * @param value Value to match against
60
115
  * @returns New array with taken items
116
+ *
117
+ * @example
118
+ * ```typescript
119
+ * take(
120
+ * [{id: 1}, {id: 2}, {id: 3}],
121
+ * item => item.id,
122
+ * 2,
123
+ * ); // => [{id: 1}]
124
+ * ```
61
125
  */
62
- declare function take<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): Item[];
126
+ declare function take<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): Item[];
63
127
  /**
64
128
  * Take items from the start of an array until they match a value
129
+ *
65
130
  * @param array Original array
66
- * @param callback Callback to get an item's value for matching
131
+ * @param key Key to get an item's value for matching
67
132
  * @param value Value to match against
68
- * @return New array with taken items
133
+ * @returns New array with taken items
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * take(
138
+ * [{id: 1}, {id: 2}, {id: 3}],
139
+ * 'id',
140
+ * 2,
141
+ * ); // => [{id: 1}]
142
+ * ```
69
143
  */
70
- declare function take<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): Item[];
144
+ declare function take<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): Item[];
71
145
  /**
72
146
  * Take items from the start of an array while they match a filter
147
+ *
73
148
  * @param array Original array
74
149
  * @param callback Filter callback to match items
75
- * @return New array with taken items
150
+ * @returns New array with taken items
151
+ *
152
+ * @example
153
+ * ```typescript
154
+ * take(
155
+ * [{id: 1}, {id: 2}, {id: 3}],
156
+ * item => item.id === 2,
157
+ * ); // => [{id: 1}]
158
+ * ```
76
159
  */
77
160
  declare function take<Item extends PlainObject>(array: Item[], callback: (item: Item, index: number, array: Item[]) => boolean): Item[];
78
161
  /**
79
162
  * Take a specified number of items, from the start if `>= 0`, or from the end if `< 0`
163
+ *
80
164
  * @param array Original array
81
165
  * @param count Number of items to take
82
166
  * @returns New array with taken items
167
+ *
168
+ * @example
169
+ * ```typescript
170
+ * take([1, 2, 3, 4, 5], 2); // => [1, 2]
171
+ * take([1, 2, 3, 4, 5], -2); // => [4, 5]
172
+ * ```
83
173
  */
84
174
  declare function take(array: unknown[], count: number): unknown[];
85
175
  //#endregion
@@ -73,6 +73,7 @@ type SortDirection = 'ascending' | 'descending';
73
73
  type Sorter<Item> = {
74
74
  /**
75
75
  * Sort an array of items
76
+ *
76
77
  * @param array Array to sort
77
78
  * @returns Sorted array
78
79
  */
@@ -81,6 +82,7 @@ type Sorter<Item> = {
81
82
  * Get the index for an item _(to be inserted into an array of items)_
82
83
  *
83
84
  * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
85
+ *
84
86
  * @param array Array to get the index from
85
87
  * @param item Item to get the index for
86
88
  * @returns Index for item
@@ -88,6 +90,7 @@ type Sorter<Item> = {
88
90
  index(array: Item[], item: Item): number;
89
91
  /**
90
92
  * Is the array sorted?
93
+ *
91
94
  * @param array Array to check
92
95
  * @returns `true` if sorted, otherwise `false`
93
96
  */
@@ -99,6 +102,7 @@ type Sorter<Item> = {
99
102
  * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
100
103
  *
101
104
  * Available as `getSortedIndex` and `sort.getIndex`
105
+ *
102
106
  * @param array Array to get the index from
103
107
  * @param item Item to get the index for
104
108
  * @param sorters Sorters to use to determine sorting
@@ -112,6 +116,7 @@ declare function getSortedIndex<Item>(array: Item[], item: Item, sorters: Array<
112
116
  * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
113
117
  *
114
118
  * Available as `getSortedIndex` and `sort.getIndex`
119
+ *
115
120
  * @param array Array to get the index from
116
121
  * @param item Item to get the index for
117
122
  * @param sorter Sorter to use to determine sorting
@@ -125,6 +130,7 @@ declare function getSortedIndex<Item>(array: Item[], item: Item, sorter: ArraySo
125
130
  * _(If the array is not sorted, it will be treated as sorted, and the result may be inaccurate)_
126
131
  *
127
132
  * Available as `getSortedIndex` and `sort.getIndex`
133
+ *
128
134
  * @param array Array to get the index from
129
135
  * @param item Item to get the index for
130
136
  * @param descending Sorted in descending order? _(defaults to `false`)_
@@ -135,6 +141,7 @@ declare function getSortedIndex<Item>(array: Item[], item: Item, descending?: bo
135
141
  * Initialize a sort handler with sorters _(and an optional default direction)_
136
142
  *
137
143
  * Available as `initializeSorter` and `sort.initialize`
144
+ *
138
145
  * @param sorters Sorters to use for sorting
139
146
  * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
140
147
  * @returns Sort handler
@@ -144,6 +151,7 @@ declare function initializeSorter<Item>(sorters: Array<ArraySorter<Item>>, desce
144
151
  * Initialize a sort handler with a sorter _(and an optional default direction)_
145
152
  *
146
153
  * Available as `initializeSorter` and `sort.initialize`
154
+ *
147
155
  * @param sorter Sorter to use for sorting
148
156
  * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
149
157
  * @returns Sort handler
@@ -153,12 +161,14 @@ declare function initializeSorter<Item>(sorter: ArraySorter<Item>, descending?:
153
161
  * Initialize a sort handler _(with an optional default direction)_
154
162
  *
155
163
  * Available as `initializeSorter` and `sort.initialize`
164
+ *
156
165
  * @param descending Sort in descending order? _(defaults to `false`)_
157
166
  * @returns Sort handler
158
167
  */
159
168
  declare function initializeSorter<Item>(descending?: boolean): Sorter<Item>;
160
169
  /**
161
170
  * Is the array sorted according to the sorters _(and the optional default direction)_?
171
+ *
162
172
  * @param array Array to check
163
173
  * @param sorters Sorters to determine sorting
164
174
  * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
@@ -167,6 +177,7 @@ declare function initializeSorter<Item>(descending?: boolean): Sorter<Item>;
167
177
  declare function isSorted<Item>(array: Item[], sorters: Array<ArraySorter<Item>>, descending?: boolean): boolean;
168
178
  /**
169
179
  * Is the array sorted according to the sorter _(and the optional default direction)_?
180
+ *
170
181
  * @param array Array to check
171
182
  * @param sorter Sorter to determine sorting
172
183
  * @param descending Sorted in descending order? _(defaults to `false`; overridden by individual sorters)_
@@ -177,6 +188,7 @@ declare function isSorted<Item>(array: Item[], sorter: ArraySorter<Item>, descen
177
188
  * Is the array sorted?
178
189
  *
179
190
  * Available as `isSorted` and `sort.is`
191
+ *
180
192
  * @param array Array to check
181
193
  * @param descending Sorted in descending order? _(defaults to `false`)_
182
194
  * @returns `true` if sorted, otherwise `false`
@@ -184,10 +196,19 @@ declare function isSorted<Item>(array: Item[], sorter: ArraySorter<Item>, descen
184
196
  declare function isSorted<Item>(array: Item[], descending?: boolean): boolean;
185
197
  /**
186
198
  * Sort an array of items using a comparison callback
199
+ *
187
200
  * @param array Array to sort
188
201
  * @param comparator Comparator to use for sorting
189
202
  * @param descending Sort in descending order? _(defaults to `false`; overridden by individual sorters)_
190
203
  * @returns Sorted array
204
+ *
205
+ * @example
206
+ * ```typescript
207
+ * sort(
208
+ * [{id: 3}, {id: 1}, {id: 2}],
209
+ * (first, second) => first.id - second.id,
210
+ * ); // => [{id: 1}, {id: 2}, {id: 3}]
211
+ * ```
191
212
  */
192
213
  declare function sort<Item>(array: Item[], comparator: (first: Item, second: Item) => number, descending?: boolean): Item[];
193
214
  /**
@@ -1,34 +1,82 @@
1
1
  //#region src/array/splice.d.ts
2
2
  /**
3
3
  * Adds items into an array at a specific index and removes a specific amount of items
4
+ *
5
+ * _(Uses chunking to avoid call stack size being exceeded)_
6
+ *
4
7
  * @param array Original array
5
8
  * @param start Index to start splicing from
6
9
  * @param amount Number of items to remove
7
10
  * @param added Added items
8
11
  * @returns Removed items
12
+ *
13
+ * @example
14
+ * ```typescript
15
+ * splice(
16
+ * [1, 2, 3, 4, 5],
17
+ * 2,
18
+ * 2,
19
+ * [6, 7]
20
+ * ); // => [3, 4], array becomes [1, 2, 6, 7, 5]
21
+ * ```
9
22
  */
10
23
  declare function splice<Item>(array: Item[], start: number, amount: number, added: Item[]): Item[];
11
24
  /**
12
25
  * Adds items into an array at a specific index
26
+ *
27
+ * _(Uses chunking to avoid call stack size being exceeded)_
28
+ *
13
29
  * @param array Original array
14
30
  * @param start Index to start splicing from
15
31
  * @param added Added items
16
32
  * @returns Removed items
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * splice(
37
+ * [1, 2, 3, 4, 5],
38
+ * 2,
39
+ * [6, 7]
40
+ * ); // => [], array becomes [1, 2, 6, 7, 3, 4, 5]
41
+ * ```
17
42
  */
18
43
  declare function splice<Item>(array: Item[], start: number, added: Item[]): Item[];
19
44
  /**
20
45
  * Removes and returns _(up to)_ a specific amount of items from an array, starting from a specific index
46
+ *
47
+ * _(Uses chunking to avoid call stack size being exceeded)_
48
+ *
21
49
  * @param array Original array
22
50
  * @param start Index to start splicing from
23
51
  * @param amount Number of items to remove
24
52
  * @returns Removed items
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * splice(
57
+ * [1, 2, 3, 4, 5],
58
+ * 2,
59
+ * 2,
60
+ * ); // => [3, 4], array becomes [1, 2, 5]
61
+ * ```
25
62
  */
26
63
  declare function splice<Item>(array: Item[], start: number, amount: number): Item[];
27
64
  /**
28
65
  * Removes and returns all items from an array starting from a specific index
66
+ *
67
+ * _(Uses chunking to avoid call stack size being exceeded)_
68
+ *
29
69
  * @param array Original array
30
70
  * @param start Index to start splicing from
31
71
  * @returns Removed items
72
+ *
73
+ * @example
74
+ * ```typescript
75
+ * splice(
76
+ * [1, 2, 3, 4, 5],
77
+ * 2,
78
+ * ); // => [3, 4, 5], array becomes [1, 2]
79
+ * ```
32
80
  */
33
81
  declare function splice<Item>(array: Item[], start: number): Item[];
34
82
  //#endregion
@@ -1,7 +1,8 @@
1
1
  import { INSERT_TYPE_SPLICE, insertValues } from "../internal/array/insert.mjs";
2
2
  //#region src/array/splice.ts
3
3
  function splice(array, start, deleteCountOrItems, items) {
4
- return insertValues(INSERT_TYPE_SPLICE, array, typeof deleteCountOrItems === "number" ? items : deleteCountOrItems, start, typeof deleteCountOrItems === "number" ? deleteCountOrItems : 0);
4
+ const deleteCountIsNumber = typeof deleteCountOrItems === "number";
5
+ return insertValues(INSERT_TYPE_SPLICE, array, deleteCountIsNumber ? items : deleteCountOrItems, start, deleteCountIsNumber ? deleteCountOrItems : 0);
5
6
  }
6
7
  //#endregion
7
8
  export { splice };