@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,32 +3,52 @@ import { NumericalKeys, PlainObject } from "../models.mjs";
3
3
  //#region src/array/get.d.ts
4
4
  /**
5
5
  * Get an array from an object, where only values with numerical keys will be included
6
+ *
6
7
  * @param value Object to convert to an array
7
8
  * @returns Array holding the values of the object's numerical keys
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * getArray({0: 'a', 1: 'b', 2: 'c', d: 'd'}, true); // => ['a', 'b', 'c']
13
+ * getArray({a: 'a', b: 'b', c: 'c', d: 'd'}, true); // => []
14
+ * ```
8
15
  */
9
16
  declare function getArray<Value extends PlainObject>(value: Value, indiced: true): Value[NumericalKeys<Value>][];
10
17
  /**
11
18
  * Get an array from an object
19
+ *
12
20
  * @param value Object to convert to an array
13
21
  * @returns Array holding the values of the object
22
+ *
23
+ * @example
24
+ * ```typescript
25
+ * getArray({0: 'a', 1: 'b', 2: 'c', d: 'd'}); // => ['a', 'b', 'c', 'd']
26
+ * getArray({a: 'a', b: 'b', c: 'c', d: 'd'}); // => ['a', 'b', 'c', 'd']
27
+ * ```
14
28
  */
15
29
  declare function getArray<Value extends PlainObject>(value: Value): Value[keyof Value][];
16
30
  /**
17
- * Get an array
31
+ * Get an array from a value
32
+ *
18
33
  * @param value Original array
19
34
  * @returns Original array
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * getArray(123); // => [123]
39
+ * ```
20
40
  */
21
41
  declare function getArray<Item>(value: Item[]): Item[];
22
- /**
23
- * Get an array from a value
24
- * @param value Value to convert to an array
25
- * @returns Array holding the value
26
- */
27
- declare function getArray<Item>(value: Item): Item[];
28
42
  /**
29
43
  * Get an array from an unknown value
44
+ *
30
45
  * @param value Value to convert to an array
31
46
  * @returns Array of value
47
+ *
48
+ * @example
49
+ * ```typescript
50
+ * getArray(123); // => [123]
51
+ * ```
32
52
  */
33
53
  declare function getArray(value: unknown): unknown[];
34
54
  //#endregion
@@ -5,64 +5,130 @@ import { Key, KeyedValue, PlainObject, Simplify } from "../models.mjs";
5
5
  * Create a record from an array of items using a specific key and value
6
6
  *
7
7
  * If multiple items have the same key, the latest item's value will be used
8
+ *
8
9
  * @param array Array to group
9
10
  * @param key Callback to get an item's grouping key
10
11
  * @param value Callback to get an item's value
11
12
  * @returns Record of keyed values
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * groupBy(
17
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
18
+ * item => item.value,
19
+ * item => item,
20
+ * ); // => {10: {id: 3, value: 10}, 20: {id: 2, value: 20}}
21
+ * ```
12
22
  */
13
23
  declare function groupBy<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): Simplify<Record<ReturnType<KeyCallback>, ReturnType<ValueCallback>>>;
14
24
  /**
15
25
  * Create a record from an array of items using a specific key and value
16
26
  *
17
27
  * If multiple items have the same key, the latest item's value will be used
28
+ *
18
29
  * @param array Array to group
19
30
  * @param key Callback to get an item's grouping key
20
31
  * @param value Key to use for value
21
32
  * @returns Record of keyed values
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * groupBy(
37
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
38
+ * item => item.value,
39
+ * 'id'
40
+ * ); // => {10: 3, 20: 2}
41
+ * ```
22
42
  */
23
43
  declare function groupBy<Item extends PlainObject, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ItemValue extends keyof Item>(array: Item[], key: KeyCallback, value: ItemValue): Record<ReturnType<KeyCallback>, Item[ItemValue]>;
24
44
  /**
25
45
  * Create a record from an array of items using a specific key and value
26
46
  *
27
47
  * If multiple items have the same key, the latest item's value will be used
48
+ *
28
49
  * @param array Array to group
29
50
  * @param key Key to use for grouping
30
51
  * @param value Callback to get an item's value
31
52
  * @returns Record of keyed values
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * groupBy(
57
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
58
+ * 'value',
59
+ * item => item,
60
+ * ); // => {10: {id: 3, value: 10}, 20: {id: 2, value: 20}}
61
+ * ```
32
62
  */
