devalue.umd.js 8.9 KB

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