es-toolkit 1.23.0 → 1.24.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 (135) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +1 -1
  3. package/dist/_chunk/{isWeakSet-BAMm2l.js → isWeakSet-Bd6nry.js} +59 -46
  4. package/dist/_chunk/{range-BXlMmn.js → sumBy-BkErWJ.js} +25 -5
  5. package/dist/_chunk/{isObjectLike-aywuSF.js → toMerged-DDLv0D.js} +88 -86
  6. package/dist/_chunk/{flowRight-Dv8J0U.js → unary-BZ5Ixo.js} +27 -64
  7. package/dist/_chunk/{zipWith-EOU_KZ.js → zipWith-wpjySR.js} +8 -17
  8. package/dist/array/dropRightWhile.d.mts +2 -2
  9. package/dist/array/dropRightWhile.d.ts +2 -2
  10. package/dist/array/dropRightWhile.mjs +1 -1
  11. package/dist/array/dropWhile.d.mts +1 -1
  12. package/dist/array/dropWhile.d.ts +1 -1
  13. package/dist/array/index.js +1 -1
  14. package/dist/array/unionBy.mjs +3 -10
  15. package/dist/browser.global.js +1 -1
  16. package/dist/browser.global.js.map +1 -1
  17. package/dist/compat/_internal/MAX_ARRAY_LENGTH.mjs +3 -0
  18. package/dist/compat/_internal/isIterateeCall.mjs +17 -0
  19. package/dist/compat/array/difference.d.mts +9 -3
  20. package/dist/compat/array/difference.d.ts +9 -3
  21. package/dist/compat/array/difference.mjs +12 -3
  22. package/dist/compat/array/dropRightWhile.d.mts +60 -0
  23. package/dist/compat/array/dropRightWhile.d.ts +60 -0
  24. package/dist/compat/array/dropRightWhile.mjs +27 -0
  25. package/dist/compat/array/every.mjs +4 -5
  26. package/dist/compat/array/filter.mjs +4 -5
  27. package/dist/compat/array/find.mjs +4 -5
  28. package/dist/compat/array/includes.mjs +2 -1
  29. package/dist/compat/array/slice.d.mts +18 -0
  30. package/dist/compat/array/slice.d.ts +18 -0
  31. package/dist/compat/array/slice.mjs +38 -0
  32. package/dist/compat/array/take.d.mts +25 -0
  33. package/dist/compat/array/take.d.ts +25 -0
  34. package/dist/compat/array/take.mjs +10 -0
  35. package/dist/compat/function/before.d.mts +26 -0
  36. package/dist/compat/function/before.d.ts +26 -0
  37. package/dist/compat/function/before.mjs +20 -0
  38. package/dist/compat/function/curryRight.d.mts +50 -0
  39. package/dist/compat/function/curryRight.d.ts +50 -0
  40. package/dist/compat/function/curryRight.mjs +64 -0
  41. package/dist/compat/index.d.mts +86 -75
  42. package/dist/compat/index.d.ts +86 -75
  43. package/dist/compat/index.js +779 -496
  44. package/dist/compat/index.mjs +86 -75
  45. package/dist/compat/object/defaults.d.mts +120 -0
  46. package/dist/compat/object/defaults.d.ts +120 -0
  47. package/dist/compat/object/defaults.mjs +21 -0
  48. package/dist/compat/object/invertBy.d.mts +27 -0
  49. package/dist/compat/object/invertBy.d.ts +27 -0
  50. package/dist/compat/object/invertBy.mjs +27 -0
  51. package/dist/compat/object/omit.d.mts +1 -1
  52. package/dist/compat/object/omit.d.ts +1 -1
  53. package/dist/compat/object/pick.d.mts +1 -1
  54. package/dist/compat/object/pick.d.ts +1 -1
  55. package/dist/compat/object/toDefaulted.d.mts +122 -0
  56. package/dist/compat/object/toDefaulted.d.ts +122 -0
  57. package/dist/compat/object/toDefaulted.mjs +9 -0
  58. package/dist/compat/predicate/isArrayBuffer.d.mts +20 -0
  59. package/dist/compat/predicate/isArrayBuffer.d.ts +20 -0
  60. package/dist/compat/predicate/isArrayBuffer.mjs +7 -0
  61. package/dist/compat/predicate/isDate.d.mts +16 -0
  62. package/dist/compat/predicate/isDate.d.ts +16 -0
  63. package/dist/compat/predicate/isDate.mjs +7 -0
  64. package/dist/compat/predicate/isEqualWith.d.mts +38 -0
  65. package/dist/compat/predicate/isEqualWith.d.ts +38 -0
  66. package/dist/compat/predicate/isEqualWith.mjs +23 -0
  67. package/dist/compat/predicate/isMap.d.mts +20 -0
  68. package/dist/compat/predicate/isMap.d.ts +20 -0
  69. package/dist/compat/predicate/isMap.mjs +7 -0
  70. package/dist/compat/predicate/isMatch.mjs +2 -1
  71. package/dist/compat/predicate/isSet.d.mts +20 -0
  72. package/dist/compat/predicate/isSet.d.ts +20 -0
  73. package/dist/compat/predicate/isSet.mjs +7 -0
  74. package/dist/compat/string/upperCase.mjs +0 -1
  75. package/dist/compat/util/constant.d.mts +16 -0
  76. package/dist/compat/util/constant.d.ts +16 -0
  77. package/dist/compat/util/constant.mjs +5 -0
  78. package/dist/compat/util/defaultTo.d.mts +17 -0
  79. package/dist/compat/util/defaultTo.d.ts +17 -0
  80. package/dist/compat/util/defaultTo.mjs +8 -0
  81. package/dist/compat/util/eq.d.mts +16 -0
  82. package/dist/compat/util/eq.d.ts +16 -0
  83. package/dist/compat/util/eq.mjs +5 -0
  84. package/dist/compat/util/times.d.mts +14 -0
  85. package/dist/compat/util/times.d.ts +14 -0
  86. package/dist/compat/util/times.mjs +15 -0
  87. package/dist/compat/util/toLength.d.mts +18 -0
  88. package/dist/compat/util/toLength.d.ts +18 -0
  89. package/dist/compat/util/toLength.mjs +12 -0
  90. package/dist/function/before.d.mts +2 -2
  91. package/dist/function/before.d.ts +2 -2
  92. package/dist/function/before.mjs +2 -2
  93. package/dist/function/index.d.mts +10 -10
  94. package/dist/function/index.d.ts +10 -10
  95. package/dist/function/index.js +65 -32
  96. package/dist/function/index.mjs +10 -10
  97. package/dist/function/once.d.mts +16 -1
  98. package/dist/function/once.d.ts +16 -1
  99. package/dist/function/once.mjs +5 -7
  100. package/dist/index.d.mts +43 -41
  101. package/dist/index.d.ts +43 -41
  102. package/dist/index.js +59 -57
  103. package/dist/index.mjs +43 -41
  104. package/dist/math/index.d.mts +2 -1
  105. package/dist/math/index.d.ts +2 -1
  106. package/dist/math/index.js +9 -8
  107. package/dist/math/index.mjs +2 -1
  108. package/dist/math/rangeRight.d.mts +38 -0
  109. package/dist/math/rangeRight.d.ts +38 -0
  110. package/dist/math/rangeRight.mjs +20 -0
  111. package/dist/object/index.d.mts +7 -7
  112. package/dist/object/index.d.ts +7 -7
  113. package/dist/object/index.js +32 -32
  114. package/dist/object/index.mjs +7 -7
  115. package/dist/object/omitBy.d.mts +1 -1
  116. package/dist/object/omitBy.d.ts +1 -1
  117. package/dist/object/omitBy.mjs +4 -3
  118. package/dist/object/pickBy.mjs +4 -3
  119. package/dist/predicate/index.d.mts +11 -10
  120. package/dist/predicate/index.d.ts +11 -10
  121. package/dist/predicate/index.js +8 -7
  122. package/dist/predicate/index.mjs +11 -10
  123. package/dist/predicate/isBlob.d.mts +19 -0
  124. package/dist/predicate/isBlob.d.ts +19 -0
  125. package/dist/predicate/isBlob.mjs +8 -0
  126. package/dist/predicate/isEqualWith.mjs +2 -1
  127. package/dist/promise/index.d.mts +1 -1
  128. package/dist/promise/index.d.ts +1 -1
  129. package/dist/promise/index.mjs +1 -1
  130. package/dist/string/index.d.mts +12 -12
  131. package/dist/string/index.d.ts +12 -12
  132. package/dist/string/index.js +20 -20
  133. package/dist/string/index.mjs +12 -12
  134. package/package.json +10 -6
  135. package/dist/_chunk/{pad-BWiShN.js → upperFirst-BUECmK.js} +84 -84
