@@ -2,18 +2,25 @@ import isArray from './helpers/isArray';
22import isLogic from './helpers/isLogic' ;
33import getOperator from './helpers/getOperator' ;
44
5- function createJsonLogic ( operations = { } , visitors = { } ) {
6- Object . keys ( operations ) . forEach ( function ( name ) {
7- const operation = operations [ name ] ;
5+ function createJsonLogic ( _operations , _visitors ) {
6+ const operations = { } ;
7+ const visitors = { } ;
88
9- addOperation ( operation . code || name , operation ) ;
10- } ) ;
9+ if ( _operations ) {
10+ Object . keys ( _operations ) . forEach ( function ( name ) {
11+ const operation = _operations [ name ] ;
1112
12- Object . keys ( visitors ) . forEach ( function ( name ) {
13- const visitor = visitors [ name ] ;
13+ addOperation ( operation . code || name , operation ) ;
14+ } ) ;
15+ }
1416
15- addVisitor ( visitor . code || name , visitor ) ;
16- } ) ;
17+ if ( _visitors ) {
18+ Object . keys ( _visitors ) . forEach ( function ( name ) {
19+ const visitor = _visitors [ name ] ;
20+
21+ addVisitor ( visitor . code || name , visitor ) ;
22+ } ) ;
23+ }
1724
1825 function addOperation ( name , code ) {
1926 operations [ name ] = code ;
@@ -25,7 +32,8 @@ function createJsonLogic(operations = {}, visitors = {}) {
2532
2633 function addVisitor ( name , code ) {
2734 if ( isArray ( name ) ) {
28- name . forEach ( addVisitor ) ;
35+ name . forEach ( ( key ) => addVisitor ( key , code ) ) ;
36+ return ;
2937 }
3038
3139 visitors [ name ] = code ;
@@ -34,6 +42,7 @@ function createJsonLogic(operations = {}, visitors = {}) {
3442 function removeVisitor ( name ) {
3543 if ( isArray ( name ) ) {
3644 name . forEach ( removeVisitor ) ;
45+ return ;
3746 }
3847
3948 delete visitors [ name ] ;
@@ -75,8 +84,13 @@ function createJsonLogic(operations = {}, visitors = {}) {
7584 // The operation is called with "data" bound to its "this" and "values" passed as arguments.
7685 // Structured commands like % or > can name formal arguments while flexible commands (like missing or merge) can operate on the pseudo-array arguments
7786 // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments
78- if ( typeof operations [ op ] === "function" ) {
79- return operations [ op ] . apply ( data , values ) ;
87+ const operator = operations [ op ] ;
88+ if ( typeof operator === "function" ) {
89+ if ( operator . withApply ) {
90+ values . unshift ( apply ) ;
91+ }
92+
93+ return operator . apply ( data , values ) ;
8094 } else if ( op . indexOf ( "." ) > 0 ) { // Contains a dot, and not in the 0th position
8195 var sub_ops = String ( op ) . split ( "." ) ;
8296 var operation = operations ;
0 commit comments