es-toolkit 1.52.0-dev.2094 → 1.52.0-dev.2096

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 (42) hide show
  1. package/dist/browser.global.js +4 -4
  2. package/dist/index.d.mts +2 -1
  3. package/dist/index.d.ts +2 -1
  4. package/dist/index.js +2 -0
  5. package/dist/index.mjs +2 -1
  6. package/dist/util/hash/browser.d.mts +26 -0
  7. package/dist/util/hash/browser.d.ts +26 -0
  8. package/dist/util/hash/browser.js +31 -0
  9. package/dist/util/hash/browser.mjs +30 -0
  10. package/dist/util/hash/node.d.mts +26 -0
  11. package/dist/util/hash/node.d.ts +26 -0
  12. package/dist/util/hash/node.js +494 -0
  13. package/dist/util/hash/node.mjs +493 -0
  14. package/dist/util/hash/sha256.js +158 -0
  15. package/dist/util/hash/sha256.mjs +158 -0
  16. package/dist/util/index.d.mts +2 -1
  17. package/dist/util/index.d.ts +2 -1
  18. package/dist/util/index.js +2 -0
  19. package/dist/util/index.mjs +2 -1
  20. package/dist/util/serialize/compareValues.js +23 -0
  21. package/dist/util/serialize/compareValues.mjs +23 -0
  22. package/dist/util/serialize/serialize.d.mts +44 -0
  23. package/dist/util/serialize/serialize.d.ts +44 -0
  24. package/dist/util/serialize/serialize.js +64 -0
  25. package/dist/util/serialize/serialize.mjs +63 -0
  26. package/dist/util/serialize/serializeBigInt.js +15 -0
  27. package/dist/util/serialize/serializeBigInt.mjs +15 -0
  28. package/dist/util/serialize/serializeFunction.js +26 -0
  29. package/dist/util/serialize/serializeFunction.mjs +26 -0
  30. package/dist/util/serialize/serializeNumber.js +21 -0
  31. package/dist/util/serialize/serializeNumber.mjs +21 -0
  32. package/dist/util/serialize/serializeObject.js +85 -0
  33. package/dist/util/serialize/serializeObject.mjs +85 -0
  34. package/dist/util/serialize/serializePlainObject.js +31 -0
  35. package/dist/util/serialize/serializePlainObject.mjs +31 -0
  36. package/dist/util/serialize/serializeString.js +18 -0
  37. package/dist/util/serialize/serializeString.mjs +18 -0
  38. package/dist/util/serialize/serializeSymbol.js +23 -0
  39. package/dist/util/serialize/serializeSymbol.mjs +23 -0
  40. package/package.json +47 -1
  41. package/util/hash.d.ts +1 -0
  42. package/util/hash.js +1 -0
