From e8dbfe5145b2d143274ca4b41f53ffa40144f443 Mon Sep 17 00:00:00 2001 From: ZHAO JinXiang Date: Mon, 3 Jul 2023 16:14:01 +0800 Subject: [PATCH] feat: add esm version --- index.d.mts | 2 ++ index.d.ts | 6 ++---- index.mjs | 59 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 7 +++++++ 4 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 index.d.mts create mode 100644 index.mjs diff --git a/index.d.mts b/index.d.mts new file mode 100644 index 0000000..fba7caf --- /dev/null +++ b/index.d.mts @@ -0,0 +1,2 @@ +declare function stringify(obj: any): string; +export default stringify; diff --git a/index.d.ts b/index.d.ts index 23e46ca..f24153e 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,2 @@ -declare module 'fast-json-stable-stringify' { - function stringify(obj: any): string; - export = stringify; -} +declare function stringify(obj: any): string; +export = stringify; diff --git a/index.mjs b/index.mjs new file mode 100644 index 0000000..74072dc --- /dev/null +++ b/index.mjs @@ -0,0 +1,59 @@ +'use strict'; + +export default function (data, opts) { + if (!opts) opts = {}; + if (typeof opts === 'function') opts = { cmp: opts }; + var cycles = (typeof opts.cycles === 'boolean') ? opts.cycles : false; + + var cmp = opts.cmp && (function (f) { + return function (node) { + return function (a, b) { + var aobj = { key: a, value: node[a] }; + var bobj = { key: b, value: node[b] }; + return f(aobj, bobj); + }; + }; + })(opts.cmp); + + var seen = []; + return (function stringify (node) { + if (node && node.toJSON && typeof node.toJSON === 'function') { + node = node.toJSON(); + } + + if (node === undefined) return; + if (typeof node == 'number') return isFinite(node) ? '' + node : 'null'; + if (typeof node !== 'object') return JSON.stringify(node); + + var i, out; + if (Array.isArray(node)) { + out = '['; + for (i = 0; i < node.length; i++) { + if (i) out += ','; + out += stringify(node[i]) || 'null'; + } + return out + ']'; + } + + if (node === null) return 'null'; + + if (seen.indexOf(node) !== -1) { + if (cycles) return JSON.stringify('__cycle__'); + throw new TypeError('Converting circular structure to JSON'); + } + + var seenIndex = seen.push(node) - 1; + var keys = Object.keys(node).sort(cmp && cmp(node)); + out = ''; + for (i = 0; i < keys.length; i++) { + var key = keys[i]; + var value = stringify(node[key]); + + if (!value) continue; + if (out) out += ','; + out += JSON.stringify(key) + ':' + value; + } + seen.splice(seenIndex, 1); + return '{' + out + '}'; + })(data); +}; diff --git a/package.json b/package.json index 754d4d2..d099d6d 100644 --- a/package.json +++ b/package.json @@ -4,6 +4,12 @@ "description": "deterministic `JSON.stringify()` - a faster version of substack's json-stable-strigify without jsonify", "main": "index.js", "types": "index.d.ts", + "exports": { + ".": { + "import": "./index.mjs", + "require": "./index.js" + } + }, "dependencies": {}, "devDependencies": { "benchmark": "^2.1.4", @@ -17,6 +23,7 @@ "tape": "^4.11.0" }, "scripts": { + "build": "node --eval \"const content = fs.readFileSync('./index.js', 'utf8').replace('module.exports =', 'export default'); fs.writeFileSync('./index.mjs', content, 'utf8');\"", "eslint": "eslint index.js test", "test-spec": "tape test/*.js", "test": "npm run eslint && nyc npm run test-spec"