Skip to content

Commit 1ee72bf

Browse files
committed
Also need stringify
1 parent e288886 commit 1ee72bf

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'use strict';
2+
3+
/*
4+
json-stringify-safe
5+
Like JSON.stringify, but doesn't throw on circular references.
6+
7+
Copied from sentry-javascript/packages/raven-js/vendor/json-stringify-safe/stringify.js
8+
9+
Originally forked from https://github.com/isaacs/json-stringify-safe
10+
version 5.0.1 on 3/8/2017 and modified to handle Errors serialization
11+
and IE8 compatibility. Tests for this are in test/vendor.
12+
13+
ISC license: https://github.com/isaacs/json-stringify-safe/blob/master/LICENSE
14+
*/
15+
16+
exports = module.exports = stringify;
17+
exports.getSerialize = serializer;
18+
19+
function indexOf(haystack, needle) {
20+
for (var i = 0; i < haystack.length; ++i) {
21+
if (haystack[i] === needle) return i;
22+
}
23+
return -1;
24+
}
25+
26+
function stringify(obj, replacer, spaces, cycleReplacer) {
27+
return JSON.stringify(obj, serializer(replacer, cycleReplacer), spaces);
28+
}
29+
30+
// https://github.com/ftlabs/js-abbreviate/blob/fa709e5f139e7770a71827b1893f22418097fbda/index.js#L95-L106
31+
function stringifyError(value) {
32+
var err = {
33+
// These properties are implemented as magical getters and don't show up in for in
34+
stack: value.stack,
35+
message: value.message,
36+
name: value.name
37+
};
38+
39+
for (var i in value) {
40+
if (Object.prototype.hasOwnProperty.call(value, i)) {
41+
err[i] = value[i];
42+
}
43+
}
44+
45+
return err;
46+
}
47+
48+
function serializer(replacer, cycleReplacer) {
49+
var stack = [];
50+
var keys = [];
51+
52+
if (cycleReplacer == null) {
53+
cycleReplacer = function cycleReplacer(key, value) {
54+
if (stack[0] === value) {
55+
return '[Circular ~]';
56+
}
57+
return '[Circular ~.' + keys.slice(0, indexOf(stack, value)).join('.') + ']';
58+
};
59+
}
60+
61+
return function (key, value) {
62+
if (stack.length > 0) {
63+
var thisPos = indexOf(stack, this);
64+
~thisPos ? stack.splice(thisPos + 1) : stack.push(this);
65+
~thisPos ? keys.splice(thisPos, Infinity, key) : keys.push(key);
66+
67+
if (~indexOf(stack, value)) {
68+
value = cycleReplacer.call(this, key, value);
69+
}
70+
} else {
71+
stack.push(value);
72+
}
73+
74+
return replacer == null ? value instanceof Error ? stringifyError(value) : value : replacer.call(this, key, value);
75+
};
76+
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"tdd": "jest --watch",
99
"test": "jest --coverage --collectCoverageFrom=index.js && npm run lint",
1010
"lint": "eslint .",
11-
"build": "babel index.js --out-dir built/",
11+
"build": "babel index.js vendor/json-stringify-safe/stringify.js --out-dir built/",
1212
"prepublishOnly": "npm run test && npm run build",
1313
"serve-example": "webpack-dev-server --watch --config=example/webpack.config.js"
1414
},

0 commit comments

Comments
 (0)