From 8c312362f422e3b6c00e60a948df4a26d09c8e31 Mon Sep 17 00:00:00 2001 From: CleverLili <80835303+CleverLili@users.noreply.github.com> Date: Thu, 18 Mar 2021 11:08:50 +0000 Subject: [PATCH 1/3] Update index.ts toJSON is not called in all the appropriate places. This fix will call toJSON and fix the original data structure correctly. --- src/index.ts | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3598000..4f89c6e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,7 +35,7 @@ export default function devalue(value: any, level = defaultLogLevel) { } - function walk(thing: any) { + function walk(thing: any, parent:any, key:string, index:number) { if (typeof thing === 'function') { consola[level](`Cannot stringify a function ${thing.name}`) return @@ -60,15 +60,33 @@ export default function devalue(value: any, level = defaultLogLevel) { return; case 'Array': - thing.forEach(walk); + thing.forEach((item:any, index:number)=>{walk(item, parent, key, index)}); break; case 'Set': case 'Map': - Array.from(thing).forEach(walk); + Array.from(thing).forEach((item, index)=>{walk(item, parent,key, undefined)}); break; default: + if (thing && thing.toJSON) { + let json = thing.toJSON(); + if (getType(json) === 'String') { + // Try to parse the returned data + try { + json = JSON.parse(json); + } catch (e) { + json = thing; + }; + } + + if(typeof index !== 'undefined') + parent[key][index] = json; + else + parent[key] = json; + return; + } + const proto = Object.getPrototypeOf(thing); if ( @@ -82,14 +100,14 @@ export default function devalue(value: any, level = defaultLogLevel) { } else if (Object.getOwnPropertySymbols(thing).length > 0) { log(`Cannot stringify POJOs with symbolic keys ${Object.getOwnPropertySymbols(thing).map(symbol => symbol.toString())}`); } else { - Object.keys(thing).forEach(key => walk(thing[key])); + Object.keys(thing).forEach(function (key) { return walk(thing[key], thing, key, undefined); }); } } } } - walk(value); + walk(value, undefined, undefined, undefined); const names = new Map(); Array.from(counts) From 1a47c83788ea8f98829f87831692f66dd4fd907f Mon Sep 17 00:00:00 2001 From: CleverLili <80835303+CleverLili@users.noreply.github.com> Date: Thu, 18 Mar 2021 11:08:50 +0000 Subject: [PATCH 2/3] Fix: toJSON is not called in all the appropriate places. This fix will call toJSON and fix the original data structure correctly. --- src/index.ts | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/src/index.ts b/src/index.ts index 3598000..4f89c6e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -35,7 +35,7 @@ export default function devalue(value: any, level = defaultLogLevel) { } - function walk(thing: any) { + function walk(thing: any, parent:any, key:string, index:number) { if (typeof thing === 'function') { consola[level](`Cannot stringify a function ${thing.name}`) return @@ -60,15 +60,33 @@ export default function devalue(value: any, level = defaultLogLevel) { return; case 'Array': - thing.forEach(walk); + thing.forEach((item:any, index:number)=>{walk(item, parent, key, index)}); break; case 'Set': case 'Map': - Array.from(thing).forEach(walk); + Array.from(thing).forEach((item, index)=>{walk(item, parent,key, undefined)}); break; default: + if (thing && thing.toJSON) { + let json = thing.toJSON(); + if (getType(json) === 'String') { + // Try to parse the returned data + try { + json = JSON.parse(json); + } catch (e) { + json = thing; + }; + } + + if(typeof index !== 'undefined') + parent[key][index] = json; + else + parent[key] = json; + return; + } + const proto = Object.getPrototypeOf(thing); if ( @@ -82,14 +100,14 @@ export default function devalue(value: any, level = defaultLogLevel) { } else if (Object.getOwnPropertySymbols(thing).length > 0) { log(`Cannot stringify POJOs with symbolic keys ${Object.getOwnPropertySymbols(thing).map(symbol => symbol.toString())}`); } else { - Object.keys(thing).forEach(key => walk(thing[key])); + Object.keys(thing).forEach(function (key) { return walk(thing[key], thing, key, undefined); }); } } } } - walk(value); + walk(value, undefined, undefined, undefined); const names = new Map(); Array.from(counts) From 1d247575fd87dfcb1e1277acc6b822fa111c1517 Mon Sep 17 00:00:00 2001 From: CleverLili <80835303+CleverLili@users.noreply.github.com> Date: Sat, 20 Mar 2021 16:54:13 +0000 Subject: [PATCH 3/3] code lost during pull request mess. --- src/index.ts | 37 +++++++++++++++++++++++++++++-------- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/src/index.ts b/src/index.ts index 4f89c6e..071a20d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -24,6 +24,7 @@ const logLimit = parseInt(process.env.NUXT_ENV_DEVALUE_LOG_LIMIT) || 99; export default function devalue(value: any, level = defaultLogLevel) { const counts = new Map(); + const mapObjToJSON = new Map(); let logNum = 0; @@ -70,14 +71,34 @@ export default function devalue(value: any, level = defaultLogLevel) { default: if (thing && thing.toJSON) { - let json = thing.toJSON(); - if (getType(json) === 'String') { - // Try to parse the returned data - try { - json = JSON.parse(json); - } catch (e) { - json = thing; - }; + //remove it from existing counts map so we can come here again to replace. + counts.delete(thing); + + let json; + if( mapObjToJSON.has(thing) ){ + json = mapObjToJSON.get(thing); + + //increment the count as it used to do for "thing" + counts.set(json, counts.get(json) + 1); + } + else + { + json = thing.toJSON(); + if (getType(json) === 'String') { + // Try to parse the returned data + try { + json = JSON.parse(json); + } + catch (e) { + json = thing; + } + } + + //add to map object in case the object is referenced again + mapObjToJSON.set(thing, json); + + //set the count first time + counts.set(json, 1) } if(typeof index !== 'undefined')