es-toolkit 1.24.0 → 1.25.0-dev.786

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 (89) hide show
  1. package/CHANGELOG.md +12 -0
  2. package/dist/_chunk/isPlainObject-octpoD.js +32 -0
  3. package/dist/_chunk/{isWeakSet-Bd6nry.js → isWeakSet-BuFfzX.js} +11 -8
  4. package/dist/_chunk/{toMerged-DDLv0D.js → toMerged-B5ZVux.js} +3 -6
  5. package/dist/_chunk/{unary-BZ5Ixo.js → unary-CMvKXy.js} +2 -2
  6. package/dist/_chunk/{zipWith-wpjySR.js → zipWith-Dkv3D1.js} +7 -1
  7. package/dist/array/at.mjs +1 -1
  8. package/dist/array/index.js +2 -7
  9. package/dist/browser.global.js +1 -1
  10. package/dist/browser.global.js.map +1 -1
  11. package/dist/compat/_internal/MAX_SAFE_INTEGER.mjs +3 -0
  12. package/dist/compat/array/chunk.d.mts +2 -3
  13. package/dist/compat/array/chunk.d.ts +2 -3
  14. package/dist/compat/array/chunk.mjs +3 -2
  15. package/dist/compat/array/compact.d.mts +16 -0
  16. package/dist/compat/array/compact.d.ts +16 -0
  17. package/dist/compat/array/compact.mjs +11 -0
  18. package/dist/compat/array/drop.d.mts +2 -2
  19. package/dist/compat/array/drop.d.ts +2 -2
  20. package/dist/compat/array/drop.mjs +5 -3
  21. package/dist/compat/array/dropRight.d.mts +19 -0
  22. package/dist/compat/array/dropRight.d.ts +19 -0
  23. package/dist/compat/array/dropRight.mjs +11 -0
  24. package/dist/compat/array/dropRightWhile.d.mts +8 -8
  25. package/dist/compat/array/dropRightWhile.d.ts +8 -8
  26. package/dist/compat/array/dropRightWhile.mjs +7 -0
  27. package/dist/compat/array/dropWhile.d.mts +8 -8
  28. package/dist/compat/array/dropWhile.d.ts +8 -8
  29. package/dist/compat/array/dropWhile.mjs +7 -0
  30. package/dist/compat/array/head.d.mts +18 -0
  31. package/dist/compat/array/head.d.ts +18 -0
  32. package/dist/compat/array/head.mjs +11 -0
  33. package/dist/compat/array/intersection.d.mts +20 -0
  34. package/dist/compat/array/intersection.d.ts +20 -0
  35. package/dist/compat/array/intersection.mjs +23 -0
  36. package/dist/compat/array/last.d.mts +25 -0
  37. package/dist/compat/array/last.d.ts +25 -0
  38. package/dist/compat/array/last.mjs +11 -0
  39. package/dist/compat/array/sample.d.mts +80 -0
  40. package/dist/compat/array/sample.d.ts +80 -0
  41. package/dist/compat/array/sample.mjs +14 -0
  42. package/dist/compat/array/tail.d.mts +27 -0
  43. package/dist/compat/array/tail.d.ts +27 -0
  44. package/dist/compat/array/tail.mjs +11 -0
  45. package/dist/compat/array/take.d.mts +3 -3
  46. package/dist/compat/array/take.d.ts +3 -3
  47. package/dist/compat/array/take.mjs +4 -3
  48. package/dist/compat/array/takeRight.d.mts +24 -0
  49. package/dist/compat/array/takeRight.d.ts +24 -0
  50. package/dist/compat/array/takeRight.mjs +11 -0
  51. package/dist/compat/array/uniq.d.mts +18 -0
  52. package/dist/compat/array/uniq.d.ts +18 -0
  53. package/dist/compat/array/uniq.mjs +11 -0
  54. package/dist/compat/array/without.d.mts +23 -0
  55. package/dist/compat/array/without.d.ts +23 -0
  56. package/dist/compat/array/without.mjs +11 -0
  57. package/dist/compat/index.d.mts +13 -11
  58. package/dist/compat/index.d.ts +13 -11
  59. package/dist/compat/index.js +143 -30
  60. package/dist/compat/index.mjs +13 -11
  61. package/dist/compat/object/pick.mjs +4 -0
  62. package/dist/compat/string/escape.d.mts +16 -0
  63. package/dist/compat/string/escape.d.ts +16 -0
  64. package/dist/compat/string/escape.mjs +8 -0
  65. package/dist/compat/util/toSafeInteger.d.mts +20 -0
  66. package/dist/compat/util/toSafeInteger.d.ts +20 -0
  67. package/dist/compat/util/toSafeInteger.mjs +12 -0
  68. package/dist/function/after.d.mts +2 -2
  69. package/dist/function/after.d.ts +2 -2
  70. package/dist/function/after.mjs +2 -2
  71. package/dist/function/index.js +1 -1
  72. package/dist/index.d.mts +1 -0
  73. package/dist/index.d.ts +1 -0
  74. package/dist/index.js +7 -6
  75. package/dist/index.mjs +1 -0
  76. package/dist/object/cloneDeep.mjs +3 -5
  77. package/dist/object/index.js +1 -1
  78. package/dist/predicate/index.d.mts +1 -0
  79. package/dist/predicate/index.d.ts +1 -0
  80. package/dist/predicate/index.js +3 -2
  81. package/dist/predicate/index.mjs +1 -0
  82. package/dist/predicate/isFile.d.mts +21 -0
  83. package/dist/predicate/isFile.d.ts +21 -0
  84. package/dist/predicate/isFile.mjs +10 -0
  85. package/dist/predicate/isPlainObject.d.mts +34 -5
  86. package/dist/predicate/isPlainObject.d.ts +34 -5
  87. package/dist/predicate/isPlainObject.mjs +7 -13
  88. package/package.json +1 -1
  89. package/dist/_chunk/isPlainObject-DgrsU7.js +0 -33
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Converts `value` to a safe integer.
3
+ *
4
+ * A safe integer can be compared and represented correctly.
5
+ *
6
+ * @param {unknown} value - The value to convert.
7
+ * @returns {number} Returns the value converted to a safe integer.
8
+ *
9
+ * @example
10
+ * toSafeInteger(3.2); // => 3
11
+ * toSafeInteger(Number.MAX_VALUE); // => 9007199254740991
12
+ * toSafeInteger(Infinity); // => 9007199254740991
13
+ * toSafeInteger('3.2'); // => 3
14
+ * toSafeInteger(NaN); // => 0
15
+ * toSafeInteger(null); // => 0
16
+ * toSafeInteger(-Infinity); // => -9007199254740991
17
+ */
18
+ declare function toSafeInteger(value?: unknown): number;
19
+
20
+ export { toSafeInteger };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * Converts `value` to a safe integer.
3
+ *
4
+ * A safe integer can be compared and represented correctly.
5
+ *
6
+ * @param {unknown} value - The value to convert.
7
+ * @returns {number} Returns the value converted to a safe integer.
8
+ *
9
+ * @example
10
+ * toSafeInteger(3.2); // => 3
11
+ * toSafeInteger(Number.MAX_VALUE); // => 9007199254740991
12
+ * toSafeInteger(Infinity); // => 9007199254740991
13
+ * toSafeInteger('3.2'); // => 3
14
+ * toSafeInteger(NaN); // => 0
15
+ * toSafeInteger(null); // => 0
16
+ * toSafeInteger(-Infinity); // => -9007199254740991
17
+ */
18
+ declare function toSafeInteger(value?: unknown): number;
19
+
20
+ export { toSafeInteger };
@@ -0,0 +1,12 @@
1
+ import { toInteger } from './toInteger.mjs';
2
+ import { MAX_SAFE_INTEGER } from '../_internal/MAX_SAFE_INTEGER.mjs';
3
+ import { clamp } from '../math/clamp.mjs';
4
+
5
+ function toSafeInteger(value) {
6
+ if (value == null) {
7
+ return 0;
8
+ }
9
+ return clamp(toInteger(value), -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER);
10
+ }
11
+
12
+ export { toSafeInteger };
@@ -8,7 +8,7 @@
8
8
  * @template F - The type of the function to be invoked.