33
63
  declare function groupBy<Item extends PlainObject, ItemKey extends keyof Item, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: ItemKey, value: ValueCallback): Simplify<Record<KeyedValue<Item, ItemKey>, ReturnType<ValueCallback>>>;
34
64
  /**
35
65
  * Create a record from an array of items using a specific key and value
36
66
  *
37
67
  * If multiple items have the same key, the latest item's value will be used
68
+ *
38
69
  * @param array Array to group
39
70
  * @param key Key to use for grouping
40
71
  * @param value Key to use for value
41
72
  * @returns Record of keyed values
73
+ *
74
+ * @example
75
+ * ```typescript
76
+ * groupBy(
77
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
78
+ * 'value',
79
+ * 'id'
80
+ * ); // => {10: 3, 20: 2}
81
+ * ```
42
82
  */
43
83
  declare function groupBy<Item extends PlainObject, ItemKey extends keyof Item, ItemValue extends keyof Item>(array: Item[], key: ItemKey, value: ItemValue): Simplify<Record<KeyedValue<Item, ItemKey>, Item[ItemValue]>>;
44
84
  /**
45
85
  * Create a record from an array of items using a specific key
46
86
  *
47
87
  * If multiple items have the same key, the latest item will be used
88
+ *
48
89
  * @param array Array to group
49
90
  * @param callback Callback to get an item's grouping key
50
91
  * @returns Record of keyed items
92
+ *
93
+ * @example
94
+ * ```typescript
95
+ * groupBy(
96
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
97
+ * item => item.value,
98
+ * ); // => {10: {id: 3, value: 10}, 20: {id: 2, value: 20}}
99
+ * ```
51
100
  */
52
101
  declare function groupBy<Item, Callback extends (item: Item, index: number, array: Item[]) => Key>(array: Item[], callback: Callback): Record<ReturnType<Callback>, Item>;
53
102
  /**
54
103
  * Create a record from an array of items using a specific key
55
104
  *
56
105
  * If multiple items have the same key, the latest item will be used
106
+ *
57
107
  * @param array Array to group
58
108
  * @param key Key to use for grouping
59
109
  * @returns Record of keyed items
110
+ *
111
+ * @example
112
+ * ```typescript
113
+ * groupBy(
114
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
115
+ * 'value',
116
+ * ); // => {10: {id: 3, value: 10}, 20: {id: 2, value: 20}}
117
+ * ```
60
118
  */
61
119
  declare function groupBy<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey): Simplify<Record<KeyedValue<Item, ItemKey>, Item>>;
62
120
  /**
63
121
  * Create a record from an array of items _(using indices as keys)_
122
+ *
64
123
  * @param array Array to group
65
124
  * @returns Record of indiced items
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * groupBy(
129
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
130
+ * ); // => {0: {id: 1, value: 10}, 1: {id: 2, value: 20}, 2: {id: 3, value: 10}}
131
+ * ```
66
132
  */
67
133
  declare function groupBy<Item>(array: Item[]): Record<number, Item>;
