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

Adds ability to use equal with more than two arguments #31

Merged
merged 1 commit into from
May 19, 2017
Merged
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
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is .every in every browser ... hahaha ... I kill myself ... but seriously ... it is in IE9 so yay!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦‍♂️

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');
});