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
57 changes: 57 additions & 0 deletions esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
export default function stringify(data, opts) {
if (!opts) opts = {};
if (typeof opts === 'function') opts = { cmp: opts };
const cycles = (typeof opts.cycles === 'boolean') ? opts.cycles : false;

const cmp = opts.cmp && (function (f) {
return function (node) {
return function (a, b) {
const aobj = { key: a, value: node[a] };
const bobj = { key: b, value: node[b] };
return f(aobj, bobj);
};
};
})(opts.cmp);

const seen = [];
return (function stringifyNode(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);

let i, out;
if (Array.isArray(node)) {
out = '[';
for (i = 0; i < node.length; i++) {
if (i) out += ',';
out += stringifyNode(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');
}

const seenIndex = seen.push(node) - 1;
const keys = Object.keys(node).sort(cmp && cmp(node));
out = '';
for (i = 0; i < keys.length; i++) {
const key = keys[i];
const value = stringifyNode(node[key]);

if (!value) continue;
if (out) out += ',';
out += JSON.stringify(key) + ':' + value;
}
seen.splice(seenIndex, 1);
return '{' + out + '}';
})(data);
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"version": "2.0.0",
"description": "deterministic `JSON.stringify()` - a faster version of substack's json-stable-strigify without jsonify",
"main": "index.js",
"module": "esm.js",
"types": "index.d.ts",
"dependencies": {},
"devDependencies": {
Expand Down