Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: ability to add custom operators which are not automatically traversed for evaluation, v2 #120

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html
}

var jsonLogic = {};
var opOptions = {};
var operations = {
"==": function(a, b) {
return a == b;
Expand Down Expand Up @@ -348,6 +349,8 @@ http://ricostacruz.com/cheatsheets/umdjs.html
}
}
return false; // None were truthy
} else if (opOptions.hasOwnProperty(op) && !opOptions[op].traverse) {
return operations[op](values, data, jsonLogic);
}

// Everyone else gets immediate depth-first recursion
Expand Down Expand Up @@ -404,12 +407,16 @@ http://ricostacruz.com/cheatsheets/umdjs.html
return arrayUnique(collection);
};

jsonLogic.add_operation = function(name, code) {
jsonLogic.add_operation = function(name, code, options) {
operations[name] = code;
opOptions[name] = {
traverse: Boolean(!options || options.traverse),
};
};

jsonLogic.rm_operation = function(name) {
delete operations[name];
delete opOptions[name];
};

jsonLogic.rule_like = function(rule, pattern) {
Expand Down
45 changes: 45 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,4 +307,49 @@ QUnit.module('basic', () => {
jsonLogic.apply({"or": [{"push": [true]}, {"push": [true]}]});
assert.deepEqual(i, [true]);
});

QUnit.test("Expanding functionality with add_operator - traverse", function(assert) {
// assert that controlled execution doesn't do pre-evaluation
var customOp = function(values) {
return values[0];
}

jsonLogic.add_operation('customOp', customOp, { traverse: false });

assert.deepEqual(jsonLogic.apply({ customOp: [{ "var": "" }, { "var": "test" }]}, { test: 123 }), { var: "" });
assert.deepEqual(jsonLogic.apply({ customOp: [{ "var": "test" }, { "var": "" }]}, { test: 123 }), { var: "test" });

// assert that controlled execution custom operators can be removed as normal
jsonLogic.rm_operation('customOp');

assert.throws(() => jsonLogic.apply({ customOp: [] }), Error, "Unrecognized operation customOp");

// assert that controlled-execution custom operators have access to jsonLogic object
// and can run on external data
const externalData = {
specialReference: 'external reference'
};
customOp = function(values, data, jsonLogic) {
return jsonLogic.apply(values[0], externalData);
}

jsonLogic.add_operation('customOp', customOp, { traverse: false });

assert.deepEqual(jsonLogic.apply({ customOp: [{ var: "specialReference" }] }, { specialReference: 'pre-evaluation value' }), 'external reference');

// assert that operators are added with normal functionality when options is omitted
jsonLogic.add_operation('customOp', customOp);

assert.throws(() => jsonLogic.apply({ customOp: [{ var: "specialReference" }] }, { specialReference: 'pre-evaluation value' }), TypeError, "Cannot read property 'apply' of undefined");

// assert that adding a custom operator without controlled-execution still
// results in pre-evaluation
customOp = function(value) {
return value;
}

jsonLogic.add_operation('customOp', customOp);

assert.deepEqual(jsonLogic.apply({ customOp: [{ var: "specialReference" }] }, { specialReference: 'pre-evaluation value' }), 'pre-evaluation value');
});
});