68
134
  declare namespace groupBy {
@@ -72,58 +138,134 @@ declare namespace groupBy {
72
138
  * Create a record from an array of items using a specific key and value, grouping values into arrays
73
139
  *
74
140
  * Available as `groupArraysBy` and `groupBy.arrays`
141
+ *
75
142
  * @param array Array to group
76
143
  * @param key Callback to get an item's grouping key
77
144
  * @param value Callback to get an item's value
78
145
  * @returns Record of keyed values
146
+ *
147
+ * @example
148
+ * ```typescript
149
+ * groupArraysBy(
150
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
151
+ * item => item.value,
152
+ * item => item,
153
+ * ); // => {
154
+ * // 10: [{id: 1, value: 10}, {id: 3, value: 10}],
155
+ * // 20: [{id: 2, value: 20}],
156
+ * // }
157
+ * ```
79
158
  */
80
159
  declare function groupArraysBy<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): Record<ReturnType<KeyCallback>, ReturnType<ValueCallback>[]>;
81
160
  /**
82
161
  * Create a record from an array of items using a specific key and value, grouping values into arrays
83
162
  *
84
163
  * Available as `groupArraysBy` and `groupBy.arrays`
164
+ *
85
165
  * @param array Array to group
86
166
  * @param key Callback to get an item's grouping key
87
167
  * @param value Key to use for value
88
168
  * @returns Record of keyed values
169
+ *
170
+ * @example
171
+ * ```typescript
172
+ * groupArraysBy(
173
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
174
+ * item => item.value,
175
+ * 'id',
176
+ * ); // => {
177
+ * // 10: [1, 3],
178
+ * // 20: [2],
179
+ * // }
180
+ * ```
89
181
  */
90
182
  declare function groupArraysBy<Item extends PlainObject, KeyCallback extends (item: Item, index: number, array: Item[]) => Key, ItemValue extends keyof Item>(array: Item[], key: KeyCallback, value: ItemValue): Record<ReturnType<KeyCallback>, Item[ItemValue][]>;
91
183
  /**
92
184
  * Create a record from an array of items using a specific key and value, grouping values into arrays
93
185
  *
94
186
  * Available as `groupArraysBy` and `groupBy.arrays`
187
+ *
95
188
  * @param array Array to group
96
189
  * @param key Key to use for grouping
97
190
  * @param value Callback to get an item's value
98
191
  * @returns Record of keyed values
192
+ *
193
+ * @example
194
+ * ```typescript
195
+ * groupArraysBy(
196
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
197
+ * 'value',
198
+ * item => item,
199
+ * ); // => {
200
+ * // 10: [{id: 1, value: 10}, {id: 3, value: 10}],
201
+ * // 20: [{id: 2, value: 20}],
202
+ * // }
203
+ * ```
99
204
  */
100
205
  declare function groupArraysBy<Item extends PlainObject, ItemKey extends keyof Item, ValueCallback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], key: ItemKey, value: ValueCallback): Simplify<Record<KeyedValue<Item, ItemKey>, ReturnType<ValueCallback>[]>>;
101
206
  /**
102
207
  * Create a record from an array of items using a specific key and value, grouping values into arrays
103
208
  *
104
209
  * Available as `groupArraysBy` and `groupBy.arrays`
210
+ *
105
211
  * @param array Array to group
106
212
  * @param key Key to use for grouping
107
213
  * @param value Key to use for value
108
214
  * @returns Record of keyed values
215
+ *
216
+ * @example
217
+ * ```typescript
218
+ * groupArraysBy(
219
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
220
+ * 'value',
221
+ * 'id',
222
+ * ); // => {
223
+ * // 10: [1, 3],
224
+ * // 20: [2],
225
+ * // }
226
+ * ```
109
227
  */
110
228
  declare function groupArraysBy<Item extends PlainObject, ItemKey extends keyof Item, ItemValue extends keyof Item>(array: Item[], key: ItemKey, value: ItemValue): Simplify<Record<KeyedValue<Item, ItemKey>, Item[ItemValue][]>>;
111
229
  /**
112
230
  * Create a record from an array of items using a specific key, grouping items into arrays
113
231
  *
114
232
  * Available as `groupArraysBy` and `groupBy.arrays`
233
+ *
115
234
  * @param array Array to group
116
235
  * @param callback Callback to get an item's grouping key
117
236
  * @returns Record of keyed items
237
+ *
238
+ * @example
239
+ * ```typescript
240
+ * groupArraysBy(
241
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
242
+ * item => item.value,
243
+ * ); // => {
244
+ * // 10: [{id: 1, value: 10}, {id: 3, value: 10}],
245
+ * // 20: [{id: 2, value: 20}],
246
+ * // }
247
+ * ```
118
248
  */
119
249
  declare function groupArraysBy<Item, Callback extends (item: Item, index: number, array: Item[]) => Key>(array: Item[], callback: Callback): Record<ReturnType<Callback>, Item[]>;
120
250
  /**
121
251
  * Create a record from an array of items using a specific key, grouping items into arrays
122
252
  *
123
253
  * Available as `groupArraysBy` and `groupBy.arrays`
254
+ *
124
255
  * @param array Array to group
125
256
  * @param key Key to use for grouping
126
257
  * @returns Record of keyed items
258
+ *
259
+ * @example
260
+ * ```typescript
261
+ * groupArraysBy(
262
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
263
+ * 'value',
264
+ * ); // => {
265
+ * // 10: [{id: 1, value: 10}, {id: 3, value: 10}],
266
+ * // 20: [{id: 2, value: 20}],
267
+ * // }
268
+ * ```
127
269
  */
128
270
  declare function groupArraysBy<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey): Simplify<Record<KeyedValue<Item, ItemKey>, Item[]>>;
129
271
  //#endregion
@@ -1,17 +1,33 @@
1
1
  //#region src/array/insert.d.ts
