devalue.js 10 KB

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