json-canonicalize
Advanced tools
Comparing version
@@ -1,3 +0,4 @@ | ||
function canonicalize(obj) { | ||
function canonicalize(obj, allowCircular) { | ||
let buffer = ''; | ||
const visited = new WeakMap(); | ||
serialize(obj); | ||
@@ -20,2 +21,10 @@ return buffer; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '['; | ||
@@ -42,2 +51,10 @@ let next = false; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '{'; | ||
@@ -75,2 +92,4 @@ const vKeys = Object.keys(object).filter((k) => object[k] !== undefined).sort(); | ||
vInclude.sort(); | ||
const visited = new WeakMap(); | ||
const allowCircular = options && (options === null || options === void 0 ? void 0 : options.allowCircular); | ||
serialize(obj); | ||
@@ -93,2 +112,10 @@ return buffer; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '['; | ||
@@ -112,2 +139,10 @@ let next = false; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '{'; | ||
@@ -114,0 +149,0 @@ if (!parent && vInclude) { |
@@ -1,2 +0,2 @@ | ||
function t(t){let e="";return r(t),e;function r(t){if(null===t||"object"!=typeof t||null!=t.toJSON)e+=JSON.stringify(t);else if(Array.isArray(t)){e+="[";let n=!1;t.forEach((t=>{n&&(e+=","),n=!0,void 0===t&&(t=null),r(t)})),e+="]"}else{e+="{";Object.keys(t).filter((e=>void 0!==t[e])).sort().forEach(((n,o)=>function(t,n,o){o>0&&(e+=",");e+=JSON.stringify(n),e+=":",r(t[n])}(t,n,o))),e+="}"}}}function e(t,e){let r="";const n=e&&e.include;let o=e&&e.exclude;return o&&"string"==typeof o&&(o=[o]),n&&n.sort(),f(t),r;function f(t,e){if(null===t||"object"!=typeof t||null!=t.toJSON)r+=JSON.stringify(t);else if(Array.isArray(t)){r+="[";let e=!1;t.forEach((n=>{e&&(r+=","),e=!0,f(n,t)})),r+="]"}else{if(r+="{",!e&&n)n.forEach(((e,r)=>{t.hasOwnProperty(e)&&i(t,e,r)}));else{Object.keys(t).sort().forEach(((e,r)=>i(t,e,r)))}r+="}"}}function i(t,e,n){if(o&&o.length)for(const t of o)if(t===e)return;n>0&&(r+=","),r+=JSON.stringify(e),r+=":",f(t[e],t)}}export{t as canonicalize,e as canonicalizeEx}; | ||
function r(r,e){let t="";const i=new WeakMap;return n(r),t;function n(r){if(null===r||"object"!=typeof r||null!=r.toJSON)t+=JSON.stringify(r);else if(Array.isArray(r)){if(i.has(r)){if(!e)throw new Error("Circular reference detected");return void(t+='"[Circular]"')}i.set(r,!0),t+="[";let o=!1;r.forEach((r=>{o&&(t+=","),o=!0,void 0===r&&(r=null),n(r)})),t+="]"}else{if(i.has(r)){if(!e)throw new Error("Circular reference detected");return void(t+='"[Circular]"')}i.set(r,!0),t+="{";Object.keys(r).filter((e=>void 0!==r[e])).sort().forEach(((e,i)=>function(r,e,i){i>0&&(t+=",");t+=JSON.stringify(e),t+=":",n(r[e])}(r,e,i))),t+="}"}}}function e(r,e){let t="";const i=e&&e.include;let n=e&&e.exclude;n&&"string"==typeof n&&(n=[n]),i&&i.sort();const o=new WeakMap,f=e&&(null==e?void 0:e.allowCircular);return c(r),t;function c(r,e){if(null===r||"object"!=typeof r||null!=r.toJSON)t+=JSON.stringify(r);else if(Array.isArray(r)){if(o.has(r)){if(!f)throw new Error("Circular reference detected");return void(t+='"[Circular]"')}o.set(r,!0),t+="[";let e=!1;r.forEach((i=>{e&&(t+=","),e=!0,c(i,r)})),t+="]"}else{if(o.has(r)){if(!f)throw new Error("Circular reference detected");return void(t+='"[Circular]"')}if(o.set(r,!0),t+="{",!e&&i)i.forEach(((e,t)=>{r.hasOwnProperty(e)&&l(r,e,t)}));else{Object.keys(r).sort().forEach(((e,t)=>l(r,e,t)))}t+="}"}}function l(r,e,i){if(n&&n.length)for(const r of n)if(r===e)return;i>0&&(t+=","),t+=JSON.stringify(e),t+=":",c(r[e],r)}}export{r as canonicalize,e as canonicalizeEx}; | ||
//# sourceMappingURL=index.esm.min.js.map |
@@ -7,4 +7,5 @@ (function (global, factory) { | ||
function canonicalize(obj) { | ||
function canonicalize(obj, allowCircular) { | ||
var buffer = ''; | ||
var visited = new WeakMap(); | ||
serialize(obj); | ||
@@ -27,2 +28,10 @@ return buffer; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '['; | ||
@@ -49,2 +58,10 @@ var next_1 = false; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '{'; | ||
@@ -82,2 +99,4 @@ var vKeys = Object.keys(object).filter(function (k) { return object[k] !== undefined; }).sort(); | ||
vInclude.sort(); | ||
var visited = new WeakMap(); | ||
var allowCircular = options && (options === null || options === void 0 ? void 0 : options.allowCircular); | ||
serialize(obj); | ||
@@ -100,2 +119,10 @@ return buffer; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '['; | ||
@@ -119,2 +146,10 @@ var next_1 = false; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '{'; | ||
@@ -121,0 +156,0 @@ if (!parent && vInclude) { |
@@ -7,5 +7,5 @@ (function (global, factory) { | ||
function canonicalize(n){var r="";return i(n),r;function i(n){if(null===n||"object"!=typeof n||null!=n.toJSON)r+=JSON.stringify(n);else if(Array.isArray(n)){r+="[";var t=!1;n.forEach((function(n){t&&(r+=","),t=!0,void 0===n&&(n=null),i(n);})),r+="]";}else {r+="{",Object.keys(n).filter((function(r){return void 0!==n[r]})).sort().forEach((function(t,o){return function(n,t,o){o>0&&(r+=",");r+=JSON.stringify(t),r+=":",i(n[t]);}(n,t,o)})),r+="}";}}} | ||
function canonicalize(r,e){var n="",i=new WeakMap;return t(r),n;function t(r){if(null===r||"object"!=typeof r||null!=r.toJSON)n+=JSON.stringify(r);else if(Array.isArray(r)){if(i.has(r)){if(!e)throw new Error("Circular reference detected");return void(n+='"[Circular]"')}i.set(r,!0),n+="[";var o=!1;r.forEach((function(r){o&&(n+=","),o=!0,void 0===r&&(r=null),t(r);})),n+="]";}else {if(i.has(r)){if(!e)throw new Error("Circular reference detected");return void(n+='"[Circular]"')}i.set(r,!0),n+="{",Object.keys(r).filter((function(e){return void 0!==r[e]})).sort().forEach((function(e,i){return function(r,e,i){i>0&&(n+=",");n+=JSON.stringify(e),n+=":",t(r[e]);}(r,e,i)})),n+="}";}}} | ||
function canonicalizeEx(n,r){var t="",e=r&&r.include,i=r&&r.exclude;return i&&"string"==typeof i&&(i=[i]),e&&e.sort(),f(n),t;function f(n,r){if(null===n||"object"!=typeof n||null!=n.toJSON)t+=JSON.stringify(n);else if(Array.isArray(n)){t+="[";var i=!1;n.forEach((function(r){i&&(t+=","),i=!0,f(r,n);})),t+="]";}else {if(t+="{",!r&&e)e.forEach((function(r,t){n.hasOwnProperty(r)&&o(n,r,t);}));else Object.keys(n).sort().forEach((function(r,t){return o(n,r,t)}));t+="}";}}function o(n,r,e){if(i&&i.length)for(var o=0,c=i;o<c.length;o++){if(c[o]===r)return}e>0&&(t+=","),t+=JSON.stringify(r),t+=":",f(n[r],n);}} | ||
function canonicalizeEx(r,e){var n="",t=e&&e.include,i=e&&e.exclude;i&&"string"==typeof i&&(i=[i]),t&&t.sort();var o=new WeakMap,f=e&&(null==e?void 0:e.allowCircular);return c(r),n;function c(r,e){if(null===r||"object"!=typeof r||null!=r.toJSON)n+=JSON.stringify(r);else if(Array.isArray(r)){if(o.has(r)){if(!f)throw new Error("Circular reference detected");return void(n+='"[Circular]"')}o.set(r,!0),n+="[";var i=!1;r.forEach((function(e){i&&(n+=","),i=!0,c(e,r);})),n+="]";}else {if(o.has(r)){if(!f)throw new Error("Circular reference detected");return void(n+='"[Circular]"')}if(o.set(r,!0),n+="{",!e&&t)t.forEach((function(e,n){r.hasOwnProperty(e)&&a(r,e,n);}));else Object.keys(r).sort().forEach((function(e,n){return a(r,e,n)}));n+="}";}}function a(r,e,t){if(i&&i.length)for(var o=0,f=i;o<f.length;o++){if(f[o]===e)return}t>0&&(n+=","),n+=JSON.stringify(e),n+=":",c(r[e],r);}} | ||
@@ -12,0 +12,0 @@ exports.canonicalize = canonicalize; |
@@ -42,3 +42,38 @@ // tslint:disable max-line-length | ||
}); | ||
it('should throw error when canonicalize array item circular ref', () => { | ||
const obj = { | ||
arr: [undefined, null, 56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.arr.push(obj.arr); | ||
expect(() => canonicalize(obj)).toThrowError('Circular reference detected'); | ||
}); | ||
it('should allow canonicalize array item circular ref', () => { | ||
const obj = { | ||
arr: [undefined, null, 56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.arr.push(obj.arr); | ||
expect(canonicalize(obj, { allowCircular: true })).toEqual('{"arr":[undefined,null,56,"a","12",{"a":123,"t":"455A"},"[Circular]"]}'); | ||
}); | ||
it('should throw error when canonicalize obj item circular ref', () => { | ||
const obj = { | ||
text: undefined, | ||
num: 47734.12, | ||
dt: new Date('2018-12-17T01:08:19.719Z'), | ||
arr: [56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.cir = obj; | ||
expect(() => canonicalize(obj)).toThrowError('Circular reference detected'); | ||
}); | ||
it('should allow canonicalize obj item circular ref', () => { | ||
const obj = { | ||
text: undefined, | ||
num: 47734.12, | ||
dt: new Date('2018-12-17T01:08:19.719Z'), | ||
arr: [56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.cir = obj; | ||
const result = canonicalize(obj, { allowCircular: true }); | ||
expect(result).toEqual('{"arr":[56,"a","12",{"a":123,"t":"455A"}],"cir":"[Circular]","dt":"2018-12-17T01:08:19.719Z","num":47734.12,"text":undefined}'); | ||
}); | ||
}); | ||
//# sourceMappingURL=canonicalize-ex.spec.js.map |
@@ -29,3 +29,40 @@ // tslint:disable max-line-length | ||
}); | ||
it('should throw error when canonicalize array item circular ref', () => { | ||
const obj = { | ||
arr: [undefined, null, 56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.arr.push(obj.arr); | ||
expect(() => canonicalize(obj)).toThrowError('Circular reference detected'); | ||
}); | ||
it('should allow canonicalize array item circular ref', () => { | ||
const obj = { | ||
arr: [undefined, null, 56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.arr.push(obj.arr); | ||
expect(canonicalize(obj, true)).toEqual('{"arr":[null,null,56,"a","12",{"a":123,"t":"455A"},"[Circular]"]}'); | ||
}); | ||
it('should throw error when canonicalize obj item circular ref', () => { | ||
const obj = { | ||
text: undefined, | ||
num: 47734.12, | ||
dt: new Date('2018-12-17T01:08:19.719Z'), | ||
arr: [56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.cir = obj; | ||
// expect(canonicalize(obj)).toEqual( | ||
// '{"arr":[56,"a","12",{"a":123,"t":"455A"}],"dt":"2018-12-17T01:08:19.719Z","num":47734.12}' | ||
// ) | ||
expect(() => canonicalize(obj)).toThrowError('Circular reference detected'); | ||
}); | ||
it('should allow canonicalize obj item circular ref', () => { | ||
const obj = { | ||
text: undefined, | ||
num: 47734.12, | ||
dt: new Date('2018-12-17T01:08:19.719Z'), | ||
arr: [56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.cir = obj; | ||
expect(canonicalize(obj, true)).toEqual('{"arr":[56,"a","12",{"a":123,"t":"455A"}],"cir":"[Circular]","dt":"2018-12-17T01:08:19.719Z","num":47734.12}'); | ||
}); | ||
}); | ||
//# sourceMappingURL=canonicalize.spec.js.map |
@@ -11,2 +11,4 @@ export function canonicalizeEx(obj, options) { | ||
vInclude.sort(); | ||
const visited = new WeakMap(); | ||
const allowCircular = options && (options === null || options === void 0 ? void 0 : options.allowCircular); | ||
serialize(obj); | ||
@@ -29,2 +31,10 @@ return buffer; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '['; | ||
@@ -48,2 +58,10 @@ let next = false; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '{'; | ||
@@ -50,0 +68,0 @@ if (!parent && vInclude) { |
@@ -1,3 +0,4 @@ | ||
export function canonicalize(obj) { | ||
export function canonicalize(obj, allowCircular) { | ||
let buffer = ''; | ||
const visited = new WeakMap(); | ||
serialize(obj); | ||
@@ -20,2 +21,10 @@ return buffer; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '['; | ||
@@ -42,2 +51,10 @@ let next = false; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '{'; | ||
@@ -44,0 +61,0 @@ const vKeys = Object.keys(object).filter((k) => object[k] !== undefined).sort(); |
@@ -42,3 +42,38 @@ // tslint:disable max-line-length | ||
}); | ||
it('should throw error when canonicalize array item circular ref', function () { | ||
var obj = { | ||
arr: [undefined, null, 56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.arr.push(obj.arr); | ||
expect(function () { return canonicalize(obj); }).toThrowError('Circular reference detected'); | ||
}); | ||
it('should allow canonicalize array item circular ref', function () { | ||
var obj = { | ||
arr: [undefined, null, 56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.arr.push(obj.arr); | ||
expect(canonicalize(obj, { allowCircular: true })).toEqual('{"arr":[undefined,null,56,"a","12",{"a":123,"t":"455A"},"[Circular]"]}'); | ||
}); | ||
it('should throw error when canonicalize obj item circular ref', function () { | ||
var obj = { | ||
text: undefined, | ||
num: 47734.12, | ||
dt: new Date('2018-12-17T01:08:19.719Z'), | ||
arr: [56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.cir = obj; | ||
expect(function () { return canonicalize(obj); }).toThrowError('Circular reference detected'); | ||
}); | ||
it('should allow canonicalize obj item circular ref', function () { | ||
var obj = { | ||
text: undefined, | ||
num: 47734.12, | ||
dt: new Date('2018-12-17T01:08:19.719Z'), | ||
arr: [56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.cir = obj; | ||
var result = canonicalize(obj, { allowCircular: true }); | ||
expect(result).toEqual('{"arr":[56,"a","12",{"a":123,"t":"455A"}],"cir":"[Circular]","dt":"2018-12-17T01:08:19.719Z","num":47734.12,"text":undefined}'); | ||
}); | ||
}); | ||
//# sourceMappingURL=canonicalize-ex.spec.js.map |
@@ -29,3 +29,40 @@ // tslint:disable max-line-length | ||
}); | ||
it('should throw error when canonicalize array item circular ref', function () { | ||
var obj = { | ||
arr: [undefined, null, 56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.arr.push(obj.arr); | ||
expect(function () { return canonicalize(obj); }).toThrowError('Circular reference detected'); | ||
}); | ||
it('should allow canonicalize array item circular ref', function () { | ||
var obj = { | ||
arr: [undefined, null, 56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.arr.push(obj.arr); | ||
expect(canonicalize(obj, true)).toEqual('{"arr":[null,null,56,"a","12",{"a":123,"t":"455A"},"[Circular]"]}'); | ||
}); | ||
it('should throw error when canonicalize obj item circular ref', function () { | ||
var obj = { | ||
text: undefined, | ||
num: 47734.12, | ||
dt: new Date('2018-12-17T01:08:19.719Z'), | ||
arr: [56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.cir = obj; | ||
// expect(canonicalize(obj)).toEqual( | ||
// '{"arr":[56,"a","12",{"a":123,"t":"455A"}],"dt":"2018-12-17T01:08:19.719Z","num":47734.12}' | ||
// ) | ||
expect(function () { return canonicalize(obj); }).toThrowError('Circular reference detected'); | ||
}); | ||
it('should allow canonicalize obj item circular ref', function () { | ||
var obj = { | ||
text: undefined, | ||
num: 47734.12, | ||
dt: new Date('2018-12-17T01:08:19.719Z'), | ||
arr: [56, 'a', '12', { t: '455A', a: 123 }], | ||
}; | ||
obj.cir = obj; | ||
expect(canonicalize(obj, true)).toEqual('{"arr":[56,"a","12",{"a":123,"t":"455A"}],"cir":"[Circular]","dt":"2018-12-17T01:08:19.719Z","num":47734.12}'); | ||
}); | ||
}); | ||
//# sourceMappingURL=canonicalize.spec.js.map |
@@ -11,2 +11,4 @@ export function canonicalizeEx(obj, options) { | ||
vInclude.sort(); | ||
var visited = new WeakMap(); | ||
var allowCircular = options && (options === null || options === void 0 ? void 0 : options.allowCircular); | ||
serialize(obj); | ||
@@ -29,2 +31,10 @@ return buffer; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '['; | ||
@@ -48,2 +58,10 @@ var next_1 = false; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '{'; | ||
@@ -50,0 +68,0 @@ if (!parent && vInclude) { |
@@ -1,3 +0,4 @@ | ||
export function canonicalize(obj) { | ||
export function canonicalize(obj, allowCircular) { | ||
var buffer = ''; | ||
var visited = new WeakMap(); | ||
serialize(obj); | ||
@@ -20,2 +21,10 @@ return buffer; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '['; | ||
@@ -42,2 +51,10 @@ var next_1 = false; | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected'); | ||
} | ||
buffer += '"[Circular]"'; | ||
return; | ||
} | ||
visited.set(object, true); | ||
buffer += '{'; | ||
@@ -44,0 +61,0 @@ var vKeys = Object.keys(object).filter(function (k) { return object[k] !== undefined; }).sort(); |
{ | ||
"name": "json-canonicalize", | ||
"version": "1.0.6", | ||
"version": "1.1.0", | ||
"description": "JSON canonicalize function ", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -51,2 +51,47 @@ // tslint:disable max-line-length | ||
}) | ||
it('should throw error when canonicalize array item circular ref', () => { | ||
const obj: any = { | ||
arr: [undefined, null, 56, 'a', '12', { t: '455A', a: 123 }], | ||
} | ||
obj.arr.push(obj.arr); | ||
expect(() => canonicalize(obj)).toThrowError('Circular reference detected') | ||
}) | ||
it('should allow canonicalize array item circular ref', () => { | ||
const obj: any = { | ||
arr: [undefined, null, 56, 'a', '12', { t: '455A', a: 123 }], | ||
} | ||
obj.arr.push(obj.arr); | ||
expect(canonicalize(obj, {allowCircular: true})).toEqual( | ||
'{"arr":[undefined,null,56,"a","12",{"a":123,"t":"455A"},"[Circular]"]}' | ||
) | ||
}) | ||
it('should throw error when canonicalize obj item circular ref', () => { | ||
const obj: any = { | ||
text: undefined, | ||
num: 47734.12, | ||
dt: new Date('2018-12-17T01:08:19.719Z'), | ||
arr: [56, 'a', '12', { t: '455A', a: 123 }], | ||
} | ||
obj.cir = obj; | ||
expect(() => canonicalize(obj)).toThrowError('Circular reference detected') | ||
}) | ||
it('should allow canonicalize obj item circular ref', () => { | ||
const obj: any = { | ||
text: undefined, | ||
num: 47734.12, | ||
dt: new Date('2018-12-17T01:08:19.719Z'), | ||
arr: [56, 'a', '12', { t: '455A', a: 123 }], | ||
} | ||
obj.cir = obj; | ||
const result = canonicalize(obj, {allowCircular: true}) | ||
expect(result).toEqual( | ||
'{"arr":[56,"a","12",{"a":123,"t":"455A"}],"cir":"[Circular]","dt":"2018-12-17T01:08:19.719Z","num":47734.12,"text":undefined}' | ||
) | ||
}) | ||
}) |
@@ -36,2 +36,50 @@ // tslint:disable max-line-length | ||
}) | ||
it('should throw error when canonicalize array item circular ref', () => { | ||
const obj: any = { | ||
arr: [undefined, null, 56, 'a', '12', { t: '455A', a: 123 }], | ||
} | ||
obj.arr.push(obj.arr); | ||
expect(() => canonicalize(obj)).toThrowError('Circular reference detected') | ||
}) | ||
it('should allow canonicalize array item circular ref', () => { | ||
const obj: any = { | ||
arr: [undefined, null, 56, 'a', '12', { t: '455A', a: 123 }], | ||
} | ||
obj.arr.push(obj.arr); | ||
expect(canonicalize(obj, true)).toEqual( | ||
'{"arr":[null,null,56,"a","12",{"a":123,"t":"455A"},"[Circular]"]}' | ||
) | ||
}) | ||
it('should throw error when canonicalize obj item circular ref', () => { | ||
const obj: any = { | ||
text: undefined, | ||
num: 47734.12, | ||
dt: new Date('2018-12-17T01:08:19.719Z'), | ||
arr: [56, 'a', '12', { t: '455A', a: 123 }], | ||
} | ||
obj.cir = obj; | ||
// expect(canonicalize(obj)).toEqual( | ||
// '{"arr":[56,"a","12",{"a":123,"t":"455A"}],"dt":"2018-12-17T01:08:19.719Z","num":47734.12}' | ||
// ) | ||
expect(() => canonicalize(obj)).toThrowError('Circular reference detected') | ||
}) | ||
it('should allow canonicalize obj item circular ref', () => { | ||
const obj: any = { | ||
text: undefined, | ||
num: 47734.12, | ||
dt: new Date('2018-12-17T01:08:19.719Z'), | ||
arr: [56, 'a', '12', { t: '455A', a: 123 }], | ||
} | ||
obj.cir = obj; | ||
expect(canonicalize(obj, true)).toEqual( | ||
'{"arr":[56,"a","12",{"a":123,"t":"455A"}],"cir":"[Circular]","dt":"2018-12-17T01:08:19.719Z","num":47734.12}' | ||
) | ||
}) | ||
}) |
export interface IOptions { | ||
exclude?: string | string[] | ||
include?: string[] | ||
allowCircular?: boolean | ||
} | ||
@@ -15,2 +16,4 @@ | ||
const visited = new WeakMap<object, boolean>() | ||
const allowCircular = options && options?.allowCircular | ||
serialize(obj) | ||
@@ -37,2 +40,12 @@ | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected') | ||
} | ||
buffer += '"[Circular]"' | ||
return | ||
} | ||
visited.set(object, true) | ||
buffer += '[' | ||
@@ -55,2 +68,12 @@ let next = false | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected') | ||
} | ||
buffer += '"[Circular]"' | ||
return | ||
} | ||
visited.set(object, true) | ||
buffer += '{' | ||
@@ -57,0 +80,0 @@ if (!parent && vInclude) { |
@@ -1,4 +0,6 @@ | ||
export function canonicalize(obj: any) { | ||
export function canonicalize(obj: any, allowCircular?: boolean) { | ||
let buffer = '' | ||
const visited = new WeakMap<object, boolean>() | ||
serialize(obj) | ||
@@ -25,2 +27,12 @@ | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected') | ||
} | ||
buffer += '"[Circular]"' | ||
return | ||
} | ||
visited.set(object, true) | ||
buffer += '[' | ||
@@ -44,2 +56,12 @@ let next = false | ||
///////////////////////////////////////////////// | ||
if (visited.has(object)) { | ||
if (!allowCircular) { | ||
throw new Error('Circular reference detected') | ||
} | ||
buffer += '"[Circular]"' | ||
return | ||
} | ||
visited.set(object, true) | ||
buffer += '{' | ||
@@ -46,0 +68,0 @@ const vKeys = Object.keys(object).filter((k)=> object[k] !== undefined).sort() |
export interface IOptions { | ||
exclude?: string | string[]; | ||
include?: string[]; | ||
allowCircular?: boolean; | ||
} | ||
export declare function canonicalizeEx(obj: any, options?: IOptions): string; | ||
//# sourceMappingURL=canonicalize-ex.d.ts.map |
@@ -1,2 +0,2 @@ | ||
export declare function canonicalize(obj: any): string; | ||
export declare function canonicalize(obj: any, allowCircular?: boolean): string; | ||
//# sourceMappingURL=canonicalize.d.ts.map |
@@ -1,2 +0,2 @@ | ||
// Type definitions for json-canonicalize 1.0.6 | ||
// Type definitions for json-canonicalize 1.1.0 | ||
// Project: https://github.com/snowyu/json-canonicalize.ts | ||
@@ -3,0 +3,0 @@ // Definitions by: Riceball LEE <snowyu.lee@gmail.com> <https://github.com/snowyu> |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
125490
36.1%1431
39.47%