Last change
on this file was 6a3a178, checked in by Ema <ema_spirova@…>, 3 years ago |
initial commit
|
-
Property mode
set to
100644
|
File size:
436 bytes
|
Line | |
---|
1 | 'use strict';
|
---|
2 |
|
---|
3 | function parseString(str) {
|
---|
4 | try {
|
---|
5 | if (str[0] === '"') {
|
---|
6 | return JSON.parse(str);
|
---|
7 | }
|
---|
8 |
|
---|
9 | if (str[0] === "'" && str.substr(str.length - 1) === "'") {
|
---|
10 | return parseString(
|
---|
11 | str
|
---|
12 | .replace(/\\.|"/g, (x) => (x === '"' ? '\\"' : x))
|
---|
13 | .replace(/^'|'$/g, '"')
|
---|
14 | );
|
---|
15 | }
|
---|
16 |
|
---|
17 | return JSON.parse('"' + str + '"');
|
---|
18 | } catch (e) {
|
---|
19 | return str;
|
---|
20 | }
|
---|
21 | }
|
---|
22 |
|
---|
23 | module.exports = parseString;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.