Skip to content

Commit bfa1626

Browse files
committed
Make json-stringify-safe IE8 compatible
1 parent 399171c commit bfa1626

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed
Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
1+
'use strict';
2+
3+
/*
4+
json-stringify-safe
5+
Like JSON.stringify, but doesn't throw on circular references.
6+
7+
Originally forked from https://github.com/isaacs/json-stringify-safe
8+
version 5.0.1 on 3/8/2017 and modified for IE8 compatibility.
9+
Tests for this are in test/vendor.
10+
11+
ISC license: https://github.com/isaacs/json-stringify-safe/blob/master/LICENSE
12+
*/
13+
114
exports = module.exports = stringify
215
exports.getSerialize = serializer
316

17+
function indexOf(haystack, needle) {
18+
for (var i = 0; i < haystack.length; ++i) {
19+
if (haystack[i] === needle) return i;
20+
}
21+
return -1;
22+
}
23+
424
function stringify(obj, replacer, spaces, cycleReplacer) {
525
return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces)
626
}
@@ -9,19 +29,19 @@ function serializer(replacer, cycleReplacer) {
929
var stack = [], keys = []
1030

1131
if (cycleReplacer == null) cycleReplacer = function(key, value) {
12-
if (stack[0] === value) return "[Circular ~]"
13-
return "[Circular ~." + keys.slice(0, stack.indexOf(value)).join(".") + "]"
32+
if (stack[0] === value) return '[Circular ~]'
33+
return '[Circular ~.' + keys.slice(0, indexOf(stack, value)).join('.') + ']'
1434
}
1535

1636
return function(key, value) {
1737
if (stack.length > 0) {
18-
var thisPos = stack.indexOf(this)
38+
var thisPos = indexOf(stack, this);
1939
~thisPos ? stack.splice(thisPos + 1) : stack.push(this)
2040
~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key)
21-
if (~stack.indexOf(value)) value = cycleReplacer.call(this, key, value)
41+
if (~indexOf(stack, value)) value = cycleReplacer.call(this, key, value)
2242
}
2343
else stack.push(value)
2444

2545
return replacer == null ? value : replacer.call(this, key, value)
2646
}
27-
}
47+
}

0 commit comments

Comments
 (0)