1 | (function (global, factory) {
|
2 | typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
|
3 | typeof define === 'function' && define.amd ? define(factory) :
|
4 | (global = global || self, global.devalue = factory());
|
5 | }(this, function () { 'use strict';
|
6 |
|
7 | var consola = console;
|
8 | var chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_$';
|
9 | var unsafeChars = /[<>\b\f\n\r\t\0\u2028\u2029]/g;
|
10 | var reserved = /^(?:do|if|in|for|int|let|new|try|var|byte|case|char|else|enum|goto|long|this|void|with|await|break|catch|class|const|final|float|short|super|throw|while|yield|delete|double|export|import|native|return|switch|throws|typeof|boolean|default|extends|finally|package|private|abstract|continue|debugger|function|volatile|interface|protected|transient|implements|instanceof|synchronized)$/;
|
11 | var escaped = {
|
12 | '<': '\\u003C',
|
13 | '>': '\\u003E',
|
14 | '/': '\\u002F',
|
15 | '\\': '\\\\',
|
16 | '\b': '\\b',
|
17 | '\f': '\\f',
|
18 | '\n': '\\n',
|
19 | '\r': '\\r',
|
20 | '\t': '\\t',
|
21 | '\0': '\\0',
|
22 | '\u2028': '\\u2028',
|
23 | '\u2029': '\\u2029'
|
24 | };
|
25 | var objectProtoOwnPropertyNames = Object.getOwnPropertyNames(Object.prototype).sort().join('\0');
|
26 |
|
27 | var defaultLogLevel = process.env.NUXT_ENV_DEVALUE_LOG_LEVEL || 'warn';
|
28 | var logLimit = parseInt(process.env.NUXT_ENV_DEVALUE_LOG_LIMIT) || 99;
|
29 | function devalue(value, level) {
|
30 | if (level === void 0) { level = defaultLogLevel; }
|
31 | var counts = new Map();
|
32 | var logNum = 0;
|
33 | function log(message) {
|
34 | if (logNum < logLimit) {
|
35 | consola[level](message);
|
36 | logNum += 1;
|
37 | }
|
38 | }
|
39 | function walk(thing) {
|
40 | if (typeof thing === 'function') {
|
41 | consola[level]("Cannot stringify a function " + thing.name);
|
42 | return;
|
43 | }
|
44 | if (counts.has(thing)) {
|
45 | counts.set(thing, counts.get(thing) + 1);
|
46 | return;
|
47 | }
|
48 | counts.set(thing, 1);
|
49 | if (!isPrimitive(thing)) {
|
50 | var type = getType(thing);
|
51 | switch (type) {
|
52 | case 'Number':
|
53 | case 'String':
|
54 | case 'Boolean':
|
55 | case 'Date':
|
56 | case 'RegExp':
|
57 | return;
|
58 | case 'Array':
|
59 | thing.forEach(walk);
|
60 | break;
|
61 | case 'Set':
|
62 | case 'Map':
|
63 | Array.from(thing).forEach(walk);
|
64 | break;
|
65 | default:
|
66 | var proto = Object.getPrototypeOf(thing);
|
67 | if (proto !== Object.prototype &&
|
68 | proto !== null &&
|
69 | Object.getOwnPropertyNames(proto).sort().join('\0') !== objectProtoOwnPropertyNames) {
|
70 | if (typeof thing.toJSON !== "function") {
|
71 | log("Cannot stringify arbitrary non-POJOs " + thing.constructor.name);
|
72 | }
|
73 | }
|
74 | else if (Object.getOwnPropertySymbols(thing).length > 0) {
|
75 | log("Cannot stringify POJOs with symbolic keys " + Object.getOwnPropertySymbols(thing).map(function (symbol) { return symbol.toString(); }));
|
76 | }
|
77 | else {
|
78 | Object.keys(thing).forEach(function (key) { return walk(thing[key]); });
|
79 | }
|
80 | }
|
81 | }
|
82 | }
|
83 | walk(value);
|
84 | var names = new Map();
|
85 | Array.from(counts)
|
86 | .filter(function (entry) { return entry[1] > 1; })
|
87 | .sort(function (a, b) { return b[1] - a[1]; })
|
88 | .forEach(function (entry, i) {
|
89 | names.set(entry[0], getName(i));
|
90 | });
|
91 | function stringify(thing) {
|
92 | if (names.has(thing)) {
|
93 | return names.get(thing);
|
94 | }
|
95 | if (isPrimitive(thing)) {
|
96 | return stringifyPrimitive(thing);
|
97 | }
|
98 | var type = getType(thing);
|
99 | switch (type) {
|
100 | case 'Number':
|
101 | case 'String':
|
102 | case 'Boolean':
|
103 | return "Object(" + stringify(thing.valueOf()) + ")";
|
104 | case 'RegExp':
|
105 | return thing.toString();
|
106 | case 'Date':
|
107 | return "new Date(" + thing.getTime() + ")";
|
108 | case 'Array':
|
109 | var members = thing.map(function (v, i) { return i in thing ? stringify(v) : ''; });
|
110 | var tail = thing.length === 0 || (thing.length - 1 in thing) ? '' : ',';
|
111 | return "[" + members.join(',') + tail + "]";
|
112 | case 'Set':
|
113 | case 'Map':
|
114 | return "new " + type + "([" + Array.from(thing).map(stringify).join(',') + "])";
|
115 | default:
|
116 | if (thing.toJSON) {
|
117 | var json = thing.toJSON();
|
118 | if (getType(json) === 'String') {
|
119 |
|
120 | try {
|
121 | json = JSON.parse(json);
|
122 | }
|
123 | catch (e) { }
|
124 | }
|
125 | return stringify(json);
|
126 | }
|
127 | if (Object.getPrototypeOf(thing) === null) {
|
128 | if (Object.keys(thing).length === 0) {
|
129 | return 'Object.create(null)';
|
130 | }
|
131 | return "Object.create(null,{" + Object.keys(thing).map(function (key) { return safeKey(key) + ":{writable:true,enumerable:true,value:" + stringify(thing[key]) + "}"; }).join(',') + "})";
|
132 | }
|
133 | return "{" + Object.keys(thing).map(function (key) { return safeKey(key) + ":" + stringify(thing[key]); }).join(',') + "}";
|
134 | }
|
135 | }
|
136 | var str = stringify(value);
|
137 | if (names.size) {
|
138 | var params_1 = [];
|
139 | var statements_1 = [];
|
140 | var values_1 = [];
|
141 | names.forEach(function (name, thing) {
|
142 | params_1.push(name);
|
143 | if (isPrimitive(thing)) {
|
144 | values_1.push(stringifyPrimitive(thing));
|
145 | return;
|
146 | }
|
147 | var type = getType(thing);
|
148 | switch (type) {
|
149 | case 'Number':
|
150 | case 'String':
|
151 | case 'Boolean':
|
152 | values_1.push("Object(" + stringify(thing.valueOf()) + ")");
|
153 | break;
|
154 | case 'RegExp':
|
155 | values_1.push(thing.toString());
|
156 | break;
|
157 | case 'Date':
|
158 | values_1.push("new Date(" + thing.getTime() + ")");
|
159 | break;
|
160 | case 'Array':
|
161 | values_1.push("Array(" + thing.length + ")");
|
162 | thing.forEach(function (v, i) {
|
163 | statements_1.push(name + "[" + i + "]=" + stringify(v));
|
164 | });
|
165 | break;
|
166 | case 'Set':
|
167 | values_1.push("new Set");
|
168 | statements_1.push(name + "." + Array.from(thing).map(function (v) { return "add(" + stringify(v) + ")"; }).join('.'));
|
169 | break;
|
170 | case 'Map':
|
171 | values_1.push("new Map");
|
172 | statements_1.push(name + "." + Array.from(thing).map(function (_a) {
|
173 | var k = _a[0], v = _a[1];
|
174 | return "set(" + stringify(k) + ", " + stringify(v) + ")";
|
175 | }).join('.'));
|
176 | break;
|
177 | default:
|
178 | values_1.push(Object.getPrototypeOf(thing) === null ? 'Object.create(null)' : '{}');
|
179 | Object.keys(thing).forEach(function (key) {
|
180 | statements_1.push("" + name + safeProp(key) + "=" + stringify(thing[key]));
|
181 | });
|
182 | }
|
183 | });
|
184 | statements_1.push("return " + str);
|
185 | return "(function(" + params_1.join(',') + "){" + statements_1.join(';') + "}(" + values_1.join(',') + "))";
|
186 | }
|
187 | else {
|
188 | return str;
|
189 | }
|
190 | }
|
191 | function getName(num) {
|
192 | var name = '';
|
193 | do {
|
194 | name = chars[num % chars.length] + name;
|
195 | num = ~~(num / chars.length) - 1;
|
196 | } while (num >= 0);
|
197 | return reserved.test(name) ? name + "_" : name;
|
198 | }
|
199 | function isPrimitive(thing) {
|
200 | return Object(thing) !== thing;
|
201 | }
|
202 | function stringifyPrimitive(thing) {
|
203 | if (typeof thing === 'string')
|
204 | return stringifyString(thing);
|
205 | if (thing === void 0)
|
206 | return 'void 0';
|
207 | if (thing === 0 && 1 / thing < 0)
|
208 | return '-0';
|
209 | var str = String(thing);
|
210 | if (typeof thing === 'number')
|
211 | return str.replace(/^(-)?0\./, '$1.');
|
212 | return str;
|
213 | }
|
214 | function getType(thing) {
|
215 | return Object.prototype.toString.call(thing).slice(8, -1);
|
216 | }
|
217 | function escapeUnsafeChar(c) {
|
218 | return escaped[c] || c;
|
219 | }
|
220 | function escapeUnsafeChars(str) {
|
221 | return str.replace(unsafeChars, escapeUnsafeChar);
|
222 | }
|
223 | function safeKey(key) {
|
224 | return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? key : escapeUnsafeChars(JSON.stringify((key)));
|
225 | }
|
226 | function safeProp(key) {
|
227 | return /^[_$a-zA-Z][_$a-zA-Z0-9]*$/.test(key) ? "." + key : "[" + escapeUnsafeChars(JSON.stringify(key)) + "]";
|
228 | }
|
229 | function stringifyString(str) {
|
230 | var result = '"';
|
231 | for (var i = 0; i < str.length; i += 1) {
|
232 | var char = str.charAt(i);
|
233 | var code = char.charCodeAt(0);
|
234 | if (char === '"') {
|
235 | result += '\\"';
|
236 | }
|
237 | else if (char in escaped) {
|
238 | result += escaped[char];
|
239 | }
|
240 | else if (code >= 0xd800 && code <= 0xdfff) {
|
241 | var next = str.charCodeAt(i + 1);
|
242 |
|
243 |
|
244 | if (code <= 0xdbff && (next >= 0xdc00 && next <= 0xdfff)) {
|
245 | result += char + str[++i];
|
246 | }
|
247 | else {
|
248 | result += "\\u" + code.toString(16).toUpperCase();
|
249 | }
|
250 | }
|
251 | else {
|
252 | result += char;
|
253 | }
|
254 | }
|
255 | result += '"';
|
256 | return result;
|
257 | }
|
258 |
|
259 | return devalue;
|
260 |
|
261 | }));
|