jsesc.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. 'use strict';
  2. const object = {};
  3. const hasOwnProperty = object.hasOwnProperty;
  4. const forOwn = (object, callback) => {
  5. for (const key in object) {
  6. if (hasOwnProperty.call(object, key)) {
  7. callback(key, object[key]);
  8. }
  9. }
  10. };
  11. const extend = (destination, source) => {
  12. if (!source) {
  13. return destination;
  14. }
  15. forOwn(source, (key, value) => {
  16. destination[key] = value;
  17. });
  18. return destination;
  19. };
  20. const forEach = (array, callback) => {
  21. const length = array.length;
  22. let index = -1;
  23. while (++index < length) {
  24. callback(array[index]);
  25. }
  26. };
  27. const fourHexEscape = (hex) => {
  28. return '\\u' + ('0000' + hex).slice(-4);
  29. }
  30. const hexadecimal = (code, lowercase) => {
  31. let hexadecimal = code.toString(16);
  32. if (lowercase) return hexadecimal;
  33. return hexadecimal.toUpperCase();
  34. };
  35. const toString = object.toString;
  36. const isArray = Array.isArray;
  37. const isBuffer = (value) => {
  38. return typeof Buffer === 'function' && Buffer.isBuffer(value);
  39. };
  40. const isObject = (value) => {
  41. // This is a very simple check, but it’s good enough for what we need.
  42. return toString.call(value) == '[object Object]';
  43. };
  44. const isString = (value) => {
  45. return typeof value == 'string' ||
  46. toString.call(value) == '[object String]';
  47. };
  48. const isNumber = (value) => {
  49. return typeof value == 'number' ||
  50. toString.call(value) == '[object Number]';
  51. };
  52. const isBigInt = (value) => {
  53. return typeof value == 'bigint';
  54. };
  55. const isFunction = (value) => {
  56. return typeof value == 'function';
  57. };
  58. const isMap = (value) => {
  59. return toString.call(value) == '[object Map]';
  60. };
  61. const isSet = (value) => {
  62. return toString.call(value) == '[object Set]';
  63. };
  64. /*--------------------------------------------------------------------------*/
  65. // https://mathiasbynens.be/notes/javascript-escapes#single
  66. const singleEscapes = {
  67. '\\': '\\\\',
  68. '\b': '\\b',
  69. '\f': '\\f',
  70. '\n': '\\n',
  71. '\r': '\\r',
  72. '\t': '\\t'
  73. // `\v` is omitted intentionally, because in IE < 9, '\v' == 'v'.
  74. // '\v': '\\x0B'
  75. };
  76. const regexSingleEscape = /[\\\b\f\n\r\t]/;
  77. const regexDigit = /[0-9]/;
  78. const regexWhitespace = /[\xA0\u1680\u2000-\u200A\u2028\u2029\u202F\u205F\u3000]/;
  79. const escapeEverythingRegex = /([\uD800-\uDBFF][\uDC00-\uDFFF])|([\uD800-\uDFFF])|(['"`])|[^]/g;
  80. const escapeNonAsciiRegex = /([\uD800-\uDBFF][\uDC00-\uDFFF])|([\uD800-\uDFFF])|(['"`])|[^ !#-&\(-\[\]-_a-~]/g;
  81. const jsesc = (argument, options) => {
  82. const increaseIndentation = () => {
  83. oldIndent = indent;
  84. ++options.indentLevel;
  85. indent = options.indent.repeat(options.indentLevel)
  86. };
  87. // Handle options
  88. const defaults = {
  89. 'escapeEverything': false,
  90. 'minimal': false,
  91. 'isScriptContext': false,
  92. 'quotes': 'single',
  93. 'wrap': false,
  94. 'es6': false,
  95. 'json': false,
  96. 'compact': true,
  97. 'lowercaseHex': false,
  98. 'numbers': 'decimal',
  99. 'indent': '\t',
  100. 'indentLevel': 0,
  101. '__inline1__': false,
  102. '__inline2__': false
  103. };
  104. const json = options && options.json;
  105. if (json) {
  106. defaults.quotes = 'double';
  107. defaults.wrap = true;
  108. }
  109. options = extend(defaults, options);
  110. if (
  111. options.quotes != 'single' &&
  112. options.quotes != 'double' &&
  113. options.quotes != 'backtick'
  114. ) {
  115. options.quotes = 'single';
  116. }
  117. const quote = options.quotes == 'double' ?
  118. '"' :
  119. (options.quotes == 'backtick' ?
  120. '`' :
  121. '\''
  122. );
  123. const compact = options.compact;
  124. const lowercaseHex = options.lowercaseHex;
  125. let indent = options.indent.repeat(options.indentLevel);
  126. let oldIndent = '';
  127. const inline1 = options.__inline1__;
  128. const inline2 = options.__inline2__;
  129. const newLine = compact ? '' : '\n';
  130. let result;
  131. let isEmpty = true;
  132. const useBinNumbers = options.numbers == 'binary';
  133. const useOctNumbers = options.numbers == 'octal';
  134. const useDecNumbers = options.numbers == 'decimal';
  135. const useHexNumbers = options.numbers == 'hexadecimal';
  136. if (json && argument && isFunction(argument.toJSON)) {
  137. argument = argument.toJSON();
  138. }
  139. if (!isString(argument)) {
  140. if (isMap(argument)) {
  141. if (argument.size == 0) {
  142. return 'new Map()';
  143. }
  144. if (!compact) {
  145. options.__inline1__ = true;
  146. options.__inline2__ = false;
  147. }
  148. return 'new Map(' + jsesc(Array.from(argument), options) + ')';
  149. }
  150. if (isSet(argument)) {
  151. if (argument.size == 0) {
  152. return 'new Set()';
  153. }
  154. return 'new Set(' + jsesc(Array.from(argument), options) + ')';
  155. }
  156. if (isBuffer(argument)) {
  157. if (argument.length == 0) {
  158. return 'Buffer.from([])';
  159. }
  160. return 'Buffer.from(' + jsesc(Array.from(argument), options) + ')';
  161. }
  162. if (isArray(argument)) {
  163. result = [];
  164. options.wrap = true;
  165. if (inline1) {
  166. options.__inline1__ = false;
  167. options.__inline2__ = true;
  168. }
  169. if (!inline2) {
  170. increaseIndentation();
  171. }
  172. forEach(argument, (value) => {
  173. isEmpty = false;
  174. if (inline2) {
  175. options.__inline2__ = false;
  176. }
  177. result.push(
  178. (compact || inline2 ? '' : indent) +
  179. jsesc(value, options)
  180. );
  181. });
  182. if (isEmpty) {
  183. return '[]';
  184. }
  185. if (inline2) {
  186. return '[' + result.join(', ') + ']';
  187. }
  188. return '[' + newLine + result.join(',' + newLine) + newLine +
  189. (compact ? '' : oldIndent) + ']';
  190. } else if (isNumber(argument) || isBigInt(argument)) {
  191. if (json) {
  192. // Some number values (e.g. `Infinity`) cannot be represented in JSON.
  193. // `BigInt` values less than `-Number.MAX_VALUE` or greater than
  194. // `Number.MAX_VALUE` cannot be represented in JSON so they will become
  195. // `-Infinity` or `Infinity`, respectively, and then become `null` when
  196. // stringified.
  197. return JSON.stringify(Number(argument));
  198. }
  199. let result;
  200. if (useDecNumbers) {
  201. result = String(argument);
  202. } else if (useHexNumbers) {
  203. let hexadecimal = argument.toString(16);
  204. if (!lowercaseHex) {
  205. hexadecimal = hexadecimal.toUpperCase();
  206. }
  207. result = '0x' + hexadecimal;
  208. } else if (useBinNumbers) {
  209. result = '0b' + argument.toString(2);
  210. } else if (useOctNumbers) {
  211. result = '0o' + argument.toString(8);
  212. }
  213. if (isBigInt(argument)) {
  214. return result + 'n';
  215. }
  216. return result;
  217. } else if (isBigInt(argument)) {
  218. if (json) {
  219. // `BigInt` values less than `-Number.MAX_VALUE` or greater than
  220. // `Number.MAX_VALUE` will become `-Infinity` or `Infinity`,
  221. // respectively, and cannot be represented in JSON.
  222. return JSON.stringify(Number(argument));
  223. }
  224. return argument + 'n';
  225. } else if (!isObject(argument)) {
  226. if (json) {
  227. // For some values (e.g. `undefined`, `function` objects),
  228. // `JSON.stringify(value)` returns `undefined` (which isn’t valid
  229. // JSON) instead of `'null'`.
  230. return JSON.stringify(argument) || 'null';
  231. }
  232. return String(argument);
  233. } else { // it’s an object
  234. result = [];
  235. options.wrap = true;
  236. increaseIndentation();
  237. forOwn(argument, (key, value) => {
  238. isEmpty = false;
  239. result.push(
  240. (compact ? '' : indent) +
  241. jsesc(key, options) + ':' +
  242. (compact ? '' : ' ') +
  243. jsesc(value, options)
  244. );
  245. });
  246. if (isEmpty) {
  247. return '{}';
  248. }
  249. return '{' + newLine + result.join(',' + newLine) + newLine +
  250. (compact ? '' : oldIndent) + '}';
  251. }
  252. }
  253. const regex = options.escapeEverything ? escapeEverythingRegex : escapeNonAsciiRegex;
  254. result = argument.replace(regex, (char, pair, lone, quoteChar, index, string) => {
  255. if (pair) {
  256. if (options.minimal) return pair;
  257. const first = pair.charCodeAt(0);
  258. const second = pair.charCodeAt(1);
  259. if (options.es6) {
  260. // https://mathiasbynens.be/notes/javascript-encoding#surrogate-formulae
  261. const codePoint = (first - 0xD800) * 0x400 + second - 0xDC00 + 0x10000;
  262. const hex = hexadecimal(codePoint, lowercaseHex);
  263. return '\\u{' + hex + '}';
  264. }
  265. return fourHexEscape(hexadecimal(first, lowercaseHex)) + fourHexEscape(hexadecimal(second, lowercaseHex));
  266. }
  267. if (lone) {
  268. return fourHexEscape(hexadecimal(lone.charCodeAt(0), lowercaseHex));
  269. }
  270. if (
  271. char == '\0' &&
  272. !json &&
  273. !regexDigit.test(string.charAt(index + 1))
  274. ) {
  275. return '\\0';
  276. }
  277. if (quoteChar) {
  278. if (quoteChar == quote || options.escapeEverything) {
  279. return '\\' + quoteChar;
  280. }
  281. return quoteChar;
  282. }
  283. if (regexSingleEscape.test(char)) {
  284. // no need for a `hasOwnProperty` check here
  285. return singleEscapes[char];
  286. }
  287. if (options.minimal && !regexWhitespace.test(char)) {
  288. return char;
  289. }
  290. const hex = hexadecimal(char.charCodeAt(0), lowercaseHex);
  291. if (json || hex.length > 2) {
  292. return fourHexEscape(hex);
  293. }
  294. return '\\x' + ('00' + hex).slice(-2);
  295. });
  296. if (quote == '`') {
  297. result = result.replace(/\$\{/g, '\\${');
  298. }
  299. if (options.isScriptContext) {
  300. // https://mathiasbynens.be/notes/etago
  301. result = result
  302. .replace(/<\/(script|style)/gi, '<\\/$1')
  303. .replace(/<!--/g, json ? '\\u003C!--' : '\\x3C!--');
  304. }
  305. if (options.wrap) {
  306. result = quote + result + quote;
  307. }
  308. return result;
  309. };
  310. jsesc.version = '3.0.2';
  311. module.exports = jsesc;