Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

string-parse.js 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. 'use strict';
  2. // adapted from https://github.com/jridgewell/string-dedent
  3. var getBuiltIn = require('../internals/get-built-in');
  4. var uncurryThis = require('../internals/function-uncurry-this');
  5. var fromCharCode = String.fromCharCode;
  6. var fromCodePoint = getBuiltIn('String', 'fromCodePoint');
  7. var charAt = uncurryThis(''.charAt);
  8. var charCodeAt = uncurryThis(''.charCodeAt);
  9. var stringIndexOf = uncurryThis(''.indexOf);
  10. var stringSlice = uncurryThis(''.slice);
  11. var ZERO_CODE = 48;
  12. var NINE_CODE = 57;
  13. var LOWER_A_CODE = 97;
  14. var LOWER_F_CODE = 102;
  15. var UPPER_A_CODE = 65;
  16. var UPPER_F_CODE = 70;
  17. var isDigit = function (str, index) {
  18. var c = charCodeAt(str, index);
  19. return c >= ZERO_CODE && c <= NINE_CODE;
  20. };
  21. var parseHex = function (str, index, end) {
  22. if (end >= str.length) return -1;
  23. var n = 0;
  24. for (; index < end; index++) {
  25. var c = hexToInt(charCodeAt(str, index));
  26. if (c === -1) return -1;
  27. n = n * 16 + c;
  28. }
  29. return n;
  30. };
  31. var hexToInt = function (c) {
  32. if (c >= ZERO_CODE && c <= NINE_CODE) return c - ZERO_CODE;
  33. if (c >= LOWER_A_CODE && c <= LOWER_F_CODE) return c - LOWER_A_CODE + 10;
  34. if (c >= UPPER_A_CODE && c <= UPPER_F_CODE) return c - UPPER_A_CODE + 10;
  35. return -1;
  36. };
  37. module.exports = function (raw) {
  38. var out = '';
  39. var start = 0;
  40. // We need to find every backslash escape sequence, and cook the escape into a real char.
  41. var i = 0;
  42. var n;
  43. while ((i = stringIndexOf(raw, '\\', i)) > -1) {
  44. out += stringSlice(raw, start, i);
  45. // If the backslash is the last char of the string, then it was an invalid sequence.
  46. // This can't actually happen in a tagged template literal, but could happen if you manually
  47. // invoked the tag with an array.
  48. if (++i === raw.length) return;
  49. var next = charAt(raw, i++);
  50. switch (next) {
  51. // Escaped control codes need to be individually processed.
  52. case 'b':
  53. out += '\b';
  54. break;
  55. case 't':
  56. out += '\t';
  57. break;
  58. case 'n':
  59. out += '\n';
  60. break;
  61. case 'v':
  62. out += '\v';
  63. break;
  64. case 'f':
  65. out += '\f';
  66. break;
  67. case 'r':
  68. out += '\r';
  69. break;
  70. // Escaped line terminators just skip the char.
  71. case '\r':
  72. // Treat `\r\n` as a single terminator.
  73. if (i < raw.length && charAt(raw, i) === '\n') ++i;
  74. // break omitted
  75. case '\n':
  76. case '\u2028':
  77. case '\u2029':
  78. break;
  79. // `\0` is a null control char, but `\0` followed by another digit is an illegal octal escape.
  80. case '0':
  81. if (isDigit(raw, i)) return;
  82. out += '\0';
  83. break;
  84. // Hex escapes must contain 2 hex chars.
  85. case 'x':
  86. n = parseHex(raw, i, i + 2);
  87. if (n === -1) return;
  88. i += 2;
  89. out += fromCharCode(n);
  90. break;
  91. // Unicode escapes contain either 4 chars, or an unlimited number between `{` and `}`.
  92. // The hex value must not overflow 0x10FFFF.
  93. case 'u':
  94. if (i < raw.length && charAt(raw, i) === '{') {
  95. var end = stringIndexOf(raw, '}', ++i);
  96. if (end === -1) return;
  97. n = parseHex(raw, i, end);
  98. i = end + 1;
  99. } else {
  100. n = parseHex(raw, i, i + 4);
  101. i += 4;
  102. }
  103. if (n === -1 || n > 0x10FFFF) return;
  104. out += fromCodePoint(n);
  105. break;
  106. default:
  107. if (isDigit(next, 0)) return;
  108. out += next;
  109. }
  110. start = i;
  111. }
  112. return out + stringSlice(raw, start);
  113. };