.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / circular-json / build / circular-json.node.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
24   // should be a not so common char
25   // possibly one JSON does not encode
26   // possibly one encodeURIComponent does not encode
27   // right now this char is '~' but this might change in the future
28   specialChar = '~',
29   safeSpecialChar = '\\x' + (
30     '0' + specialChar.charCodeAt(0).toString(16)
31   ).slice(-2),
32   escapedSafeSpecialChar = '\\' + safeSpecialChar,
33   specialCharRG = new RegExp(safeSpecialChar, 'g'),
34   safeSpecialCharRG = new RegExp(escapedSafeSpecialChar, 'g'),
35
36   safeStartWithSpecialCharRG = new RegExp('(?:^|([^\\\\]))' + escapedSafeSpecialChar),
37
38   indexOf = [].indexOf || function(v){
39     for(var i=this.length;i--&&this[i]!==v;);
40     return i;
41   },
42   $String = String  // there's no way to drop warnings in JSHint
43                     // about new String ... well, I need that here!
44                     // faked, and happy linter!
45 ;
46
47 function generateReplacer(value, replacer, resolve) {
48   var
49     path = [],
50     all  = [value],
51     seen = [value],
52     mapp = [resolve ? specialChar : '[Circular]'],
53     last = value,
54     lvl  = 1,
55     i
56   ;
57   return function(key, value) {
58     // the replacer has rights to decide
59     // if a new object should be returned
60     // or if there's some key to drop
61     // let's call it here rather than "too late"
62     if (replacer) value = replacer.call(this, key, value);
63
64     // did you know ? Safari passes keys as integers for arrays
65     // which means if (key) when key === 0 won't pass the check
66     if (key !== '') {
67       if (last !== this) {
68         i = lvl - indexOf.call(all, this) - 1;
69         lvl -= i;
70         all.splice(lvl, all.length);
71         path.splice(lvl - 1, path.length);
72         last = this;
73       }
74       // console.log(lvl, key, path);
75       if (typeof value === 'object' && value) {
76         // if object isn't referring to parent object, add to the
77         // object path stack. Otherwise it is already there.
78         if (indexOf.call(all, value) < 0) {
79           all.push(last = value);
80         }
81         lvl = all.length;
82         i = indexOf.call(seen, value);
83         if (i < 0) {
84           i = seen.push(value) - 1;
85           if (resolve) {
86             // key cannot contain specialChar but could be not a string
87             path.push(('' + key).replace(specialCharRG, safeSpecialChar));
88             mapp[i] = specialChar + path.join(specialChar);
89           } else {
90             mapp[i] = mapp[0];
91           }
92         } else {
93           value = mapp[i];
94         }
95       } else {
96         if (typeof value === 'string' && resolve) {
97           // ensure no special char involved on deserialization
98           // in this case only first char is important
99           // no need to replace all value (better performance)
100           value = value .replace(safeSpecialChar, escapedSafeSpecialChar)
101                         .replace(specialChar, safeSpecialChar);
102         }
103       }
104     }
105     return value;
106   };
107 }
108
109 function retrieveFromPath(current, keys) {
110   for(var i = 0, length = keys.length; i < length; current = current[
111     // keys should be normalized back here
112     keys[i++].replace(safeSpecialCharRG, specialChar)
113   ]);
114   return current;
115 }
116
117 function generateReviver(reviver) {
118   return function(key, value) {
119     var isString = typeof value === 'string';
120     if (isString && value.charAt(0) === specialChar) {
121       return new $String(value.slice(1));
122     }
123     if (key === '') value = regenerate(value, value, {});
124     // again, only one needed, do not use the RegExp for this replacement
125     // only keys need the RegExp
126     if (isString) value = value .replace(safeStartWithSpecialCharRG, '$1' + specialChar)
127                                 .replace(escapedSafeSpecialChar, safeSpecialChar);
128     return reviver ? reviver.call(this, key, value) : value;
129   };
130 }
131
132 function regenerateArray(root, current, retrieve) {
133   for (var i = 0, length = current.length; i < length; i++) {
134     current[i] = regenerate(root, current[i], retrieve);
135   }
136   return current;
137 }
138
139 function regenerateObject(root, current, retrieve) {
140   for (var key in current) {
141     if (current.hasOwnProperty(key)) {
142       current[key] = regenerate(root, current[key], retrieve);
143     }
144   }
145   return current;
146 }
147
148 function regenerate(root, current, retrieve) {
149   return current instanceof Array ?
150     // fast Array reconstruction
151     regenerateArray(root, current, retrieve) :
152     (
153       current instanceof $String ?
154         (
155           // root is an empty string
156           current.length ?
157             (
158               retrieve.hasOwnProperty(current) ?
159                 retrieve[current] :
160                 retrieve[current] = retrieveFromPath(
161                   root, current.split(specialChar)
162                 )
163             ) :
164             root
165         ) :
166         (
167           current instanceof Object ?
168             // dedicated Object parser
169             regenerateObject(root, current, retrieve) :
170             // value as it is
171             current
172         )
173     )
174   ;
175 }
176
177 function stringifyRecursion(value, replacer, space, doNotResolve) {
178   return JSON.stringify(value, generateReplacer(value, replacer, !doNotResolve), space);
179 }
180
181 function parseRecursion(text, reviver) {
182   return JSON.parse(text, generateReviver(reviver));
183 }
184 this.stringify = stringifyRecursion;
185 this.parse = parseRecursion;