@@ -0,0 +1,3 @@
1
+ const MAX_ARRAY_LENGTH = 4_294_967_295;
2
+
3
+ export { MAX_ARRAY_LENGTH };
@@ -0,0 +1,17 @@
1
+ import { isIndex } from './isIndex.mjs';
2
+ import { isArrayLike } from '../predicate/isArrayLike.mjs';
3
+ import { isObject } from '../predicate/isObject.mjs';
4
+ import { eq } from '../util/eq.mjs';
5
+
6
+ function isIterateeCall(value, index, object) {
7
+ if (!isObject(object)) {
8
+ return false;
9
+ }
10
+ if ((typeof index === 'number' && isArrayLike(object) && isIndex(index) && index < object.length) ||
11
+ (typeof index === 'string' && index in object)) {
12
+ return eq(object[index], value);
13
+ }
14
+ return false;
15
+ }
16
+
17
+ export { isIterateeCall };
@@ -2,9 +2,9 @@
2
2
  * Computes the difference between an array and multiple arrays.
3
3
  *
4
4
  * @template T
5
- * @param {T[]} arr - The primary array from which to derive the difference. This is the main array
5
+ * @param {ArrayLike<T> | undefined | null} arr - The primary array from which to derive the difference. This is the main array
6
6
  * from which elements will be compared and filtered.