@@ -0,0 +1,85 @@
1
+ const require_isTypedArray = require("../../predicate/isTypedArray.js");
2
+ const require_isPlainObject = require("../../predicate/isPlainObject.js");
3
+ const require_isArrayBuffer = require("../../predicate/isArrayBuffer.js");
4
+ const require_isDate = require("../../predicate/isDate.js");
5
+ const require_isError = require("../../predicate/isError.js");
6
+ const require_isMap = require("../../predicate/isMap.js");
7
+ const require_isRegExp = require("../../predicate/isRegExp.js");
8
+ const require_isSet = require("../../predicate/isSet.js");
9
+ const require_compareValues = require("./compareValues.js");
10
+ const require_serializeString = require("./serializeString.js");
11
+ const require_serializePlainObject = require("./serializePlainObject.js");
12
+ const require_serialize = require("./serialize.js");
13
+ //#region src/util/serialize/serializeObject.ts
14
+ /**
15
+ * Serializes an object, handling circular references and repeated references.
16
+ *
17
+ * The first time an object is visited, it is registered as `#ref{n}` where `n`
18
+ * is the visit order; if the object is reached again while it is still being
19
+ * serialized, the back-reference is emitted instead. Once completed, the
20
+ * serialized string is memoized so that repeated references serialize
21
+ * in constant time.
22
+ *
23
+ * @param value - The object to serialize, or `null`.
24
+ * @param refs - The circular reference context shared across one serialization.
25
+ * @returns The serialized string.
26
+ * @throws {TypeError} If the object cannot be serialized.
27
+ */
28
+ function serializeObject(value, refs) {
29
+ if (value === null) return "null";
30
+ const cached = refs.get(value);
31
+ if (cached !== void 0) return cached;
32
+ refs.set(value, `#ref${refs.size}`);
33
+ const result = serializeObjectImpl(value, refs);
34
+ refs.set(value, result);
35
+ return result;
36
+ }
37
+ function serializeObjectImpl(value, refs) {
38
+ if (Array.isArray(value)) return serializeArray(value, refs);
39
+ if (require_isPlainObject.isPlainObject(value)) return require_serializePlainObject.serializePlainObject(value, refs);
40
+ if (require_isDate.isDate(value)) return Number.isNaN(value.getTime()) ? "Date(null)" : `Date(${require_serializeString.serializeString(value.toISOString())})`;
41
+ if (require_isRegExp.isRegExp(value)) return `RegExp(${value.toString()})`;
42
+ if (require_isSet.isSet(value)) return `Set${serializeArray(Array.from(value).sort((a, b) => require_compareValues.compareValues(a, b, refs)), refs)}`;
43
+ if (require_isMap.isMap(value)) return serializeEntries("Map", value.entries(), refs);
44
+ if (require_isTypedArray.isTypedArray(value)) {
45
+ const name = value[Symbol.toStringTag];
46
+ if (name === "BigInt64Array" || name === "BigUint64Array") return `${name}[${value.join("n,")}${value.length > 0 ? "n" : ""}]`;
47
+ return `${name}[${value.join(",")}]`;
48
+ }
49
+ if (require_isArrayBuffer.isArrayBuffer(value)) return `ArrayBuffer[${new Uint8Array(value).join(",")}]`;
50
+ if (require_isError.isError(value)) return `Error(${value.name}: ${require_serializeString.serializeString(value.message)})`;
51
+ const tag = Object.prototype.toString.call(value).slice(8, -1);
52
+ if (tag === "Object") return serializeClassInstance(value, refs);
53
+ if (typeof value.entries === "function") return serializeEntries(tag, value.entries(), refs);
54
+ throw new TypeError(`Cannot serialize ${tag}`);
55
+ }
56
+ function serializeArray(array, refs) {
57
+ let result = "[";
58
+ for (let i = 0; i < array.length; i++) {
59
+ if (i > 0) result += ",";
60
+ result += require_serialize.serializeValue(array[i], refs);
61
+ }
62
+ return result + "]";
63
+ }
64
+ function serializeEntries(tag, entries, refs) {
65
+ const sortedEntries = Array.from(entries).sort((a, b) => require_compareValues.compareValues(a[0], b[0], refs));
66
+ let result = `${tag}{`;
67
+ for (let i = 0; i < sortedEntries.length; i++) {
68
+ const [key, value] = sortedEntries[i];
69
+ if (i > 0) result += ",";
70
+ result += `${require_serialize.serializeValue(key, refs)}:${require_serialize.serializeValue(value, refs)}`;
71
+ }
72
+ return result + "}";
73
+ }
74
+ function serializeClassInstance(value, refs) {
75
+ const constructor = value.constructor;
76
+ const name = constructor === Object || constructor === void 0 ? "" : constructor.name;
77
+ if ("toJSON" in value && typeof value.toJSON === "function") {
78
+ const json = value.toJSON();
79
+ if (json !== null && typeof json === "object") return name + serializeObject(json, refs);
80
+ return `${name}(${require_serialize.serializeValue(json, refs)})`;
81
+ }
82
+ return name + require_serializePlainObject.serializePlainObject(value, refs);
83
+ }
84
+ //#endregion
85
+ exports.serializeObject = serializeObject;
@@ -0,0 +1,85 @@
1
+ import { isTypedArray } from "../../predicate/isTypedArray.mjs";
2
+ import { isPlainObject } from "../../predicate/isPlainObject.mjs";
3
+ import { isArrayBuffer } from "../../predicate/isArrayBuffer.mjs";
4
+ import { isDate } from "../../predicate/isDate.mjs";
5
+ import { isError } from "../../predicate/isError.mjs";
6
+ import { isMap } from "../../predicate/isMap.mjs";
7
+ import { isRegExp } from "../../predicate/isRegExp.mjs";
8
+ import { isSet } from "../../predicate/isSet.mjs";
9
+ import { compareValues } from "./compareValues.mjs";
10
+ import { serializeString } from "./serializeString.mjs";
11
+ import { serializePlainObject } from "./serializePlainObject.mjs";
12
+ import { serializeValue } from "./serialize.mjs";
13
+ //#region src/util/serialize/serializeObject.ts
14
+ /**
15
+ * Serializes an object, handling circular references and repeated references.
16
+ *
17
+ * The first time an object is visited, it is registered as `#ref{n}` where `n`
18
+ * is the visit order; if the object is reached again while it is still being
19
+ * serialized, the back-reference is emitted instead. Once completed, the
20
+ * serialized string is memoized so that repeated references serialize
21
+ * in constant time.
22
+ *
23
+ * @param value - The object to serialize, or `null`.
24
+ * @param refs - The circular reference context shared across one serialization.
25
+ * @returns The serialized string.
26
+ * @throws {TypeError} If the object cannot be serialized.
27
+ */
28
+ function serializeObject(value, refs) {
29
+ if (value === null) return "null";
30
+ const cached = refs.get(value);
31
+ if (cached !== void 0) return cached;
32
+ refs.set(value, `#ref${refs.size}`);
33
+ const result = serializeObjectImpl(value, refs);
34
+ refs.set(value, result);
35
+ return result;
36
+ }
37
+ function serializeObjectImpl(value, refs) {
38
+ if (Array.isArray(value)) return serializeArray(value, refs);
39
+ if (isPlainObject(value)) return serializePlainObject(value, refs);
40
+ if (isDate(value)) return Number.isNaN(value.getTime()) ? "Date(null)" : `Date(${serializeString(value.toISOString())})`;
41
+ if (isRegExp(value)) return `RegExp(${value.toString()})`;
42
+ if (isSet(value)) return `Set${serializeArray(Array.from(value).sort((a, b) => compareValues(a, b, refs)), refs)}`;
43
+ if (isMap(value)) return serializeEntries("Map", value.entries(), refs);
44
+ if (isTypedArray(value)) {
45
+ const name = value[Symbol.toStringTag];
46
+ if (name === "BigInt64Array" || name === "BigUint64Array") return `${name}[${value.join("n,")}${value.length > 0 ? "n" : ""}]`;
47
+ return `${name}[${value.join(",")}]`;
48
+ }
49
+ if (isArrayBuffer(value)) return `ArrayBuffer[${new Uint8Array(value).join(",")}]`;
50
+ if (isError(value)) return `Error(${value.name}: ${serializeString(value.message)})`;
51
+ const tag = Object.prototype.toString.call(value).slice(8, -1);
52
+ if (tag === "Object") return serializeClassInstance(value, refs);
53
+ if (typeof value.entries === "function") return serializeEntries(tag, value.entries(), refs);
54
+ throw new TypeError(`Cannot serialize ${tag}`);
55
+ }
56
+ function serializeArray(array, refs) {
57
+ let result = "[";
58
+ for (let i = 0; i < array.length; i++) {
59
+ if (i > 0) result += ",";
60
+ result += serializeValue(array[i], refs);
61
+ }
62
+ return result + "]";
63
+ }
64
+ function serializeEntries(tag, entries, refs) {
65
+ const sortedEntries = Array.from(entries).sort((a, b) => compareValues(a[0], b[0], refs));
66
+ let result = `${tag}{`;
67
+ for (let i = 0; i < sortedEntries.length; i++) {
68
+ const [key, value] = sortedEntries[i];
69
+ if (i > 0) result += ",";
70
+ result += `${serializeValue(key, refs)}:${serializeValue(value, refs)}`;
71
+ }
72
+ return result + "}";
73
+ }
74
+ function serializeClassInstance(value, refs) {
75
+ const constructor = value.constructor;
76
+ const name = constructor === Object || constructor === void 0 ? "" : constructor.name;
77
+ if ("toJSON" in value && typeof value.toJSON === "function") {
78
+ const json = value.toJSON();
79
+ if (json !== null && typeof json === "object") return name + serializeObject(json, refs);
80
+ return `${name}(${serializeValue(json, refs)})`;
81
+ }
82
+ return name + serializePlainObject(value, refs);
83
+ }
84
+ //#endregion
85
+ export { serializeObject };
@@ -0,0 +1,31 @@
1
+ const require_serializeString = require("./serializeString.js");
2
+ const require_serialize = require("./serialize.js");
3
+ //#region src/util/serialize/serializePlainObject.ts
4
+ /**
5
+ * Serializes the own enumerable string-keyed properties of an object
6
+ * into a `{'key':value}` string.
7
+ *
8
+ * Keys are sorted by code unit so that the output does not depend on
9
+ * property insertion order, and always quoted so that a key containing
10
+ * `:` or `,` cannot be confused with the surrounding structure. Symbol
11
+ * keys and non-enumerable properties are ignored.
12
+ *
13
+ * @param object - The object to serialize.
14
+ * @param refs - The circular reference context shared with the surrounding serialization.
15
+ * @returns The serialized string.
16
+ *
17
+ * @example
18
+ * serializePlainObject({ b: 2, a: 1 }, new Map()); // "{'a':1,'b':2}"
19
+ */
20
+ function serializePlainObject(object, refs) {
21
+ const keys = Object.keys(object).sort();
22
+ let result = "{";
23
+ for (let i = 0; i < keys.length; i++) {
24
+ const key = keys[i];
25
+ if (i > 0) result += ",";
26
+ result += `${require_serializeString.serializeString(key)}:${require_serialize.serializeValue(object[key], refs)}`;
27
+ }
28
+ return result + "}";
29
+ }
30
+ //#endregion
31
+ exports.serializePlainObject = serializePlainObject;
@@ -0,0 +1,31 @@
1
+ import { serializeString } from "./serializeString.mjs";
2
+ import { serializeValue } from "./serialize.mjs";
3
+ //#region src/util/serialize/serializePlainObject.ts
4
+ /**
5
+ * Serializes the own enumerable string-keyed properties of an object
6
+ * into a `{'key':value}` string.
7
+ *
8
+ * Keys are sorted by code unit so that the output does not depend on
9
+ * property insertion order, and always quoted so that a key containing
10
+ * `:` or `,` cannot be confused with the surrounding structure. Symbol
11
+ * keys and non-enumerable properties are ignored.
12
+ *
13
+ * @param object - The object to serialize.
14
+ * @param refs - The circular reference context shared with the surrounding serialization.
15
+ * @returns The serialized string.
16
+ *
17
+ * @example
18
+ * serializePlainObject({ b: 2, a: 1 }, new Map()); // "{'a':1,'b':2}"
19
+ */
20
+ function serializePlainObject(object, refs) {
21
+ const keys = Object.keys(object).sort();
22
+ let result = "{";
23
+ for (let i = 0; i < keys.length; i++) {
24
+ const key = keys[i];
25
+ if (i > 0) result += ",";
26
+ result += `${serializeString(key)}:${serializeValue(object[key], refs)}`;
27
+ }
28
+ return result + "}";
29
+ }
30
+ //#endregion
31
+ export { serializePlainObject };
@@ -0,0 +1,18 @@
1
+ //#region src/util/serialize/serializeString.ts
2
+ /**
3
+ * Serializes a string into a single-quoted string literal.
4
+ *
5
+ * The string is not escaped; the output is intended for hashing and
6
+ * change detection, not for parsing or re-evaluation.
7
+ *
8
+ * @param value - The string to serialize.
9
+ * @returns The serialized string.
10
+ *
11
+ * @example
12
+ * serializeString('abc'); // "'abc'"
13
+ */
14
+ function serializeString(value) {
15
+ return `'${value}'`;
16
+ }
17
+ //#endregion
18
+ exports.serializeString = serializeString;
@@ -0,0 +1,18 @@
1
+ //#region src/util/serialize/serializeString.ts
2
+ /**
3
+ * Serializes a string into a single-quoted string literal.
4
+ *
5
+ * The string is not escaped; the output is intended for hashing and
6
+ * change detection, not for parsing or re-evaluation.
7
+ *
8
+ * @param value - The string to serialize.
9
+ * @returns The serialized string.
10
+ *
11
+ * @example
12
+ * serializeString('abc'); // "'abc'"
13
+ */
14
+ function serializeString(value) {
15
+ return `'${value}'`;
16
+ }
17
+ //#endregion
18
+ export { serializeString };
@@ -0,0 +1,23 @@
1
+ const require_serializeString = require("./serializeString.js");
2
+ //#region src/util/serialize/serializeSymbol.ts
3
+ /**
4
+ * Serializes a symbol into a string using its description.
5
+ *
6
+ * The description is quoted like any other string, so a symbol without a
7
+ * description (`Symbol()`) is distinguishable from one with an empty
8
+ * description (`Symbol('')`). Note that two different symbols with the same
9
+ * description still serialize to the same string, since a symbol's identity
10
+ * cannot be captured in a string.
11
+ *
12
+ * @param value - The symbol to serialize.
13
+ * @returns The serialized string.
14
+ *
15
+ * @example
16
+ * serializeSymbol(Symbol('test')); // "Symbol('test')"
17
+ * serializeSymbol(Symbol()); // "Symbol()"
18
+ */
19
+ function serializeSymbol(value) {
20
+ return value.description === void 0 ? "Symbol()" : `Symbol(${require_serializeString.serializeString(value.description)})`;
21
+ }
22
+ //#endregion
23
+ exports.serializeSymbol = serializeSymbol;
@@ -0,0 +1,23 @@
1
+ import { serializeString } from "./serializeString.mjs";
2
+ //#region src/util/serialize/serializeSymbol.ts
3
+ /**
4
+ * Serializes a symbol into a string using its description.
5
+ *
6
+ * The description is quoted like any other string, so a symbol without a
7
+ * description (`Symbol()`) is distinguishable from one with an empty
8
+ * description (`Symbol('')`). Note that two different symbols with the same
9
+ * description still serialize to the same string, since a symbol's identity
10
+ * cannot be captured in a string.
11
+ *
12
+ * @param value - The symbol to serialize.
13
+ * @returns The serialized string.
14
+ *
15
+ * @example
16
+ * serializeSymbol(Symbol('test')); // "Symbol('test')"
17
+ * serializeSymbol(Symbol()); // "Symbol()"
18
+ */
19
+ function serializeSymbol(value) {
20
+ return value.description === void 0 ? "Symbol()" : `Symbol(${serializeString(value.description)})`;
21
+ }
22
+ //#endregion
23
+ export { serializeSymbol };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "es-toolkit",
3
- "version": "1.52.0-dev.2094+ff2aa64a",
3
+ "version": "1.52.0-dev.2096+6380c75e",
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",
@@ -212,6 +212,28 @@
212
212
  "default": "./dist/util/index.js"
