es-toolkit 1.15.1 → 1.16.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 (99) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/dist/_chunk/{initial-CBsbzo.js → initial-y0QrPY.js} +28 -0
  3. package/dist/_chunk/{isFunction-D0hq6d.js → isFunction-aCEz9d.js} +5 -7
  4. package/dist/_chunk/{isObjectLike-BeLCsr.js → toMerged-BGwYW5.js} +25 -0
  5. package/dist/_internal/compareValues.mjs +11 -0
  6. package/dist/array/index.d.mts +1 -0
  7. package/dist/array/index.d.ts +1 -0
  8. package/dist/array/index.js +3 -11
  9. package/dist/array/index.mjs +1 -0
  10. package/dist/array/orderBy.mjs +2 -9
  11. package/dist/array/sortBy.d.mts +35 -0
  12. package/dist/array/sortBy.d.ts +35 -0
  13. package/dist/array/sortBy.mjs +19 -0
  14. package/dist/browser.global.js +1 -1
  15. package/dist/browser.global.js.map +1 -1
  16. package/dist/compat/_internal/getSymbols.mjs +1 -2
  17. package/dist/compat/_internal/toKey.mjs +13 -0
  18. package/dist/compat/array/find.d.mts +122 -0
  19. package/dist/compat/array/find.d.ts +122 -0
  20. package/dist/compat/array/find.mjs +42 -0
  21. package/dist/compat/array/findIndex.d.mts +62 -0
  22. package/dist/compat/array/findIndex.d.ts +62 -0
  23. package/dist/compat/array/findIndex.mjs +26 -0
  24. package/dist/compat/array/indexOf.d.mts +21 -0
  25. package/dist/compat/array/indexOf.d.ts +21 -0
  26. package/dist/compat/array/indexOf.mjs +20 -0
  27. package/dist/compat/function/ary.d.mts +2 -1
  28. package/dist/compat/function/ary.d.ts +2 -1
  29. package/dist/compat/function/bind.d.mts +3 -1
  30. package/dist/compat/function/bind.d.ts +3 -1
  31. package/dist/compat/function/rest.d.mts +33 -0
  32. package/dist/compat/function/rest.d.ts +33 -0
  33. package/dist/compat/function/rest.mjs +11 -0
  34. package/dist/compat/index.d.mts +13 -0
  35. package/dist/compat/index.d.ts +13 -0
  36. package/dist/compat/index.js +413 -259
  37. package/dist/compat/index.mjs +13 -0
  38. package/dist/compat/object/get.mjs +2 -4
  39. package/dist/compat/object/has.d.mts +32 -0
  40. package/dist/compat/object/has.d.ts +32 -0
  41. package/dist/compat/object/has.mjs +34 -0
  42. package/dist/compat/object/merge.d.mts +1 -0
  43. package/dist/compat/object/merge.d.ts +1 -0
  44. package/dist/compat/object/mergeWith.d.mts +6 -0
  45. package/dist/compat/object/mergeWith.d.ts +6 -0
  46. package/dist/compat/object/mergeWith.mjs +0 -3
  47. package/dist/compat/object/set.mjs +1 -5
  48. package/dist/compat/predicate/isArrayLike.mjs +1 -2
  49. package/dist/compat/predicate/isString.d.mts +20 -0
  50. package/dist/compat/predicate/isString.d.ts +20 -0
  51. package/dist/compat/predicate/isString.mjs +13 -0
  52. package/dist/compat/predicate/matchesProperty.d.mts +28 -0
  53. package/dist/compat/predicate/matchesProperty.d.ts +28 -0
  54. package/dist/compat/predicate/matchesProperty.mjs +22 -0
  55. package/dist/compat/string/padEnd.d.mts +20 -0
  56. package/dist/compat/string/padEnd.d.ts +20 -0
  57. package/dist/compat/string/padEnd.mjs +5 -0
  58. package/dist/function/ary.d.mts +1 -1
  59. package/dist/function/ary.d.ts +1 -1
  60. package/dist/function/index.d.mts +4 -0
  61. package/dist/function/index.d.ts +4 -0
  62. package/dist/function/index.js +76 -0
  63. package/dist/function/index.mjs +4 -0
  64. package/dist/function/memoize.d.mts +87 -0
  65. package/dist/function/memoize.d.ts +87 -0
  66. package/dist/function/memoize.mjs +16 -0
  67. package/dist/function/partial.d.mts +33 -0
  68. package/dist/function/partial.d.ts +33 -0
  69. package/dist/function/partial.mjs +23 -0
  70. package/dist/function/partialRight.d.mts +33 -0
  71. package/dist/function/partialRight.d.ts +33 -0
  72. package/dist/function/partialRight.mjs +25 -0
  73. package/dist/function/rest.d.mts +33 -0
  74. package/dist/function/rest.d.ts +33 -0
  75. package/dist/function/rest.mjs +12 -0
  76. package/dist/function/unary.d.mts +1 -1
  77. package/dist/function/unary.d.ts +1 -1
  78. package/dist/index.d.mts +7 -0
  79. package/dist/index.d.ts +7 -0
  80. package/dist/index.js +21 -14
  81. package/dist/index.mjs +7 -0
  82. package/dist/object/index.d.mts +1 -0
  83. package/dist/object/index.d.ts +1 -0
  84. package/dist/object/index.js +14 -32
  85. package/dist/object/index.mjs +1 -0
  86. package/dist/object/toMerged.d.mts +45 -0
  87. package/dist/object/toMerged.d.ts +45 -0
  88. package/dist/object/toMerged.mjs +8 -0
  89. package/dist/predicate/index.d.mts +1 -0
  90. package/dist/predicate/index.d.ts +1 -0
  91. package/dist/predicate/index.js +6 -1
  92. package/dist/predicate/index.mjs +1 -0
  93. package/dist/predicate/isEqual.mjs +4 -5
  94. package/dist/predicate/isString.d.mts +20 -0
  95. package/dist/predicate/isString.d.ts +20 -0
  96. package/dist/predicate/isString.mjs +5 -0
  97. package/dist/string/camelCase.mjs +2 -2
  98. package/dist/string/index.js +2 -2
  99. package/package.json +1 -1
