.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / circular-json / build / circular-json.max.js
1 /*!
2 Copyright (C) 2013 by WebReflection
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21
22 */
23 var CircularJSON = (function(JSON, RegExp){
24 var
25   // should be a not so common char
26   // possibly one JSON does not encode
27   // possibly one encodeURIComponent does not encode
28   // right now this char is '~' but this might change in the future
29   specialChar = '~',
30   safeSpecialChar = '\\x' + (
31     '0' + specialChar.charCodeAt(0).toString(16)
32   ).slice(-2),
33   escapedSafeSpecialChar = '\\' + safeSpecialChar,
34   specialCharRG = new RegExp(safeSpecialChar, 'g'),
35   safeSpecialCharRG = new RegExp(escapedSafeSpecialChar, 'g'),
36
37   safeStartWithSpecialCharRG = new RegExp('(?:^|([^\\\\]))' + escapedSafeSpecialChar),
38
39   indexOf = [].indexOf || function(v){
40     for(var i=this.length;i--&&this[i]!==v;);
41     return i;
42   },
43   $String = String  // there's no way to drop warnings in JSHint
44                     // about new String ... well, I need that here!
45                     // faked, and happy linter!
46 ;
47
48 function generateReplacer(value, replacer, resolve) {
49   var
50     path = [],
51     all  = [value],
52     seen = [value],
53     mapp = [resolve ? specialChar : '[Circular]'],
54     last = value,
55     lvl  = 1,
56     i
57   ;
58   return function(key, value) {
59     // the replacer has rights to decide
60     // if a new object should be returned
61     // or if there's some key to drop
62     // let's call it here rather than "too late"
63     if (replacer) value = replacer.call(this, key, value);
64
65     // did you know ? Safari passes keys as integers for arrays
66     // which means if (key) when key === 0 won't pass the check
67     if (key !== '') {
68       if (last !== this) {
69         i = lvl - indexOf.call(all, this) - 1;
70         lvl -= i;
71         all.splice(lvl, all.length);
72         path.splice(lvl - 1, path.length);
73         last = this;
74       }
75       // console.log(lvl, key, path);
76       if (typeof value === 'object' && value) {
77         // if object isn't referring to parent object, add to the
78         // object path stack. Otherwise it is already there.
79         if (indexOf.call(all, value) < 0) {
80           all.push(last = value);
81         }
82         lvl = all.length;
83         i = indexOf.call(seen, value);
84         if (i < 0) {
85           i = seen.push(value) - 1;
86           if (resolve) {
87             // key cannot contain specialChar but could be not a string
88             path.push(('' + key).replace(specialCharRG, safeSpecialChar));
89             mapp[i] = specialChar + path.join(specialChar);
90           } else {
91             mapp[i] = mapp[0];
92           }
93         } else {
94           value = mapp[i];
95         }
96       } else {
97         if (typeof value === 'string' && resolve) {
98           // ensure no special char involved on deserialization
99           // in this case only first char is important
100           // no need to replace all value (better performance)
101           value = value .replace(safeSpecialChar, escapedSafeSpecialChar)
102                         .replace(specialChar, safeSpecialChar);
103         }
104       }
105     }
106     return value;
107   };
108 }
109
110 function retrieveFromPath(current, keys) {
111   for(var i = 0, length = keys.length; i < length; current = current[
112     // keys should be normalized back here
113     keys[i++].replace(safeSpecialCharRG, specialChar)
114   ]);
115   return current;
116 }
117
118 function generateReviver(reviver) {
119   return function(key, value) {
120     var isString = typeof value === 'string';
121     if (isString && value.charAt(0) === specialChar) {
122       return new $String(value.slice(1));
123     }
124     if (key === '') value = regenerate(value, value, {});
125     // again, only one needed, do not use the RegExp for this replacement
126     // only keys need the RegExp
127     if (isString) value = value .replace(safeStartWithSpecialCharRG, '$1' + specialChar)
128                                 .replace(escapedSafeSpecialChar, safeSpecialChar);
129     return reviver ? reviver.call(this, key, value) : value;
130   };
131 }
132
133 function regenerateArray(root, current, retrieve) {
134   for (var i = 0, length = current.length; i < length; i++) {
135     current[i] = regenerate(root, current[i], retrieve);
136   }
137   return current;
138 }
139
140 function regenerateObject(root, current, retrieve) {
141   for (var key in current) {
142     if (current.hasOwnProperty(key)) {
143       current[key] = regenerate(root, current[key], retrieve);
144     }
145   }
146   return current;
147 }
148
149 function regenerate(root, current, retrieve) {
150   return current instanceof Array ?
151     // fast Array reconstruction
152     regenerateArray(root, current, retrieve) :
153     (
154       current instanceof $String ?
155         (
156           // root is an empty string
157           current.length ?
158             (
159               retrieve.hasOwnProperty(current) ?
160                 retrieve[current] :
161                 retrieve[current] = retrieveFromPath(
162                   root, current.split(specialChar)
163                 )
164             ) :
165             root
166         ) :
167         (
168           current instanceof Object ?
169             // dedicated Object parser
170             regenerateObject(root, current, retrieve) :
171             // value as it is
172             current
173         )
174     )
175   ;
176 }
177
178 function stringifyRecursion(value, replacer, space, doNotResolve) {
179   return JSON.stringify(value, generateReplacer(value, replacer, !doNotResolve), space);
180 }
181
182 function parseRecursion(text, reviver) {
183   return JSON.parse(text, generateReviver(reviver));
184 }
185 return {
186   stringify: stringifyRecursion,
187   parse: parseRecursion
188 };
189 }(JSON, RegExp));