es-toolkit 1.26.1-dev.858 → 1.26.1-dev.860

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,106 @@
1
+ /**
2
+ * Iterates over each element of the array invoking the provided callback function for each element.
3
+ *
4
+ * @template K - The type of elements in the array.
5
+ * @template T - The type of the array.
6
+ * @param {T | null | undefined} array - The array to iterate over.
7
+ * @param {(value: K, index: number, array: T) => unknown} [callback] - The function invoked for each element.
8
+ * The callback function receives three arguments:
9
+ * - 'value': The current element being processed in the array.
10
+ * - 'index': The index of the current element being processed in the array.
11
+ * - 'array': The array 'forEach' was called upon.
12
+ * @returns {T} Returns the original array.
13
+ *
14
+ * @example
15
+ * forEach([1, 2, 3], (value, index, array) => console.log(value, index));
16
+ * // Output:
17
+ * // 1 0
18
+ * // 2 1
19
+ * // 3 2
20
+ *
21
+ */
22
+ declare function forEach<T>(array: T[], callback?: (value: T, index: number, array: T[]) => unknown): T[];
23
+ /**
24
+ * Iterates over each element of the array invoking the provided callback function for each element.
25
+ *
26
+ * @template K - The type of elements in the array.
27
+ * @template T - The type of the array.
28
+ * @param {T | null | undefined} array - The array to iterate over.
29
+ * @param {(value: K, index: number, array: T) => unknown} [callback] - The function invoked for each element.
30
+ * The callback function receives three arguments:
31
+ * - 'value': The current element being processed in the array.
32
+ * - 'index': The index of the current element being processed in the array.
33
+ * - 'array': The array 'forEach' was called upon.
34
+ * @returns {T} Returns the original array.
35
+ *
36
+ * @example
37
+ * forEach([1, 2, 3], (value, index, array) => console.log(value, index));
38
+ * // Output:
39
+ * // 1 0
40
+ * // 2 1
41
+ * // 3 2
42
+ *
43
+ */
44
+ declare function forEach<T>(array: readonly T[], callback?: (value: T, index: number, array: T[]) => unknown): readonly T[];
45
+ /**
46
+ * Iterates over each element of the array invoking the provided callback function for each element.
47
+ *
48
+ * @template T - The type of string.
49
+ * @param {T | null | undefined} string - The string to iterate over
50
+ * @param {(value: T, index: number, string: T) => unknown} [callback] - The function invoked for each char.
51
+ * The callback function receives three arguments:
52
+ * - 'char': The current char being processed in the string.
53
+ * - 'index': The index of the current char being processed in the string.
54
+ * - 'string': The string 'forEach' was called upon.
55
+ * @returns {T} Returns the original string.
56
+ *
57
+ * @example
58
+ * forEach('abc', (char, index, string) => console.log(char, index));
59
+ * // Output:
60
+ * // 'a' 0
61
+ * // 'b' 1
62
+ * // 'c' 2
63
+ */
64
+ declare function forEach<T extends string | null | undefined>(string: T, callback?: (char: string, index: number, string: string) => unknown): T;
65
+ /**
66
+ * Iterates over each element of the array invoking the provided callback function for each element.
67
+ *
68
+ * @template T - The type of elements in the array.
69
+ * @param { ArrayLike<T> } array - The array to iterate over.
70
+ * @param {(value: T, index: number, array: ArrayLike<T>) => unknown} [callback] - The function invoked for each element.
71
+ * The callback function receives three arguments:
72
+ * - 'value': The current element being processed in the array.
73
+ * - 'index': The index of the current element being processed in the array.
74
+ * - 'array': The array 'forEach' was called upon.
75
+ * @returns {T} Returns the original array.
76
+ *
77
+ * @example
78
+ * forEach([1, 2, 3], (value, index, array) => console.log(value, index));
79
+ * // Output:
80
+ * // 1 0
81
+ * // 2 1
82
+ * // 3 2
83
+ *
84
+ */
85
+ declare function forEach<T>(array: ArrayLike<T>, callback?: (value: T, index: number, array: ArrayLike<T>) => unknown): ArrayLike<T>;
86
+ /**
87
+ * Iterates over each element of the object invoking the provided callback function for each property.
88
+ *
89
+ * @template T - The type of object.
90
+ * @param {T} object - The object to iterate over
91
+ * @param {(value: T[keyof T], key: keyof T, object: T) => unknown} [callback] - The function invoked for each property.
92
+ * The callback function receives three arguments:
93
+ * - 'value': The current property being processed in the object.
94
+ * - 'key': The key of the current property being processed in the object.
95
+ * - 'object': The object 'forEach' was called upon.
96
+ * @returns {T} Returns the original object.
97
+ *
98
+ * @example
99
+ * forEach({'a': 1, 'b': 2 }, (value, key, object) => console.log(value, key));
100
+ * // Output:
101
+ * // 1 'a'
102
+ * // 2 'b'
103
+ */
104
+ declare function forEach<T extends object | null | undefined>(object: T, callback?: (value: T[keyof T], key: keyof T, object: T) => unknown): T;
105
+
106
+ export { forEach };
@@ -0,0 +1,106 @@
1
+ /**
2
+ * Iterates over each element of the array invoking the provided callback function for each element.
3
+ *
4
+ * @template K - The type of elements in the array.
5
+ * @template T - The type of the array.
6
+ * @param {T | null | undefined} array - The array to iterate over.
7
+ * @param {(value: K, index: number, array: T) => unknown} [callback] - The function invoked for each element.
8
+ * The callback function receives three arguments:
9
+ * - 'value': The current element being processed in the array.
10
+ * - 'index': The index of the current element being processed in the array.
11
+ * - 'array': The array 'forEach' was called upon.
12
+ * @returns {T} Returns the original array.
13
+ *
14
+ * @example
15
+ * forEach([1, 2, 3], (value, index, array) => console.log(value, index));
16
+ * // Output:
17
+ * // 1 0
18
+ * // 2 1
19
+ * // 3 2
20
+ *
21
+ */
22
+ declare function forEach<T>(array: T[], callback?: (value: T, index: number, array: T[]) => unknown): T[];
23
+ /**
24
+ * Iterates over each element of the array invoking the provided callback function for each element.
25
+ *
26
+ * @template K - The type of elements in the array.
27
+ * @template T - The type of the array.
28
+ * @param {T | null | undefined} array - The array to iterate over.
29
+ * @param {(value: K, index: number, array: T) => unknown} [callback] - The function invoked for each element.
30
+ * The callback function receives three arguments:
31
+ * - 'value': The current element being processed in the array.
32
+ * - 'index': The index of the current element being processed in the array.
33
+ * - 'array': The array 'forEach' was called upon.
34
+ * @returns {T} Returns the original array.
35
+ *
36
+ * @example
37
+ * forEach([1, 2, 3], (value, index, array) => console.log(value, index));
38
+ * // Output:
39
+ * // 1 0
40
+ * // 2 1
41
+ * // 3 2
42
+ *
43
+ */
44
+ declare function forEach<T>(array: readonly T[], callback?: (value: T, index: number, array: T[]) => unknown): readonly T[];
45
+ /**
46
+ * Iterates over each element of the array invoking the provided callback function for each element.
47
+ *
48
+ * @template T - The type of string.
49
+ * @param {T | null | undefined} string - The string to iterate over
50
+ * @param {(value: T, index: number, string: T) => unknown} [callback] - The function invoked for each char.
51
+ * The callback function receives three arguments:
52
+ * - 'char': The current char being processed in the string.
53
+ * - 'index': The index of the current char being processed in the string.
54
+ * - 'string': The string 'forEach' was called upon.
55
+ * @returns {T} Returns the original string.
56
+ *
57
+ * @example
58
+ * forEach('abc', (char, index, string) => console.log(char, index));
59
+ * // Output:
60
+ * // 'a' 0
61
+ * // 'b' 1
62
+ * // 'c' 2
63
+ */
64
+ declare function forEach<T extends string | null | undefined>(string: T, callback?: (char: string, index: number, string: string) => unknown): T;
65
+ /**
66
+ * Iterates over each element of the array invoking the provided callback function for each element.
67
+ *
68
+ * @template T - The type of elements in the array.
69
+ * @param { ArrayLike<T> } array - The array to iterate over.
70
+ * @param {(value: T, index: number, array: ArrayLike<T>) => unknown} [callback] - The function invoked for each element.
71
+ * The callback function receives three arguments:
72
+ * - 'value': The current element being processed in the array.
73
+ * - 'index': The index of the current element being processed in the array.
74
+ * - 'array': The array 'forEach' was called upon.
75
+ * @returns {T} Returns the original array.
76
+ *
77
+ * @example
78
+ * forEach([1, 2, 3], (value, index, array) => console.log(value, index));
79
+ * // Output:
80
+ * // 1 0
81
+ * // 2 1
82
+ * // 3 2
83
+ *
84
+ */
85
+ declare function forEach<T>(array: ArrayLike<T>, callback?: (value: T, index: number, array: ArrayLike<T>) => unknown): ArrayLike<T>;
86
+ /**
87
+ * Iterates over each element of the object invoking the provided callback function for each property.
88
+ *
89
+ * @template T - The type of object.
90
+ * @param {T} object - The object to iterate over
91
+ * @param {(value: T[keyof T], key: keyof T, object: T) => unknown} [callback] - The function invoked for each property.
92
+ * The callback function receives three arguments:
93
+ * - 'value': The current property being processed in the object.
94
+ * - 'key': The key of the current property being processed in the object.
95
+ * - 'object': The object 'forEach' was called upon.
96
+ * @returns {T} Returns the original object.
97
+ *
98
+ * @example
99
+ * forEach({'a': 1, 'b': 2 }, (value, key, object) => console.log(value, key));
100
+ * // Output:
101
+ * // 1 'a'
102
+ * // 2 'b'
103
+ */
104
+ declare function forEach<T extends object | null | undefined>(object: T, callback?: (value: T[keyof T], key: keyof T, object: T) => unknown): T;
105
+
106
+ export { forEach };
@@ -0,0 +1,21 @@
1
+ import { identity } from '../../function/identity.mjs';
2
+ import { range } from '../../math/range.mjs';
3
+ import { isArrayLike } from '../predicate/isArrayLike.mjs';
4
+
5
+ function forEach(collection, callback = identity) {
6
+ if (!collection) {
7
+ return collection;
8
+ }
9
+ const keys = isArrayLike(collection) || Array.isArray(collection) ? range(0, collection.length) : Object.keys(collection);
10
+ for (let i = 0; i < keys.length; i++) {
11
+ const key = keys[i];
12
+ const value = collection[key];
13
+ const result = callback(value, key, collection);
14
+ if (result === false) {
15
+ break;
16
+ }
17
+ }
18
+ return collection;
19
+ }
20
+
21
+ export { forEach };
@@ -96,6 +96,7 @@ export { findLastIndex } from './array/findLastIndex.mjs';
96
96
  export { flatten } from './array/flatten.mjs';