@@ -1,6 +1,5 @@
1
1
  function getSymbols(object) {
2
- return Object.getOwnPropertySymbols(object)
3
- .filter(symbol => object.propertyIsEnumerable(symbol));
2
+ return Object.getOwnPropertySymbols(object).filter(symbol => Object.prototype.propertyIsEnumerable.call(object, symbol));
4
3
  }
5
4
 
6
5
  export { getSymbols };
@@ -0,0 +1,13 @@
1
+ import { isSymbol } from '../predicate/isSymbol.mjs';
2
+
3
+ function toKey(value) {
4
+ if (typeof value === 'string' || isSymbol(value)) {
5
+ return value;
6
+ }
7
+ if (Object.is(value?.valueOf(), -0)) {
8
+ return '-0';
9
+ }
10
+ return `${value}`;
11
+ }
12
+
13
+ export { toKey };
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Finds the first item in an array that matches the given predicate function.
3
+ *
4
+ * @template T
5
+ * @param {T[]} arr - The array to search through.
6
+ * @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - A function that takes an item, its index, and the array, and returns a truthy value if the item matches the criteria.
7
+ * @returns {T | undefined} - The first item that matches the predicate, or `undefined` if no match is found.
8
+ *
9
+ * @example
10
+ * // Using a predicate function
11
+ * const items = [1, 2, 3, 4, 5];
12
+ * const result = find(items, (item) => item > 3);
13
+ * console.log(result); // 4
14
+ */
15
+ declare function find<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): T | undefined;
16
+ /**
17
+ * Finds the first item in an array that matches the given partial object.
18
+ *
19
+ * @template T
20
+ * @param {readonly T[]} arr - The array to search through.
21
+ * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
22
+ * @returns {T | undefined} - The first item that matches the partial object, or `undefined` if no match is found.
23
+ *
24
+ * @example
25
+ * // Using a partial object
26
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
27
+ * const result = find(items, { name: 'Bob' });
28
+ * console.log(result); // { id: 2, name: 'Bob' }
29
+ */
30
+ declare function find<T>(arr: readonly T[], doesMatch: Partial<T>): T | undefined;
31
+ /**
32
+ * Finds the first item in an array that matches a property with a specific value.
33
+ *
34
+ * @template T
35
+ * @param {readonly T[]} arr - The array to search through.
36
+ * @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
37
+ * @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
38
+ *
39
+ * @example
40
+ * // Using a property-value pair
41
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
42
+ * const result = find(items, ['name', 'Alice']);
43
+ * console.log(result); // { id: 1, name: 'Alice' }
44
+ */
45
+ declare function find<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): T | undefined;
46
+ /**
47
+ * Finds the first item in an array that has a specific property, where the property name is provided as a string.
48
+ *
49
+ * @template T
50
+ * @param {readonly T[]} arr - The array to search through.
51
+ * @param {string} propertyToCheck - The property name to check.
52
+ * @returns {T | undefined} - The first item that has the specified property, or `undefined` if no match is found.
53
+ *
54
+ * @example
55
+ * // Using a property name
56
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
57
+ * const result = find(items, 'name');
58
+ * console.log(result); // { id: 1, name: 'Alice' }
59
+ */
60
+ declare function find<T>(arr: readonly T[], propertyToCheck: string): T | undefined;
61
+ /**
62
+ * Finds the first item in an object that matches the given predicate function.
63
+ *
64
+ * @template T
65
+ * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
66
+ * @param {(item: T[keyof T], index: number, arr: T) => unknown} doesMatch - A function that takes an item, its key, and the object, and returns a truthy value if the item matches the criteria.
67
+ * @returns {T | undefined} - The first property value that matches the predicate, or `undefined` if no match is found.
68
+ *
69
+ * @example
70
+ * // Using a predicate function
71
+ * const obj = { a: 1, b: 2, c: 3 };
72
+ * const result = find(obj, (item) => item > 2);
73
+ * console.log(result); // 3
74
+ */
75
+ declare function find<T extends Record<string, unknown>>(object: T, doesMatch: (item: T[keyof T], index: number, object: T) => unknown): T | undefined;
76
+ /**
77
+ * Finds the first item in an object that matches the given partial value.
78
+ *
79
+ * @template T
80
+ * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
81
+ * @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
82
+ * @returns {T | undefined} - The first property value that matches the partial value, or `undefined` if no match is found.
83
+ *
84
+ * @example
85
+ * // Using a partial value
86
+ * const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
87
+ * const result = find(obj, { name: 'Bob' });
88
+ * console.log(result); // { id: 2, name: 'Bob' }
89
+ */
90
+ declare function find<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): T | undefined;
91
+ /**
92
+ * Finds the first item in an object that matches a property with a specific value.
93
+ *
94
+ * @template T
95
+ * @param {readonly T[]} object - The object to search through.
96
+ * @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
97
+ * @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
98
+ *
99
+ * @example
100
+ * // Using a property-value pair
101
+ * const items = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
102
+ * const result = find(items, ['name', 'Alice']);
103
+ * console.log(result); // { id: 1, name: 'Alice' }
104
+ */
105
+ declare function find<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): T | undefined;
106
+ /**
107
+ * Finds the first item in an object that has a specific property, where the property name is provided as a string.
108
+ *
109
+ * @template T
110
+ * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
111
+ * @param {string} propertyToCheck - The property name to check.
112
+ * @returns {T | undefined} - The first property value that has the specified property, or `undefined` if no match is found.
113
+ *
114
+ * @example
115
+ * // Using a property name
116
+ * const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
117
+ * const result = find(obj, 'name');
118
+ * console.log(result); // { id: 1, name: 'Alice' }
119
+ */
120
+ declare function find<T extends Record<string, unknown>>(object: T, propertyToCheck: string): T | undefined;
121
+
122
+ export { find };
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Finds the first item in an array that matches the given predicate function.
3
+ *
4
+ * @template T
5
+ * @param {T[]} arr - The array to search through.
6
+ * @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - A function that takes an item, its index, and the array, and returns a truthy value if the item matches the criteria.
7
+ * @returns {T | undefined} - The first item that matches the predicate, or `undefined` if no match is found.
8
+ *
9
+ * @example
10
+ * // Using a predicate function
11
+ * const items = [1, 2, 3, 4, 5];
12
+ * const result = find(items, (item) => item > 3);
13
+ * console.log(result); // 4
14
+ */
15
+ declare function find<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): T | undefined;
16
+ /**
17
+ * Finds the first item in an array that matches the given partial object.
18
+ *
19
+ * @template T
20
+ * @param {readonly T[]} arr - The array to search through.
21
+ * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
22
+ * @returns {T | undefined} - The first item that matches the partial object, or `undefined` if no match is found.
23
+ *
24
+ * @example
25
+ * // Using a partial object
26
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
27
+ * const result = find(items, { name: 'Bob' });
28
+ * console.log(result); // { id: 2, name: 'Bob' }
29
+ */
30
+ declare function find<T>(arr: readonly T[], doesMatch: Partial<T>): T | undefined;
31
+ /**
32
+ * Finds the first item in an array that matches a property with a specific value.
33
+ *
34
+ * @template T
35
+ * @param {readonly T[]} arr - The array to search through.
36
+ * @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
37
+ * @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
38
+ *
39
+ * @example
40
+ * // Using a property-value pair
41
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
42
+ * const result = find(items, ['name', 'Alice']);
43
+ * console.log(result); // { id: 1, name: 'Alice' }
44
+ */
45
+ declare function find<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): T | undefined;
46
+ /**
47
+ * Finds the first item in an array that has a specific property, where the property name is provided as a string.
48
+ *
49
+ * @template T
50
+ * @param {readonly T[]} arr - The array to search through.
51
+ * @param {string} propertyToCheck - The property name to check.
52
+ * @returns {T | undefined} - The first item that has the specified property, or `undefined` if no match is found.
53
+ *
54
+ * @example
55
+ * // Using a property name
56
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
57
+ * const result = find(items, 'name');
58
+ * console.log(result); // { id: 1, name: 'Alice' }
59
+ */
60
+ declare function find<T>(arr: readonly T[], propertyToCheck: string): T | undefined;
61
+ /**
62
+ * Finds the first item in an object that matches the given predicate function.
63
+ *
64
+ * @template T
65
+ * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
66
+ * @param {(item: T[keyof T], index: number, arr: T) => unknown} doesMatch - A function that takes an item, its key, and the object, and returns a truthy value if the item matches the criteria.
67
+ * @returns {T | undefined} - The first property value that matches the predicate, or `undefined` if no match is found.
68
+ *
69
+ * @example
70
+ * // Using a predicate function
71
+ * const obj = { a: 1, b: 2, c: 3 };
72
+ * const result = find(obj, (item) => item > 2);
73
+ * console.log(result); // 3
74
+ */
75
+ declare function find<T extends Record<string, unknown>>(object: T, doesMatch: (item: T[keyof T], index: number, object: T) => unknown): T | undefined;
76
+ /**
77
+ * Finds the first item in an object that matches the given partial value.
78
+ *
79
+ * @template T
80
+ * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
81
+ * @param {Partial<T[keyof T]>} doesMatch - A partial value to match against the values of the object.
82
+ * @returns {T | undefined} - The first property value that matches the partial value, or `undefined` if no match is found.
83
+ *
84
+ * @example
85
+ * // Using a partial value
86
+ * const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
87
+ * const result = find(obj, { name: 'Bob' });
88
+ * console.log(result); // { id: 2, name: 'Bob' }
89
+ */
90
+ declare function find<T extends Record<string, unknown>>(object: T, doesMatch: Partial<T[keyof T]>): T | undefined;
91
+ /**
92
+ * Finds the first item in an object that matches a property with a specific value.
93
+ *
94
+ * @template T
95
+ * @param {readonly T[]} object - The object to search through.
96
+ * @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
97
+ * @returns {T | undefined} - The first item that has the specified property value, or `undefined` if no match is found.
98
+ *
99
+ * @example
100
+ * // Using a property-value pair
101
+ * const items = { alice: { id: 1, name: 'Alice' }, bob: { id: 2, name: 'Bob' } };
102
+ * const result = find(items, ['name', 'Alice']);
103
+ * console.log(result); // { id: 1, name: 'Alice' }
104
+ */
105
+ declare function find<T extends Record<string, unknown>>(object: T, doesMatchProperty: [keyof T, unknown]): T | undefined;
106
+ /**
107
+ * Finds the first item in an object that has a specific property, where the property name is provided as a string.
108
+ *
109
+ * @template T
110
+ * @param {T extends Record<string, unknown> ? T : never} object - The object to search through.
111
+ * @param {string} propertyToCheck - The property name to check.
112
+ * @returns {T | undefined} - The first property value that has the specified property, or `undefined` if no match is found.
113
+ *
114
+ * @example
115
+ * // Using a property name
116
+ * const obj = { a: { id: 1, name: 'Alice' }, b: { id: 2, name: 'Bob' } };
117
+ * const result = find(obj, 'name');
118
+ * console.log(result); // { id: 1, name: 'Alice' }
119
+ */
120
+ declare function find<T extends Record<string, unknown>>(object: T, propertyToCheck: string): T | undefined;
121
+
122
+ export { find };
@@ -0,0 +1,42 @@
1
+ import { property } from '../object/property.mjs';
2
+ import { matches } from '../predicate/matches.mjs';
3
+ import { matchesProperty } from '../predicate/matchesProperty.mjs';
4
+
5
+ function find(source, doesMatch) {
6
+ let values = source;
7
+ if (!Array.isArray(source)) {
8
+ values = Object.values(source);
9
+ }
10
+ switch (typeof doesMatch) {
11
+ case 'function': {
12
+ if (!Array.isArray(source)) {
13
+ const entries = Object.entries(source);
14
+ for (let i = 0; i < entries.length; i++) {
15
+ const entry = entries[i];
16
+ const key = entry[0];
17
+ const value = entry[1];
18
+ if (doesMatch(value, key, source)) {
19
+ return value;
20
+ }
21
+ }
22
+ return undefined;
23
+ }
24
+ return values.find(doesMatch);
25
+ }
26
+ case 'object': {
27
+ if (Array.isArray(doesMatch) && doesMatch.length === 2) {
28
+ const key = doesMatch[0];
29
+ const value = doesMatch[1];
30
+ return values.find(matchesProperty(key, value));
31
+ }
32
+ else {
33
+ return values.find(matches(doesMatch));
34
+ }
35
+ }
36
+ case 'string': {
37
+ return values.find(property(doesMatch));
38
+ }
39
+ }
40
+ }
41
+
42
+ export { find };
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Finds the index of the first item in an array that matches the given predicate function.
3
+ *
4
+ * @template T
5
+ * @param {T[]} arr - The array to search through.
6
+ * @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - A function that takes an item, its index, and the array, and returns a truthy value if the item matches the criteria.
7
+ * @returns {number} - The index of the first item that matches the predicate, or `undefined` if no match is found.
8
+ *
9
+ * @example
10
+ * // Using a predicate function
11
+ * const items = [1, 2, 3, 4, 5];
12
+ * const result = find(items, (item) => item > 3);
13
+ * console.log(result); // 4
14
+ */
15
+ declare function findIndex<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): number;
16
+ /**
17
+ * Finds the index of the first item in an array that matches the given partial object.
18
+ *
19
+ * @template T
20
+ * @param {readonly T[]} arr - The array to search through.
21
+ * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
22
+ * @returns {number} - The index of the first item that matches the partial object, or `undefined` if no match is found.
23
+ *
24
+ * @example
25
+ * // Using a partial object
26
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
27
+ * const result = findIndex(items, { name: 'Bob' });
28
+ * console.log(result); // 1
29
+ */
30
+ declare function findIndex<T>(arr: readonly T[], doesMatch: Partial<T>): number;
31
+ /**
32
+ * Finds the index of the first item in an array that matches a property with a specific value.
33
+ *
34
+ * @template T
35
+ * @param {readonly T[]} arr - The array to search through.
36
+ * @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
37
+ * @returns {number} - The index of the first item that has the specified property value, or `undefined` if no match is found.
38
+ *
39
+ * @example
40
+ * // Using a property-value pair
41
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
42
+ * const result = findIndex(items, ['name', 'Alice']);
43
+ * console.log(result); // 0
44
+ */
45
+ declare function findIndex<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): number;
46
+ /**
47
+ * Finds the index of the first item in an array that has a specific property, where the property name is provided as a string.
48
+ *
49
+ * @template T
50
+ * @param {readonly T[]} arr - The array to search through.
51
+ * @param {string} propertyToCheck - The property name to check.
52
+ * @returns {number} - The index of the first item that has the specified property, or `undefined` if no match is found.
53
+ *
54
+ * @example
55
+ * // Using a property name
56
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
57
+ * const result = findIndex(items, 'name');
58
+ * console.log(result); // 0
59
+ */
60
+ declare function findIndex<T>(arr: readonly T[], propertyToCheck: string): number;
61
+
62
+ export { findIndex };
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Finds the index of the first item in an array that matches the given predicate function.
3
+ *
4
+ * @template T
5
+ * @param {T[]} arr - The array to search through.
6
+ * @param {(item: T, index: number, arr: T[]) => unknown} doesMatch - A function that takes an item, its index, and the array, and returns a truthy value if the item matches the criteria.
7
+ * @returns {number} - The index of the first item that matches the predicate, or `undefined` if no match is found.
8
+ *
9
+ * @example
10
+ * // Using a predicate function
11
+ * const items = [1, 2, 3, 4, 5];
12
+ * const result = find(items, (item) => item > 3);
13
+ * console.log(result); // 4
14
+ */
15
+ declare function findIndex<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown): number;
16
+ /**
17
+ * Finds the index of the first item in an array that matches the given partial object.
18
+ *
19
+ * @template T
20
+ * @param {readonly T[]} arr - The array to search through.
21
+ * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
22
+ * @returns {number} - The index of the first item that matches the partial object, or `undefined` if no match is found.
23
+ *
24
+ * @example
25
+ * // Using a partial object
26
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
27
+ * const result = findIndex(items, { name: 'Bob' });
28
+ * console.log(result); // 1
29
+ */
30
+ declare function findIndex<T>(arr: readonly T[], doesMatch: Partial<T>): number;
31
+ /**
32
+ * Finds the index of the first item in an array that matches a property with a specific value.
33
+ *
34
+ * @template T
35
+ * @param {readonly T[]} arr - The array to search through.
36
+ * @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
37
+ * @returns {number} - The index of the first item that has the specified property value, or `undefined` if no match is found.
38
+ *
39
+ * @example
40
+ * // Using a property-value pair
41
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
42
+ * const result = findIndex(items, ['name', 'Alice']);
43
+ * console.log(result); // 0
44
+ */
45
+ declare function findIndex<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown]): number;
46
+ /**
47
+ * Finds the index of the first item in an array that has a specific property, where the property name is provided as a string.
48
+ *
49
+ * @template T
50
+ * @param {readonly T[]} arr - The array to search through.
51
+ * @param {string} propertyToCheck - The property name to check.
52
+ * @returns {number} - The index of the first item that has the specified property, or `undefined` if no match is found.
53
+ *
54
+ * @example
55
+ * // Using a property name
56
+ * const items = [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }];
57
+ * const result = findIndex(items, 'name');
58
+ * console.log(result); // 0
59
+ */
60
+ declare function findIndex<T>(arr: readonly T[], propertyToCheck: string): number;
61
+
62
+ export { findIndex };
@@ -0,0 +1,26 @@
1
+ import { property } from '../object/property.mjs';
2
+ import { matches } from '../predicate/matches.mjs';
3
+ import { matchesProperty } from '../predicate/matchesProperty.mjs';
4
+
5
+ function findIndex(source, doesMatch) {
6
+ switch (typeof doesMatch) {
7
+ case 'function': {
8
+ return source.findIndex(doesMatch);
9
+ }
10
+ case 'object': {
11
+ if (Array.isArray(doesMatch) && doesMatch.length === 2) {
12
+ const key = doesMatch[0];
13
+ const value = doesMatch[1];
14
+ return source.findIndex(matchesProperty(key, value));
15
+ }
16
+ else {
17
+ return source.findIndex(matches(doesMatch));
18
+ }
19
+ }
20
+ case 'string': {
21
+ return source.findIndex(property(doesMatch));
22
+ }
23
+ }
24
+ }
25
+
26
+ export { findIndex };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Finds the index of the first occurrence of a value in an array.
3
+ *
4
+ * This method is similar to `Array.prototype.indexOf`, but it also finds `NaN` values.
5
+ * It uses strict equality (`===`) to compare elements.
6
+ *
7
+ * @template T - The type of elements in the array.
8
+ * @template U - The type of the value to search for.
9
+ * @param {T[] | null | undefined} array - The array to search.
10
+ * @param {T} searchElement - The value to search for.
11
+ * @param {number} [fromIndex] - The index to start the search at.
12
+ * @returns {number} The index (zero-based) of the first occurrence of the value in the array, or `-1` if the value is not found.
13
+ *
14
+ * @example
15
+ * const array = [1, 2, 3, NaN];
16
+ * indexOf(array, 3); // => 2
17
+ * indexOf(array, NaN); // => 3
18
+ */
19
+ declare function indexOf<T>(array: T[] | null | undefined, searchElement: T, fromIndex?: number): number;
20
+
21
+ export { indexOf };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Finds the index of the first occurrence of a value in an array.
3
+ *
4
+ * This method is similar to `Array.prototype.indexOf`, but it also finds `NaN` values.
5
+ * It uses strict equality (`===`) to compare elements.
6
+ *
7
+ * @template T - The type of elements in the array.
8
+ * @template U - The type of the value to search for.
9
+ * @param {T[] | null | undefined} array - The array to search.
10
+ * @param {T} searchElement - The value to search for.
11
+ * @param {number} [fromIndex] - The index to start the search at.
12
+ * @returns {number} The index (zero-based) of the first occurrence of the value in the array, or `-1` if the value is not found.
13
+ *
14
+ * @example
15
+ * const array = [1, 2, 3, NaN];
16
+ * indexOf(array, 3); // => 2
17
+ * indexOf(array, NaN); // => 3
18
+ */
19
+ declare function indexOf<T>(array: T[] | null | undefined, searchElement: T, fromIndex?: number): number;
20
+
21
+ export { indexOf };
@@ -0,0 +1,20 @@
1
+ function indexOf(array, searchElement, fromIndex) {
2
+ if (array == null) {
3
+ return -1;
4
+ }
5
+ if (Number.isNaN(searchElement)) {
6
+ fromIndex = fromIndex ?? 0;
7
+ if (fromIndex < 0) {
8
+ fromIndex = Math.max(0, array.length + fromIndex);
9
+ }
10
+ for (let i = fromIndex; i < array.length; i++) {
11
+ if (Number.isNaN(array[i])) {
12
+ return i;
13
+ }
14
+ }
15
+ return -1;
16
+ }
17
+ return array.indexOf(searchElement, fromIndex);
18
+ }
19
+
20
+ export { indexOf };
@@ -3,9 +3,10 @@
3
3
  *
