From 8f67aa3f183bcfa3e180cd6b8554aea76841acb3 Mon Sep 17 00:00:00 2001 From: Jeremy Ashkenas Date: Fri, 15 Apr 2011 16:17:58 -0400 Subject: [PATCH] _.barrier -> _.after, switch the order of arguments ... fix test formatting. --- test/functions.js | 29 +++++++++++++---------------- underscore.js | 2 +- 2 files changed, 14 insertions(+), 17 deletions(-) diff --git a/test/functions.js b/test/functions.js index 77ad72b39..c3eb99fe4 100644 --- a/test/functions.js +++ b/test/functions.js @@ -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'); @@ -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); + test("functions: after", function() { + var testAfter = function(afterAmount, timesCalled) { + var afterCalled = 0; + var after = _.after(afterAmount, function() { + afterCalled++; + }); + while (timesCalled--) after(); + return afterCalled; + }; - - while (timesCalled--) { - barrier(); - } - - return barrierCalled; - } - - 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"); }); }); diff --git a/underscore.js b/underscore.js index a45ca8907..d18d72030 100644 --- a/underscore.js +++ b/underscore.js @@ -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); } };