Skip to content

Commit 12e4f09

Browse files
committed
Switched to for marshalling strings to objects.
1 parent 0e69bbf commit 12e4f09

File tree

3 files changed

+24
-25
lines changed

3 files changed

+24
-25
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ The marshalled version of the input data.
5353

5454
## Changelog:
5555

56+
**v1.12.0**:
57+
58+
- Switched to `new Function()` for marshalling strings to objects.
59+
5660
**v1.11.0**:
5761

5862
- Fixed a bug with marshalling strings that contains the word "function".

src/Marshaller.ts

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -965,13 +965,13 @@ export class Marshaller implements IMarshaller {
965965
return toNum;
966966
}
967967

968-
const asObj = this.attemptStringToObjectConversion(primitive);
969-
if (asObj != null) return asObj;
970-
971968
if (Marshaller.FUNCTION_REGEX_1.test(primitive.trim())) return new Function(`return ${primitive}`)();
972969
if (Marshaller.FUNCTION_REGEX_2.test(primitive.trim())) return new Function(`return ${primitive}`)();
973970
if (Marshaller.FUNCTION_REGEX_3.test(primitive.trim())) return new Function(`return ${primitive}`)();
974971

972+
const asObj = this.attemptStringToObjectConversion(primitive);
973+
if (asObj != null) return asObj;
974+
975975
try {
976976
return JSON.parse(primitive);
977977
} catch (e) {
@@ -1022,25 +1022,19 @@ export class Marshaller implements IMarshaller {
10221022
/**
10231023
* Attempts to convert a string into a regular object. Returns null if it fails.
10241024
* @param {string} primitive
1025-
* @param {number} [attempt=0]
10261025
* @returns {object|null}
10271026
*/
1028-
private attemptStringToObjectConversion (primitive: string, attempt: number = 0): {[key: string]: string}|null {
1027+
private attemptStringToObjectConversion (primitive: string): {[key: string]: string}|null {
10291028
try {
10301029
return JSON.parse(primitive);
10311030
} catch (e) {
1032-
1033-
if (attempt === 0) {
1034-
// Try to format the string so it fits the JSON standard.
1035-
let trimmed = primitive
1036-
.replace(/([{,:}"\]])([ \t\r\n]*)/g, (_, p1) => `${p1}`)
1037-
.replace(/([{,])(\w+)(:)/g, (_, p1, p2, p3) => `${p1}"${p2}"${p3}`)
1038-
.replace(/`([^`]*)`/g, (_, p1) => `"${p1}"`)
1039-
.trim();
1040-
if (trimmed.endsWith(";")) trimmed = trimmed.slice(0, trimmed.length - 1);
1041-
return this.attemptStringToObjectConversion(trimmed, ++attempt);
1031+
try {
1032+
const evaluated = new Function(`return (${primitive})`)();
1033+
if (this.typeDetector.isObject(evaluated)) return <{[key: string]: string}>evaluated;
1034+
return null;
1035+
} catch (e) {
1036+
return null;
10421037
}
1043-
return null;
10441038
}
10451039
}
10461040

test/Marshaller.test.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,11 @@ test(`'marshal()' string -> object. #2`, t => {
2828
c: "hello world!"
2929
}
3030
},
31-
b: ["foo", "bar", true, false]
31+
d: ["foo", "bar", true, false]
3232
};
3333

3434
const input = `
35-
{
36-
a: {
37-
b: {
38-
c: "hello world!"
39-
}
40-
},
41-
b: ["foo", "bar", true, false]
42-
};
35+
{a: {b: {c: "hello world!"}}, d: ["foo", "bar", true, false]}
4336
`;
4437

4538
t.deepEqual<Object|null|undefined>(marshaller.marshal(input, Object), expected);
@@ -174,6 +167,14 @@ test(`'marshal()' string -> best guess. #4`, t => {
174167
else t.deepEqual(marshalled.toString(), expected.toString());
175168
});
176169

170+
test(`'marshal()' string -> best guess. #5`, t => {
171+
const input = `{"hmm": ((arg) => { return (arg === undefined ? undefined : arg)+10;})}`;
172+
// const expected = {"hmm": ((_) => { return (_ === undefined ? undefined : _)+10;})};
173+
174+
const marshalled = marshaller.marshal(input);
175+
t.true(typeDetector.isObject(marshalled));
176+
});
177+
177178

178179
test(`'marshal()' object -> string. #1`, t => {
179180
const expected = '{"a": 2}';

0 commit comments

Comments
 (0)