Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/v3' into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
Asherda committed Nov 1, 2021
2 parents 7f44a6c + 65b5e1f commit 473bad3
Showing 1 changed file with 33 additions and 28 deletions.
61 changes: 33 additions & 28 deletions shims/stringify.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,36 @@
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
// IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

function stringify(obj, replacer, spaces, cycleReplacer) {
return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces)
}

function serializer(replacer, cycleReplacer) {
var stack = [], keys = []

if (cycleReplacer == null) cycleReplacer = function(key, value) {
if (stack[0] === value) return "[Circular ~]"
return "[Circular ~." + keys.slice(0, stack.indexOf(value)).join(".") + "]"
}

return function(key, value) {
if (stack.length > 0) {
var thisPos = stack.indexOf(this)
~thisPos ? stack.splice(thisPos + 1) : stack.push(this)
~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key)
if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value)
}
else stack.push(value)

return replacer == null ? value : replacer.call(this, key, value)
}
}

JSON.origStringify = JSON.stringify;

JSON.stringify = (obj, replacer, spaces, cycleReplacer) => JSON.origStringify(obj, serializer(replacer, cycleReplacer), spaces);
// This shim exists to resolve the "cannot serialize cyclic structures" error
// in certain versions of react native. It breaks the stringification of ETH transactions
// and may prevent ETH transactions from displaying properly. If the cyclic structures
// bug returns, uncomment and fix the "replacer.call is not a function" bug on line 43.

// function stringify(obj, replacer, spaces, cycleReplacer) {
// return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces)
// }

// function serializer(replacer, cycleReplacer) {
// var stack = [], keys = []

// if (cycleReplacer == null) cycleReplacer = function(key, value) {
// if (stack[0] === value) return "[Circular ~]"
// return "[Circular ~." + keys.slice(0, stack.indexOf(value)).join(".") + "]"
// }

// return function(key, value) {
// if (stack.length > 0) {
// var thisPos = stack.indexOf(this)
// ~thisPos ? stack.splice(thisPos + 1) : stack.push(this)
// ~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key)
// if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value)
// }
// else stack.push(value)

// return replacer == null ? value : replacer.call(this, key, value)
// }
// }

// JSON.origStringify = JSON.stringify;

// JSON.stringify = (obj, replacer, spaces, cycleReplacer) => JSON.origStringify(obj, serializer(replacer, cycleReplacer), spaces);

0 comments on commit 473bad3

Please sign in to comment.