7
- * @param {...T[]} values - Multiple arrays containing elements to be excluded from the primary array.
7
+ * @param {Array<ArrayLike<T>>} values - Multiple arrays containing elements to be excluded from the primary array.
8
8
  * These arrays will be flattened into a single array, and each element in this array will be checked against the primary array.
9
9
  * If a match is found, that element will be excluded from the result.
10
10
  * @returns {T[]} A new array containing the elements that are present in the primary array but not
@@ -16,7 +16,13 @@
16
16
  * const array3 = [5, 6];
17
17
  * const result = difference(array1, array2, array3);
18
18
  * // result will be [1, 3] since 2, 4, and 5 are in the other arrays and are excluded from the result.
19
+ *
20
+ * @example
21
+ * const arrayLike1 = { 0: 1, 1: 2, 2: 3, length: 3 };
22
+ * const arrayLike2 = { 0: 2, 1: 4, length: 2 };
23
+ * const result = difference(arrayLike1, arrayLike2);
24
+ * // result will be [1, 3] since 2 is in both array-like objects and is excluded from the result.
19
25
  */
20
- declare function difference<T>(arr: readonly T[], ...values: Array<readonly T[]>): T[];
26
+ declare function difference<T>(arr: ArrayLike<T> | undefined | null, ...values: Array<ArrayLike<T>>): T[];
21
27
 
22
28
  export { difference };
@@ -2,9 +2,9 @@
2
2
  * Computes the difference between an array and multiple arrays.
3
3
  *
4
4
  * @template T
5
- * @param {T[]} arr - The primary array from which to derive the difference. This is the main array
5
+ * @param {ArrayLike<T> | undefined | null} arr - The primary array from which to derive the difference. This is the main array
6
6
  * from which elements will be compared and filtered.
7
- * @param {...T[]} values - Multiple arrays containing elements to be excluded from the primary array.
7
+ * @param {Array<ArrayLike<T>>} values - Multiple arrays containing elements to be excluded from the primary array.
8
8
  * These arrays will be flattened into a single array, and each element in this array will be checked against the primary array.
9
9
  * If a match is found, that element will be excluded from the result.
10
10
  * @returns {T[]} A new array containing the elements that are present in the primary array but not
@@ -16,7 +16,13 @@
16
16
  * const array3 = [5, 6];
17
17
  * const result = difference(array1, array2, array3);
18
18
  * // result will be [1, 3] since 2, 4, and 5 are in the other arrays and are excluded from the result.
19
+ *
20
+ * @example
21
+ * const arrayLike1 = { 0: 1, 1: 2, 2: 3, length: 3 };
22
+ * const arrayLike2 = { 0: 2, 1: 4, length: 2 };
23
+ * const result = difference(arrayLike1, arrayLike2);
24
+ * // result will be [1, 3] since 2 is in both array-like objects and is excluded from the result.
19
25
  */
20
- declare function difference<T>(arr: readonly T[], ...values: Array<readonly T[]>): T[];
26
+ declare function difference<T>(arr: ArrayLike<T> | undefined | null, ...values: Array<ArrayLike<T>>): T[];
21
27
 
22
28
  export { difference };
@@ -1,9 +1,18 @@
1
1
  import { difference as difference$1 } from '../../array/difference.mjs';
2
- import { flatten } from '../../array/flatten.mjs';
2
+ import { isArrayLikeObject } from '../predicate/isArrayLikeObject.mjs';
3
3
 
4
4
  function difference(arr, ...values) {
5
- const arr1 = arr;
6
- const arr2 = flatten(values);
5
+ if (!isArrayLikeObject(arr)) {
6
+ return [];
7
+ }
8
+ const arr1 = Array.from(arr);
9
+ const arr2 = [];
10
+ for (let i = 0; i < values.length; i++) {
11
+ const value = values[i];
12
+ if (isArrayLikeObject(value)) {
13
+ arr2.push(...Array.from(value));
14
+ }
15
+ }
7
16
  return difference$1(arr1, arr2);
8
17
  }
