devalue.esm.js 8.4 KB

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