es-toolkit 1.15.1-dev.431 → 1.15.1-dev.433
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.
- package/dist/browser.global.js +1 -1
- package/dist/browser.global.js.map +1 -1
- package/dist/compat/array/findIndex.d.mts +62 -0
- package/dist/compat/array/findIndex.d.ts +62 -0
- package/dist/compat/array/findIndex.mjs +26 -0
- package/dist/compat/index.d.mts +1 -0
- package/dist/compat/index.d.ts +1 -0
- package/dist/compat/index.js +22 -0
- package/dist/compat/index.mjs +1 -0
- package/package.json +1 -1
|
@@ -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 };
|
package/dist/compat/index.d.mts
CHANGED
|
@@ -97,6 +97,7 @@ export { concat } from './array/concat.mjs';
|
|
|
97
97
|
export { difference } from './array/difference.mjs';
|
|
98
98
|
export { fill } from './array/fill.mjs';
|
|
99
99
|
export { find } from './array/find.mjs';
|
|
100
|
+
export { findIndex } from './array/findIndex.mjs';
|
|
100
101
|
export { flatten } from './array/flatten.mjs';
|
|
101
102
|
export { flattenDeep } from './array/flattenDeep.mjs';
|
|
102
103
|
export { flattenDepth } from './array/flattenDepth.mjs';
|
package/dist/compat/index.d.ts
CHANGED
|
@@ -97,6 +97,7 @@ export { concat } from './array/concat.js';
|
|
|
97
97
|
export { difference } from './array/difference.js';
|
|
98
98
|
export { fill } from './array/fill.js';
|
|
99
99
|
export { find } from './array/find.js';
|
|
100
|
+
export { findIndex } from './array/findIndex.js';
|
|
100
101
|
export { flatten } from './array/flatten.js';
|
|
101
102
|
export { flattenDeep } from './array/flattenDeep.js';
|
|
102
103
|
export { flattenDepth } from './array/flattenDepth.js';
|
package/dist/compat/index.js
CHANGED
|
@@ -369,6 +369,27 @@ function find(source, doesMatch) {
|
|
|
369
369
|
}
|
|
370
370
|
}
|
|
371
371
|
|
|
372
|
+
function findIndex(source, doesMatch) {
|
|
373
|
+
switch (typeof doesMatch) {
|
|
374
|
+
case 'function': {
|
|
375
|
+
return source.findIndex(doesMatch);
|
|
376
|
+
}
|
|
377
|
+
case 'object': {
|
|
378
|
+
if (Array.isArray(doesMatch) && doesMatch.length === 2) {
|
|
379
|
+
const key = doesMatch[0];
|
|
380
|
+
const value = doesMatch[1];
|
|
381
|
+
return source.findIndex(matchesProperty(key, value));
|
|
382
|
+
}
|
|
383
|
+
else {
|
|
384
|
+
return source.findIndex(matches(doesMatch));
|
|
385
|
+
}
|
|
386
|
+
}
|
|
387
|
+
case 'string': {
|
|
388
|
+
return source.findIndex(property(doesMatch));
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
372
393
|
function flatten(value, depth = 1) {
|
|
373
394
|
const result = [];
|
|
374
395
|
const flooredDepth = Math.floor(depth);
|
|
@@ -853,6 +874,7 @@ exports.difference = difference;
|
|
|
853
874
|
exports.endsWith = endsWith;
|
|
854
875
|
exports.fill = fill;
|
|
855
876
|
exports.find = find;
|
|
877
|
+
exports.findIndex = findIndex;
|
|
856
878
|
exports.flatten = flatten;
|
|
857
879
|
exports.flattenDeep = flattenDeep;
|
|
858
880
|
exports.flattenDepth = flattenDepth;
|
package/dist/compat/index.mjs
CHANGED
|
@@ -98,6 +98,7 @@ export { concat } from './array/concat.mjs';
|
|
|
98
98
|
export { difference } from './array/difference.mjs';
|
|
99
99
|
export { fill } from './array/fill.mjs';
|
|
100
100
|
export { find } from './array/find.mjs';
|
|
101
|
+
export { findIndex } from './array/findIndex.mjs';
|
|
101
102
|
export { flatten } from './array/flatten.mjs';
|
|
102
103
|
export { flattenDeep } from './array/flattenDeep.mjs';
|
|
103
104
|
export { flattenDepth } from './array/flattenDepth.mjs';
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "es-toolkit",
|
|
3
3
|
"description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
|
|
4
|
-
"version": "1.15.1-dev.
|
|
4
|
+
"version": "1.15.1-dev.433+c9587f38",
|
|
5
5
|
"homepage": "https://es-toolkit.slash.page",
|
|
6
6
|
"bugs": "https://github.com/toss/es-toolkit/issues",
|
|
7
7
|
"repository": {
|