4
4
  * @template F - The type of the function.
5
5
  * @param {F} func - The function to cap arguments for.
6
+ * @param guard
6
7
  * @param {number} n - The arity cap.
7
8
  * @returns {(...args: any[]) => ReturnType<F>} Returns the new capped function.
8
9
  */
9
- declare function ary<F extends (...args: any[]) => any>(func: F, n?: number, guard?: unknown): ((...args: any[]) => ReturnType<F>);
10
+ declare function ary<F extends (...args: any[]) => any>(func: F, n?: number, guard?: unknown): (...args: any[]) => ReturnType<F>;
10
11
 
11
12
  export { ary };
@@ -3,9 +3,10 @@
3
3
  *
4
4
  * @template F - The type of the function.
5
5
  * @param {F} func - The function to cap arguments for.
6
+ * @param guard
6
7
  * @param {number} n - The arity cap.
7
8
  * @returns {(...args: any[]) => ReturnType<F>} Returns the new capped function.
8
9
  */
9
- declare function ary<F extends (...args: any[]) => any>(func: F, n?: number, guard?: unknown): ((...args: any[]) => ReturnType<F>);
10
+ declare function ary<F extends (...args: any[]) => any>(func: F, n?: number, guard?: unknown): (...args: any[]) => ReturnType<F>;
10
11
 
