diff --git a/logic.js b/logic.js index 6b827df..99ad97f 100644 --- a/logic.js +++ b/logic.js @@ -11,7 +11,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html } else { root.jsonLogic = factory(); } -}(this, function() { +}(this, function JSONLogic() { "use strict"; /* globals console:false */ @@ -460,5 +460,7 @@ http://ricostacruz.com/cheatsheets/umdjs.html return false; }; + jsonLogic.JSONLogic = JSONLogic; + return jsonLogic; })); diff --git a/tests/tests.js b/tests/tests.js index b201bf6..95380a9 100644 --- a/tests/tests.js +++ b/tests/tests.js @@ -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"); + +});