2
2
  /**
3
3
  * Insert items into an array at a specified index
4
+ *
5
+ * _(Uses chunking to avoid call stack size being exceeded)_
6
+ *
4
7
  * @param array Original array
5
8
  * @param index Index to insert at
6
9
  * @param items Inserted items
7
10
  * @returns Updated array
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * insert([1, 2, 3], 1, [4, 5]); // => [1, 4, 5, 2, 3]
15
+ * ```
8
16
  */
9
17
  declare function insert<Item>(array: Item[], index: number, items: Item[]): Item[];
10
18
  /**
11
19
  * Insert items into an array _(at the end)_
20
+ *
21
+ * _(Uses chunking to avoid call stack size being exceeded)_
22
+ *
12
23
  * @param array Original array
13
24
  * @param items Inserted items
14
25
  * @returns Updated array
26
+ *
27
+ * @example
28
+ * ```typescript
29
+ * insert([1, 2, 3], [4, 5]); // => [1, 2, 3, 4, 5]
30
+ * ```
15
31
  */
16
32
  declare function insert<Item>(array: Item[], items: Item[]): Item[];
17
33
  //#endregion
@@ -1,25 +1,54 @@
1
1
  //#region src/array/intersection.d.ts
2
2
  /**
3
3
  * Get the common values between two arrays
4
+ *
4
5
  * @param first First array
5
6
  * @param second Second array
6
7
  * @param callback Callback to get an item's value for comparison
7
8
  * @returns Common values from both arrays
9
+ *
10
+ * @example
11
+ * ```typescript
12
+ * intersection(
13
+ * [{id: 1}, {id: 2}, {id: 3}],
14
+ * [{id: 2}, {id: 3}, {id: 4}],
15
+ * item => item.id,
16
+ * ); // => [{id: 2}, {id: 3}]
17
+ * ```
8
18
  */
9
19
  declare function intersection<First, Second>(first: First[], second: Second[], callback: (item: First | Second) => unknown): First[];
10
20
  /**
11
21
  * Get the common values between two arrays
22
+ *
12
23
  * @param first First array
13
24
  * @param second Second array
14
25
  * @param key Key to get an item's value for comparison
15
26
  * @returns Common values from both arrays
27
+ *
28
+ * @example
29
+ * ```typescript
30
+ * intersection(
31
+ * [{id: 1}, {id: 2}, {id: 3}],
32
+ * [{id: 2}, {id: 3}, {id: 4}],
33
+ * 'id',
34
+ * ); // => [{id: 2}, {id: 3}]
35
+ * ```
16
36
  */
17
37
  declare function intersection<First extends Record<string, unknown>, Second extends Record<string, unknown>, SharedKey extends keyof First & keyof Second>(first: First[], second: Second[], key: SharedKey): First[];
18
38
  /**
19
39
  * Get the common values between two arrays
40
+ *
20
41
  * @param first First array
21
42
  * @param second Second array
22
43
  * @returns Common values from both arrays
44
+ *
45
+ * @example
46
+ * ```typescript
47
+ * intersection(
48
+ * [1, 2, 3],
49
+ * [2, 3, 4],
50
+ * ); // => [2, 3]
51
+ * ```
23
52
  */
24
53
  declare function intersection<First, Second>(first: First[], second: Second[]): First[];
25
54
  //#endregion
@@ -3,31 +3,66 @@ import { PlainObject } from "../models.mjs";
3
3
  //#region src/array/last.d.ts
4
4
  /**
5
5
  * Get the last items matching the given value
6
+ *
6
7
  * @param array Array to search in
7
8
  * @param callback Callback to get an item's value for matching
8
9
  * @param value Value to match against
9
10
  * @returns Last item that matches the value, or `undefined` if no match is found
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * last(
15
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
16
+ * item => item.value,
17
+ * 10,
18
+ * ); // => {id: 3, value: 10}
19
+ * ```
10
20
  */
11
21
  declare function last<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], callback: Callback, value: ReturnType<Callback>): Item | undefined;
12
22
  /**
13
23
  * Get the first item matching the given value by key
24
+ *
14
25
  * @param array Array to search in
15
26
  * @param key Key to get an item's value for matching
16
27
  * @param value Value to match against
17
28
  * @returns Last item that matches the value, or `undefined` if no match is found
29
+ *
30
+ * @example
31
+ * ```typescript
32
+ * last(
33
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
34
+ * 'value',
35
+ * 10,
36
+ * ); // => {id: 3, value: 10}
37
+ * ```
18
38
  */
