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.

parse-json-string.js 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. 'use strict';
  2. var uncurryThis = require('../internals/function-uncurry-this');
  3. var hasOwn = require('../internals/has-own-property');
  4. var $SyntaxError = SyntaxError;
  5. var $parseInt = parseInt;
  6. var fromCharCode = String.fromCharCode;
  7. var at = uncurryThis(''.charAt);
  8. var slice = uncurryThis(''.slice);
  9. var exec = uncurryThis(/./.exec);
  10. var codePoints = {
  11. '\\"': '"',
  12. '\\\\': '\\',
  13. '\\/': '/',
  14. '\\b': '\b',
  15. '\\f': '\f',
  16. '\\n': '\n',
  17. '\\r': '\r',
  18. '\\t': '\t'
  19. };
  20. var IS_4_HEX_DIGITS = /^[\da-f]{4}$/i;
  21. // eslint-disable-next-line regexp/no-control-character -- safe
  22. var IS_C0_CONTROL_CODE = /^[\u0000-\u001F]$/;
  23. module.exports = function (source, i) {
  24. var unterminated = true;
  25. var value = '';
  26. while (i < source.length) {
  27. var chr = at(source, i);
  28. if (chr === '\\') {
  29. var twoChars = slice(source, i, i + 2);
  30. if (hasOwn(codePoints, twoChars)) {
  31. value += codePoints[twoChars];
  32. i += 2;
  33. } else if (twoChars === '\\u') {
  34. i += 2;
  35. var fourHexDigits = slice(source, i, i + 4);
  36. if (!exec(IS_4_HEX_DIGITS, fourHexDigits)) throw new $SyntaxError('Bad Unicode escape at: ' + i);
  37. value += fromCharCode($parseInt(fourHexDigits, 16));
  38. i += 4;
  39. } else throw new $SyntaxError('Unknown escape sequence: "' + twoChars + '"');
  40. } else if (chr === '"') {
  41. unterminated = false;
  42. i++;
  43. break;
  44. } else {
  45. if (exec(IS_C0_CONTROL_CODE, chr)) throw new $SyntaxError('Bad control character in string literal at: ' + i);
  46. value += chr;
  47. i++;
  48. }
  49. }
  50. if (unterminated) throw new $SyntaxError('Unterminated string at: ' + i);
  51. return { value: value, end: i };
  52. };