@oscarpalmer/atoms 0.185.0 → 0.186.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 (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 +21 -13
  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 +1892 -135
  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 +21 -15
  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
@@ -1,8 +1,8 @@
1
1
  import { isArrayOrPlainObject } from "../internal/is.mjs";
2
- import { join } from "../internal/string.mjs";
3
2
  //#region src/value/merge.ts
4
3
  /**
5
- * Assign values from multiple arrays or objects to the first one
4
+ * Assign values from one or more objects to the first one
5
+ *
6
6
  * @param to Value to assign to
7
7
  * @param from Values to assign
8
8
  * @param options Assigning options
@@ -11,7 +11,7 @@ import { join } from "../internal/string.mjs";
11
11
  function assign(to, from, options) {
12
12
  const actual = getMergeOptions(options);
13
13
  actual.assignValues = true;
14
- return mergeValues([to, ...from], actual, true);
14
+ return mergeValues([to, ...from], actual);
15
15
  }
16
16
  assign.initialize = initializeAssigner;
17
17
  function getMergeOptions(options) {
@@ -32,59 +32,70 @@ function getReplaceableObjects(value) {
32
32
  const items = (Array.isArray(value) ? value : [value]).filter((item) => typeof item === "string" || item instanceof RegExp);
33
33
  if (items.length > 0) return (name) => items.some((item) => typeof item === "string" ? item === name : item.test(name));
34
34
  }
35
- function handleMerge(values, options) {
36
- return !Array.isArray(values) || values.length === 0 ? {} : mergeValues(values, options, true);
37
- }
38
35
  /**
39
36
  * Create an assigner with predefined options
40
37
  *
41
38
  * Available as `initializeAssigner` and `assign.initialize`
39
+ *
42
40
  * @param options Assigning options
43
41
  * @returns Assigner function
44
42
  */
45
43
  function initializeAssigner(options) {
46
44
  const actual = getMergeOptions(options);
47
45
  actual.assignValues = true;
48
- return ((to, from) => mergeValues([to, ...from], actual, true));
46
+ return ((to, from) => mergeValues([to, ...from], actual));
49
47
  }
50
48
  /**
51
49
  * Create a merger with predefined options
52
50
  *
53
51
  * Available as `initializeMerger` and `merge.initialize`
52
+ *
54
53
  * @param options Merging options
55
54
  * @returns Merger function
56
55
  */
57
56
  function initializeMerger(options) {
58
57
  const actual = getMergeOptions(options);
59
- return (values) => handleMerge(values, actual);
58
+ return ((values) => mergeValues(values, actual));
60
59
  }
60
+ /**
61
+ * Merge multiple arrays or objects into a single one
62
+ *
63
+ * @param values Values to merge
64
+ * @param options Merging options
65
+ * @returns Merged value
66
+ */
61
67
  function merge(values, options) {
62
- return handleMerge(values, getMergeOptions(options));
68
+ return mergeValues(values, getMergeOptions(options));
63
69
  }
64
70
  merge.initialize = initializeMerger;
65
- function mergeObjects(values, options, prefix) {
71
+ function mergeObjects(values, options, destination, prefix) {
66
72
  const { length } = values;
67
- const isArray = values.every(Array.isArray);
68
- const merged = options.assignValues ? values[0] : isArray ? [] : {};
69
- for (let outerIndex = 0; outerIndex < length; outerIndex += 1) {
73
+ const isArray = Array.isArray(destination ?? values[0]);
74
+ const merged = destination ?? (isArray ? [] : {});
75
+ const offset = destination == null ? 0 : 1;
76
+ for (let outerIndex = offset; outerIndex < length; outerIndex += 1) {
70
77
  const item = values[outerIndex];
71
78
  const keys = Object.keys(item);
72
79
  const size = keys.length;
73
80
  for (let innerIndex = 0; innerIndex < size; innerIndex += 1) {
74
81
  const key = keys[innerIndex];
75
- const full = join([prefix, key], ".");
76
82
  const next = item[key];
77
83
  const previous = merged[key];
78
84
  if (next == null && (options.skipNullableAny || isArray && options.skipNullableInArrays)) continue;
79
- if (isArrayOrPlainObject(next) && isArrayOrPlainObject(previous) && !(options.replaceableObjects?.(full) ?? false)) merged[key] = mergeValues([previous, next], options, false, full);
85
+ const full = options.replaceableObjects == null ? void 0 : prefix == null ? key : `${prefix}.${key}`;
86
+ if (isArrayOrPlainObject(next) && isArrayOrPlainObject(previous) && !(options.replaceableObjects?.(full) ?? false)) merged[key] = mergeObjects([previous, next], options, destination == null ? void 0 : merged[key], full);
80
87
  else merged[key] = next;
81
88
  }
82
89
  }
83
90
  return merged;
84
91
  }
85
- function mergeValues(values, options, validate, prefix) {
86
- const actual = validate ? values.filter(isArrayOrPlainObject) : values;
87
- return actual.length > 1 ? mergeObjects(actual, options, prefix) : actual[0] ?? {};
92
+ function mergeValues(values, options, prefix) {
93
+ if (!Array.isArray(values)) return {};
94
+ const actual = values.filter(isArrayOrPlainObject);
95
+ if (actual.length === 0) return {};
96
+ if (options.assignValues && actual.length === 2 && !Array.isArray(actual[0]) && Object.keys(actual[0]).length === 0) return Object.assign(actual[0], actual[1]);
97
+ if (actual.length > 1) return mergeObjects(actual, options, options.assignValues ? actual[0] : void 0, prefix);
98
+ return options.assignValues ? actual[0] : Array.isArray(actual[0]) ? actual[0].slice() : { ...actual[0] };
88
99
  }
89
100
  //#endregion
90
101
  export { assign, initializeAssigner, initializeMerger, merge };
@@ -28,7 +28,7 @@ declare function initializeTransformer<Value extends PlainObject>(transform: Tra
28
28
  *
29
29
  * Available as `initializeTransformer` and `transform.initialize`
30
30
  * @param transformers Keyed transformer functions
31
- * @return Transformer
31
+ * @returns Transformer
32
32
  */
33
33
  declare function initializeTransformer<Value extends PlainObject>(transformers: TransformCallbacks<Value>): Transformer<Value>;
34
34
  /**
@@ -1,14 +1,10 @@
1
- import { PlainObject, Simplify } from "../models.mjs";
1
+ import { PlainObject, Simplify, UnionToIntersection } from "../models.mjs";
2
2
 
3
3
  //#region src/value/unsmush.d.ts
4
4
  /**
5
5
  * Thanks, type-fest!
6
6
  */
7
7
  type KeysOfUnion<ObjectType> = keyof UnionToIntersection<ObjectType extends unknown ? Record<keyof ObjectType, never> : never>;
8
- /**
9
- * Thanks, type-fest!
10
- */
11
- type UnionToIntersection<Union> = (Union extends unknown ? (distributedUnion: Union) => void : never) extends ((mergedIntersection: infer Intersection) => void) ? Intersection & Union : never;
12
8
  /**
13
9
  * An unsmushed object, with all dot notation keys turned into nested keys
14
10
  */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oscarpalmer/atoms",
3
- "version": "0.185.0",
3
+ "version": "0.186.0",
4
4
  "description": "Atomic utilities for making your JavaScript better.",
5
5
  "keywords": [
6
6
  "helper",
@@ -253,13 +253,13 @@
253
253
  "watch": "npx vite build --watch"
254
254
  },
255
255
  "devDependencies": {
256
- "@oxlint/plugins": "^1.62",
256
+ "@oxlint/plugins": "^1.63",
257
257
  "@types/node": "^25.6",
258
258
  "@vitest/coverage-istanbul": "^4.1",
259
- "eslint": "^10.2",
259
+ "eslint": "^10.3",
260
260
  "jsdom": "^29.1",
261
- "tsdown": "^0.21",
262
- "typescript": "^5.9",
261
+ "tsdown": "^0.22",
262
+ "typescript": "^6",
263
263
  "vite": "npm:@voidzero-dev/vite-plus-core@latest",
264
264
  "vite-plus": "latest",
265
265
  "vitest": "npm:@voidzero-dev/vite-plus-test@latest"
@@ -5,10 +5,20 @@ import type {PlainObject} from '../models';
5
5
 
6
6
  /**
7
7
  * Get the items from the first array that are not in the second array
8
+ *
8
9
  * @param first First array
9
10
  * @param second Second array
10
11
  * @param callback Callback to get an item's value for comparison
11
12
  * @returns Unique values from the first array
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * difference(
17
+ * [{id: 1}, {id: 2}, {id: 3}],
18
+ * [{id: 2}, {id: 4}],
19
+ * item => item.id,
20
+ * ); // => [{id: 1}, {id: 3}]
21
+ * ```
12
22
  */
13
23
  export function difference<First, Second>(
14
24
  first: First[],
@@ -18,10 +28,20 @@ export function difference<First, Second>(
18
28
 
19
29
  /**
20
30
  * Get the items from the first array that are not in the second array
31
+ *
21
32
  * @param first First array
22
33
  * @param second Second array
23
34
  * @param key Key to get an item's value for comparison
24
35
  * @returns Unique values from the first array
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * difference(
40
+ * [{id: 1}, {id: 2}, {id: 3}],
41
+ * [{id: 2}, {id: 4}],
42
+ * 'id',
43
+ * ); // => [{id: 1}, {id: 3}]
44
+ * ```
25
45
  */
26
46
  export function difference<
27
47
  First extends PlainObject,
@@ -31,9 +51,18 @@ export function difference<
31
51
 
32
52
  /**
33
53
  * Get the items from the first array that are not in the second array
54
+ *
34
55
  * @param first First array
35
56
  * @param second Second array
36
57
  * @returns Unique values from the first array
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * difference(
62
+ * [1, 2, 3],
63
+ * [2, 4],
64
+ * ); // => [1, 3]
65
+ * ```
37
66
  */
38
67
  export function difference<First, Second>(first: First[], second: Second[]): First[];
39
68
 
@@ -5,10 +5,20 @@ import type {PlainObject} from '../models';
5
5
 
6
6
  /**
7
7
  * Does an item with a specific value exist in the array?
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 `true` if the item exists in the array, otherwise `false`
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * exists(
17
+ * [{id: 1}, {id: 2}, {id: 3}],
18
+ * item => item.id,
19
+ * 2,
20
+ * ); // => true
21
+ * ```
12
22
  */
13
23
  export function exists<
14
24
  Item,
@@ -17,10 +27,20 @@ export function exists<
17
27
 
18
28
  /**
19
29
  * Does an item with a specific value exist in the array?
30
+ *
20
31
  * @param array Array to search in
21
32
  * @param key Key to get an item's value for matching
22
33
  * @param value Value to match against
23
34
  * @returns `true` if the item exists in the array, otherwise `false`
35
+ *
36
+ * @example
37
+ * ```typescript
38
+ * exists(
39
+ * [{id: 1}, {id: 2}, {id: 3}],
40
+ * 'id',
41
+ * 2,
42
+ * ); // => true
43
+ * ```
24
44
  */
25
45
  export function exists<Item extends PlainObject, ItemKey extends keyof Item>(
26
46
  array: Item[],
@@ -30,9 +50,18 @@ export function exists<Item extends PlainObject, ItemKey extends keyof Item>(
30
50
 
31
51
  /**
32
52
  * Does an item in the array match the filter?
53
+ *
33
54
  * @param array Array to search in
34
55
  * @param filter Filter callback to match items
35
56
  * @returns `true` if a matching item exists, otherwise `false`
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * exists(
61
+ * [{id: 1}, {id: 2}, {id: 3}],
62
+ * item => item.id === 2,
63
+ * ); // => true
64
+ * ```
36
65
  */
37
66
  export function exists<Item>(
38
67
  array: Item[],
@@ -41,9 +70,15 @@ export function exists<Item>(
41
70
 
42
71
  /**
43
72
  * Does the item exist in the array?
73
+ *
44
74
  * @param array Array to search in
45
75
  * @param item Item to search for
46
76
  * @returns `true` if the item exists in the array, otherwise `false`
77
+ *
78
+ * @example
79
+ * ```typescript
80
+ * exists([1, 2, 3], 2); // => true
81
+ * ```
47
82
  */
48
83
  export function exists<Item>(array: Item[], item: Item): boolean;
49
84
 
@@ -4,13 +4,23 @@ import type {PlainObject} from '../models';
4
4
  // #region Functions
5
5
 
6
6
  /**
7
- * Get a filtered array of items that do not match the filter
7
+ * Get a filtered array of items that do not match the value
8
8
  *
9
9
  * Available as `exclude` and `filter.remove`
10
+ *
10
11
  * @param array Array to search in
11
12
  * @param callback Callback to get an item's value for matching
12
13
  * @param value Value to match against
13
14
  * @returns Filtered array of items that do not match the filter
15
+ *
16
+ * @example
17
+ * ```typescript
18
+ * exclude(
19
+ * [{id: 1}, {id: 2}, {id: 3}],
20
+ * item => item.id,
21
+ * 2,
22
+ * ); // => [{id: 1}, {id: 3}]
23
+ * ```
14
24
  */
15
25
  export function exclude<
16
26
  Item,
@@ -18,13 +28,23 @@ export function exclude<
18
28
  >(array: Item[], callback: Callback, value: ReturnType<Callback>): unknown[];
19
29
 
20
30
  /**
21
- * Get a filtered array of items that do not match the filter
31
+ * Get a filtered array of items that do not match the value
22
32
  *
23
33
  * Available as `exclude` and `filter.remove`
34
+ *
24
35
  * @param array Array to search in
25
36
  * @param key Key to get an item's value for matching
26
37
  * @param value Value to match against
27
38
  * @returns Filtered array of items that do not match the filter
39
+ *
40
+ * @example
41
+ * ```typescript
42
+ * exclude(
43
+ * [{id: 1}, {id: 2}, {id: 3}],
44
+ * 'id',
45
+ * 2,
46
+ * ); // => [{id: 1}, {id: 3}]
47
+ * ```
28
48
  */
29
49
  export function exclude<Item extends PlainObject, ItemKey extends keyof Item>(
30
50
  array: Item[],
@@ -36,9 +56,18 @@ export function exclude<Item extends PlainObject, ItemKey extends keyof Item>(
36
56
  * Get a filtered array of items that do not match the filter
37
57
  *
38
58
  * Available as `exclude` and `filter.remove`
59
+ *
39
60
  * @param array Array to search in
40
61
  * @param filter Filter callback to match items
41
62
  * @returns Filtered array of items that do not match the filter
63
+ *
64
+ * @example
65
+ * ```typescript
66
+ * exclude(
67
+ * [{id: 1}, {id: 2}, {id: 3}],
68
+ * item => item.id === 2,
69
+ * ); // => [{id: 1}, {id: 3}]
70
+ * ```
42
71
  */
43
72
  export function exclude<Item>(
44
73
  array: Item[],
@@ -49,9 +78,15 @@ export function exclude<Item>(
49
78
  * Get a filtered array of items that do not match the given item
50
79
  *
51
80
  * Available as `exclude` and `filter.remove`
81
+ *
52
82
  * @param array Array to search in
53
83
  * @param item Item to match against
54
84
  * @returns Filtered array of items that do not match the given item
85
+ *
86
+ * @example
87
+ * ```typescript
88
+ * exclude([1, 2, 3], 2); // => [1, 3]
89
+ * ```
55
90
  */
56
91
  export function exclude<Item>(array: Item[], item: Item): unknown[];
57
92
 
@@ -61,10 +96,20 @@ export function exclude(array: unknown[], ...parameters: unknown[]): unknown[] {
61
96
 
62
97
  /**
63
98
  * Get a filtered array of items
99
+ *
64
100
  * @param array Array to search in
65
101
  * @param callback Callback to get an item's value for matching
66
102
  * @param value Value to match against
67
103
  * @returns Filtered array of items
104
+ *
105
+ * @example
106
+ * ```typescript
107
+ * filter(
108
+ * [{id: 1}, {id: 2}, {id: 3}],
109
+ * item => item.id,
110
+ * 2,
111
+ * ); // => [{id: 2}]
112
+ * ```
68
113
  */
69
114
  export function filter<
70
115
  Item,
@@ -73,10 +118,20 @@ export function filter<
73
118
 
74
119
  /**
75
120
  * Get a filtered array of items
121
+ *
76
122
  * @param array Array to search in
77
123
  * @param key Key to get an item's value for matching
78
124
  * @param value Value to match against
79
125
  * @returns Filtered array of items
126
+ *
127
+ * @example
128
+ * ```typescript
129
+ * filter(
130
+ * [{id: 1}, {id: 2}, {id: 3}],
131
+ * 'id',
132
+ * 2,
133
+ * ); // => [{id: 2}]
134
+ * ```
80
135
  */
81
136
  export function filter<Item extends PlainObject, ItemKey extends keyof Item>(
82
137
  array: Item[],
@@ -86,9 +141,18 @@ export function filter<Item extends PlainObject, ItemKey extends keyof Item>(
86
141
 
87
142
  /**
88
143
  * Get a filtered array of items matching the filter
144
+ *
89
145
  * @param array Array to search in
90
146
  * @param filter Filter callback to match items
91
147
  * @returns Filtered array of items
148
+ *
149
+ * @example
150
+ * ```typescript
151
+ * filter(
152
+ * [{id: 1}, {id: 2}, {id: 3}],
153
+ * item => item.id === 2,
154
+ * ); // => [{id: 2}]
155
+ * ```
92
156
  */
93
157
  export function filter<Item>(
94
158
  array: Item[],
@@ -97,9 +161,15 @@ export function filter<Item>(
97
161
 
98
162
  /**
99
163
  * Get a filtered array of items matching the given item
164
+ *
100
165
  * @param array Array to search in
101
166
  * @param item Item to match against
102
167
  * @returns Filtered array of items
168
+ *
169
+ * @example
170
+ * ```typescript
171
+ * filter([1, 2, 3], 2); // => [2]
172
+ * ```
103
173
  */
104
174
  export function filter<Item>(array: Item[], item: Item): Item[];
105
175
 
package/src/array/find.ts CHANGED
@@ -5,10 +5,20 @@ import type {PlainObject} from '../models';
5
5
 
6
6
  /**
7
7
  * Get the first item matching the given 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 First item that matches the value, or `undefined` if no match is found
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * find(
17
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
18
+ * item => item.value,
19
+ * 10,
20
+ * ); // => {id: 1, value: 10}
21
+ * ```
12
22
  */
13
23
  export function find<Item, Callback extends (item: Item, index: number, array: Item[]) => unknown>(
14
24
  array: Item[],
@@ -18,10 +28,20 @@ export function find<Item, Callback extends (item: Item, index: number, array: I
18
28
 
19
29
  /**
20
30
  * Get the first item matching the given value by key
31
+ *
21
32
  * @param array Array to search in
22
33
  * @param key Key to get an item's value for matching
23
34
  * @param value Value to match against
24
35
  * @returns First item that matches the value, or `undefined` if no match is found
36
+ *
37
+ * @example
38
+ * ```typescript
39
+ * find(
40
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
41
+ * 'value',
42
+ * 10,
43
+ * ); // => {id: 1, value: 10}
44
+ * ```
25
45
  */
26
46
  export function find<Item extends PlainObject, ItemKey extends keyof Item>(
27
47
  array: Item[],
@@ -31,9 +51,18 @@ export function find<Item extends PlainObject, ItemKey extends keyof Item>(
31
51
 
32
52
  /**
33
53
  * Get the first item matching the filter
54
+ *
34
55
  * @param array Array to search in
35
56
  * @param filter Filter callback to match items
36
57
  * @returns First item that matches the filter, or `undefined` if no match is found
58
+ *
59
+ * @example
60
+ * ```typescript
61
+ * find(
62
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
63
+ * item => item.value === 10,
64
+ * ); // => {id: 1, value: 10}
65
+ * ```
37
66
  */
38
67
  export function find<Item>(
39
68
  array: Item[],
@@ -42,9 +71,15 @@ export function find<Item>(
42
71
 
43
72
  /**
44
73
  * Get the first item matching the given value
74
+ *
45
75
  * @param array Array to search in
46
76
  * @param value Value to match against
47
77
  * @returns First item that matches the value, or `undefined` if no match is found
78
+ *
79
+ * @example
80
+ * ```typescript
81
+ * find([1, 2, 3, 2, 1], 1); // => 1
82
+ * ```
48
83
  */
49
84
  export function find<Item>(array: Item[], value: Item): Item | undefined;
50
85
 
@@ -58,10 +93,20 @@ find.last = findLast;
58
93
  * Get the last item matching the given value
59
94
  *
60
95
  * Available as `findLast` and `find.last`
96
+ *
61
97
  * @param array Array to search in
62
98
  * @param callback Callback to get an item's value for matching
63
99
  * @param value Value to match against
64
100
  * @returns Last item that matches the value, or `undefined` if no match is found
101
+ *
102
+ * @example
103
+ * ```typescript
104
+ * findLast(
105
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
106
+ * item => item.value,
107
+ * 10,
108
+ * ); // => {id: 3, value: 10}
109
+ * ```
65
110
  */
66
111
  export function findLast<
67
112
  Item,
@@ -72,10 +117,20 @@ export function findLast<
72
117
  * Get the last item matching the given value by key
73
118
  *
74
119
  * Available as `findLast` and `find.last`
120
+ *
75
121
  * @param array Array to search in
76
122
  * @param key Key to get an item's value for matching
77
123
  * @param value Value to match against
78
124
  * @returns Last item that matches the value, or `undefined` if no match is found
125
+ *
126
+ * @example
127
+ * ```typescript
128
+ * findLast(
129
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
130
+ * 'value',
131
+ * 10,
132
+ * ); // => {id: 3, value: 10}
133
+ * ```
79
134
  */
80
135
  export function findLast<Item extends PlainObject, ItemKey extends keyof Item>(
81
136
  array: Item[],
@@ -87,9 +142,18 @@ export function findLast<Item extends PlainObject, ItemKey extends keyof Item>(
87
142
  * Get the last item matching the filter
88
143
  *
89
144
  * Available as `findLast` and `find.last`
145
+ *
90
146
  * @param array Array to search in
91
147
  * @param filter Filter callback to match items
92
148
  * @returns Last item that matches the filter, or `undefined` if no match is found
149
+ *
150
+ * @example
151
+ * ```typescript
152
+ * findLast(
153
+ * [{id: 1, value: 10}, {id: 2, value: 20}, {id: 3, value: 10}],
154
+ * item => item.value === 10,
155
+ * ); // => {id: 3, value: 10}
156
+ * ```
93
157
  */
94
158
  export function findLast<Item>(
95
159
  array: Item[],
@@ -100,9 +164,15 @@ export function findLast<Item>(
100
164
  * Get the last item matching the given value
101
165
  *
102
166
  * Available as `findLast` and `find.last`
167
+ *
103
168
  * @param array Array to search in
104
169
  * @param value Value to match against
105
170
  * @returns Last item that matches the value, or `undefined` if no match is found
171
+ *
172
+ * @example
173
+ * ```typescript
174
+ * findLast([1, 2, 3, 2, 1], 1); // => 1
175
+ * ```
106
176
  */
107
177
  export function findLast<Item>(array: Item[], value: Item): Item | undefined;
108
178