Skip to content

Commit

Permalink
Merge pull request #31 from canjs/dev/multiple-equal
Browse files Browse the repository at this point in the history
Adds ability to use equal with more than two arguments
  • Loading branch information
phillipskevin authored May 19, 2017
2 parents f25605e + 64d7b65 commit 944be12
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
26 changes: 20 additions & 6 deletions can-stache-converters.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
var stache = require("can-stache");
var stringToAny = require("can-util/js/string-to-any/string-to-any");
var makeArray = require("can-util/js/make-array/make-array");
require("can-stache-bindings");

stache.registerConverter("boolean-to-inList", {
Expand Down Expand Up @@ -69,13 +70,26 @@ stache.registerConverter("either-or", {
});

stache.registerConverter("equal", {
get: function(compute, comparer){
var val = (compute && compute.isComputed) ? compute() : compute;
return val === comparer;
get: function(){
var args = makeArray(arguments);
if (args.length > 1) {
var comparer = args.pop();

return args.every(function(compute) {
return (compute && compute.isComputed ? compute() : compute) === comparer;
});
}
},
set: function(b, compute, comparer){
if(b) {
compute(comparer);
set: function(){
var args = makeArray(arguments);
if (args.length > 2) {
var b = args.shift();
var comparer = args.pop();
if(b) {
for(var i = 0; i < args.length; i++) {
args[i](comparer);
}
}
}
}
});
28 changes: 28 additions & 0 deletions test/equal_test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,31 @@ QUnit.test("Allows one-way binding when passed a non-compute as the first argume

QUnit.equal(attending(), true, 'does not change compute');
});

QUnit.test("Allow multiple expressions to be passed in", function() {
var template = stache('<input type="radio" {($checked)}="equal(~foo, ~bar, true)" />');
var foo = compute(true);
var bar = compute(false);

var input = template({
foo: foo,
bar: bar
}).firstChild;

QUnit.equal(input.checked, false, 'initially unchecked');

bar(true);

QUnit.equal(input.checked, true, 'now checked');

foo(false);
bar(false);

QUnit.equal(input.checked, false, 'now unchecked');

input.checked = true;
canEvent.trigger.call(input, "change");

QUnit.equal(foo(), true, 'computed foo value is true');
QUnit.equal(bar(), true, 'computed bar value is true');
});

0 comments on commit 944be12

Please sign in to comment.