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

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';
@@ -118,37 +118,68 @@ function toKey(value) {
118
118
  return value.toString();
119
119
  }
120
120
 
121
- const DOTS_KEY = /^[\w.]+$/g;
122
- const ESCAPE_REGEXP = /\\(\\)?/g;
123
- const PROPERTY_REGEXP = RegExp('[^.[\\]]+' +
124
- '|' +
125
- '\\[(?:' +
126
- '([^"\'][^[]*)' +
127
- '|' +
128
- '(["\'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2' +
129
- ')\\]' +
130
- '|' +
131
- '(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))', 'g');
132
121
  function toPath(deepKey) {
133
- if (DOTS_KEY.test(deepKey)) {
134
- return deepKey.split('.');
135
- }
136
122
  const result = [];
137
- if (deepKey[0] === '.') {
123
+ const length = deepKey.length;
124
+ if (length === 0) {
125
+ return result;
126
+ }
127
+ let index = 0;
128
+ let key = '';
129
+ let quoteChar = '';
130
+ let bracket = false;
131
+ if (deepKey.charCodeAt(0) === 46) {
138
132
  result.push('');
133
+ index++;
139
134
  }
140
- const matches = deepKey.matchAll(PROPERTY_REGEXP);
141
- for (const match of matches) {
142
- let key = match[0];
143
- const expr = match[1];
144
- const quote = match[2];
145
- const substr = match[3];
146
- if (quote) {
147
- key = substr.replace(ESCAPE_REGEXP, '$1');
135
+ while (index < length) {
136
+ const char = deepKey[index];
137
+ if (quoteChar) {
138
+ if (char === '\\' && index + 1 < length) {
139
+ index++;
140
+ key += deepKey[index];
141
+ }
142
+ else if (char === quoteChar) {
143
+ quoteChar = '';
144
+ }
145
+ else {
146
+ key += char;
147
+ }
148
148
  }
149
- else if (expr) {
150
- key = expr;
149
+ else if (bracket) {
150
+ if (char === '"' || char === "'") {
151
+ quoteChar = char;
152
+ }
153
+ else if (char === ']') {
154
+ bracket = false;
155
+ result.push(key);
156
+ key = '';
157
+ }
158
+ else {
159
+ key += char;
160
+ }
161
+ }
162
+ else {
163
+ if (char === '[') {
164
+ bracket = true;
165
+ if (key) {
166
+ result.push(key);
167
+ key = '';
168
+ }
169
+ }
170
+ else if (char === '.') {
171
+ if (key) {
172
+ result.push(key);
173
+ key = '';
174
+ }
175
+ }
176
+ else {
177
+ key += char;
178
+ }
151
179
  }
180
+ index++;
181
+ }
182
+ if (key) {
152
183
  result.push(key);
153
184
  }
154
185
  return result;
@@ -785,6 +816,22 @@ function flattenDepth(value, depth = 1) {
785
816
  return flatten(value, depth);
786
817
  }
787
818
 
819
+ function forEach(collection, callback = unary.identity) {
820
+ if (!collection) {
821
+ return collection;
822
+ }
823
+ const keys = isArrayLike(collection) || Array.isArray(collection) ? rangeRight.range(0, collection.length) : Object.keys(collection);
824
+ for (let i = 0; i < keys.length; i++) {
825
+ const key = keys[i];
826
+ const value = collection[key];
827
+ const result = callback(value, key, collection);
828
+ if (result === false) {
829
+ break;
830
+ }
831
+ }
832
+ return collection;
833
+ }
834
+
788
835
  function head(arr) {
789
836
  if (!isArrayLike(arr)) {
790
837
  return undefined;
@@ -2585,6 +2632,7 @@ exports.drop = drop;
2585
2632
  exports.dropRight = dropRight;
2586
2633
  exports.dropRightWhile = dropRightWhile;
2587
2634
  exports.dropWhile = dropWhile;
2635
+ exports.each = forEach;
2588
2636
  exports.endsWith = endsWith;
2589
2637
  exports.escape = escape;
2590
2638
  exports.every = every;
@@ -2601,6 +2649,7 @@ exports.flip = flip;
2601
2649
  exports.floor = floor;
2602
2650
  exports.flow = flow;
2603
2651
  exports.flowRight = flowRight;
2652
+ exports.forEach = forEach;
2604
2653
  exports.fromPairs = fromPairs;
2605
2654
  exports.get = get;
2606
2655
  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';
@@ -1,34 +1,65 @@
1
- const DOTS_KEY = /^[\w.]+$/g;
2
- const ESCAPE_REGEXP = /\\(\\)?/g;
3
- const PROPERTY_REGEXP = RegExp('[^.[\\]]+' +
4
- '|' +
5
- '\\[(?:' +
6
- '([^"\'][^[]*)' +
7
- '|' +
8
- '(["\'])((?:(?!\\2)[^\\\\]|\\\\.)*?)\\2' +
9
- ')\\]' +
10
- '|' +
11
- '(?=(?:\\.|\\[\\])(?:\\.|\\[\\]|$))', 'g');
12
1
  function toPath(deepKey) {
13
- if (DOTS_KEY.test(deepKey)) {
14
- return deepKey.split('.');
15
- }
16
2
  const result = [];
17
- if (deepKey[0] === '.') {
3
+ const length = deepKey.length;
4
+ if (length === 0) {
5
+ return result;
6
+ }
7
+ let index = 0;
8
+ let key = '';
9
+ let quoteChar = '';
10
+ let bracket = false;
11
+ if (deepKey.charCodeAt(0) === 46) {
18
12
  result.push('');
13
+ index++;
19
14
  }
20
- const matches = deepKey.matchAll(PROPERTY_REGEXP);
21
- for (const match of matches) {
22
- let key = match[0];
23
- const expr = match[1];
24
- const quote = match[2];
25
- const substr = match[3];
26
- if (quote) {
27
- key = substr.replace(ESCAPE_REGEXP, '$1');
15
+ while (index < length) {
16
+ const char = deepKey[index];
17
+ if (quoteChar) {
18
+ if (char === '\\' && index + 1 < length) {
19
+ index++;
20
+ key += deepKey[index];
21
+ }
22
+ else if (char === quoteChar) {
23
+ quoteChar = '';
24
+ }
25
+ else {
26
+ key += char;
27
+ }
28
28
  }
29
- else if (expr) {
30
- key = expr;
29
+ else if (bracket) {
30
+ if (char === '"' || char === "'") {
31
+ quoteChar = char;
32
+ }
33
+ else if (char === ']') {
34
+ bracket = false;
35
+ result.push(key);
36
+ key = '';
37
+ }
38
+ else {
39
+ key += char;
40
+ }
31
41
  }
42
+ else {
43
+ if (char === '[') {
44
+ bracket = true;
45
+ if (key) {
46
+ result.push(key);
47
+ key = '';
48
+ }
49
+ }
50
+ else if (char === '.') {
51
+ if (key) {
52
+ result.push(key);
53
+ key = '';
54
+ }
55
+ }
56
+ else {
57
+ key += char;
58
+ }
59
+ }
60
+ index++;
61
+ }
62
+ if (key) {
32
63
  result.push(key);
33
64
  }
34
65
  return result;
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.861+c739a739",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {