es-toolkit 1.45.1 → 1.46.0-dev.1786

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 (50) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/README.md +18 -1
  3. package/dist/_internal/globalThis.js +14 -0
  4. package/dist/_internal/globalThis.mjs +10 -0
  5. package/dist/array/fill.d.mts +12 -15
  6. package/dist/array/fill.d.ts +12 -15
  7. package/dist/browser.global.js +1 -1
  8. package/dist/compat/array/dropWhile.d.mts +2 -2
  9. package/dist/compat/array/dropWhile.d.ts +2 -2
  10. package/dist/compat/array/invokeMap.js +1 -0
  11. package/dist/compat/array/invokeMap.mjs +1 -0
  12. package/dist/compat/array/lastIndexOf.d.mts +1 -1
  13. package/dist/compat/array/lastIndexOf.d.ts +1 -1
  14. package/dist/compat/array/sortedLastIndex.d.mts +1 -1
  15. package/dist/compat/array/sortedLastIndex.d.ts +1 -1
  16. package/dist/compat/object/mergeWith.js +2 -1
  17. package/dist/compat/object/mergeWith.mjs +2 -1
  18. package/dist/compat/object/setWith.d.mts +2 -2
  19. package/dist/compat/object/setWith.d.ts +2 -2
  20. package/dist/compat/predicate/isEmpty.js +2 -1
  21. package/dist/compat/predicate/isEmpty.mjs +2 -1
  22. package/dist/compat/util/over.d.mts +1 -1
  23. package/dist/compat/util/over.d.ts +1 -1
  24. package/dist/error/AbortError.d.mts +2 -2
  25. package/dist/error/AbortError.d.ts +2 -2
  26. package/dist/error/AbortError.js +1 -2
  27. package/dist/error/AbortError.mjs +1 -2
  28. package/dist/error/TimeoutError.d.mts +3 -3
  29. package/dist/error/TimeoutError.d.ts +3 -3
  30. package/dist/error/TimeoutError.js +1 -2
  31. package/dist/error/TimeoutError.mjs +1 -2
  32. package/dist/function/memoize.d.mts +1 -1
  33. package/dist/function/memoize.d.ts +1 -1
  34. package/dist/function/partial.d.mts +69 -0
  35. package/dist/function/partial.d.ts +69 -0
  36. package/dist/map/index.d.mts +1 -0
  37. package/dist/map/index.d.ts +1 -0
  38. package/dist/map/index.js +2 -0
  39. package/dist/map/index.mjs +1 -0
  40. package/dist/map/keyBy.d.mts +31 -0
  41. package/dist/map/keyBy.d.ts +31 -0
  42. package/dist/map/keyBy.js +14 -0
  43. package/dist/map/keyBy.mjs +10 -0
  44. package/dist/object/cloneDeepWith.js +2 -1
  45. package/dist/object/cloneDeepWith.mjs +2 -1
  46. package/dist/predicate/isBuffer.js +3 -1
  47. package/dist/predicate/isBuffer.mjs +3 -1
  48. package/dist/predicate/isEqualWith.js +2 -1
  49. package/dist/predicate/isEqualWith.mjs +2 -1
  50. package/package.json +1 -1
@@ -5,6 +5,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5
5
  const isArguments = require('./isArguments.js');
6
6
  const isArrayLike = require('./isArrayLike.js');
7
7
  const isTypedArray = require('./isTypedArray.js');
8
+ const isBuffer = require('../../predicate/isBuffer.js');
8
9
  const isPrototype = require('../_internal/isPrototype.js');
9
10
 