11
12
  export { ary };
@@ -8,6 +8,8 @@
8
8
  * @param {(...args: any[]) => any} func The function to bind.
9
9
  * @param {any} thisArg The `this` binding of `func`.
10
10
  * @param {any[]} partials The arguments to be partially applied.
11
+ * @param thisObj
12
+ * @param {...any} partialArgs
11
13
  * @returns {(...args: any[]) => any} Returns the new bound function.
12
14
  *
13
15
  * @example
@@ -23,7 +25,7 @@
23
25
  * bound('hi');
24
26
  * // => 'hi fred!'
25
27
  */
26
- declare function bind<F extends Function>(func: F, thisObj?: unknown, ...partialArgs: any[]): F;
28
+ declare function bind<F extends (...args: any[]) => any>(func: F, thisObj?: unknown, ...partialArgs: any[]): F;
27
29
  declare namespace bind {
28
30
  var placeholder: typeof bindPlaceholder;
29
31
  }
@@ -8,6 +8,8 @@
8
8
  * @param {(...args: any[]) => any} func The function to bind.
9
9
  * @param {any} thisArg The `this` binding of `func`.
10
10
  * @param {any[]} partials The arguments to be partially applied.
11
+ * @param thisObj
12
+ * @param {...any} partialArgs
11
13
  * @returns {(...args: any[]) => any} Returns the new bound function.