213
213
  }
214
214
  },
215
+ "./util/hash": {
216
+ "node": {
217
+ "import": {
218
+ "types": "./dist/util/hash/node.d.mts",
219
+ "default": "./dist/util/hash/node.mjs"
220
+ },
221
+ "require": {
222
+ "types": "./dist/util/hash/node.d.ts",
223
+ "default": "./dist/util/hash/node.js"
224
+ }
225
+ },
226
+ "default": {
227
+ "import": {
228
+ "types": "./dist/util/hash/browser.d.mts",
229
+ "default": "./dist/util/hash/browser.mjs"
230
+ },
231
+ "require": {
232
+ "types": "./dist/util/hash/browser.d.ts",
233
+ "default": "./dist/util/hash/browser.js"
234
+ }
235
+ }
236
+ },
215
237
  "./package.json": "./package.json"
216
238
  },
217
239
  "files": [
@@ -226,6 +248,8 @@
226
248
  "fp.js",
227
249
  "fp/iterator.js",
228
250
  "fp/iterator.d.ts",
251
+ "util/hash.js",
252
+ "util/hash.d.ts",
229
253
  "function.js",
230
254
  "iterator.js",
231
255
  "map.js",
@@ -499,6 +523,28 @@
499
523
  "default": "./dist/util/index.js"
500
524
  }
501
525
  },
526
+ "./util/hash": {
527
+ "node": {
528
+ "import": {
529
+ "types": "./dist/util/hash/node.d.mts",
530
+ "default": "./dist/util/hash/node.mjs"
531
+ },
532
+ "require": {
533
+ "types": "./dist/util/hash/node.d.ts",
534
+ "default": "./dist/util/hash/node.js"
535
+ }
536
+ },
537
+ "default": {
538
+ "import": {
539
+ "types": "./dist/util/hash/browser.d.mts",
540
+ "default": "./dist/util/hash/browser.mjs"
541
+ },
542
+ "require": {
543
+ "types": "./dist/util/hash/browser.d.ts",
544
+ "default": "./dist/util/hash/browser.js"
545
+ }
546
+ }
547
+ },
502
548
  "./package.json": "./package.json"
503
549
  },
504
550
  "types": "./dist/index.d.ts"
package/util/hash.d.ts ADDED
@@ -0,0 +1 @@
1
+ export * from '../dist/util/hash/node';
package/util/hash.js ADDED
@@ -0,0 +1 @@
1
+ module.exports = require('../dist/util/hash/node');