Skip to content

Commit

Permalink
_.barrier -> _.after, switch the order of arguments ... fix test form…
Browse files Browse the repository at this point in the history
…atting.
  • Loading branch information
jashkenas committed Apr 15, 2011
1 parent c7b47ed commit 8f67aa3
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 17 deletions.
29 changes: 13 additions & 16 deletions test/functions.js
Expand Up @@ -21,7 +21,7 @@ $(document).ready(function() {
var func = _.bind(func, this, 'curly');
equals(func(), 'hello: curly', 'the function was completely applied in advance');

var func = function(salutation, firstname, lastname) { return salutation + ': ' + firstname + ' ' + lastname };
var func = function(salutation, firstname, lastname) { return salutation + ': ' + firstname + ' ' + lastname; };
func = _.bind(func, this, 'hello', 'moe', 'curly');
equals(func(), 'hello: moe curly', 'the function was partially applied in advance and can accept multiple arguments');

Expand Down Expand Up @@ -137,22 +137,19 @@ $(document).ready(function() {
equals(composed('moe'), 'hi: moe!', 'in this case, the functions are also commutative');
});

test("functions: barrier", function() {
var testBarrier = function(barrierAmount, timesCalled) {
var barrierCalled = 0;
var barrier = _.barrier(function(){ barrierCalled++; }, barrierAmount);


while (timesCalled--) {
barrier();
}

return barrierCalled;
}
test("functions: after", function() {
var testAfter = function(afterAmount, timesCalled) {
var afterCalled = 0;
var after = _.after(afterAmount, function() {
afterCalled++;
});
while (timesCalled--) after();
return afterCalled;
};

equals(testBarrier(5, 5), 1, "barrier(N) should fire after being called N times");
equals(testBarrier(5, 4), 0, "barrier(N) should not fire unless called N times");
equals(testBarrier(5, 6), 1, "barrier(N) should fire only once even if called more than N times");
equals(testAfter(5, 5), 1, "after(N) should fire after being called N times");
equals(testAfter(5, 4), 0, "after(N) should not fire unless called N times");
equals(testAfter(5, 6), 1, "after(N) should fire only once even if called more than N times");
});

});
2 changes: 1 addition & 1 deletion underscore.js
Expand Up @@ -511,7 +511,7 @@
};

// Returns a function that will only be executed after being called N times.
_.barrier = function(func, times) {
_.after = function(times, func) {
return function() {
if (--times === 0) { return func.apply(this, arguments); }
};
Expand Down

0 comments on commit 8f67aa3

Please sign in to comment.