mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-10 02:47:50 +00:00
Update vendor/underscore and continue to tweak _.throttle unit tests to avoid false fails.
Former-commit-id: b5ba7b53e3bbebb3fa42da7e197f746515c8efb0
This commit is contained in:
@@ -1833,7 +1833,7 @@
|
|||||||
|
|
||||||
asyncTest('should trigger trailing call when invoked repeatedly', function() {
|
asyncTest('should trigger trailing call when invoked repeatedly', function() {
|
||||||
var counter = 0,
|
var counter = 0,
|
||||||
limit = 80,
|
limit = 48,
|
||||||
throttled = _.throttle(function() { counter++; }, 32),
|
throttled = _.throttle(function() { counter++; }, 32),
|
||||||
start = new Date;
|
start = new Date;
|
||||||
|
|
||||||
|
|||||||
122
vendor/underscore/test/functions.js
vendored
122
vendor/underscore/test/functions.js
vendored
@@ -95,80 +95,67 @@ $(document).ready(function() {
|
|||||||
asyncTest("throttle", 2, function() {
|
asyncTest("throttle", 2, function() {
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
var incr = function(){ counter++; };
|
var incr = function(){ counter++; };
|
||||||
var throttledIncr = _.throttle(incr, 100);
|
var throttledIncr = _.throttle(incr, 32);
|
||||||
throttledIncr(); throttledIncr(); throttledIncr();
|
throttledIncr(); throttledIncr();
|
||||||
setTimeout(throttledIncr, 70);
|
|
||||||
setTimeout(throttledIncr, 120);
|
equal(counter, 1, "incr was called immediately");
|
||||||
setTimeout(throttledIncr, 140);
|
_.delay(function(){ equal(counter, 2, "incr was throttled"); start(); }, 64);
|
||||||
setTimeout(throttledIncr, 190);
|
|
||||||
setTimeout(throttledIncr, 220);
|
|
||||||
setTimeout(throttledIncr, 240);
|
|
||||||
_.delay(function(){ equal(counter, 1, "incr was called immediately"); }, 30);
|
|
||||||
_.delay(function(){ equal(counter, 4, "incr was throttled"); start(); }, 400);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest("throttle arguments", 2, function() {
|
asyncTest("throttle arguments", 2, function() {
|
||||||
var value = 0;
|
var value = 0;
|
||||||
var update = function(val){ value = val; };
|
var update = function(val){ value = val; };
|
||||||
var throttledUpdate = _.throttle(update, 100);
|
var throttledUpdate = _.throttle(update, 32);
|
||||||
throttledUpdate(1); throttledUpdate(2); throttledUpdate(3);
|
throttledUpdate(1); throttledUpdate(2);
|
||||||
setTimeout(function(){ throttledUpdate(4); }, 120);
|
_.delay(function(){ throttledUpdate(3); }, 64);
|
||||||
setTimeout(function(){ throttledUpdate(5); }, 140);
|
equal(value, 1, "updated to latest value");
|
||||||
setTimeout(function(){ throttledUpdate(6); }, 250);
|
_.delay(function(){ equal(value, 3, "updated to latest value"); start(); }, 96);
|
||||||
_.delay(function(){ equal(value, 1, "updated to latest value"); }, 40);
|
|
||||||
_.delay(function(){ equal(value, 6, "updated to latest value"); start(); }, 400);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest("throttle once", 2, function() {
|
asyncTest("throttle once", 2, function() {
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
var incr = function(){ return ++counter; };
|
var incr = function(){ return ++counter; };
|
||||||
var throttledIncr = _.throttle(incr, 100);
|
var throttledIncr = _.throttle(incr, 32);
|
||||||
var result = throttledIncr();
|
var result = throttledIncr();
|
||||||
_.delay(function(){
|
_.delay(function(){
|
||||||
equal(result, 1, "throttled functions return their value");
|
equal(result, 1, "throttled functions return their value");
|
||||||
equal(counter, 1, "incr was called once"); start();
|
equal(counter, 1, "incr was called once"); start();
|
||||||
}, 220);
|
}, 64);
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest("throttle twice", 1, function() {
|
asyncTest("throttle twice", 1, function() {
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
var incr = function(){ counter++; };
|
var incr = function(){ counter++; };
|
||||||
var throttledIncr = _.throttle(incr, 100);
|
var throttledIncr = _.throttle(incr, 32);
|
||||||
throttledIncr(); throttledIncr();
|
throttledIncr(); throttledIncr();
|
||||||
_.delay(function(){ equal(counter, 2, "incr was called twice"); start(); }, 220);
|
_.delay(function(){ equal(counter, 2, "incr was called twice"); start(); }, 64);
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest("throttle repeatedly with results", 9, function() {
|
asyncTest("throttle repeatedly with results", 6, function() {
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
var incr = function(){ return ++counter; };
|
var incr = function(){ return ++counter; };
|
||||||
var throttledIncr = _.throttle(incr, 100);
|
var throttledIncr = _.throttle(incr, 64);
|
||||||
var results = [];
|
var results = [];
|
||||||
var saveResult = function() { results.push(throttledIncr()); };
|
var saveResult = function() { results.push(throttledIncr()); };
|
||||||
saveResult(); saveResult(); saveResult();
|
saveResult(); saveResult();
|
||||||
setTimeout(saveResult, 70);
|
_.delay(saveResult, 32);
|
||||||
setTimeout(saveResult, 120);
|
_.delay(saveResult, 80);
|
||||||
setTimeout(saveResult, 140);
|
_.delay(saveResult, 96);
|
||||||
setTimeout(saveResult, 190);
|
_.delay(saveResult, 144);
|
||||||
setTimeout(saveResult, 240);
|
|
||||||
setTimeout(saveResult, 260);
|
|
||||||
_.delay(function() {
|
_.delay(function() {
|
||||||
equal(results[0], 1, "incr was called once");
|
equal(results[0], 1, "incr was called once");
|
||||||
equal(results[1], 1, "incr was throttled");
|
equal(results[1], 1, "incr was throttled");
|
||||||
equal(results[2], 1, "incr was throttled");
|
equal(results[2], 1, "incr was throttled");
|
||||||
equal(results[3], 1, "incr was throttled");
|
equal(results[3], 2, "incr was called twice");
|
||||||
equal(results[4], 2, "incr was called twice");
|
equal(results[4], 2, "incr was throttled");
|
||||||
equal(results[5], 2, "incr was throttled");
|
equal(results[5], 3, "incr was called trailing");
|
||||||
equal(results[6], 2, "incr was throttled");
|
|
||||||
equal(results[7], 3, "incr was called thrice");
|
|
||||||
equal(results[8], 3, "incr was throttled");
|
|
||||||
start();
|
start();
|
||||||
}, 400);
|
}, 192);
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest("throttle triggers trailing call after repeatedly invoked", 2, function() {
|
asyncTest("throttle triggers trailing call when invoked repeatedly", 2, function() {
|
||||||
var actual;
|
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
var limit = 80;
|
var limit = 48;
|
||||||
var incr = function(){ counter++; };
|
var incr = function(){ counter++; };
|
||||||
var throttledIncr = _.throttle(incr, 32);
|
var throttledIncr = _.throttle(incr, 32);
|
||||||
|
|
||||||
@@ -176,62 +163,49 @@ $(document).ready(function() {
|
|||||||
while ((new Date - stamp) < limit) {
|
while ((new Date - stamp) < limit) {
|
||||||
throttledIncr();
|
throttledIncr();
|
||||||
}
|
}
|
||||||
_.delay(function() {
|
var lastCount = counter;
|
||||||
actual = counter + 2;
|
|
||||||
throttledIncr();
|
|
||||||
throttledIncr();
|
|
||||||
}, 64);
|
|
||||||
|
|
||||||
_.delay(function() {
|
|
||||||
equal(counter, actual);
|
|
||||||
start();
|
|
||||||
}, 128);
|
|
||||||
|
|
||||||
ok(counter > 1);
|
ok(counter > 1);
|
||||||
|
|
||||||
|
_.delay(function() {
|
||||||
|
ok(counter > lastCount);
|
||||||
|
start();
|
||||||
|
}, 96);
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest("debounce", 1, function() {
|
asyncTest("debounce", 1, function() {
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
var incr = function(){ counter++; };
|
var incr = function(){ counter++; };
|
||||||
var debouncedIncr = _.debounce(incr, 50);
|
var debouncedIncr = _.debounce(incr, 32);
|
||||||
debouncedIncr(); debouncedIncr(); debouncedIncr();
|
debouncedIncr(); debouncedIncr();
|
||||||
setTimeout(debouncedIncr, 30);
|
_.delay(debouncedIncr, 16);
|
||||||
setTimeout(debouncedIncr, 60);
|
_.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 96);
|
||||||
setTimeout(debouncedIncr, 90);
|
|
||||||
setTimeout(debouncedIncr, 120);
|
|
||||||
setTimeout(debouncedIncr, 150);
|
|
||||||
_.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 220);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest("debounce asap", 5, function() {
|
asyncTest("debounce asap", 4, function() {
|
||||||
var a, b, c;
|
var a, b;
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
var incr = function(){ return ++counter; };
|
var incr = function(){ return ++counter; };
|
||||||
var debouncedIncr = _.debounce(incr, 50, true);
|
var debouncedIncr = _.debounce(incr, 64, true);
|
||||||
a = debouncedIncr();
|
a = debouncedIncr();
|
||||||
b = debouncedIncr();
|
b = debouncedIncr();
|
||||||
c = debouncedIncr();
|
|
||||||
equal(a, 1);
|
equal(a, 1);
|
||||||
equal(b, 1);
|
equal(b, 1);
|
||||||
equal(c, 1);
|
|
||||||
equal(counter, 1, 'incr was called immediately');
|
equal(counter, 1, 'incr was called immediately');
|
||||||
setTimeout(debouncedIncr, 30);
|
_.delay(debouncedIncr, 16);
|
||||||
setTimeout(debouncedIncr, 60);
|
_.delay(debouncedIncr, 32);
|
||||||
setTimeout(debouncedIncr, 90);
|
_.delay(debouncedIncr, 48);
|
||||||
setTimeout(debouncedIncr, 120);
|
_.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 128);
|
||||||
setTimeout(debouncedIncr, 150);
|
|
||||||
_.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 220);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
asyncTest("debounce asap recursively", 2, function() {
|
asyncTest("debounce asap recursively", 2, function() {
|
||||||
var counter = 0;
|
var counter = 0;
|
||||||
var debouncedIncr = _.debounce(function(){
|
var debouncedIncr = _.debounce(function(){
|
||||||
counter++;
|
counter++;
|
||||||
if (counter < 5) debouncedIncr();
|
if (counter < 10) debouncedIncr();
|
||||||
}, 50, true);
|
}, 32, true);
|
||||||
debouncedIncr();
|
debouncedIncr();
|
||||||
equal(counter, 1, 'incr was called immediately');
|
equal(counter, 1, "incr was called immediately");
|
||||||
_.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 70);
|
_.delay(function(){ equal(counter, 1, "incr was debounced"); start(); }, 96);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("once", function() {
|
test("once", function() {
|
||||||
|
|||||||
2
vendor/underscore/underscore-min.js
vendored
2
vendor/underscore/underscore-min.js
vendored
File diff suppressed because one or more lines are too long
16
vendor/underscore/underscore.js
vendored
16
vendor/underscore/underscore.js
vendored
@@ -938,8 +938,8 @@
|
|||||||
return obj === Object(obj);
|
return obj === Object(obj);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Add some isType methods: isArguments, isFunction, isString, isNumber, isDate, isRegExp.
|
// Add some isType methods: isArguments, isString, isNumber, isDate, isRegExp.
|
||||||
each(['Arguments', 'Function', 'String', 'Number', 'Date', 'RegExp'], function(name) {
|
each(['Arguments', 'String', 'Number', 'Date', 'RegExp'], function(name) {
|
||||||
_['is' + name] = function(obj) {
|
_['is' + name] = function(obj) {
|
||||||
return toString.call(obj) == '[object ' + name + ']';
|
return toString.call(obj) == '[object ' + name + ']';
|
||||||
};
|
};
|
||||||
@@ -953,10 +953,16 @@
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Optimize `isFunction` if appropriate.
|
// Is a given variable a function?
|
||||||
if (typeof (/./) !== 'function') {
|
_.isFunction = function(obj) {
|
||||||
|
return typeof obj === 'function';
|
||||||
|
};
|
||||||
|
|
||||||
|
// Define a fallback for older versions of Chrome, Firefox, and Safari, where
|
||||||
|
// a regexp is `typeof` "function".
|
||||||
|
if (_.isFunction(/./)) {
|
||||||
_.isFunction = function(obj) {
|
_.isFunction = function(obj) {
|
||||||
return typeof obj === 'function';
|
return toString.call(obj) == '[object Function]';
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user