-
Notifications
You must be signed in to change notification settings - Fork 935
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds better circular dependency checking and value replacements. (#437)
- Loading branch information
Showing
7 changed files
with
191 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
packages/reactotron-core-server/src/repairSerialization.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* JSON.stringify() clobbers falsy values, so we had to "encode" those | ||
* values before that step on the client. | ||
* | ||
* This is the "decoding" part of that equation. | ||
* | ||
* Thanks Stack Overflow. | ||
* | ||
* https://stackoverflow.com/questions/30128834/deep-changing-values-in-a-javascript-object | ||
*/ | ||
// on the server side, we'll be swapping out these values | ||
const replacements = Object.create(null) | ||
replacements['~~~ undefined ~~~'] = undefined | ||
replacements['~~~ null ~~~'] = null | ||
replacements['~~~ false ~~~'] = false | ||
replacements['~~~ zero ~~~'] = 0 | ||
replacements['~~~ empty string ~~~'] = '' | ||
replacements['~~~ anonymous function ~~~'] = 'fn()' | ||
replacements['~~~ NaN ~~~'] = NaN | ||
replacements['~~~ Infinity ~~~'] = Infinity | ||
replacements['~~~ -Infinity ~~~'] = -Infinity | ||
|
||
/** | ||
* Walks an object replacing any values with new values. This mutates! | ||
* | ||
* @param {*} payload The object | ||
* @return {*} The same object with some values replaced. | ||
*/ | ||
export function repair (payload) { | ||
// we only want objects | ||
if (typeof payload !== 'object') return payload | ||
|
||
// the recursive iterator | ||
function walker (obj) { | ||
let k | ||
const has = Object.prototype.hasOwnProperty.bind(obj) | ||
for (k in obj) { | ||
if (has(k)) { | ||
switch (typeof obj[k]) { | ||
// should we recurse thru sub-objects and arrays? | ||
case 'object': | ||
walker(obj[k]) | ||
break | ||
|
||
// mutate in-place with one of our replacements | ||
case 'string': | ||
if (obj[k].toLowerCase() in replacements) { | ||
// look for straight up replacements | ||
obj[k] = replacements[obj[k].toLowerCase()] | ||
} else if (obj[k].length > 9) { | ||
// fancy function replacements | ||
if (obj[k].startsWith('~~~ ') && obj[k].endsWith(' ~~~')) { | ||
obj[k] = obj[k].replace(/~~~/g, '') | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
// set it running | ||
walker(payload) | ||
} |