es-toolkit 1.25.0-dev.791 → 1.25.0-dev.793

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.
@@ -2,7 +2,7 @@
2
2
  * Iterates through an array in reverse order and returns the index of the first item that matches the given predicate function.
3
3
  *
4
4
  * @template T
5
- * @param {T[]} arr - The array to search through.
5
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
6
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
7
  * @param {number} [fromIndex=arr.length - 1] - The index to start the search from, defaults to the last index of the array.
8
8
  * @returns {number} - The index of the first item that matches the predicate, or `undefined` if no match is found.
@@ -13,12 +13,12 @@
13
13
  * const result = findLastIndex(items, (item) => item > 3)
14
14
  * console.log(result); // 4
15
15
  */
16
- declare function findLastIndex<T>(arr: readonly T[], doesMatch: (item: T, index: number, arr: readonly T[]) => unknown, fromIndex?: number): number;
16
+ declare function findLastIndex<T>(arr: ArrayLike<T> | null | undefined, doesMatch: (item: T, index: number, arr: readonly T[]) => unknown, fromIndex?: number): number;
17
17
  /**
18
18
  * Finds the index of the first item in an array that matches the given partial object.
19
19
  *
20
20
  * @template T
21
- * @param {readonly T[]} arr - The array to search through.
21
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
22
22
  * @param {Partial<T>} doesMatch - A partial object that specifies the properties to match.
23
23
  * @param {number} [fromIndex=arr.length - 1] - The index to start the search from, defaults to the last index of the array.
24
24
  * @returns {number} - The index of the first item that matches the partial object, or `undefined` if no match is found.
@@ -29,12 +29,12 @@ declare function findLastIndex<T>(arr: readonly T[], doesMatch: (item: T, index:
29
29
  * const result = findLastIndex(items, { name: 'Bob' });
30
30
  * console.log(result); // 1
31
31
  */
32
- declare function findLastIndex<T>(arr: readonly T[], doesMatch: Partial<T>, fromIndex?: number): number;
32
+ declare function findLastIndex<T>(arr: ArrayLike<T> | null | undefined, doesMatch: Partial<T>, fromIndex?: number): number;
33
33
  /**
34
34
  * Finds the index of the first item in an array that matches a property with a specific value.
35
35
  *
36
36
  * @template T
37
- * @param {readonly T[]} arr - The array to search through.
37
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
38
38
  * @param {[keyof T, unknown]} doesMatchProperty - An array where the first element is the property key and the second element is the value to match.
39
39
  * @param {number} [fromIndex=arr.length - 1] - The index to start the search from, defaults to the last index of the array.
40
40
  * @returns {number} - The index of the first item that has the specified property value, or `undefined` if no match is found.
@@ -45,12 +45,12 @@ declare function findLastIndex<T>(arr: readonly T[], doesMatch: Partial<T>, from
45
45
  * const result = findLastIndex(items, ['name', 'Alice']);
46
46
  * console.log(result); // 0
47
47
  */
48
- declare function findLastIndex<T>(arr: readonly T[], doesMatchProperty: [keyof T, unknown], fromIndex?: number): number;
48
+ declare function findLastIndex<T>(arr: ArrayLike<T> | null | undefined, doesMatchProperty: [keyof T, unknown], fromIndex?: number): number;
49
49
  /**
50
50
  * Finds the index of the first item in an array that has a specific property, where the property name is provided as a string.
51
51
  *
52
52
  * @template T
53
- * @param {readonly T[]} arr - The array to search through.
53
+ * @param {ArrayLike<T> | null | undefined} arr - The array to search through.
54
54
  * @param {string} propertyToCheck - The property name to check.
55
55
  * @param {number} [fromIndex=arr.length - 1] - The index to start the search from, defaults to the last index of the array.
56
56
  * @returns {number} - The index of the first item that has the specified property, or `undefined` if no match is found.
@@ -61,6 +61,6 @@ declare function findLastIndex<T>(arr: readonly T[], doesMatchProperty: [keyof T
61
61
  * const result = findLastIndex(items, 'name');
62
62
  * console.log(result); // 1
63
63
  */