9
9
  * @param {number} n - The number of calls required for `func` to execute.
10
10
  * @param {F} func - The function to be invoked.
11
- * @returns {F} - A new function that:
11
+ * @returns {(...args: Parameters<F>) => ReturnType<F> | undefined} - A new function that:
12
12
  * - Tracks the number of calls.
13
13
  * - Invokes `func` starting from the `n`-th call.
14
14
  * - Returns `undefined` if fewer than `n` calls have been made.
@@ -26,6 +26,6 @@
26
26
  * // Will log 'called'.
27
27
  * afterFn()
28
28
  */
29
- declare function after<F extends (...args: any[]) => any>(n: number, func: F): F;
29
+ declare function after<F extends (...args: any[]) => any>(n: number, func: F): (...args: Parameters<F>) => ReturnType<F> | undefined;
30
30
 
31
31
  export { after };
@@ -8,7 +8,7 @@
8
8
  * @template F - The type of the function to be invoked.
9
9
  * @param {number} n - The number of calls required for `func` to execute.
10
10
  * @param {F} func - The function to be invoked.
11
- * @returns {F} - A new function that:
11
+ * @returns {(...args: Parameters<F>) => ReturnType<F> | undefined} - A new function that:
12
12
  * - Tracks the number of calls.
13
13
  * - Invokes `func` starting from the `n`-th call.