9
18
 
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Drops elements from the end of an array while the predicate function returns truthy.
3
+ *
4
+ * @template T - The type of elements in the array.
5
+ * @param {T[]} arr - The array from which to drop elements.
6
+ * @param {(item: T, index: number, arr: T[]) => unknown} canContinueDropping - A predicate function that determines
7
+ * whether to continue dropping elements. The function is called with each element, index, and array, and dropping
8
+ * continues as long as it returns true.
9
+ * @returns {T[]} A new array with the elements remaining after the predicate returns false.
10
+ *
11
+ * @example
12
+ * const array = [5, 4, 3, 2, 1];
13
+ * const result = dropRightWhile(array, x => x < 3);
14
+ * result will be [5, 4, 3] since elements less than 3 are dropped.
15
+ */
16
+ declare function dropRightWhile<T>(arr: readonly T[], canContinueDropping: (item: T, index: number, arr: readonly T[]) => unknown): T[];
17
+ /**
18
+ * Drops elements from the end of an array while the specified object properties match.
19
+ *
20
+ * @template T - The type of elements in the array.
21
+ * @param {T[]} arr - The array from which to drop elements.
22
+ * @param {Partial<T>} objectToDrop - An object specifying the properties to match for dropping elements.
23
+ * @returns {T[]} A new array with the elements remaining after the predicate returns false.
24
+ *
25
+ * @example
26
+ * const array = [{ a: 1 }, { a: 2 }, { a: 3 }];
27
+ * const result = dropRightWhile(array, { a: 3 });
28
+ * result will be [{ a: 1 }, { a: 2 }] since the last object matches the properties of the provided object.
29
+ */
30
+ declare function dropRightWhile<T>(arr: readonly T[], objectToDrop: Partial<T>): T[];
31
+ /**
32
+ * Drops elements from the end of an array while the specified property matches a given value.
33
+ *
34
+ * @template T - The type of elements in the array.
35
+ * @param {T[]} arr - The array from which to drop elements.
36
+ * @param {[keyof T, unknown]} propertyToDrop - A tuple containing the property key and the value to match for dropping elements.
37
+ * @returns {T[]} A new array with the elements remaining after the predicate returns false.
38
+ *
39
+ * @example
40
+ * const array = [{ id: 1 }, { id: 2 }, { id: 3 }];
41
+ * const result = dropRightWhile(array, ['id', 3]);
42
+ * result will be [{ id: 1 }, { id: 2 }] since the last object has the id property matching the value 3.
43
+ */
44
+ declare function dropRightWhile<T>(arr: readonly T[], propertyToDrop: [keyof T, unknown]): T[];
45
+ /**
46
+ * Drops elements from the end of an array while the specified property name matches.
47
+ *
48
+ * @template T - The type of elements in the array.
49
+ * @param {T[]} arr - The array from which to drop elements.
50
+ * @param {string} propertyToDrop - The name of the property to match for dropping elements.
51
+ * @returns {T[]} A new array with the elements remaining after the predicate returns false.
52
+ *
53
+ * @example
54
+ * const array = [{ isActive: false }, { isActive: true }, { isActive: true }];
55
+ * const result = dropRightWhile(array, 'isActive');
56
+ * result will be [{ isActive: false }] since it drops elements until it finds one with a falsy isActive property.
57
+ */
58
+ declare function dropRightWhile<T>(arr: readonly T[], propertyToDrop: string): T[];
59
+
60
+ export { dropRightWhile };
@@ -0,0 +1,60 @@
1
+ /**
2
+ * Drops elements from the end of an array while the predicate function returns truthy.
3
+ *
4
+ * @template T - The type of elements in the array.
5
+ * @param {T[]} arr - The array from which to drop elements.
6
+ * @param {(item: T, index: number, arr: T[]) => unknown} canContinueDropping - A predicate function that determines
7
+ * whether to continue dropping elements. The function is called with each element, index, and array, and dropping
8
+ * continues as long as it returns true.
9
+ * @returns {T[]} A new array with the elements remaining after the predicate returns false.
10
+ *
11
+ * @example
12
+ * const array = [5, 4, 3, 2, 1];
13
+ * const result = dropRightWhile(array, x => x < 3);
14
+ * result will be [5, 4, 3] since elements less than 3 are dropped.
15
+ */
16
+ declare function dropRightWhile<T>(arr: readonly T[], canContinueDropping: (item: T, index: number, arr: readonly T[]) => unknown): T[];
17
+ /**
18
+ * Drops elements from the end of an array while the specified object properties match.
19
+ *
20
+ * @template T - The type of elements in the array.
21
+ * @param {T[]} arr - The array from which to drop elements.
22
+ * @param {Partial<T>} objectToDrop - An object specifying the properties to match for dropping elements.
23
+ * @returns {T[]} A new array with the elements remaining after the predicate returns false.
24
+ *
25
+ * @example
26
+ * const array = [{ a: 1 }, { a: 2 }, { a: 3 }];
27
+ * const result = dropRightWhile(array, { a: 3 });
28
+ * result will be [{ a: 1 }, { a: 2 }] since the last object matches the properties of the provided object.
29
+ */
30
+ declare function dropRightWhile<T>(arr: readonly T[], objectToDrop: Partial<T>): T[];
31
+ /**
32
+ * Drops elements from the end of an array while the specified property matches a given value.
33
+ *
34
+ * @template T - The type of elements in the array.
35
+ * @param {T[]} arr - The array from which to drop elements.
36
+ * @param {[keyof T, unknown]} propertyToDrop - A tuple containing the property key and the value to match for dropping elements.
37
+ * @returns {T[]} A new array with the elements remaining after the predicate returns false.
38
+ *
39
+ * @example
40
+ * const array = [{ id: 1 }, { id: 2 }, { id: 3 }];
41
+ * const result = dropRightWhile(array, ['id', 3]);
42
+ * result will be [{ id: 1 }, { id: 2 }] since the last object has the id property matching the value 3.
43
+ */
44
+ declare function dropRightWhile<T>(arr: readonly T[], propertyToDrop: [keyof T, unknown]): T[];
45
+ /**
46
+ * Drops elements from the end of an array while the specified property name matches.
47
+ *
48
+ * @template T - The type of elements in the array.
49
+ * @param {T[]} arr - The array from which to drop elements.
50
+ * @param {string} propertyToDrop - The name of the property to match for dropping elements.
51
+ * @returns {T[]} A new array with the elements remaining after the predicate returns false.
52
+ *
53
+ * @example
54
+ * const array = [{ isActive: false }, { isActive: true }, { isActive: true }];
55
+ * const result = dropRightWhile(array, 'isActive');
56
+ * result will be [{ isActive: false }] since it drops elements until it finds one with a falsy isActive property.
57
+ */
58
+ declare function dropRightWhile<T>(arr: readonly T[], propertyToDrop: string): T[];
59
+
60
+ export { dropRightWhile };
@@ -0,0 +1,27 @@
1
+ import { dropRightWhile as dropRightWhile$1 } from '../../array/dropRightWhile.mjs';
2
+ import { property } from '../object/property.mjs';
3
+ import { matches } from '../predicate/matches.mjs';
4
+ import { matchesProperty } from '../predicate/matchesProperty.mjs';
5
+
6
+ function dropRightWhile(arr, predicate) {
7
+ switch (typeof predicate) {
8
+ case 'function': {
9
+ return dropRightWhile$1(arr, (item, index, arr) => Boolean(predicate(item, index, arr)));
10
+ }
11
+ case 'object': {
12
+ if (Array.isArray(predicate) && predicate.length === 2) {
13
+ const key = predicate[0];
14
+ const value = predicate[1];
15
+ return dropRightWhile$1(arr, matchesProperty(key, value));
16
+ }
17
+ else {
18
+ return dropRightWhile$1(arr, matches(predicate));
19
+ }
20
+ }
21
+ case 'string': {
22
+ return dropRightWhile$1(arr, property(predicate));
23
+ }
24
+ }
25
+ }
26
+
27
+ export { dropRightWhile };
@@ -17,11 +17,10 @@ function every(source, doesMatch) {
17
17
  switch (typeof doesMatch) {
18
18
  case 'function': {
19
19
  if (!Array.isArray(source)) {
20
- const entries = Object.entries(source);
21
- for (let i = 0; i < entries.length; i++) {
22
- const entry = entries[i];
23
- const key = entry[0];
24
- const value = entry[1];
20
+ const keys = Object.keys(source);
21
+ for (let i = 0; i < keys.length; i++) {
22
+ const key = keys[i];
23
+ const value = source[key];
25
24
  if (!doesMatch(value, key, source)) {
26
25
  return false;
27
26
  }
@@ -13,11 +13,10 @@ function filter(source, predicate) {
13
13
  case 'function': {
14
14
  if (!Array.isArray(source)) {
15
15
  const result = [];
16
- const entries = Object.entries(source);
17
- for (let i = 0; i < entries.length; i++) {
18
- const entry = entries[i];
19
- const key = entry[0];
20
- const value = entry[1];
16
+ const keys = Object.keys(source);
17
+ for (let i = 0; i < keys.length; i++) {
18
+ const key = keys[i];
19
+ const value = source[key];
21
20
  if (predicate(value, key, source)) {
22
21
  result.push(value);
23
22
  }
@@ -10,11 +10,10 @@ function find(source, doesMatch) {
10
10
  switch (typeof doesMatch) {
11
11
  case 'function': {
12
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];
13
+ const keys = Object.keys(source);
14
+ for (let i = 0; i < keys.length; i++) {
15
+ const key = keys[i];
16
+ const value = source[key];
18
17
  if (doesMatch(value, key, source)) {
19
18
  return value;
20
19
  }
@@ -1,4 +1,5 @@
1
1
  import { isString } from '../predicate/isString.mjs';
2
+ import { eq } from '../util/eq.mjs';
2
3
  import { toInteger } from '../util/toInteger.mjs';
3
4
 
4
5
  function includes(source, target, fromIndex, guard) {
@@ -29,7 +30,7 @@ function includes(source, target, fromIndex, guard) {
29
30
  }
30
31
  for (let i = fromIndex; i < keys.length; i++) {
31
32
  const value = Reflect.get(source, keys[i]);
32
- if (value === target || (Number.isNaN(value) && Number.isNaN(target))) {
33
+ if (eq(value, target)) {
33
34
  return true;
34
35
  }
35
36
  }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Create a slice of `array` from `start` up to, but not including, `end`.
3
+ *
4
+ * It does not return a dense array for sparse arrays unlike the native `Array.prototype.slice`.
5
+ *
6
+ * @template T - The type of the array elements.
7
+ * @param {T[]} array - The array to slice.
8
+ * @param {number} [start=0] - The start position.
9
+ * @param {number} [end=array.length] - The end position.
10
+ * @returns {T[]} - Returns the slice of `array`.
11
+ *
12
+ * @example
13
+ * slice([1, 2, 3], 1, 2); // => [2]
14
+ * slice(new Array(3)); // => [undefined, undefined, undefined]
15
+ */
16
+ declare function slice<T>(array: readonly T[], start?: number, end?: number): T[];
17
+
18
+ export { slice };
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Create a slice of `array` from `start` up to, but not including, `end`.
3
+ *
4
+ * It does not return a dense array for sparse arrays unlike the native `Array.prototype.slice`.
5
+ *
6
+ * @template T - The type of the array elements.
7
+ * @param {T[]} array - The array to slice.
8
+ * @param {number} [start=0] - The start position.
9
+ * @param {number} [end=array.length] - The end position.
10
+ * @returns {T[]} - Returns the slice of `array`.
11
+ *
12
+ * @example
13
+ * slice([1, 2, 3], 1, 2); // => [2]
14
+ * slice(new Array(3)); // => [undefined, undefined, undefined]
15
+ */
16
+ declare function slice<T>(array: readonly T[], start?: number, end?: number): T[];
17
+
18
+ export { slice };
@@ -0,0 +1,38 @@
1
+ import { isIterateeCall } from '../_internal/isIterateeCall.mjs';
2
+ import { toInteger } from '../util/toInteger.mjs';
3
+
4
+ function slice(array, start, end) {
5
+ if (array == null) {
6
+ return [];
7
+ }
8
+ const length = array.length;
9
+ if (end === undefined) {
10
+ end = length;
11
+ }
12
+ else if (typeof end !== 'number' && isIterateeCall(array, start, end)) {
13
+ start = 0;
14
+ end = length;
15
+ }
16
+ start = toInteger(start);
17
+ end = toInteger(end);
18
+ if (start < 0) {
19
+ start = Math.max(length + start, 0);
20
+ }
21
+ else {
22
+ start = Math.min(start, length);
23
+ }
24
+ if (end < 0) {
25
+ end = Math.max(length + end, 0);
26
+ }
27
+ else {
28
+ end = Math.min(end, length);
29
+ }
30
+ const resultLength = Math.max(end - start, 0);
31
+ const result = new Array(resultLength);
32
+ for (let i = 0; i < resultLength; ++i) {
33
+ result[i] = array[start + i];
34
+ }
35
+ return result;
36
+ }
37
+
38
+ export { slice };
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Returns a new array containing the first `count` elements from the input array `arr`.
3
+ * If `count` is greater than the length of `arr`, the entire array is returned.
4
+ *
5
+ * @template T - Type of elements in the input array.
6
+ *
7
+ * @param {T[]} arr - The array to take elements from.
8
+ * @param {number} count - The number of elements to take.
9
+ * @returns {T[]} A new array containing the first `count` elements from `arr`.
10
+ *
11
+ * @example
12
+ * // Returns [1, 2, 3]
13
+ * take([1, 2, 3, 4, 5], 3);
14
+ *
15
+ * @example
16
+ * // Returns ['a', 'b']
17
+ * take(['a', 'b', 'c'], 2);
18
+ *
19
+ * @example
20
+ * // Returns [1, 2, 3]
21
+ * take([1, 2, 3], 5);
22
+ */
23
+ declare function take<T>(arr: readonly T[], count: number): T[];
24
+
25
+ export { take };
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Returns a new array containing the first `count` elements from the input array `arr`.
3
+ * If `count` is greater than the length of `arr`, the entire array is returned.
4
+ *
5
+ * @template T - Type of elements in the input array.
6
+ *
7
+ * @param {T[]} arr - The array to take elements from.
8
+ * @param {number} count - The number of elements to take.
9
+ * @returns {T[]} A new array containing the first `count` elements from `arr`.
10
+ *
11
+ * @example
12
+ * // Returns [1, 2, 3]
13
+ * take([1, 2, 3, 4, 5], 3);
14
+ *
15
+ * @example
16
+ * // Returns ['a', 'b']
17
+ * take(['a', 'b', 'c'], 2);
18
+ *
19
+ * @example
20
+ * // Returns [1, 2, 3]
21
+ * take([1, 2, 3], 5);
22
+ */
23
+ declare function take<T>(arr: readonly T[], count: number): T[];
24
+
25
+ export { take };
@@ -0,0 +1,10 @@
1
+ import { take as take$1 } from '../../array/take.mjs';
2
+
3
+ function take(arr, count) {
4
+ if (count < 1) {
5
+ return [];
6
+ }
7
+ return take$1(arr, count);
8
+ }
9
+
10
+ export { take };
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Creates a function that invokes `func`, with the `this` binding and arguments
3
+ * of the created function, while it's called less than `n` times. Subsequent
4
+ * calls to the created function return the result of the last `func` invocation.
5
+ *
6
+ * @template F - The type of the function to be invoked.
7
+ * @param {number} n - The number of times the returned function is allowed to call `func` before stopping.
8
+ * - If `n` is 0, `func` will never be called.
9
+ * - If `n` is a positive integer, `func` will be called up to `n-1` times.
10
+ * @param {F} func - The function to be called with the limit applied.
11
+ * @returns {(...args: Parameters<F>) => ReturnType<F> } - A new function that:
12
+ * - Tracks the number of calls.
13
+ * - Invokes `func` until the `n-1`-th call.
14
+ * - Returns last result of `func`, if `n` is reached.
15
+ * @throws {TypeError} - If `func` is not a function.
16
+ * @example
17
+ * let count = 0;
18
+ * const before3 = before(3, () => ++count);
19
+ *
20
+ * before3(); // => 1
21
+ * before3(); // => 2
22
+ * before3(); // => 2
23
+ */
24
+ declare function before<F extends (...args: any[]) => any>(n: number, func: F): (...args: Parameters<F>) => ReturnType<F>;
25
+
26
+ export { before };
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Creates a function that invokes `func`, with the `this` binding and arguments
3
+ * of the created function, while it's called less than `n` times. Subsequent
4
+ * calls to the created function return the result of the last `func` invocation.
5
+ *
6
+ * @template F - The type of the function to be invoked.
7
+ * @param {number} n - The number of times the returned function is allowed to call `func` before stopping.
8
+ * - If `n` is 0, `func` will never be called.
9
+ * - If `n` is a positive integer, `func` will be called up to `n-1` times.
10
+ * @param {F} func - The function to be called with the limit applied.
11
+ * @returns {(...args: Parameters<F>) => ReturnType<F> } - A new function that:
12
+ * - Tracks the number of calls.
13
+ * - Invokes `func` until the `n-1`-th call.
14
+ * - Returns last result of `func`, if `n` is reached.
15
+ * @throws {TypeError} - If `func` is not a function.
16
+ * @example
17
+ * let count = 0;
18
+ * const before3 = before(3, () => ++count);
19
+ *
20
+ * before3(); // => 1
21
+ * before3(); // => 2
22
+ * before3(); // => 2
23
+ */
24
+ declare function before<F extends (...args: any[]) => any>(n: number, func: F): (...args: Parameters<F>) => ReturnType<F>;
25
+
26
+ export { before };
@@ -0,0 +1,20 @@
1
+ import { toInteger } from '../util/toInteger.mjs';
2
+
3
+ function before(n, func) {
4
+ if (typeof func !== 'function') {
5
+ throw new TypeError('Expected a function');
6
+ }
7
+ let result;
8
+ n = toInteger(n);
9
+ return function (...args) {
10
+ if (--n > 0) {
11
+ result = func.apply(this, args);
12
+ }
13
+ if (n <= 1 && func) {
14
+ func = undefined;
15
+ }
16
+ return result;
17
+ };
18
+ }
19
+
20
+ export { before };
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least `arity` number of arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on.
3
+ * The arity of `func` may be specified if `func.length` is not sufficient.
4
+ *
5
+ * Unlike `curry`, this function curries the function from right to left.
6
+ *
7
+ * The `curryRight.placeholder` value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
8
+ *
9
+ * Note: This method doesn't set the `length` property of curried functions.
10
+ *
11
+ * @param {(...args: any[]) => any} func - The function to curry.
12
+ * @param {number=func.length} arity - The arity of func.
13
+ * @param {unknown} guard - Enables use as an iteratee for methods like `Array#map`.
14
+ * @returns {((...args: any[]) => any) & { placeholder: typeof curryRight.placeholder }} - Returns the new curried function.
15
+ *
16
+ * @example
17
+ * const abc = function(a, b, c) {
18
+ * return Array.from(arguments);
19
+ * };
20
+ *
21
+ * let curried = curryRight(abc);
22
+ *
23
+ * curried(3)(2)(1);
24
+ * // => [1, 2, 3]
25
+ *
26
+ * curried(2, 3)(1);
27
+ * // => [1, 2, 3]
28
+ *
29
+ * curried(1, 2, 3);
30
+ * // => [1, 2, 3]
31
+ *
32
+ * // Curried with placeholders.
33
+ * curried(3)(curryRight.placeholder, 2)(1);
34
+ * // => [1, 2, 3]
35
+ *
36
+ * // Curried with arity.
37
+ * curried = curryRight(abc, 2);
38
+ *
39
+ * curried(2)(1);
40
+ * // => [1, 2]
41
+ */
42
+ declare function curryRight(func: (...args: any[]) => any, arity?: number, guard?: unknown): ((...args: any[]) => any) & {
43
+ placeholder: typeof curryRight.placeholder;
44
+ };
45
+ declare namespace curryRight {
46
+ var placeholder: typeof curryRightPlaceholder;
47
+ }
48
+ declare const curryRightPlaceholder: unique symbol;
49
+
50
+ export { curryRight };
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Creates a function that accepts arguments of `func` and either invokes `func` returning its result, if at least `arity` number of arguments have been provided, or returns a function that accepts the remaining `func` arguments, and so on.
3
+ * The arity of `func` may be specified if `func.length` is not sufficient.
4
+ *
5
+ * Unlike `curry`, this function curries the function from right to left.
6
+ *
7
+ * The `curryRight.placeholder` value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
8
+ *
9
+ * Note: This method doesn't set the `length` property of curried functions.
10
+ *
11
+ * @param {(...args: any[]) => any} func - The function to curry.
12
+ * @param {number=func.length} arity - The arity of func.
13
+ * @param {unknown} guard - Enables use as an iteratee for methods like `Array#map`.
14
+ * @returns {((...args: any[]) => any) & { placeholder: typeof curryRight.placeholder }} - Returns the new curried function.
15
+ *
16
+ * @example
17
+ * const abc = function(a, b, c) {
18
+ * return Array.from(arguments);
19
+ * };
20
+ *
21
+ * let curried = curryRight(abc);
22
+ *
23
+ * curried(3)(2)(1);
24
+ * // => [1, 2, 3]
25
+ *
26
+ * curried(2, 3)(1);
27
+ * // => [1, 2, 3]
28
+ *
29
+ * curried(1, 2, 3);
30
+ * // => [1, 2, 3]
31
+ *
32
+ * // Curried with placeholders.
33
+ * curried(3)(curryRight.placeholder, 2)(1);
34
+ * // => [1, 2, 3]
35
+ *
36
+ * // Curried with arity.
37
+ * curried = curryRight(abc, 2);
38
+ *
39
+ * curried(2)(1);
40
+ * // => [1, 2]
41
+ */
42
+ declare function curryRight(func: (...args: any[]) => any, arity?: number, guard?: unknown): ((...args: any[]) => any) & {
43
+ placeholder: typeof curryRight.placeholder;
44
+ };
45
+ declare namespace curryRight {
46
+ var placeholder: typeof curryRightPlaceholder;
47
+ }
48
+ declare const curryRightPlaceholder: unique symbol;
49
+
50
+ export { curryRight };