@@ -7,9 +7,9 @@ import {ITypeDetector, IArbitraryObject} from "@wessberg/typedetector";
77 */
88export class Marshaller implements IMarshaller {
99 private static readonly SYMBOL_REGEX : RegExp = / S y m b o l \( ( [ ^ ) ] * ) \) / ;
10- private static readonly FUNCTION_REGEX_1 : RegExp = / f u n c t i o n \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 = / ^ f u n c t i o n \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 ) ;
0 commit comments