@@ -889,38 +889,31 @@ JSONPath.prototype._slice = function (loc, expr, val, path, parent, parentPropNa
889
889
} ;
890
890
891
891
JSONPath . prototype . _eval = function ( code , _v , _vname , path , parent , parentPropName ) {
892
- if ( code . includes ( '@parentProperty' ) ) {
893
- this . currSandbox . _$_parentProperty = parentPropName ;
894
- code = code . replace ( / @ p a r e n t P r o p e r t y / g, '_$_parentProperty' ) ;
895
- }
896
-
897
- if ( code . includes ( '@parent' ) ) {
898
- this . currSandbox . _$_parent = parent ;
899
- code = code . replace ( / @ p a r e n t / g, '_$_parent' ) ;
892
+ this . currSandbox . _$_parentProperty = parentPropName ;
893
+ this . currSandbox . _$_parent = parent ;
894
+ this . currSandbox . _$_property = _vname ;
895
+ this . currSandbox . _$_root = this . json ;
896
+ this . currSandbox . _$_v = _v ;
897
+ var containsPath = code . includes ( '@path' ) ;
898
+
899
+ if ( containsPath ) {
900
+ this . currSandbox . _$_path = JSONPath . toPathString ( path . concat ( [ _vname ] ) ) ;
900
901
}
901
902
902
- if ( code . includes ( '@property' ) ) {
903
- this . currSandbox . _$_property = _vname ;
904
- code = code . replace ( / @ p r o p e r t y / g, '_$_property' ) ;
905
- }
903
+ var scriptCacheKey = 'script:' + code ;
906
904
907
- if ( code . includes ( '@path' ) ) {
908
- this . currSandbox . _$_path = JSONPath . toPathString ( path . concat ( [ _vname ] ) ) ;
909
- code = code . replace ( / @ p a t h / g, '_$_path' ) ;
910
- }
905
+ if ( ! JSONPath . cache [ scriptCacheKey ] ) {
906
+ var script = code . replace ( / @ p a r e n t P r o p e r t y / g, '_$_parentProperty' ) . replace ( / @ p a r e n t / g, '_$_parent' ) . replace ( / @ p r o p e r t y / g, '_$_property' ) . replace ( / @ r o o t / g, '_$_root' ) . replace ( / @ ( [ \t - \r \) \. \[ \xA0 \u1680 \u2000 - \u200A \u2028 \u2029 \u202F \u205F \u3000 \uFEFF ] ) / g, '_$_v$1' ) ;
911
907
912
- if ( code . includes ( '@root' ) ) {
913
- this . currSandbox . _$_root = this . json ;
914
- code = code . replace ( / @ r o o t / g, '_$_root' ) ;
915
- }
908
+ if ( containsPath ) {
909
+ script = script . replace ( / @ p a t h / g, '_$_path' ) ;
910
+ }
916
911
917
- if ( / @ ( [ \t - \r \) \. \[ \xA0 \u1680 \u2000 - \u200A \u2028 \u2029 \u202F \u205F \u3000 \uFEFF ] ) / . test ( code ) ) {
918
- this . currSandbox . _$_v = _v ;
919
- code = code . replace ( / @ ( [ \t - \r \) \. \[ \xA0 \u1680 \u2000 - \u200A \u2028 \u2029 \u202F \u205F \u3000 \uFEFF ] ) / g, '_$_v$1' ) ;
912
+ JSONPath . cache [ scriptCacheKey ] = new this . vm . Script ( script ) ;
920
913
}
921
914
922
915
try {
923
- return this . vm . runInNewContext ( code , this . currSandbox ) ;
916
+ return JSONPath . cache [ scriptCacheKey ] . runInNewContext ( this . currSandbox ) ;
924
917
} catch ( e ) {
925
918
throw new Error ( 'jsonPath: ' + e . message + ': ' + code ) ;
926
919
}
@@ -1040,48 +1033,71 @@ var moveToAnotherArray = function moveToAnotherArray(source, target, conditionCb
1040
1033
}
1041
1034
}
1042
1035
} ;
1036
+ /**
1037
+ * In-browser replacement for NodeJS' VM.Script.
1038
+ */
1043
1039
1044
- JSONPath . prototype . vm = {
1040
+
1041
+ var Script = /*#__PURE__*/ function ( ) {
1045
1042
/**
1046
1043
* @param {string } expr Expression to evaluate
1044
+ */
1045
+ function Script ( expr ) {
1046
+ _classCallCheck ( this , Script ) ;
1047
+
1048
+ this . code = expr ;
1049
+ }
1050
+ /**
1047
1051
* @param {PlainObject } context Object whose items will be added
1048
1052
* to evaluation
1049
1053
* @returns {EvaluatedResult } Result of evaluated code
1050
1054
*/
1051
- runInNewContext : function runInNewContext ( expr , context ) {
1052
- var keys = Object . keys ( context ) ;
1053
- var funcs = [ ] ;
1054
- moveToAnotherArray ( keys , funcs , function ( key ) {
1055
- return typeof context [ key ] === 'function' ;
1056
- } ) ;
1057
- var values = keys . map ( function ( vr , i ) {
1058
- return context [ vr ] ;
1059
- } ) ;
1060
- var funcString = funcs . reduce ( function ( s , func ) {
1061
- var fString = context [ func ] . toString ( ) ;
1062
1055
1063
- if ( ! / f u n c t i o n / . test ( fString ) ) {
1064
- fString = 'function ' + fString ;
1065
- }
1066
1056
1067
- return 'var ' + func + '=' + fString + ';' + s ;
1068
- } , '' ) ;
1069
- expr = funcString + expr ; // Mitigate http://perfectionkills.com/global-eval-what-are-the-options/#new_function
1057
+ _createClass ( Script , [ {
1058
+ key : "runInNewContext" ,
1059
+ value : function runInNewContext ( context ) {
1060
+ var expr = this . code ;
1061
+ var keys = Object . keys ( context ) ;
1062
+ var funcs = [ ] ;
1063
+ moveToAnotherArray ( keys , funcs , function ( key ) {
1064
+ return typeof context [ key ] === 'function' ;
1065
+ } ) ;
1066
+ var values = keys . map ( function ( vr , i ) {
1067
+ return context [ vr ] ;
1068
+ } ) ;
1069
+ var funcString = funcs . reduce ( function ( s , func ) {
1070
+ var fString = context [ func ] . toString ( ) ;
1071
+
1072
+ if ( ! / f u n c t i o n / . test ( fString ) ) {
1073
+ fString = 'function ' + fString ;
1074
+ }
1070
1075
1071
- if ( ! / ( [ " ' ] ) u s e s t r i c t \1/ . test ( expr ) && ! keys . includes ( 'arguments' ) ) {
1072
- expr = 'var arguments = undefined;' + expr ;
1073
- } // Remove last semi so `return` will be inserted before
1074
- // the previous one instead, allowing for the return
1075
- // of a bare ending expression
1076
+ return 'var ' + func + '=' + fString + ';' + s ;
1077
+ } , '' ) ;
1078
+ expr = funcString + expr ; // Mitigate http://perfectionkills.com/global-eval-what-are-the-options/#new_function
1076
1079
1080
+ if ( ! / ( [ " ' ] ) u s e s t r i c t \1/ . test ( expr ) && ! keys . includes ( 'arguments' ) ) {
1081
+ expr = 'var arguments = undefined;' + expr ;
1082
+ } // Remove last semi so `return` will be inserted before
1083
+ // the previous one instead, allowing for the return
1084
+ // of a bare ending expression
1077
1085
1078
- expr = expr . replace ( / ; [ \t - \r \xA0 \u1680 \u2000 - \u200A \u2028 \u2029 \u202F \u205F \u3000 \uFEFF ] * $ / , '' ) ; // Insert `return`
1079
1086
1080
- var lastStatementEnd = expr . lastIndexOf ( ';' ) ;
1081
- var code = lastStatementEnd > - 1 ? expr . slice ( 0 , lastStatementEnd + 1 ) + ' return ' + expr . slice ( lastStatementEnd + 1 ) : ' return ' + expr ; // eslint-disable-next-line no-new-func
1087
+ expr = expr . replace ( / ; [ \t - \r \xA0 \u1680 \u2000 - \u200A \u2028 \u2029 \u202F \u205F \u3000 \uFEFF ] * $ / , '' ) ; // Insert `return`
1082
1088
1083
- return _construct ( Function , _toConsumableArray ( keys ) . concat ( [ code ] ) ) . apply ( void 0 , _toConsumableArray ( values ) ) ;
1084
- }
1089
+ var lastStatementEnd = expr . lastIndexOf ( ';' ) ;
1090
+ var code = lastStatementEnd > - 1 ? expr . slice ( 0 , lastStatementEnd + 1 ) + ' return ' + expr . slice ( lastStatementEnd + 1 ) : ' return ' + expr ; // eslint-disable-next-line no-new-func
1091
+
1092
+ return _construct ( Function , _toConsumableArray ( keys ) . concat ( [ code ] ) ) . apply ( void 0 , _toConsumableArray ( values ) ) ;
1093
+ }
1094
+ } ] ) ;
1095
+
1096
+ return Script ;
1097
+ } ( ) ;
1098
+
1099
+ JSONPath . prototype . vm = {
1100
+ Script : Script
1085
1101
} ;
1086
1102
1087
1103
export { JSONPath } ;
0 commit comments