@solana/fast-stable-stringify 6.3.1 → 6.3.2-canary-20260313112147
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.
- package/package.json +3 -2
- package/src/index.ts +111 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@solana/fast-stable-stringify",
|
|
3
|
-
"version": "6.3.
|
|
3
|
+
"version": "6.3.2-canary-20260313112147",
|
|
4
4
|
"description": "Deterministic stringification for when performance and bundle size matters",
|
|
5
5
|
"exports": {
|
|
6
6
|
"edge-light": {
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"types": "./dist/types/index.d.ts",
|
|
33
33
|
"type": "commonjs",
|
|
34
34
|
"files": [
|
|
35
|
-
"./dist/"
|
|
35
|
+
"./dist/",
|
|
36
|
+
"./src/"
|
|
36
37
|
],
|
|
37
38
|
"sideEffects": false,
|
|
38
39
|
"keywords": [
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* This project is a fork of [nickyout/fast-stable-stringify](https://github.com/nickyout/fast-stable-stringify)
|
|
3
|
+
*
|
|
4
|
+
* The most popular repository providing this feature is [substack's json-stable-stringify](https://www.npmjs.com/package/json-stable-stringify). The intent of this library is to provide a faster alternative for when performance is more important than features. It assumes you provide basic javascript values without circular references, and returns a non-indented string.
|
|
5
|
+
*
|
|
6
|
+
* Just like substack's, it:
|
|
7
|
+
*
|
|
8
|
+
* - handles all variations of all basic javascript values (number, string, boolean, array, object, null, Date, BigInt)
|
|
9
|
+
* - handles undefined _and_ function in the same way as `JSON.stringify`
|
|
10
|
+
* - **does not support ie8 (and below) with complete certainty**.
|
|
11
|
+
*
|
|
12
|
+
* Unlike substack's, it:
|
|
13
|
+
*
|
|
14
|
+
* - does not implement the 'replacer' or 'space' arguments of the JSON.stringify method
|
|
15
|
+
* - does not check for circular references
|
|
16
|
+
*
|
|
17
|
+
* @example
|
|
18
|
+
* ```js
|
|
19
|
+
* import stringify from '@solana/fast-stable-stringify';
|
|
20
|
+
* stringify({ d: 0, c: 1, a: 2, b: 3, e: 4 }); // '{"a":2,"b":3,"c":1,"d":0,"e":4}'
|
|
21
|
+
* ```
|
|
22
|
+
*
|
|
23
|
+
* @packageDocumentation
|
|
24
|
+
*/
|
|
25
|
+
const objToString = Object.prototype.toString;
|
|
26
|
+
const objKeys =
|
|
27
|
+
Object.keys ||
|
|
28
|
+
function (obj) {
|
|
29
|
+
const keys = [];
|
|
30
|
+
for (const name in obj) {
|
|
31
|
+
keys.push(name);
|
|
32
|
+
}
|
|
33
|
+
return keys;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
function stringify(val: unknown, isArrayProp: boolean) {
|
|
37
|
+
let i, max, str, keys, key, propVal, toStr;
|
|
38
|
+
if (val === true) {
|
|
39
|
+
return 'true';
|
|
40
|
+
}
|
|
41
|
+
if (val === false) {
|
|
42
|
+
return 'false';
|
|
43
|
+
}
|
|
44
|
+
switch (typeof val) {
|
|
45
|
+
case 'object':
|
|
46
|
+
if (val === null) {
|
|
47
|
+
return null;
|
|
48
|
+
} else if ('toJSON' in val && typeof val.toJSON === 'function') {
|
|
49
|
+
return stringify(val.toJSON(), isArrayProp);
|
|
50
|
+
} else {
|
|
51
|
+
toStr = objToString.call(val);
|
|
52
|
+
if (toStr === '[object Array]') {
|
|
53
|
+
str = '[';
|
|
54
|
+
max = (val as unknown[]).length - 1;
|
|
55
|
+
for (i = 0; i < max; i++) {
|
|
56
|
+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
|
57
|
+
str += stringify((val as unknown[])[i], true) + ',';
|
|
58
|
+
}
|
|
59
|
+
if (max > -1) {
|
|
60
|
+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
|
61
|
+
str += stringify((val as unknown[])[i], true);
|
|
62
|
+
}
|
|
63
|
+
return str + ']';
|
|
64
|
+
} else if (toStr === '[object Object]') {
|
|
65
|
+
// only object is left
|
|
66
|
+
keys = objKeys(val).sort();
|
|
67
|
+
max = keys.length;
|
|
68
|
+
str = '';
|
|
69
|
+
i = 0;
|
|
70
|
+
while (i < max) {
|
|
71
|
+
key = keys[i];
|
|
72
|
+
propVal = stringify((val as Record<typeof key, unknown>)[key], false);
|
|
73
|
+
if (propVal !== undefined) {
|
|
74
|
+
if (str) {
|
|
75
|
+
str += ',';
|
|
76
|
+
}
|
|
77
|
+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
|
78
|
+
str += JSON.stringify(key) + ':' + propVal;
|
|
79
|
+
}
|
|
80
|
+
i++;
|
|
81
|
+
}
|
|
82
|
+
return '{' + str + '}';
|
|
83
|
+
} else {
|
|
84
|
+
return JSON.stringify(val);
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
case 'function':
|
|
88
|
+
case 'undefined':
|
|
89
|
+
return isArrayProp ? null : undefined;
|
|
90
|
+
case 'bigint':
|
|
91
|
+
return `${val.toString()}n`;
|
|
92
|
+
case 'string':
|
|
93
|
+
return JSON.stringify(val);
|
|
94
|
+
default:
|
|
95
|
+
return isFinite(val as number) ? val : null;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export default function (
|
|
100
|
+
val:
|
|
101
|
+
| Function // eslint-disable-line @typescript-eslint/no-unsafe-function-type
|
|
102
|
+
| undefined,
|
|
103
|
+
): undefined;
|
|
104
|
+
export default function (val: unknown): string;
|
|
105
|
+
export default function (val: unknown): string | undefined {
|
|
106
|
+
const returnVal = stringify(val, false);
|
|
107
|
+
if (returnVal !== undefined) {
|
|
108
|
+
// eslint-disable-next-line @typescript-eslint/restrict-plus-operands
|
|
109
|
+
return '' + returnVal;
|
|
110
|
+
}
|
|
111
|
+
}
|