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.

23 lines
436 B

4 years ago
  1. 'use strict';
  2. function parseString(str) {
  3. try {
  4. if (str[0] === '"') {
  5. return JSON.parse(str);
  6. }
  7. if (str[0] === "'" && str.substr(str.length - 1) === "'") {
  8. return parseString(
  9. str
  10. .replace(/\\.|"/g, (x) => (x === '"' ? '\\"' : x))
  11. .replace(/^'|'$/g, '"')
  12. );
  13. }
  14. return JSON.parse('"' + str + '"');
  15. } catch (e) {
  16. return str;
  17. }
  18. }
  19. module.exports = parseString;