64
- declare function findLastIndex<T>(arr: readonly T[], propertyToCheck: string, fromIndex?: number): number;
64
+ declare function findLastIndex<T>(arr: ArrayLike<T> | null | undefined, propertyToCheck: string, fromIndex?: number): number;
65
65
 
66
66
  export { findLastIndex };
@@ -2,30 +2,33 @@ import { property } from '../object/property.mjs';
2
2
  import { matches } from '../predicate/matches.mjs';
3
3
  import { matchesProperty } from '../predicate/matchesProperty.mjs';
4
4
 
5
- function findLastIndex(arr, doesMatch, fromIndex = arr.length - 1) {
5
+ function findLastIndex(arr, doesMatch, fromIndex = arr ? arr.length - 1 : 0) {
6
+ if (!arr) {
7
+ return -1;
8
+ }
6
9
  if (fromIndex < 0) {
7
10
  fromIndex = Math.max(arr.length + fromIndex, 0);
8
11
  }
9
12
  else {
10
13
  fromIndex = Math.min(fromIndex, arr.length - 1);
11
14
  }
12
- arr = arr.slice(0, fromIndex + 1);
15
+ const subArray = Array.from(arr).slice(0, fromIndex + 1);
13
16
  switch (typeof doesMatch) {
14
17
  case 'function': {
15
- return arr.findLastIndex(doesMatch);
18
+ return subArray.findLastIndex(doesMatch);
16
19
  }
17
20
  case 'object': {
18
21
  if (Array.isArray(doesMatch) && doesMatch.length === 2) {
19
22
  const key = doesMatch[0];
20
23
  const value = doesMatch[1];
21
- return arr.findLastIndex(matchesProperty(key, value));
24
+ return subArray.findLastIndex(matchesProperty(key, value));
22
25
  }
23
26
  else {
24
- return arr.findLastIndex(matches(doesMatch));
27
+ return subArray.findLastIndex(matches(doesMatch));
25
28
  }
26
29
  }
27
30
  case 'string': {
28
- return arr.findLastIndex(property(doesMatch));
31
+ return subArray.findLastIndex(property(doesMatch));
29
32
  }
30
33
  }
31
34
  }
@@ -5,8 +5,7 @@
5
5
  * It uses strict equality (`===`) to compare elements.
6
6
  *
7
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.
8
+ * @param {ArrayLike<T> | null | undefined} array - The array to search.
10
9
  * @param {T} searchElement - The value to search for.
11
10
  * @param {number} [fromIndex] - The index to start the search at.
12
11
  * @returns {number} The index (zero-based) of the first occurrence of the value in the array, or `-1` if the value is not found.
@@ -16,6 +15,6 @@
16
15
  * indexOf(array, 3); // => 2
17
16
  * indexOf(array, NaN); // => 3
18
17
  */
19
- declare function indexOf<T>(array: readonly T[] | null | undefined, searchElement: T, fromIndex?: number): number;
18
+ declare function indexOf<T>(array: ArrayLike<T> | null | undefined, searchElement: T, fromIndex?: number): number;
20
19
 
21
20
  export { indexOf };
@@ -5,8 +5,7 @@
5
5
  * It uses strict equality (`===`) to compare elements.
6
6
  *
7
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.
8
+ * @param {ArrayLike<T> | null | undefined} array - The array to search.
10
9
  * @param {T} searchElement - The value to search for.
11
10
  * @param {number} [fromIndex] - The index to start the search at.
12
11
  * @returns {number} The index (zero-based) of the first occurrence of the value in the array, or `-1` if the value is not found.
@@ -16,6 +15,6 @@
16
15
  * indexOf(array, 3); // => 2
17
16
  * indexOf(array, NaN); // => 3
18
17
  */
19
- declare function indexOf<T>(array: readonly T[] | null | undefined, searchElement: T, fromIndex?: number): number;
18
+ declare function indexOf<T>(array: ArrayLike<T> | null | undefined, searchElement: T, fromIndex?: number): number;
20
19
 
21
20
  export { indexOf };
@@ -1,5 +1,7 @@
1
+ import { isArrayLike } from '../predicate/isArrayLike.mjs';
2
+
1
3
  function indexOf(array, searchElement, fromIndex) {
2
- if (array == null) {
4
+ if (!isArrayLike(array)) {
3
5
  return -1;
4
6
  }
5
7
  if (Number.isNaN(searchElement)) {
@@ -14,7 +16,7 @@ function indexOf(array, searchElement, fromIndex) {
14
16
  }
15
17
  return -1;
16
18
  }
17
- return array.indexOf(searchElement, fromIndex);
19
+ return Array.from(array).indexOf(searchElement, fromIndex);
18
20
  }
19
21
 
20
22
  export { indexOf };
@@ -2,7 +2,7 @@
2
2
  * Joins elements of an array into a string.
3
3
  *
4
4
  * @template T - The type of elements in the array.
5
- * @param {T[]} array - The array to join.
5
+ * @param {ArrayLike<T> | null | undefined} array - The array to join.
6
6
  * @param {string} separator - The separator used to join the elements, default is common separator `,`.
7
7
  * @returns {string} - Returns a string containing all elements of the array joined by the specified separator.
8
8
  *
@@ -11,6 +11,6 @@
11
11
  * const result = join(arr, "~");
12
12
  * console.log(result); // Output: "a~b~c"
13
13
  */
14
- declare function join<T>(array: readonly T[], separator?: string): string;
14
+ declare function join<T>(array: ArrayLike<T> | null | undefined, separator?: string): string;
15
15
 
16
16
  export { join };
@@ -2,7 +2,7 @@
2
2
  * Joins elements of an array into a string.
3
3
  *
4
4
  * @template T - The type of elements in the array.
5
- * @param {T[]} array - The array to join.
5
+ * @param {ArrayLike<T> | null | undefined} array - The array to join.
6
6
  * @param {string} separator - The separator used to join the elements, default is common separator `,`.
7
7
  * @returns {string} - Returns a string containing all elements of the array joined by the specified separator.
8
8
  *
@@ -11,6 +11,6 @@
11
11
  * const result = join(arr, "~");
12
12
  * console.log(result); // Output: "a~b~c"
13
13
  */
14
- declare function join<T>(array: readonly T[], separator?: string): string;
14
+ declare function join<T>(array: ArrayLike<T> | null | undefined, separator?: string): string;
15
15
 
16
16
  export { join };
@@ -1,5 +1,10 @@
1
+ import { isArrayLike } from '../predicate/isArrayLike.mjs';
2
+
1
3
  function join(array, separator = ',') {
2
- return array.join(separator);
4
+ if (!isArrayLike(array)) {
5
+ return '';
6
+ }
7
+ return Array.from(array).join(separator);
3
8
  }
4
9
 
5
10
  export { join };
@@ -8,7 +8,7 @@ const unary = require('../_chunk/unary-CMvKXy.js');
8
8
  const noop = require('../_chunk/noop-2IwLUk.js');
9
9
  const sumBy = require('../_chunk/sumBy-BkErWJ.js');
10
10
  const randomInt = require('../_chunk/randomInt-CF7bZK.js');
11
- const toMerged = require('../_chunk/toMerged-B5ZVux.js');
11
+ const toMerged = require('../_chunk/toMerged-BLnW4M.js');
12
12
  const isPlainObject$1 = require('../_chunk/isPlainObject-octpoD.js');
13
13
  const isWeakSet$1 = require('../_chunk/isWeakSet-BuFfzX.js');
14
14
  const upperFirst = require('../_chunk/upperFirst-BUECmK.js');
@@ -466,12 +466,9 @@ function identity(x) {
466
466
 
467
467
  function every(source, doesMatch) {
468
468
  if (!source) {
469
- source = [];
470
- }
471
- let values = source;
472
- if (!Array.isArray(source)) {
473
- values = Object.values(source);
469
+ return true;
474
470
  }
471
+ const values = Array.isArray(source) ? source : Object.values(source);
475
472
  if (!doesMatch) {
476
473
  doesMatch = identity;
477
474
  }
@@ -506,7 +503,17 @@ function every(source, doesMatch) {
506
503
  }
507
504
  }
508
505
 
509
- function fill(array, value, start = 0, end = array.length) {
506
+ function isString(value) {
507
+ return typeof value === 'string' || value instanceof String;
508
+ }
509
+
510
+ function fill(array, value, start = 0, end = array ? array.length : 0) {
511
+ if (!isArrayLike(array)) {
512
+ return [];
513
+ }
514
+ if (isString(array)) {
515
+ return array;
516
+ }
510
517
  start = Math.floor(start);
511
518
  end = Math.floor(end);
512
519
  if (!start) {
@@ -523,6 +530,9 @@ function isArray(value) {
523
530
  }
524
531
 
525
532
  function filter(source, predicate) {
533
+ if (!source) {
534
+ return [];
535
+ }
526
536
  if (!predicate) {
527
537
  predicate = identity;
528
538
  }
@@ -555,10 +565,10 @@ function filter(source, predicate) {
555
565
  }
556
566
 
557
567
  function find(source, doesMatch) {
558
- let values = source;
559
- if (!Array.isArray(source)) {
560
- values = Object.values(source);
568
+ if (!source) {
569
+ return undefined;
561
570
  }
571
+ const values = Array.isArray(source) ? source : Object.values(source);
562
572
  switch (typeof doesMatch) {
563
573
  case 'function': {
564
574
  if (!Array.isArray(source)) {
@@ -590,51 +600,65 @@ function find(source, doesMatch) {
590
600
  }
591
601
  }
592
602
 
593
- function findIndex(source, doesMatch) {
603
+ function findIndex(arr, doesMatch, fromIndex = 0) {
604
+ if (!arr) {
605
+ return -1;
606
+ }
607
+ if (fromIndex < 0) {
608
+ fromIndex = Math.max(arr.length + fromIndex, 0);
609
+ }
610
+ const subArray = Array.from(arr).slice(fromIndex);
611
+ let index = -1;
594
612
  switch (typeof doesMatch) {
595
613
  case 'function': {
596
- return source.findIndex(doesMatch);
614
+ index = subArray.findIndex(doesMatch);
615
+ break;
597
616
  }
598
617
  case 'object': {
599
618
  if (Array.isArray(doesMatch) && doesMatch.length === 2) {
600
619
  const key = doesMatch[0];
601
620
  const value = doesMatch[1];
602
- return source.findIndex(matchesProperty(key, value));
621
+ index = subArray.findIndex(matchesProperty(key, value));
603
622
  }
604
623
  else {
605
- return source.findIndex(matches(doesMatch));
624
+ index = subArray.findIndex(matches(doesMatch));
606
625
  }
626
+ break;
607
627
  }
608
628
  case 'string': {
609
- return source.findIndex(property(doesMatch));
629
+ index = subArray.findIndex(property(doesMatch));
610
630
  }
611
631
  }
632
+ return index === -1 ? -1 : index + fromIndex;
612
633
  }
613
634
 
614
- function findLastIndex(arr, doesMatch, fromIndex = arr.length - 1) {
635
+ function findLastIndex(arr, doesMatch, fromIndex = arr ? arr.length - 1 : 0) {
636
+ if (!arr) {
637
+ return -1;
638
+ }
615
639
  if (fromIndex < 0) {
616
640
  fromIndex = Math.max(arr.length + fromIndex, 0);
617
641
  }
618
642
  else {
619
643
  fromIndex = Math.min(fromIndex, arr.length - 1);
620
644
  }
621
- arr = arr.slice(0, fromIndex + 1);
645
+ const subArray = Array.from(arr).slice(0, fromIndex + 1);
622
646
  switch (typeof doesMatch) {
623
647
  case 'function': {
624
- return arr.findLastIndex(doesMatch);
648
+ return subArray.findLastIndex(doesMatch);
625
649
  }
626
650
  case 'object': {
627
651
  if (Array.isArray(doesMatch) && doesMatch.length === 2) {
628
652
  const key = doesMatch[0];
629
653
  const value = doesMatch[1];
630
- return arr.findLastIndex(matchesProperty(key, value));
654
+ return subArray.findLastIndex(matchesProperty(key, value));
631
655
  }
632
656
  else {
633
- return arr.findLastIndex(matches(doesMatch));
657
+ return subArray.findLastIndex(matches(doesMatch));
634
658
  }
635
659
  }
636
660
  case 'string': {
637
- return arr.findLastIndex(property(doesMatch));
661
+ return subArray.findLastIndex(property(doesMatch));
638
662
  }
639
663
  }
640
664
  }
@@ -683,10 +707,6 @@ function head(arr) {
683
707
  return zipWith.head(Array.from(arr));
684
708
  }
685
709
 
686
- function isString(value) {
687
- return typeof value === 'string' || value instanceof String;
688
- }
689
-
690
710
  function isSymbol(value) {
691
711
  return typeof value === 'symbol' || value instanceof Symbol;
692
712
  }
@@ -752,7 +772,7 @@ function includes(source, target, fromIndex, guard) {
752
772
  }
753
773
 
754
774
  function indexOf(array, searchElement, fromIndex) {
755
- if (array == null) {
775
+ if (!isArrayLike(array)) {
756
776
  return -1;
757
777
  }
758
778
  if (Number.isNaN(searchElement)) {
@@ -767,7 +787,7 @@ function indexOf(array, searchElement, fromIndex) {
767
787
  }
768
788
  return -1;
769
789
  }
770
- return array.indexOf(searchElement, fromIndex);
790
+ return Array.from(array).indexOf(searchElement, fromIndex);
771
791
  }
772
792
 
773
793
  function intersection(...arrays) {
@@ -789,7 +809,10 @@ function intersection(...arrays) {
789
809
  }
790
810
 
791
811
  function join(array, separator = ',') {
792
- return array.join(separator);
812
+ if (!isArrayLike(array)) {
813
+ return '';
814
+ }
815
+ return Array.from(array).join(separator);
793
816
  }
794
817
 
795
818
  function last(array) {
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ const noop = require('./_chunk/noop-2IwLUk.js');
11
11
  const sumBy = require('./_chunk/sumBy-BkErWJ.js');
12
12
  const randomInt = require('./_chunk/randomInt-CF7bZK.js');
13
13
  const math_index = require('./math/index.js');
14
- const toMerged = require('./_chunk/toMerged-B5ZVux.js');
14
+ const toMerged = require('./_chunk/toMerged-BLnW4M.js');
15
15
  const object_index = require('./object/index.js');
16
16
  const isWeakSet = require('./_chunk/isWeakSet-BuFfzX.js');
17
17
  const predicate_index = require('./predicate/index.js');
@@ -104,7 +104,10 @@ function copyProperties(target, source, stack) {
104
104
  const keys = [...Object.keys(source), ...getSymbols(source)];
105
105
  for (let i = 0; i < keys.length; i++) {
106
106
  const key = keys[i];
107
- target[key] = cloneDeepImpl(source[key], stack);
107
+ const descriptor = Object.getOwnPropertyDescriptor(target, key);
108
+ if (descriptor == null || descriptor.writable) {
109
+ target[key] = cloneDeepImpl(source[key], stack);
110
+ }
108
111
  }
109
112
  }
110
113
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const toMerged = require('../_chunk/toMerged-B5ZVux.js');
5
+ const toMerged = require('../_chunk/toMerged-BLnW4M.js');
6
6
 
7
7
  function mergeWith(target, source, merge) {
8
8
  const sourceKeys = Object.keys(source);
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.25.0-dev.791+3ed5a413",
4
+ "version": "1.25.0-dev.793+2f9740eb",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {