Skip to content

Commit 19c762c

Browse files
committed
Fixed a bug with marshalling strings that contains the word function.
1 parent 2dfc96f commit 19c762c

File tree

3 files changed

+49
-9
lines changed

3 files changed

+49
-9
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.11.0**:
57+
58+
- Fixed a bug with marshalling strings that contains the word "function".
59+
5660
**v1.10.0**:
5761

5862
- The Marshaller can no marshal to objects if that is the most probable type, even if a hint is not given.

src/Marshaller.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {ITypeDetector, IArbitraryObject} from "@wessberg/typedetector";
77
*/
88
export class Marshaller implements IMarshaller {
99
private static readonly SYMBOL_REGEX: RegExp = /Symbol\(([^)]*)\)/;
10-
private static readonly FUNCTION_REGEX_1: RegExp = /function\s*\w*\s*\([^)]*\)\s*{/;
11-
private static readonly FUNCTION_REGEX_2: RegExp = /\([^)]*\)\s*=>/;
12-
private static readonly FUNCTION_REGEX_3: RegExp = /\w+\s*=>/;
10+
private static readonly FUNCTION_REGEX_1: RegExp = /^function\s*\w*\s*\([^)]*\)\s*{/;
11+
private static readonly FUNCTION_REGEX_2: RegExp = /^\([^)]*\)\s*=>/;
12+
private static readonly FUNCTION_REGEX_3: RegExp = /^\w+\s*=>/;
1313

1414
constructor (private typeDetector: ITypeDetector) {}
1515

@@ -256,9 +256,9 @@ export class Marshaller implements IMarshaller {
256256
* @returns {Function}
257257
*/
258258
private marshalStringToFunction (data: string): Function {
259-
if (Marshaller.FUNCTION_REGEX_1.test(data)) return new Function(`return ${data}`)();
260-
if (Marshaller.FUNCTION_REGEX_2.test(data)) return new Function(`return ${data}`)();
261-
if (Marshaller.FUNCTION_REGEX_3.test(data)) return new Function(`return ${data}`)();
259+
if (Marshaller.FUNCTION_REGEX_1.test(data.trim())) return new Function(`return ${data}`)();
260+
if (Marshaller.FUNCTION_REGEX_2.test(data.trim())) return new Function(`return ${data}`)();
261+
if (Marshaller.FUNCTION_REGEX_3.test(data.trim())) return new Function(`return ${data}`)();
262262
return () => data;
263263
}
264264

@@ -968,9 +968,9 @@ export class Marshaller implements IMarshaller {
968968
const asObj = this.attemptStringToObjectConversion(primitive);
969969
if (asObj != null) return asObj;
970970

971-
if (Marshaller.FUNCTION_REGEX_1.test(primitive)) return new Function(`return ${primitive}`)();
972-
if (Marshaller.FUNCTION_REGEX_2.test(primitive)) return new Function(`return ${primitive}`)();
973-
if (Marshaller.FUNCTION_REGEX_3.test(primitive)) return new Function(`return ${primitive}`)();
971+
if (Marshaller.FUNCTION_REGEX_1.test(primitive.trim())) return new Function(`return ${primitive}`)();
972+
if (Marshaller.FUNCTION_REGEX_2.test(primitive.trim())) return new Function(`return ${primitive}`)();
973+
if (Marshaller.FUNCTION_REGEX_3.test(primitive.trim())) return new Function(`return ${primitive}`)();
974974

975975
try {
976976
return JSON.parse(primitive);

test/Marshaller.test.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,42 @@ test(`'marshal()' string -> best guess. #2`, t => {
138138
t.true(typeDetector.isObject(marshaller.marshal(input)));
139139
});
140140

141+
test(`'marshal()' string -> best guess. #3`, t => {
142+
const input = `() => {}`;
143+
const expected = () => {};
144+
145+
const marshalled = marshaller.marshal(input);
146+
if (marshalled == null) t.fail();
147+
else t.deepEqual(marshalled.toString(), expected.toString());
148+
});
149+
150+
test(`'marshal()' string -> best guess. #4`, t => {
151+
const input = `function () {}`;
152+
const expected = function () {};
153+
154+
const marshalled = marshaller.marshal(input);
155+
if (marshalled == null) t.fail();
156+
else t.deepEqual(marshalled.toString(), expected.toString());
157+
});
158+
159+
test(`'marshal()' string -> best guess. #5`, t => {
160+
const input = `hellofunction () {}`;
161+
const expected = input;
162+
163+
const marshalled = marshaller.marshal(input);
164+
if (marshalled == null) t.fail();
165+
else t.deepEqual(marshalled.toString(), expected.toString());
166+
});
167+
168+
test(`'marshal()' string -> best guess. #4`, t => {
169+
const input = `hello() => {}`;
170+
const expected = input;
171+
172+
const marshalled = marshaller.marshal(input);
173+
if (marshalled == null) t.fail();
174+
else t.deepEqual(marshalled.toString(), expected.toString());
175+
});
176+
141177

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

0 commit comments

Comments
 (0)