Skip to content

Allow for distinct JSONLogic instances #71

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

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
4 changes: 3 additions & 1 deletion logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html
} else {
root.jsonLogic = factory();
}
}(this, function() {
}(this, function JSONLogic() {
"use strict";
/* globals console:false */

Expand Down Expand Up @@ -460,5 +460,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html
return false;
};

jsonLogic.JSONLogic = JSONLogic;

return jsonLogic;
}));
44 changes: 44 additions & 0 deletions tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,47 @@ QUnit.test("Control structures don't eval depth-first", function(assert) {
jsonLogic.apply({"or": [{"push": [true]}, {"push": [true]}]});
assert.deepEqual(i, [true]);
});


QUnit.test("Each JSONLogic instance can have its own custom operations", function(assert) {

// This function is just here to ensure that we're calling
// the JSONLogic instances in exactly the same way each time.
function callOperationOn(instance) {
return instance.apply({"join": [["one", "two"], ","]});
}


var foo = new jsonLogic.JSONLogic();
foo.add_operation("join", function(ary, glue) {
return ary.join(glue);
});



var bar = new jsonLogic.JSONLogic();
bar.add_operation("join", function(ary, glue) {
return "Nope.";
});


assert.equal(callOperationOn(foo), "one,two");
assert.equal(callOperationOn(bar), "Nope.");
assert.throws(function() {
// This custom operator was never defined within the default
// jsonLogic instance. Therefore, it will throw an exception.
callOperationOn(jsonLogic);
});


bar.rm_operation("join");
assert.throws(function() {
// This line throws an exception now because we removed the custom
// operation from bar.
callOperationOn(bar);
});

// Calling bar.rm_operation(...) didn't somehow magically change foo's state.
assert.equal(callOperationOn(foo), "one,two");

});