Permalink
Browse files

Merge branch 'master' of https://github.com/documentcloud/underscore

…into unzip-no-sparse
  • Loading branch information...
2 parents 4d7ba20 + c57f276 commit 3e9e629d7a233a3d5e64bc6e3fa3c1899b0ed364 @almost almost committed Apr 4, 2013
Showing with 32 additions and 8 deletions.
  1. +2 −1 index.html
  2. +3 −2 package.json
  3. +3 −0 test/collections.js
  4. +14 −0 test/functions.js
  5. +4 −0 test/utility.js
  6. +6 −5 underscore.js
View
@@ -1486,7 +1486,8 @@ <h2 id="utility">Utility Functions</h2>
<b class="header">times</b><code>_.times(n, iterator, [context])</code>
<br />
Invokes the given iterator function <b>n</b> times. Each invocation of
- <b>iterator</b> is called with an <tt>index</tt> argument.
+ <b>iterator</b> is called with an <tt>index</tt> argument. Produces an
+ array of the returned values.
<br />
<i>Note: this example uses the <a href="#chaining">chaining syntax</a></i>.
</p>
View
@@ -8,9 +8,10 @@
"main" : "underscore.js",
"version" : "1.4.4",
"devDependencies": {
- "phantomjs": "1.8.1-3"
+ "phantomjs": "1.9.0-1"
},
"scripts": {
"test": "phantomjs test/vendor/runner.js test/index.html?noglobals=true"
- }
+ },
+ "license" : "MIT"
}
View
@@ -76,6 +76,9 @@ $(document).ready(function() {
var sum = _.reduce([1, 2, 3], function(sum, num){ return sum + num; });
equal(sum, 6, 'default initial value');
+ var prod = _.reduce([1, 2, 3, 4], function(prod, num){ return prod * num; });
+ equal(prod, 24, 'can reduce via multiplication');
+
var ifnull;
try {
_.reduce(null, function(){});
View
@@ -183,6 +183,20 @@ $(document).ready(function() {
}, 96);
});
+ asyncTest("throttle does not trigger leading call when immediate is set to false", 2, function() {
+ var counter = 0;
+ var incr = function(){ counter++; };
+ var throttledIncr = _.throttle(incr, 60, false);
+
+ throttledIncr(); throttledIncr();
+ ok(counter == 0);
+
+ _.delay(function() {
+ ok(counter == 1);
+ start();
+ }, 96);
+ });
+
asyncTest("debounce", 1, function() {
var counter = 0;
var incr = function(){ counter++; };
View
@@ -55,6 +55,10 @@ $(document).ready(function() {
ok(_.isEqual(vals, [0,1,2]), "works as a wrapper");
// collects return values
ok(_.isEqual([0, 1, 2], _.times(3, function(i) { return i; })), "collects return values");
+
+ deepEqual(_.times(0, _.identity), []);
+ deepEqual(_.times(-1, _.identity), []);
+ deepEqual(_.times(parseFloat('-Infinity'), _.identity), []);
});
test("mixin", function() {
View
@@ -96,7 +96,7 @@
if (obj == null) return results;
if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
each(obj, function(value, index, list) {
- results[results.length] = iterator.call(context, value, index, list);
+ results.push(iterator.call(context, value, index, list));
});
return results;
};
@@ -171,7 +171,7 @@
if (obj == null) return results;
if (nativeFilter && obj.filter === nativeFilter) return obj.filter(iterator, context);
each(obj, function(value, index, list) {
- if (iterator.call(context, value, index, list)) results[results.length] = value;
+ if (iterator.call(context, value, index, list)) results.push(value);
});
return results;
};
@@ -650,7 +650,7 @@
// Returns a function, that, when invoked, will only be triggered at most once
// during a given window of time.
- _.throttle = function(func, wait) {
+ _.throttle = function(func, wait, immediate) {
var context, args, timeout, result;
var previous = 0;
var later = function() {
@@ -660,6 +660,7 @@
};
return function() {
var now = new Date;
+ if (!previous && immediate === false) previous = now;
var remaining = wait - (now - previous);
context = this;
args = arguments;
@@ -750,7 +751,7 @@
_.keys = nativeKeys || function(obj) {
if (obj !== Object(obj)) throw new TypeError('Invalid object');
var keys = [];
- for (var key in obj) if (_.has(obj, key)) keys[keys.length] = key;
+ for (var key in obj) if (_.has(obj, key)) keys.push(key);
return keys;
};
@@ -1034,7 +1035,7 @@
// Run a function **n** times.
_.times = function(n, iterator, context) {
- var accum = Array(n);
+ var accum = Array(Math.max(0, n));
for (var i = 0; i < n; i++) accum[i] = iterator.call(context, i);
return accum;
};

0 comments on commit 3e9e629

Please sign in to comment.