es-toolkit 1.24.0-dev.769 → 1.24.0-dev.772

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,3 @@
1
+ const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
2
+
3
+ export { MAX_SAFE_INTEGER };
@@ -211,4 +211,5 @@ export { toInteger } from './util/toInteger.mjs';
211
211
  export { toLength } from './util/toLength.mjs';
212
212
  export { toNumber } from './util/toNumber.mjs';
213
213
  export { toPath } from './util/toPath.mjs';
214
+ export { toSafeInteger } from './util/toSafeInteger.mjs';
214
215
  export { toString } from './util/toString.mjs';
@@ -211,4 +211,5 @@ export { toInteger } from './util/toInteger.js';
211
211
  export { toLength } from './util/toLength.js';
212
212
  export { toNumber } from './util/toNumber.js';
213
213
  export { toPath } from './util/toPath.js';
214
+ export { toSafeInteger } from './util/toSafeInteger.js';
214
215
  export { toString } from './util/toString.js';
@@ -8,9 +8,9 @@ const unary = require('../_chunk/unary-BZ5Ixo.js');
8
8
  const noop = require('../_chunk/noop-2IwLUk.js');
9
9
  const sumBy = require('../_chunk/sumBy-BkErWJ.js');
10
10
  const randomInt = require('../_chunk/randomInt-CF7bZK.js');
11
- const toMerged = require('../_chunk/toMerged-DDLv0D.js');
12
- const isPlainObject$1 = require('../_chunk/isPlainObject-DgrsU7.js');
13
- const isWeakSet$1 = require('../_chunk/isWeakSet-Bd6nry.js');
11
+ const toMerged = require('../_chunk/toMerged-Dgkf4k.js');
12
+ const isPlainObject$1 = require('../_chunk/isPlainObject-D-qpfA.js');
13
+ const isWeakSet$1 = require('../_chunk/isWeakSet-BXPmkb.js');
14
14
  const upperFirst = require('../_chunk/upperFirst-BUECmK.js');
15
15
 
16
16
  function castArray(value) {
@@ -2078,6 +2078,15 @@ function toLength(value) {
2078
2078
  return clamp(length, 0, MAX_ARRAY_LENGTH);
2079
2079
  }
2080
2080
 
2081
+ const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
2082
+
2083
+ function toSafeInteger(value) {
2084
+ if (value == null) {
2085
+ return 0;
2086
+ }
2087
+ return clamp(toInteger(value), -MAX_SAFE_INTEGER, MAX_SAFE_INTEGER);
2088
+ }
2089
+
2081
2090
  exports.at = zipWith.at;
2082
2091
  exports.compact = zipWith.compact;
2083
2092
  exports.countBy = zipWith.countBy;
@@ -2286,6 +2295,7 @@ exports.toInteger = toInteger;
2286
2295
  exports.toLength = toLength;
2287
2296
  exports.toNumber = toNumber;
2288
2297
  exports.toPath = toPath;
2298
+ exports.toSafeInteger = toSafeInteger;
2289
2299
  exports.toString = toString;
2290
2300
  exports.trim = trim;
2291
2301
  exports.trimEnd = trimEnd;
@@ -211,4 +211,5 @@ export { toInteger } from './util/toInteger.mjs';
211
211
  export { toLength } from './util/toLength.mjs';
212
212
  export { toNumber } from './util/toNumber.mjs';
213
213
  export { toPath } from './util/toPath.mjs';
214
+ export { toSafeInteger } from './util/toSafeInteger.mjs';
214
215
  export { toString } from './util/toString.mjs';
@@ -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 };
package/dist/index.js CHANGED
@@ -11,11 +11,11 @@ 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-Dgkf4k.js');
15
15
  const object_index = require('./object/index.js');
16
- const isWeakSet = require('./_chunk/isWeakSet-Bd6nry.js');
16
+ const isWeakSet = require('./_chunk/isWeakSet-BXPmkb.js');
17
17
  const predicate_index = require('./predicate/index.js');
18
- const isPlainObject = require('./_chunk/isPlainObject-DgrsU7.js');
18
+ const isPlainObject = require('./_chunk/isPlainObject-D-qpfA.js');
19
19
  const upperFirst = require('./_chunk/upperFirst-BUECmK.js');
20
20
  const string_index = require('./string/index.js');
21
21
 
@@ -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-Dgkf4k.js');
6
6
 
7
7
  function mergeWith(target, source, merge) {
8
8
  const sourceKeys = Object.keys(source);
@@ -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-BXPmkb.js');
6
+ const isPlainObject = require('../_chunk/isPlainObject-D-qpfA.js');
7
7
 
8
8
  function isBoolean(x) {
9
9
  return typeof x === 'boolean';
@@ -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,11 @@
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) {
6
- return false;
7
- }
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;
5
+ const proto = Object.getPrototypeOf(value);
6
+ return (proto === null ||
7
+ proto === Object.prototype ||
8
+ Object.getPrototypeOf(proto) === null);
19
9
  }
20
10
 
21
11
  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-dev.769+1c780cf1",
4
+ "version": "1.24.0-dev.772+8ba7fbe4",
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;