14
14
  * - Returns `undefined` if fewer than `n` calls have been made.
@@ -26,6 +26,6 @@
26
26
  * // Will log 'called'.
27
27
  * afterFn()
28
28
  */
29
- declare function after<F extends (...args: any[]) => any>(n: number, func: F): F;
29
+ declare function after<F extends (...args: any[]) => any>(n: number, func: F): (...args: Parameters<F>) => ReturnType<F> | undefined;
30
30
 
31
31
  export { after };
@@ -3,12 +3,12 @@ function after(n, func) {
3
3
  throw new Error(`n must be a non-negative integer.`);
4
4
  }
5
5
  let counter = 0;
6
- return ((...args) => {
6
+ return (...args) => {
7
7
  if (++counter >= n) {
8
8
  return func(...args);
9
9
  }
10
10
  return undefined;
11
- });
11
+ };
12
12
  }
13
13
 
14
14
  export { after };
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const unary = require('../_chunk/unary-BZ5Ixo.js');
5
+ const unary = require('../_chunk/unary-CMvKXy.js');
6
6
  const noop = require('../_chunk/noop-2IwLUk.js');
7
7
 
8
8
  function before(n, func) {
package/dist/index.d.mts CHANGED
@@ -105,6 +105,7 @@ export { isDate } from './predicate/isDate.mjs';
105
105
  export { isEqual } from './predicate/isEqual.mjs';
106
106
  export { isEqualWith } from './predicate/isEqualWith.mjs';
107
107
  export { isError } from './predicate/isError.mjs';
108
+ export { isFile } from './predicate/isFile.mjs';
108
109
  export { isFunction } from './predicate/isFunction.mjs';
109
110
  export { isJSONArray } from './predicate/isJSONArray.mjs';
110
111
  export { isJSONObject } from './predicate/isJSONObject.mjs';
package/dist/index.d.ts CHANGED
@@ -105,6 +105,7 @@ export { isDate } from './predicate/isDate.js';
105
105
  export { isEqual } from './predicate/isEqual.js';
106
106
  export { isEqualWith } from './predicate/isEqualWith.js';
107
107
  export { isError } from './predicate/isError.js';
108
+ export { isFile } from './predicate/isFile.js';
108
109
  export { isFunction } from './predicate/isFunction.js';
109
110
  export { isJSONArray } from './predicate/isJSONArray.js';
110
111
  export { isJSONObject } from './predicate/isJSONObject.js';
package/dist/index.js CHANGED
@@ -2,20 +2,20 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const zipWith = require('./_chunk/zipWith-wpjySR.js');
5
+ const zipWith = require('./_chunk/zipWith-Dkv3D1.js');
6
6
  const array_index = require('./array/index.js');
7
7
  const promise_index = require('./_chunk/index-BGZDR9.js');
8
- const unary = require('./_chunk/unary-BZ5Ixo.js');
8
+ const unary = require('./_chunk/unary-CMvKXy.js');
9
9
  const function_index = require('./function/index.js');
10
10
  const noop = require('./_chunk/noop-2IwLUk.js');
11
11
  const sumBy = require('./_chunk/sumBy-BkErWJ.js');
12
12
  const randomInt = require('./_chunk/randomInt-CF7bZK.js');
13
13
  const math_index = require('./math/index.js');
14
- const toMerged = require('./_chunk/toMerged-DDLv0D.js');
14
+ const toMerged = require('./_chunk/toMerged-B5ZVux.js');
15
15
  const object_index = require('./object/index.js');
16
- const isWeakSet = require('./_chunk/isWeakSet-Bd6nry.js');
16
+ const isWeakSet = require('./_chunk/isWeakSet-BuFfzX.js');
17
17
  const predicate_index = require('./predicate/index.js');
18
- const isPlainObject = require('./_chunk/isPlainObject-DgrsU7.js');
18
+ const isPlainObject = require('./_chunk/isPlainObject-octpoD.js');
19
19
  const upperFirst = require('./_chunk/upperFirst-BUECmK.js');
20
20
  const string_index = require('./string/index.js');
21
21
 
@@ -28,6 +28,7 @@ exports.countBy = zipWith.countBy;
28
28
  exports.difference = zipWith.difference;
29
29
  exports.differenceBy = zipWith.differenceBy;
30
30
  exports.differenceWith = zipWith.differenceWith;
31
+ exports.drop = zipWith.drop;
31
32
  exports.dropRight = zipWith.dropRight;
32
33
  exports.dropRightWhile = zipWith.dropRightWhile;
33
34
  exports.dropWhile = zipWith.dropWhile;
@@ -74,7 +75,6 @@ exports.xorWith = zipWith.xorWith;
74
75
  exports.zip = zipWith.zip;
75
76
  exports.zipObject = zipWith.zipObject;
76
77
  exports.zipWith = zipWith.zipWith;
77
- exports.drop = array_index.drop;
78
78
  exports.orderBy = array_index.orderBy;
79
79
  exports.sortBy = array_index.sortBy;
80
80
  exports.AbortError = promise_index.AbortError;
@@ -129,6 +129,7 @@ exports.isBlob = isWeakSet.isBlob;
129
129
  exports.isDate = isWeakSet.isDate;
130
130
  exports.isEqual = isWeakSet.isEqual;
131
131
  exports.isEqualWith = isWeakSet.isEqualWith;
132
+ exports.isFile = isWeakSet.isFile;
132
133
  exports.isFunction = isWeakSet.isFunction;
133
134
  exports.isJSONArray = isWeakSet.isJSONArray;
134
135
  exports.isJSONObject = isWeakSet.isJSONObject;
package/dist/index.mjs CHANGED
@@ -105,6 +105,7 @@ export { isDate } from './predicate/isDate.mjs';
105
105
  export { isEqual } from './predicate/isEqual.mjs';
106
106
  export { isEqualWith } from './predicate/isEqualWith.mjs';
107
107
  export { isError } from './predicate/isError.mjs';
108
+ export { isFile } from './predicate/isFile.mjs';
108
109
  export { isFunction } from './predicate/isFunction.mjs';
109
110
  export { isJSONArray } from './predicate/isJSONArray.mjs';
110
111
  export { isJSONObject } from './predicate/isJSONObject.mjs';
@@ -1,3 +1,4 @@
1
+ import { getSymbols } from '../compat/_internal/getSymbols.mjs';
1
2
  import { isPrimitive } from '../predicate/isPrimitive.mjs';
2
3
  import { isTypedArray } from '../predicate/isTypedArray.mjs';
3
4
 
@@ -100,13 +101,10 @@ function cloneDeepImpl(obj, stack = new Map()) {
100
101
  return obj;
101
102
  }
102
103
  function copyProperties(target, source, stack) {
103
- const keys = Object.keys(source);
104
+ const keys = [...Object.keys(source), ...getSymbols(source)];
104
105
  for (let i = 0; i < keys.length; i++) {
105
106
  const key = keys[i];
106
- const descriptor = Object.getOwnPropertyDescriptor(source, key);
107
- if (descriptor?.writable || descriptor?.set) {
108
- target[key] = cloneDeepImpl(source[key], stack);
109
- }
107
+ target[key] = cloneDeepImpl(source[key], stack);
110
108
  }
111
109
  }
112
110
 
@@ -2,7 +2,7 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const toMerged = require('../_chunk/toMerged-DDLv0D.js');
5
+ const toMerged = require('../_chunk/toMerged-B5ZVux.js');
6
6
 
7
7
  function mergeWith(target, source, merge) {
8
8
  const sourceKeys = Object.keys(source);
@@ -5,6 +5,7 @@ export { isDate } from './isDate.mjs';
5
5
  export { isEqual } from './isEqual.mjs';
6
6
  export { isEqualWith } from './isEqualWith.mjs';
7
7
  export { isError } from './isError.mjs';
8
+ export { isFile } from './isFile.mjs';
8
9
  export { isFunction } from './isFunction.mjs';
9
10
  export { isJSONArray } from './isJSONArray.mjs';
10
11
  export { isJSONObject } from './isJSONObject.mjs';
@@ -5,6 +5,7 @@ export { isDate } from './isDate.js';
5
5
  export { isEqual } from './isEqual.js';
6
6
  export { isEqualWith } from './isEqualWith.js';
7
7
  export { isError } from './isError.js';
8
+ export { isFile } from './isFile.js';
8
9
  export { isFunction } from './isFunction.js';
9
10
  export { isJSONArray } from './isJSONArray.js';
10
11
  export { isJSONObject } from './isJSONObject.js';
@@ -2,8 +2,8 @@
2
2
 
3
3
  Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
4
4
 
5
- const isWeakSet = require('../_chunk/isWeakSet-Bd6nry.js');
6
- const isPlainObject = require('../_chunk/isPlainObject-DgrsU7.js');
5
+ const isWeakSet = require('../_chunk/isWeakSet-BuFfzX.js');
6
+ const isPlainObject = require('../_chunk/isPlainObject-octpoD.js');
7
7
 
8
8
  function isBoolean(x) {
9
9
  return typeof x === 'boolean';
@@ -26,6 +26,7 @@ exports.isBlob = isWeakSet.isBlob;
26
26
  exports.isDate = isWeakSet.isDate;
27
27
  exports.isEqual = isWeakSet.isEqual;
28
28
  exports.isEqualWith = isWeakSet.isEqualWith;
29
+ exports.isFile = isWeakSet.isFile;
29
30
  exports.isFunction = isWeakSet.isFunction;
30
31
  exports.isJSONArray = isWeakSet.isJSONArray;
31
32
  exports.isJSONObject = isWeakSet.isJSONObject;
@@ -5,6 +5,7 @@ export { isDate } from './isDate.mjs';
5
5
  export { isEqual } from './isEqual.mjs';
6
6
  export { isEqualWith } from './isEqualWith.mjs';
7
7
  export { isError } from './isError.mjs';
8
+ export { isFile } from './isFile.mjs';
8
9
  export { isFunction } from './isFunction.mjs';
9
10
  export { isJSONArray } from './isJSONArray.mjs';
10
11
  export { isJSONObject } from './isJSONObject.mjs';
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Checks if the given value is a File.
3
+ *
4
+ * This function tests whether the provided value is an instance of `File`.
5
+ * It returns `true` if the value is an instance of `File`, and `false` otherwise.
6
+ *
7
+ * @param {unknown} x - The value to test if it is a File.
8
+ * @returns {x is File} True if the value is a File, false otherwise.
9
+ *
10
+ * @example
11
+ * const value1 = new File(["content"], "example.txt");
12
+ * const value2 = {};
13
+ * const value3 = new Blob(["content"], { type: "text/plain" });
14
+ *
15
+ * console.log(isFile(value1)); // true
16
+ * console.log(isFile(value2)); // false
17
+ * console.log(isFile(value3)); // false
18
+ */
19
+ declare function isFile(x: unknown): x is File;
20
+
21
+ export { isFile };
@@ -0,0 +1,21 @@
1
+ /**
2
+ * Checks if the given value is a File.
3
+ *
4
+ * This function tests whether the provided value is an instance of `File`.
5
+ * It returns `true` if the value is an instance of `File`, and `false` otherwise.
6
+ *
7
+ * @param {unknown} x - The value to test if it is a File.
8
+ * @returns {x is File} True if the value is a File, false otherwise.
9
+ *
10
+ * @example
11
+ * const value1 = new File(["content"], "example.txt");
12
+ * const value2 = {};
13
+ * const value3 = new Blob(["content"], { type: "text/plain" });
14
+ *
15
+ * console.log(isFile(value1)); // true
16
+ * console.log(isFile(value2)); // false
17
+ * console.log(isFile(value3)); // false
18
+ */
19
+ declare function isFile(x: unknown): x is File;
20
+
21
+ export { isFile };
@@ -0,0 +1,10 @@
1
+ import { isBlob } from './isBlob.mjs';
2
+
3
+ function isFile(x) {
4
+ if (typeof File === 'undefined') {
5
+ return false;
6
+ }
7
+ return isBlob(x) && x instanceof File;
8
+ }
9
+
10
+ export { isFile };
@@ -5,11 +5,40 @@
5
5
  * @returns {value is Record<PropertyKey, any>} - True if the value is a plain object, otherwise false.
6
6
  *
7
7
  * @example
8
- * console.log(isPlainObject({})); // true
9
- * console.log(isPlainObject([])); // false
10
- * console.log(isPlainObject(null)); // false
11
- * console.log(isPlainObject(Object.create(null))); // true
12
- * console.log(Buffer.from('hello, world')); // false
8
+ * ```typescript
9
+ * // ✅👇 True
10
+ *
11
+ * isPlainObject({ }); //
12
+ * isPlainObject({ key: 'value' }); //
13
+ * isPlainObject({ key: new Date() }); // ✅
14
+ * isPlainObject(new Object()); // ✅
15
+ * isPlainObject(Object.create(null)); // ✅
16
+ * isPlainObject({ nested: { key: true} }); // ✅
17
+ * isPlainObject(new Proxy({}, {})); // ✅
18
+ * isPlainObject({ [Symbol('tag')]: 'A' }); // ✅
19
+ *
20
+ * // ✅👇 (cross-realms, node context, workers, ...)
21
+ * const runInNewContext = await import('node:vm').then(
22
+ * (mod) => mod.runInNewContext
23
+ * );
24
+ * isPlainObject(runInNewContext('({})')); // ✅
25
+ *
26
+ * // ❌👇 False
27
+ *
28
+ * class Test { };
29
+ * isPlainObject(new Test()) // ❌
30
+ * isPlainObject(10); // ❌
31
+ * isPlainObject(null); // ❌
32
+ * isPlainObject('hello'); // ❌
33
+ * isPlainObject([]); // ❌
34
+ * isPlainObject(new Date()); // ❌
35
+ * isPlainObject(new Uint8Array([1])); // ❌
36
+ * isPlainObject(Buffer.from('ABC')); // ❌
37
+ * isPlainObject(Promise.resolve({})); // ❌
38
+ * isPlainObject(Object.create({})); // ❌
39
+ * isPlainObject(new (class Cls {})); // ❌
40
+ * isPlainObject(globalThis); // ❌,
41
+ * ```
13
42
  */
14
43
  declare function isPlainObject(value: unknown): value is Record<PropertyKey, any>;
15
44
 
@@ -5,11 +5,40 @@
5
5
  * @returns {value is Record<PropertyKey, any>} - True if the value is a plain object, otherwise false.
6
6
  *
7
7
  * @example
8
- * console.log(isPlainObject({})); // true
9
- * console.log(isPlainObject([])); // false
10
- * console.log(isPlainObject(null)); // false
11
- * console.log(isPlainObject(Object.create(null))); // true
12
- * console.log(Buffer.from('hello, world')); // false
8
+ * ```typescript
9
+ * // ✅👇 True
10
+ *
11
+ * isPlainObject({ }); //
12
+ * isPlainObject({ key: 'value' }); //
13
+ * isPlainObject({ key: new Date() }); // ✅
14
+ * isPlainObject(new Object()); // ✅
15
+ * isPlainObject(Object.create(null)); // ✅
16
+ * isPlainObject({ nested: { key: true} }); // ✅
17
+ * isPlainObject(new Proxy({}, {})); // ✅
18
+ * isPlainObject({ [Symbol('tag')]: 'A' }); // ✅
19
+ *
20
+ * // ✅👇 (cross-realms, node context, workers, ...)
21
+ * const runInNewContext = await import('node:vm').then(
22
+ * (mod) => mod.runInNewContext
23
+ * );
24
+ * isPlainObject(runInNewContext('({})')); // ✅
25
+ *
26
+ * // ❌👇 False
27
+ *
28
+ * class Test { };
29
+ * isPlainObject(new Test()) // ❌
30
+ * isPlainObject(10); // ❌
31
+ * isPlainObject(null); // ❌
32
+ * isPlainObject('hello'); // ❌
33
+ * isPlainObject([]); // ❌
34
+ * isPlainObject(new Date()); // ❌
35
+ * isPlainObject(new Uint8Array([1])); // ❌
36
+ * isPlainObject(Buffer.from('ABC')); // ❌
37
+ * isPlainObject(Promise.resolve({})); // ❌
38
+ * isPlainObject(Object.create({})); // ❌
39
+ * isPlainObject(new (class Cls {})); // ❌
40
+ * isPlainObject(globalThis); // ❌,
41
+ * ```
13
42
  */
14
43
  declare function isPlainObject(value: unknown): value is Record<PropertyKey, any>;
15
44
 
@@ -1,21 +1,15 @@
1
1
  function isPlainObject(value) {
2
- if (typeof value !== 'object') {
2
+ if (!value || typeof value !== 'object') {
3
3
  return false;
4
4
  }
5
- if (value == null) {
5
+ const proto = Object.getPrototypeOf(value);
6
+ const hasObjectPrototype = proto === null ||
7
+ proto === Object.prototype ||
8
+ Object.getPrototypeOf(proto) === null;
9
+ if (!hasObjectPrototype) {
6
10
  return false;
7
11
  }
8
- if (Object.getPrototypeOf(value) === null) {
9
- return true;
10
- }
11
- if (value.toString() !== '[object Object]') {
12
- return false;
13
- }
14
- let proto = value;
15
- while (Object.getPrototypeOf(proto) !== null) {
16
- proto = Object.getPrototypeOf(proto);
17
- }
18
- return Object.getPrototypeOf(value) === proto;
12
+ return Object.prototype.toString.call(value) === '[object Object]';
19
13
  }
20
14
 
21
15
  export { isPlainObject };
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.24.0",
4
+ "version": "1.25.0-dev.786+56835199",
5
5
  "homepage": "https://es-toolkit.slash.page",
6
6
  "bugs": "https://github.com/toss/es-toolkit/issues",
7
7
  "repository": {
@@ -1,33 +0,0 @@
1
- 'use strict';
2
-
3
- function isPrimitive(value) {
4
- return value == null || (typeof value !== 'object' && typeof value !== 'function');
5
- }
6
-
7
- function isTypedArray(x) {
8
- return ArrayBuffer.isView(x) && !(x instanceof DataView);
9
- }
10
-
11
- function isPlainObject(value) {
12
- if (typeof value !== 'object') {
13
- return false;
14
- }
15
- if (value == null) {
16
- return false;
17
- }
18
- if (Object.getPrototypeOf(value) === null) {
19
- return true;
20
- }
21
- if (value.toString() !== '[object Object]') {
22
- return false;
23
- }
24
- let proto = value;
25
- while (Object.getPrototypeOf(proto) !== null) {
26
- proto = Object.getPrototypeOf(proto);
27
- }
28
- return Object.getPrototypeOf(value) === proto;
29
- }
30
-
31
- exports.isPlainObject = isPlainObject;
32
- exports.isPrimitive = isPrimitive;
33
- exports.isTypedArray = isTypedArray;