Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions index.d.mts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare function stringify(obj: any): string;
export default stringify;
6 changes: 2 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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;
59 changes: 59 additions & 0 deletions index.mjs
Original file line number Diff line number Diff line change
@@ -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);
};
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down