json-canonicalize
Version:
JSON canonicalize function
182 lines (179 loc) • 6.76 kB
JavaScript
function canonicalize(obj, allowCircular) {
let buffer = '';
const visited = new WeakMap();
serialize(obj);
return buffer;
function serialize(object) {
if (object === null ||
typeof object !== 'object' ||
object.toJSON != null) {
/////////////////////////////////////////////////
// Primitive data type - Use ES6/JSON //
/////////////////////////////////////////////////
buffer += JSON.stringify(object);
// } else if (object instanceof Date) {
// buffer += JSON.stringify(object);
}
else if (Array.isArray(object)) {
/////////////////////////////////////////////////
// Array - Maintain element order //
/////////////////////////////////////////////////
if (visited.has(object)) {
if (!allowCircular) {
throw new Error('Circular reference detected');
}
buffer += '"[Circular]"';
return;
}
visited.set(object, true);
buffer += '[';
let next = false;
object.forEach((element) => {
if (next) {
buffer += ',';
}
next = true;
if (element === undefined) {
element = null;
}
/////////////////////////////////////////
// Array element - Recursive expansion //
/////////////////////////////////////////
serialize(element);
});
buffer += ']';
}
else {
/////////////////////////////////////////////////
// Object - Sort properties before serializing //
/////////////////////////////////////////////////
if (visited.has(object)) {
if (!allowCircular) {
throw new Error('Circular reference detected');
}
buffer += '"[Circular]"';
return;
}
visited.set(object, true);
buffer += '{';
const vKeys = Object.keys(object).filter((k) => object[k] !== undefined).sort();
vKeys.forEach((property, index) => addProp(object, property, index));
buffer += '}';
}
}
function addProp(object, property, index) {
if (index > 0) {
buffer += ',';
}
///////////////////////////////////////////////
// Property names are strings - Use ES6/JSON //
///////////////////////////////////////////////
buffer += JSON.stringify(property);
buffer += ':';
//////////////////////////////////////////
// Property value - Recursive expansion //
//////////////////////////////////////////
serialize(object[property]);
}
}
function canonicalizeEx(obj, options) {
let buffer = '';
const vInclude = options && options.include;
let vExclude = options && options.exclude;
if (vExclude) {
if (typeof vExclude === 'string')
vExclude = [vExclude];
}
if (vInclude)
vInclude.sort();
const visited = new WeakMap();
const allowCircular = options && (options === null || options === void 0 ? void 0 : options.allowCircular);
serialize(obj);
return buffer;
function serialize(object, parent) {
if (object === null ||
typeof object !== 'object' ||
object.toJSON != null) {
/////////////////////////////////////////////////
// Primitive data type - Use ES6/JSON //
/////////////////////////////////////////////////
buffer += JSON.stringify(object);
// } else if (object instanceof Date) {
// buffer += JSON.stringify(object);
}
else if (Array.isArray(object)) {
/////////////////////////////////////////////////
// Array - Maintain element order //
/////////////////////////////////////////////////
if (visited.has(object)) {
if (!allowCircular) {
throw new Error('Circular reference detected');
}
buffer += '"[Circular]"';
return;
}
visited.set(object, true);
buffer += '[';
let next = false;
object.forEach((element) => {
if (next) {
buffer += ',';
}
next = true;
/////////////////////////////////////////
// Array element - Recursive expansion //
/////////////////////////////////////////
serialize(element, object);
});
buffer += ']';
}
else {
/////////////////////////////////////////////////
// Object - Sort properties before serializing //
/////////////////////////////////////////////////
if (visited.has(object)) {
if (!allowCircular) {
throw new Error('Circular reference detected');
}
buffer += '"[Circular]"';
return;
}
visited.set(object, true);
buffer += '{';
if (!parent && vInclude) {
vInclude.forEach((property, index) => {
if (!object.hasOwnProperty(property))
return;
addProp(object, property, index);
});
}
else {
const vKeys = Object.keys(object).sort();
vKeys.forEach((property, index) => addProp(object, property, index));
}
buffer += '}';
}
}
function addProp(object, property, index) {
if (vExclude && vExclude.length) {
for (const v of vExclude) {
if (v === property)
return;
}
}
if (index > 0) {
buffer += ',';
}
///////////////////////////////////////////////
// Property names are strings - Use ES6/JSON //
///////////////////////////////////////////////
buffer += JSON.stringify(property);
buffer += ':';
//////////////////////////////////////////
// Property value - Recursive expansion //
//////////////////////////////////////////
serialize(object[property], object);
}
}
export { canonicalize, canonicalizeEx };
//# sourceMappingURL=index.esm.js.map