19
39
  declare function last<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], key: ItemKey, value: Item[ItemKey]): Item | undefined;
20
40
  /**
21
41
  * Get the last item matching the filter
42
+ *
22
43
  * @param array Array to search in
23
44
  * @param filter Filter callback to match items
24
45
  * @returns Last item that matches the filter, or `undefined` if no match is found
46
+ *
47
+ * @example
48
+ * ```typescript
49
+ * last(
50
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
51
+ * item => item.value === 10,
52
+ * ); // => {id: 3, value: 10}
53
+ * ```
25
54
  */
26
55
  declare function last<Item>(array: Item[], filter: (item: Item, index: number, array: Item[]) => boolean): Item | undefined;
27
56
  /**
28
57
  * Get the last item from an array
58
+ *
29
59
  * @param array Array to get from
30
- * @return Last item from the array, or `undefined` if the array is empty
60
+ * @returns Last item from the array, or `undefined` if the array is empty
61
+ *
62
+ * @example
63
+ * ```typescript
64
+ * last([1, 2, 3]); // => 3
65
+ * ```
31
66
  */
32
67
  declare function last<Item>(array: Item[]): Item | undefined;
33
68
  declare namespace last {
@@ -38,41 +73,79 @@ declare namespace last {
38
73
  * Get the last item matching the given value
39
74
  *
40
75
  * Available as `lastOrDefault` and `last.default`
76
+ *
41
77
  * @param array Array to search in
42
78
  * @param defaultValue Default value to return if no match is found
43
79
  * @param callback Callback to get an item's value for matching
44
80
  * @param value Value to match against
45
81
  * @returns Last item that matches the value, or the default value if no match is found
82
+ *
83
+ * @example
84
+ * ```typescript
85
+ * lastOrDefault(
86
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
87
+ * {id: -1, value: 30},
88
+ * item => item.value,
89
+ * 30,
90
+ * ); // => {id: -1, value: 30}
91
+ * ```
46
92
  */
47
93
  declare function lastOrDefault<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(array: Item[], defaultValue: Item, callback: Callback, value: ReturnType<Callback>): Item;
48
94
  /**
49
95
  * Get the last item matching the given value by key
50
96
  *
51
97
  * Available as `lastOrDefault` and `last.default`
98
+ *
52
99
  * @param array Array to search in
53
100
  * @param defaultValue Default value to return if no match is found
54
101
  * @param key Key to get an item's value for matching
55
102
  * @param value Value to match against
56
103
  * @returns Last item that matches the value, or the default value if no match is found
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * lastOrDefault(
108
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
109
+ * {id: -1, value: 30},
110
+ * 'value',
111
+ * 30,
112
+ * ); // => {id: -1, value: 30}
113
+ * ```
57
114
  */
58
115
  declare function lastOrDefault<Item extends PlainObject, ItemKey extends keyof Item>(array: Item[], defaultValue: Item, key: ItemKey, value: Item[ItemKey]): Item;
59
116
  /**
60
117
  * Get the last item matching the filter
61
118
  *
62
119
  * Available as `lastOrDefault` and `last.default`
120
+ *
63
121
  * @param array Array to search in
64
122
  * @param defaultValue Default value to return if no match is found
65
123
  * @param filter Filter callback to match items
66
124
  * @returns Last item that matches the filter, or the default value if no match is found
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * lastOrDefault(
129
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
130
+ * {id: -1, value: 30},
131
+ * item => item.value === 30,
132
+ * ); // => {id: -1, value: 30}
133
+ * ```
67
134
  */
68
135
  declare function lastOrDefault<Item>(array: Item[], defaultValue: Item, filter: (item: Item, index: number, array: Item[]) => boolean): Item;
69
136
  /**
70
137
  * Get the last item from an array
71
138
  *
72
139
  * Available as `lastOrDefault` and `last.default`
140
+ *
73
141
  * @param array Array to get from
74
142
  * @param defaultValue Default value to return if the array is empty
75
- * @return Last item from the array, or the default value if the array is empty
143
+ * @returns Last item from the array, or the default value if the array is empty
144
+ *
145
+ * @example
146
+ * ```typescript
147
+ * lastOrDefault([], {id: -1, value: 30}); // => {id: -1, value: 30}
148
+ * ```
76
149
  */
77
150
  declare function lastOrDefault<Item>(array: Item[], defaultValue: Item): Item;
78
151
  //#endregion