97
97
  export { flattenDeep } from './array/flattenDeep.mjs';
98
98
  export { flattenDepth } from './array/flattenDepth.mjs';
99
+ export { forEach as each, forEach } from './array/forEach.mjs';
99
100
  export { head as first, head } from './array/head.mjs';
100
101
  export { includes } from './array/includes.mjs';
101
102
  export { indexOf } from './array/indexOf.mjs';
@@ -96,6 +96,7 @@ export { findLastIndex } from './array/findLastIndex.js';
96
96
  export { flatten } from './array/flatten.js';
97
97
  export { flattenDeep } from './array/flattenDeep.js';
98
98
  export { flattenDepth } from './array/flattenDepth.js';
99
+ export { forEach as each, forEach } from './array/forEach.js';
99
100
  export { head as first, head } from './array/head.js';
100
101
  export { includes } from './array/includes.js';
101
102
  export { indexOf } from './array/indexOf.js';
@@ -785,6 +785,22 @@ function flattenDepth(value, depth = 1) {
785
785
  return flatten(value, depth);
786
786
  }
787
787
 
788
+ function forEach(collection, callback = unary.identity) {
789
+ if (!collection) {
790
+ return collection;
791
+ }
792
+ const keys = isArrayLike(collection) || Array.isArray(collection) ? rangeRight.range(0, collection.length) : Object.keys(collection);
793
+ for (let i = 0; i < keys.length; i++) {
794
+ const key = keys[i];
795
+ const value = collection[key];
796
+ const result = callback(value, key, collection);
797
+ if (result === false) {
798
+ break;
799
+ }
800
+ }
801
+ return collection;
802
+ }
803
+
788
804
  function head(arr) {
789
805
  if (!isArrayLike(arr)) {
790
806
  return undefined;
@@ -2585,6 +2601,7 @@ exports.drop = drop;
2585
2601
  exports.dropRight = dropRight;
2586
2602
  exports.dropRightWhile = dropRightWhile;
2587
2603
  exports.dropWhile = dropWhile;
2604
+ exports.each = forEach;
2588
2605
  exports.endsWith = endsWith;
2589
2606
  exports.escape = escape;
2590
2607
  exports.every = every;
@@ -2601,6 +2618,7 @@ exports.flip = flip;
2601
2618
  exports.floor = floor;
2602
2619
  exports.flow = flow;
2603
2620
  exports.flowRight = flowRight;
2621
+ exports.forEach = forEach;
2604
2622
  exports.fromPairs = fromPairs;
2605
2623
  exports.get = get;
2606
2624
  exports.has = has;
@@ -98,6 +98,7 @@ export { findLastIndex } from './array/findLastIndex.mjs';
98
98
  export { flatten } from './array/flatten.mjs';
99
99
  export { flattenDeep } from './array/flattenDeep.mjs';
100
100
  export { flattenDepth } from './array/flattenDepth.mjs';
101
+ export { forEach as each, forEach } from './array/forEach.mjs';
101
102
  export { head as first, head } from './array/head.mjs';
102
103
  export { includes } from './array/includes.mjs';
103
104
  export { indexOf } from './array/indexOf.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.26.1-dev.858+cbecb215",
4
+ "version": "1.26.1-dev.860+67ea6bfe",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {