es-toolkit 1.15.1-dev.432 → 1.15.1-dev.434

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.
@@ -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 | U} 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, U>(array: T[] | null | undefined, searchElement: T | U, 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 | U} 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, U>(array: T[] | null | undefined, searchElement: T | U, 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 };
@@ -97,12 +97,14 @@ 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';
103
104
  export { orderBy } from './array/orderBy.mjs';
104
105
  export { size } from './array/size.mjs';
105
106
  export { zipObjectDeep } from './array/zipObjectDeep.mjs';
107
+ export { indexOf } from './array/indexOf.mjs';
106
108
  export { ary } from './function/ary.mjs';
107
109
  export { bind } from './function/bind.mjs';
108
110
  export { get } from './object/get.mjs';
@@ -97,12 +97,14 @@ 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';
103
104
  export { orderBy } from './array/orderBy.js';
104
105
  export { size } from './array/size.js';
105
106
  export { zipObjectDeep } from './array/zipObjectDeep.js';
107
+ export { indexOf } from './array/indexOf.js';
106
108
  export { ary } from './function/ary.js';
107
109
  export { bind } from './function/bind.js';
108
110
  export { get } from './object/get.js';
@@ -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);
@@ -525,6 +546,25 @@ function zipObjectDeep(keys, values) {
525
546
  return result;
526
547
  }
527
548
 
549
+ function indexOf(array, searchElement, fromIndex) {
550
+ if (array == null) {
551
+ return -1;
552
+ }
553
+ if (Number.isNaN(searchElement)) {
554
+ fromIndex = fromIndex ?? 0;
555
+ if (fromIndex < 0) {
556
+ fromIndex = Math.max(0, array.length + fromIndex);
557
+ }
558
+ for (let i = fromIndex; i < array.length; i++) {
559
+ if (Number.isNaN(array[i])) {
560
+ return i;
561
+ }
562
+ }
563
+ return -1;
564
+ }
565
+ return array.indexOf(searchElement, fromIndex);
566
+ }
567
+
528
568
  function ary(func, n = func.length, guard) {
529
569
  if (guard) {
530
570
  n = func.length;
@@ -853,11 +893,13 @@ exports.difference = difference;
853
893
  exports.endsWith = endsWith;
854
894
  exports.fill = fill;
855
895
  exports.find = find;
896
+ exports.findIndex = findIndex;
856
897
  exports.flatten = flatten;
857
898
  exports.flattenDeep = flattenDeep;
858
899
  exports.flattenDepth = flattenDepth;
859
900
  exports.get = get;
860
901
  exports.has = has;
902
+ exports.indexOf = indexOf;
861
903
  exports.isArguments = isArguments;
862
904
  exports.isArray = isArray;
863
905
  exports.isArrayLike = isArrayLike;
@@ -98,12 +98,14 @@ 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';
104
105
  export { orderBy } from './array/orderBy.mjs';
105
106
  export { size } from './array/size.mjs';
106
107
  export { zipObjectDeep } from './array/zipObjectDeep.mjs';
108
+ export { indexOf } from './array/indexOf.mjs';
107
109
  export { ary } from './function/ary.mjs';
108
110
  export { bind } from './function/bind.mjs';
109
111
  export { get } from './object/get.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.432+84827937",
4
+ "version": "1.15.1-dev.434+f55662d9",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {