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+ }
0 commit comments