10
11
  function isEmpty(value) {
@@ -14,7 +15,7 @@ function isEmpty(value) {
14
15
  if (isArrayLike.isArrayLike(value)) {
15
16
  if (typeof value.splice !== 'function' &&
16
17
  typeof value !== 'string' &&
17
- (typeof Buffer === 'undefined' || !Buffer.isBuffer(value)) &&
18
+ !isBuffer.isBuffer(value) &&
18
19
  !isTypedArray.isTypedArray(value) &&
19
20
  !isArguments.isArguments(value)) {
20
21
  return false;
@@ -1,6 +1,7 @@
1
1
  import { isArguments } from './isArguments.mjs';
2
2
  import { isArrayLike } from './isArrayLike.mjs';
3
3
  import { isTypedArray } from './isTypedArray.mjs';
4
+ import { isBuffer } from '../../predicate/isBuffer.mjs';
4
5
  import { isPrototype } from '../_internal/isPrototype.mjs';
5
6
 
6
7
  function isEmpty(value) {
@@ -10,7 +11,7 @@ function isEmpty(value) {
10
11
  if (isArrayLike(value)) {
11
12
  if (typeof value.splice !== 'function' &&
12
13
  typeof value !== 'string' &&
13
- (typeof Buffer === 'undefined' || !Buffer.isBuffer(value)) &&
14
+ !isBuffer(value) &&
14
15
  !isTypedArray(value) &&
15
16
  !isArguments(value)) {
16
17
  return false;
@@ -18,7 +18,7 @@
18
18
  *
19
19
  * const func = over([{ a: 1 }, { b: 2 }]);
20
20
  * func({ a: 1, b: 2 });
21
- * // => [true, false]
21
+ * // => [true, true]
22
22
  *
23
23
  * const func = over([['a', 1], ['b', 2]]);
24
24
  * func({ a: 1, b: 2 });
@@ -18,7 +18,7 @@
18
18
  *
19
19
  * const func = over([{ a: 1 }, { b: 2 }]);
20
20
  * func({ a: 1, b: 2 });
21
- * // => [true, false]
21
+ * // => [true, true]
22
22
  *
23
23
  * const func = over([['a', 1], ['b', 2]]);
24
24
  * func({ a: 1, b: 2 });
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * An error class representing an aborted operation.
3
- * @augments Error
3
+ * @augments DOMException
4
4
  */
5
- declare class AbortError extends Error {
5
+ declare class AbortError extends DOMException {
6
6
  constructor(message?: string);
7
7
  }
8
8
 
@@ -1,8 +1,8 @@
1
1
  /**
2
2
  * An error class representing an aborted operation.
3
- * @augments Error
3
+ * @augments DOMException
4
4
  */
5
- declare class AbortError extends Error {
5
+ declare class AbortError extends DOMException {
6
6
  constructor(message?: string);
7
7
  }
8
8
 
@@ -2,10 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- class AbortError extends Error {
5
+ class AbortError extends DOMException {
6
6
  constructor(message = 'The operation was aborted') {
7
7
  super(message);
8
- this.name = 'AbortError';
9
8
  }
10
9
  }
11
10
 
@@ -1,7 +1,6 @@
1
- class AbortError extends Error {
1
+ class AbortError extends DOMException {
2
2
  constructor(message = 'The operation was aborted') {
3
3
  super(message);
4
- this.name = 'AbortError';
5
4
  }
6
5
  }
7
6
 
@@ -1,8 +1,8 @@
1
1
  /**
2
- * An error class representing an timeout operation.
3
- * @augments Error
2
+ * An error class representing a timeout operation.
3
+ * @augments DOMException
4
4
  */
5
- declare class TimeoutError extends Error {
5
+ declare class TimeoutError extends DOMException {
6
6
  constructor(message?: string);
7
7
  }
8
8
 
@@ -1,8 +1,8 @@
1
1
  /**
2
- * An error class representing an timeout operation.
3
- * @augments Error
2
+ * An error class representing a timeout operation.
3
+ * @augments DOMException
4
4
  */
5
- declare class TimeoutError extends Error {
5
+ declare class TimeoutError extends DOMException {
6
6
  constructor(message?: string);
7
7
  }
8
8
 
@@ -2,10 +2,9 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- class TimeoutError extends Error {
5
+ class TimeoutError extends DOMException {
6
6
  constructor(message = 'The operation was timed out') {
7
7
  super(message);
8
- this.name = 'TimeoutError';
9
8
  }
10
9
  }
11
10
 
@@ -1,7 +1,6 @@
1
- class TimeoutError extends Error {
1
+ class TimeoutError extends DOMException {
2
2
  constructor(message = 'The operation was timed out') {
3
3
  super(message);
4
- this.name = 'TimeoutError';
5
4
  }
6
5
  }
7
6
 
@@ -68,7 +68,7 @@
68
68
  * const memoizedSumWithCustomCache = memoize(sum, { cache: customCache });
69
69
  * console.log(memoizedSumWithCustomCache([1, 2])); // 3
70
70
  * console.log(memoizedSumWithCustomCache([1, 2])); // 3 (cached result)
71
- * console.log(memoizedAddWithCustomCache.cache.size); // 1
71
+ * console.log(memoizedSumWithCustomCache.cache.size); // 1
72
72
  */
73
73
  declare function memoize<F extends (...args: any) => any>(fn: F, options?: {
74
74
  cache?: MemoizeCache<any, ReturnType<F>>;
@@ -68,7 +68,7 @@
68
68
  * const memoizedSumWithCustomCache = memoize(sum, { cache: customCache });
69
69
  * console.log(memoizedSumWithCustomCache([1, 2])); // 3
70
70
  * console.log(memoizedSumWithCustomCache([1, 2])); // 3 (cached result)
71
- * console.log(memoizedAddWithCustomCache.cache.size); // 1
71
+ * console.log(memoizedSumWithCustomCache.cache.size); // 1
72
72
  */
73
73
  declare function memoize<F extends (...args: any) => any>(fn: F, options?: {
74
74
  cache?: MemoizeCache<any, ReturnType<F>>;
@@ -58,6 +58,27 @@ declare function partial<T1, T2, R>(func: (arg1: T1, arg2: T2) => R, arg1: T1):
58
58
  * console.log(greetWithHello('Hello')); // => 'Hello, John!'
59
59
  */
60
60
  declare function partial<T1, T2, R>(func: (arg1: T1, arg2: T2) => R, placeholder: Placeholder, arg2: T2): (arg1: T1) => R;
61
+ /**
62
+ * Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
63
+ *
64
+ * The partial.placeholder value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
65
+ *
66
+ * Note: This method doesn't set the `length` property of partially applied functions.
67
+ *
68
+ * @template T1 The type of the first argument.
69
+ * @template T2 The type of the second argument.
70
+ * @template R The return type of the function.
71
+ * @param {function(arg1: T1, arg2: T2): R} func The function to partially apply.
72
+ * @param {T1} arg1 The first argument to apply.
73
+ * @param {T2} arg2 The second argument to apply.
74
+ * @returns {function(): R} A new function that takes no arguments and returns the result of the original function.
75
+ *
76
+ * @example
77
+ * const add = (x: number, y: number) => x + y;
78
+ * const addThree = partial(add, 1, 2);
79
+ * console.log(addThree()); // => 3
80
+ */
81
+ declare function partial<T1, T2, R>(func: (arg1: T1, arg2: T2) => R, arg1: T1, arg2: T2): () => R;
61
82
  /**
62
83
  * Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
63
84
  *
@@ -170,6 +191,29 @@ declare function partial<T1, T2, T3, R>(func: (arg1: T1, arg2: T2, arg3: T3) =>
170
191
  * console.log(greetWithPlaceholder('John')); // => 'Hello, John!'
171
192
  */
172
193
  declare function partial<T1, T2, T3, R>(func: (arg1: T1, arg2: T2, arg3: T3) => R, plc1: Placeholder, arg2: T2, arg3: T3): (arg1: T1) => R;
194
+ /**
195
+ * Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
196
+ *
197
+ * The partial.placeholder value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
198
+ *
199
+ * Note: This method doesn't set the `length` property of partially applied functions.
200
+ *
201
+ * @template T1 The type of the first argument.
202
+ * @template T2 The type of the second argument.
203
+ * @template T3 The type of the third argument.
204
+ * @template R The return type of the function.
205
+ * @param {function(arg1: T1, arg2: T2, arg3: T3): R} func The function to partially apply.
206
+ * @param {T1} arg1 The first argument to apply.
207
+ * @param {T2} arg2 The second argument to apply.
208
+ * @param {T3} arg3 The third argument to apply.
209
+ * @returns {function(): R} A new function that takes no arguments and returns the result of the original function.
210
+ *
211
+ * @example
212
+ * const sum = (a: number, b: number, c: number) => a + b + c;
213
+ * const sumAll = partial(sum, 1, 2, 3);
214
+ * console.log(sumAll()); // => 6
215
+ */
216
+ declare function partial<T1, T2, T3, R>(func: (arg1: T1, arg2: T2, arg3: T3) => R, arg1: T1, arg2: T2, arg3: T3): () => R;
173
217
  /**
174
218
  * Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
175
219
  *
@@ -414,6 +458,31 @@ declare function partial<T1, T2, T3, T4, R>(func: (arg1: T1, arg2: T2, arg3: T3,
414
458
  * @returns {function(arg1: T1): R} A new function that takes the first argument and returns the result of the original function.
415
459
  */
416
460
  declare function partial<T1, T2, T3, T4, R>(func: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => R, arg1: Placeholder, arg2: T2, arg3: T3, arg4: T4): (arg1: T1) => R;
461
+ /**
462
+ * Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
463
+ *
464
+ * The partial.placeholder value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
465
+ *
466
+ * Note: This method doesn't set the `length` property of partially applied functions.
467
+ *
468
+ * @template T1 The type of the first argument.
469
+ * @template T2 The type of the second argument.
470
+ * @template T3 The type of the third argument.
471
+ * @template T4 The type of the fourth argument.
472
+ * @template R The return type of the function.
473
+ * @param {function(arg1: T1, arg2: T2, arg3: T3, arg4: T4): R} func The function to partially apply.
474
+ * @param {T1} arg1 The first argument to apply.
475
+ * @param {T2} arg2 The second argument to apply.
476
+ * @param {T3} arg3 The third argument to apply.
477
+ * @param {T4} arg4 The fourth argument to apply.
478
+ * @returns {function(): R} A new function that takes no arguments and returns the result of the original function.
479
+ *
480
+ * @example
481
+ * const sumFour = (a: number, b: number, c: number, d: number) => a + b + c + d;
482
+ * const sumAll = partial(sumFour, 1, 2, 3, 4);
483
+ * console.log(sumAll()); // => 10
484
+ */
485
+ declare function partial<T1, T2, T3, T4, R>(func: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => R, arg1: T1, arg2: T2, arg3: T3, arg4: T4): () => R;
417
486
  /**
418
487
  * Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
419
488
  *
@@ -58,6 +58,27 @@ declare function partial<T1, T2, R>(func: (arg1: T1, arg2: T2) => R, arg1: T1):
58
58
  * console.log(greetWithHello('Hello')); // => 'Hello, John!'
59
59
  */
60
60
  declare function partial<T1, T2, R>(func: (arg1: T1, arg2: T2) => R, placeholder: Placeholder, arg2: T2): (arg1: T1) => R;
61
+ /**
62
+ * Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
63
+ *
64
+ * The partial.placeholder value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
65
+ *
66
+ * Note: This method doesn't set the `length` property of partially applied functions.
67
+ *
68
+ * @template T1 The type of the first argument.
69
+ * @template T2 The type of the second argument.
70
+ * @template R The return type of the function.
71
+ * @param {function(arg1: T1, arg2: T2): R} func The function to partially apply.
72
+ * @param {T1} arg1 The first argument to apply.
73
+ * @param {T2} arg2 The second argument to apply.
74
+ * @returns {function(): R} A new function that takes no arguments and returns the result of the original function.
75
+ *
76
+ * @example
77
+ * const add = (x: number, y: number) => x + y;
78
+ * const addThree = partial(add, 1, 2);
79
+ * console.log(addThree()); // => 3
80
+ */
81
+ declare function partial<T1, T2, R>(func: (arg1: T1, arg2: T2) => R, arg1: T1, arg2: T2): () => R;
61
82
  /**
62
83
  * Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
63
84
  *
@@ -170,6 +191,29 @@ declare function partial<T1, T2, T3, R>(func: (arg1: T1, arg2: T2, arg3: T3) =>
170
191
  * console.log(greetWithPlaceholder('John')); // => 'Hello, John!'
171
192
  */
172
193
  declare function partial<T1, T2, T3, R>(func: (arg1: T1, arg2: T2, arg3: T3) => R, plc1: Placeholder, arg2: T2, arg3: T3): (arg1: T1) => R;
194
+ /**
195
+ * Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
196
+ *
197
+ * The partial.placeholder value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
198
+ *
199
+ * Note: This method doesn't set the `length` property of partially applied functions.
200
+ *
201
+ * @template T1 The type of the first argument.
202
+ * @template T2 The type of the second argument.
203
+ * @template T3 The type of the third argument.
204
+ * @template R The return type of the function.
205
+ * @param {function(arg1: T1, arg2: T2, arg3: T3): R} func The function to partially apply.
206
+ * @param {T1} arg1 The first argument to apply.
207
+ * @param {T2} arg2 The second argument to apply.
208
+ * @param {T3} arg3 The third argument to apply.
209
+ * @returns {function(): R} A new function that takes no arguments and returns the result of the original function.
210
+ *
211
+ * @example
212
+ * const sum = (a: number, b: number, c: number) => a + b + c;
213
+ * const sumAll = partial(sum, 1, 2, 3);
214
+ * console.log(sumAll()); // => 6
215
+ */
216
+ declare function partial<T1, T2, T3, R>(func: (arg1: T1, arg2: T2, arg3: T3) => R, arg1: T1, arg2: T2, arg3: T3): () => R;
173
217
  /**
174
218
  * Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
175
219
  *
@@ -414,6 +458,31 @@ declare function partial<T1, T2, T3, T4, R>(func: (arg1: T1, arg2: T2, arg3: T3,
414
458
  * @returns {function(arg1: T1): R} A new function that takes the first argument and returns the result of the original function.
415
459
  */
416
460
  declare function partial<T1, T2, T3, T4, R>(func: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => R, arg1: Placeholder, arg2: T2, arg3: T3, arg4: T4): (arg1: T1) => R;
461
+ /**
462
+ * Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
463
+ *
464
+ * The partial.placeholder value, which defaults to a `symbol`, may be used as a placeholder for partially applied arguments.
465
+ *
466
+ * Note: This method doesn't set the `length` property of partially applied functions.
467
+ *
468
+ * @template T1 The type of the first argument.
469
+ * @template T2 The type of the second argument.
470
+ * @template T3 The type of the third argument.
471
+ * @template T4 The type of the fourth argument.
472
+ * @template R The return type of the function.
473
+ * @param {function(arg1: T1, arg2: T2, arg3: T3, arg4: T4): R} func The function to partially apply.
474
+ * @param {T1} arg1 The first argument to apply.
475
+ * @param {T2} arg2 The second argument to apply.
476
+ * @param {T3} arg3 The third argument to apply.
477
+ * @param {T4} arg4 The fourth argument to apply.
478
+ * @returns {function(): R} A new function that takes no arguments and returns the result of the original function.
479
+ *
480
+ * @example
481
+ * const sumFour = (a: number, b: number, c: number, d: number) => a + b + c + d;
482
+ * const sumAll = partial(sumFour, 1, 2, 3, 4);
483
+ * console.log(sumAll()); // => 10
484
+ */
485
+ declare function partial<T1, T2, T3, T4, R>(func: (arg1: T1, arg2: T2, arg3: T3, arg4: T4) => R, arg1: T1, arg2: T2, arg3: T3, arg4: T4): () => R;
417
486
  /**
418
487
  * Creates a function that invokes `func` with `partialArgs` prepended to the arguments it receives. This method is like `bind` except it does not alter the `this` binding.
419
488
  *
@@ -3,6 +3,7 @@ export { filter } from './filter.mjs';
3
3
  export { findKey } from './findKey.mjs';
4
4
  export { findValue } from './findValue.mjs';
5
5
  export { hasValue } from './hasValue.mjs';
6
+ export { keyBy } from './keyBy.mjs';
6
7
  export { mapKeys } from './mapKeys.mjs';
7
8
  export { mapValues } from './mapValues.mjs';
8
9
  export { reduce } from './reduce.mjs';
@@ -3,6 +3,7 @@ export { filter } from './filter.js';
3
3
  export { findKey } from './findKey.js';
4
4
  export { findValue } from './findValue.js';
5
5
  export { hasValue } from './hasValue.js';
6
+ export { keyBy } from './keyBy.js';
6
7
  export { mapKeys } from './mapKeys.js';
7
8
  export { mapValues } from './mapValues.js';
8
9
  export { reduce } from './reduce.js';
package/dist/map/index.js CHANGED
@@ -7,6 +7,7 @@ const filter = require('./filter.js');
7
7
  const findKey = require('./findKey.js');
8
8
  const findValue = require('./findValue.js');
9
9
  const hasValue = require('./hasValue.js');
10
+ const keyBy = require('./keyBy.js');
10
11
  const mapKeys = require('./mapKeys.js');
11
12
  const mapValues = require('./mapValues.js');
12
13
  const reduce = require('./reduce.js');
@@ -19,6 +20,7 @@ exports.filter = filter.filter;
19
20
  exports.findKey = findKey.findKey;
20
21
  exports.findValue = findValue.findValue;
21
22
  exports.hasValue = hasValue.hasValue;
23
+ exports.keyBy = keyBy.keyBy;
22
24
  exports.mapKeys = mapKeys.mapKeys;
23
25
  exports.mapValues = mapValues.mapValues;
24
26
  exports.reduce = reduce.reduce;
@@ -3,6 +3,7 @@ export { filter } from './filter.mjs';
3
3
  export { findKey } from './findKey.mjs';
4
4
  export { findValue } from './findValue.mjs';
5
5
  export { hasValue } from './hasValue.mjs';
6
+ export { keyBy } from './keyBy.mjs';
6
7
  export { mapKeys } from './mapKeys.mjs';
7
8
  export { mapValues } from './mapValues.mjs';
8
9
  export { reduce } from './reduce.mjs';
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Maps each entry of a Map based on a provided key-generating function.
3
+ *
4
+ * This function takes a Map and a function that generates a key from each value-key pair.
5
+ * It returns a new Map where the keys are generated by the key function and the values are
6
+ * the corresponding values from the original map. If multiple entries produce the same key,
7
+ * the last value encountered will be used.
8
+ *
9
+ * @template K - The type of keys in the original Map.
10
+ * @template V - The type of values in the original Map.
11
+ * @template K2 - The type of keys to produce in the returned Map.
12
+ * @param {Map<K, V>} map - The map of entries to be mapped.
13
+ * @param {(value: V, key: K, object: Map<K, V>) => K2} getKeyFromEntry - A function that generates a key from a value-key pair.
14
+ * @returns {Map<K2, V>} A Map where the generated keys are mapped to each entry's value.
15
+ *
16
+ * @example
17
+ * const map = new Map([
18
+ * ['x', { type: 'fruit', name: 'apple' }],
19
+ * ['y', { type: 'fruit', name: 'banana' }],
20
+ * ['z', { type: 'vegetable', name: 'carrot' }]
21
+ * ]);
22
+ * const result = keyBy(map, item => item.type);
23
+ * // result will be:
24
+ * // Map(2) {
25
+ * // 'fruit' => { type: 'fruit', name: 'banana' },
26
+ * // 'vegetable' => { type: 'vegetable', name: 'carrot' }
27
+ * // }
28
+ */
29
+ declare function keyBy<K, V, K2>(map: Map<K, V>, getKeyFromEntry: (value: V, key: K, object: Map<K, V>) => K2): Map<K2, V>;
30
+
31
+ export { keyBy };
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Maps each entry of a Map based on a provided key-generating function.
3
+ *
4
+ * This function takes a Map and a function that generates a key from each value-key pair.
5
+ * It returns a new Map where the keys are generated by the key function and the values are
6
+ * the corresponding values from the original map. If multiple entries produce the same key,
7
+ * the last value encountered will be used.
8
+ *
9
+ * @template K - The type of keys in the original Map.
10
+ * @template V - The type of values in the original Map.
11
+ * @template K2 - The type of keys to produce in the returned Map.
12
+ * @param {Map<K, V>} map - The map of entries to be mapped.
13
+ * @param {(value: V, key: K, object: Map<K, V>) => K2} getKeyFromEntry - A function that generates a key from a value-key pair.
14
+ * @returns {Map<K2, V>} A Map where the generated keys are mapped to each entry's value.
15
+ *
16
+ * @example
17
+ * const map = new Map([
18
+ * ['x', { type: 'fruit', name: 'apple' }],
19
+ * ['y', { type: 'fruit', name: 'banana' }],
20
+ * ['z', { type: 'vegetable', name: 'carrot' }]
21
+ * ]);
22
+ * const result = keyBy(map, item => item.type);
23
+ * // result will be:
24
+ * // Map(2) {
25
+ * // 'fruit' => { type: 'fruit', name: 'banana' },
26
+ * // 'vegetable' => { type: 'vegetable', name: 'carrot' }
27
+ * // }
28
+ */
29
+ declare function keyBy<K, V, K2>(map: Map<K, V>, getKeyFromEntry: (value: V, key: K, object: Map<K, V>) => K2): Map<K2, V>;
30
+
31
+ export { keyBy };
@@ -0,0 +1,14 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
+
5
+ function keyBy(map, getKeyFromEntry) {
6
+ const result = new Map();
7
+ for (const [key, value] of map) {
8
+ const newKey = getKeyFromEntry(value, key, map);
9
+ result.set(newKey, value);
10
+ }
11
+ return result;
12
+ }
13
+
14
+ exports.keyBy = keyBy;
@@ -0,0 +1,10 @@
1
+ function keyBy(map, getKeyFromEntry) {
2
+ const result = new Map();
3
+ for (const [key, value] of map) {
4
+ const newKey = getKeyFromEntry(value, key, map);
5
+ result.set(newKey, value);
6
+ }
7
+ return result;
8
+ }
9
+
10
+ export { keyBy };
@@ -5,6 +5,7 @@ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5
5
  const getSymbols = require('../compat/_internal/getSymbols.js');
6
6
  const getTag = require('../compat/_internal/getTag.js');
7
7
  const tags = require('../compat/_internal/tags.js');
8
+ const isBuffer = require('../predicate/isBuffer.js');
8
9
  const isPrimitive = require('../predicate/isPrimitive.js');
9
10
  const isTypedArray = require('../predicate/isTypedArray.js');
10
11
 
@@ -60,7 +61,7 @@ function cloneDeepWithImpl(valueToClone, keyToClone, objectToClone, stack = new
60
61
  }
61
62
  return result;
62
63
  }
63
- if (typeof Buffer !== 'undefined' && Buffer.isBuffer(valueToClone)) {
64
+ if (isBuffer.isBuffer(valueToClone)) {
64
65
  return valueToClone.subarray();
65
66
  }
66
67
  if (isTypedArray.isTypedArray(valueToClone)) {
@@ -1,6 +1,7 @@
1
1
  import { getSymbols } from '../compat/_internal/getSymbols.mjs';
2
2
  import { getTag } from '../compat/_internal/getTag.mjs';
3
3
  import { uint32ArrayTag, uint16ArrayTag, uint8ClampedArrayTag, uint8ArrayTag, symbolTag, stringTag, setTag, regexpTag, objectTag, numberTag, mapTag, int32ArrayTag, int16ArrayTag, int8ArrayTag, float64ArrayTag, float32ArrayTag, dateTag, booleanTag, dataViewTag, arrayBufferTag, arrayTag, argumentsTag } from '../compat/_internal/tags.mjs';
4
+ import { isBuffer } from '../predicate/isBuffer.mjs';
4
5
  import { isPrimitive } from '../predicate/isPrimitive.mjs';
5
6
  import { isTypedArray } from '../predicate/isTypedArray.mjs';
6
7
 
@@ -56,7 +57,7 @@ function cloneDeepWithImpl(valueToClone, keyToClone, objectToClone, stack = new
56
57
  }
57
58
  return result;
58
59
  }
59
- if (typeof Buffer !== 'undefined' && Buffer.isBuffer(valueToClone)) {
60
+ if (isBuffer(valueToClone)) {
60
61
  return valueToClone.subarray();
61
62
  }
62
63
  if (isTypedArray(valueToClone)) {
@@ -2,8 +2,10 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
+ const globalThis = require('../_internal/globalThis.js');
6
+
5
7
  function isBuffer(x) {
6
- return typeof Buffer !== 'undefined' && Buffer.isBuffer(x);
8
+ return typeof globalThis.globalThis.Buffer !== 'undefined' && globalThis.globalThis.Buffer.isBuffer(x);
7
9
  }
8
10
 
9
11
  exports.isBuffer = isBuffer;
@@ -1,5 +1,7 @@
1
+ import { globalThis as globalThis_ } from '../_internal/globalThis.mjs';
2
+
1
3
  function isBuffer(x) {
2
- return typeof Buffer !== 'undefined' && Buffer.isBuffer(x);
4
+ return typeof globalThis_.Buffer !== 'undefined' && globalThis_.Buffer.isBuffer(x);
3
5
  }
4
6
 
5
7
  export { isBuffer };
@@ -2,6 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
+ const isBuffer = require('./isBuffer.js');
5
6
  const isPlainObject = require('./isPlainObject.js');
6
7
  const getSymbols = require('../compat/_internal/getSymbols.js');
7
8
  const getTag = require('../compat/_internal/getTag.js');
@@ -123,7 +124,7 @@ function areObjectsEqual(a, b, stack, areValuesEqual) {
123
124
  case tags.bigInt64ArrayTag:
124
125
  case tags.float32ArrayTag:
125
126
  case tags.float64ArrayTag: {
126
- if (typeof Buffer !== 'undefined' && Buffer.isBuffer(a) !== Buffer.isBuffer(b)) {
127
+ if (isBuffer.isBuffer(a) !== isBuffer.isBuffer(b)) {
127
128
  return false;
128
129
  }
129
130
  if (a.length !== b.length) {
@@ -1,3 +1,4 @@
1
+ import { isBuffer } from './isBuffer.mjs';
1
2
  import { isPlainObject } from './isPlainObject.mjs';
2
3
  import { getSymbols } from '../compat/_internal/getSymbols.mjs';
3
4
  import { getTag } from '../compat/_internal/getTag.mjs';
@@ -119,7 +120,7 @@ function areObjectsEqual(a, b, stack, areValuesEqual) {
119
120
  case bigInt64ArrayTag:
120
121
  case float32ArrayTag:
121
122
  case float64ArrayTag: {
122
- if (typeof Buffer !== 'undefined' && Buffer.isBuffer(a) !== Buffer.isBuffer(b)) {
123
+ if (isBuffer(a) !== isBuffer(b)) {
123
124
  return false;
124
125
  }
125
126
  if (a.length !== b.length) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
- "version": "1.45.1",
3
+ "version": "1.46.0-dev.1786+95111dd0",
4
4
  "description": "A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.",
5
5
  "homepage": "https://es-toolkit.dev",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",