12
14
  *
13
15
  * @example
@@ -23,7 +25,7 @@
23
25
  * bound('hi');
24
26
  * // => 'hi fred!'
25
27
  */
26
- declare function bind<F extends Function>(func: F, thisObj?: unknown, ...partialArgs: any[]): F;
28
+ declare function bind<F extends (...args: any[]) => any>(func: F, thisObj?: unknown, ...partialArgs: any[]): F;
27
29
  declare namespace bind {
28
30
  var placeholder: typeof bindPlaceholder;
29
31
  }
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Creates a function that transforms the arguments of the provided function `func`.
3
+ * The transformed arguments are passed to `func` such that the arguments starting from a specified index
4
+ * are grouped into an array, while the previous arguments are passed as individual elements.
5
+ *
6
+ * @template F - The type of the function being transformed.
7
+ * @param {F} func - The function whose arguments are to be transformed.
8
+ * @param {number} [startIndex=func.length - 1] - The index from which to start grouping the remaining arguments into an array.
9
+ * Defaults to `func.length - 1`, grouping all arguments after the last parameter.
10
+ * @returns {(...args: any[]) => ReturnType<F>} A new function that, when called, returns the result of calling `func` with the transformed arguments.
11
+ *
12
+ * The transformed arguments are:
13
+ * - The first `start` arguments as individual elements.
14
+ * - The remaining arguments from index `start` onward grouped into an array.
15
+ * @example
16
+ * function fn(a, b, c) {
17
+ * return [a, b, c];
18
+ * }
19
+ *
20
+ * // Using default start index (func.length - 1, which is 2 in this case)
21
+ * const transformedFn = rest(fn);
22
+ * console.log(transformedFn(1, 2, 3, 4)); // [1, 2, [3, 4]]
23
+ *
24
+ * // Using start index 1
25
+ * const transformedFnWithStart = rest(fn, 1);
26
+ * console.log(transformedFnWithStart(1, 2, 3, 4)); // [1, [2, 3, 4]]
27
+ *
28
+ * // With fewer arguments than the start index
29
+ * console.log(transformedFn(1)); // [1, undefined, []]
30
+ */
31
+ declare function rest<F extends (...args: any[]) => any>(func: F, start?: number): (...args: any[]) => ReturnType<F>;
32
+
33
+ export { rest };
@@ -0,0 +1,33 @@
1
+ /**
2
+ * Creates a function that transforms the arguments of the provided function `func`.
3
+ * The transformed arguments are passed to `func` such that the arguments starting from a specified index
4
+ * are grouped into an array, while the previous arguments are passed as individual elements.
5
+ *
6
+ * @template F - The type of the function being transformed.
7
+ * @param {F} func - The function whose arguments are to be transformed.
8
+ * @param {number} [startIndex=func.length - 1] - The index from which to start grouping the remaining arguments into an array.
9
+ * Defaults to `func.length - 1`, grouping all arguments after the last parameter.
10
+ * @returns {(...args: any[]) => ReturnType<F>} A new function that, when called, returns the result of calling `func` with the transformed arguments.
11
+ *
12
+ * The transformed arguments are:
13
+ * - The first `start` arguments as individual elements.
14
+ * - The remaining arguments from index `start` onward grouped into an array.
15
+ * @example
16
+ * function fn(a, b, c) {
17
+ * return [a, b, c];
18
+ * }
19
+ *
20
+ * // Using default start index (func.length - 1, which is 2 in this case)
21
+ * const transformedFn = rest(fn);
22
+ * console.log(transformedFn(1, 2, 3, 4)); // [1, 2, [3, 4]]
23
+ *
24
+ * // Using start index 1
25
+ * const transformedFnWithStart = rest(fn, 1);
26
+ * console.log(transformedFnWithStart(1, 2, 3, 4)); // [1, [2, 3, 4]]
27
+ *
28
+ * // With fewer arguments than the start index
29
+ * console.log(transformedFn(1)); // [1, undefined, []]
30
+ */
31
+ declare function rest<F extends (...args: any[]) => any>(func: F, start?: number): (...args: any[]) => ReturnType<F>;
32
+
33
+ export { rest };