parseString.js 386B

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