Drop boolean options param support in _.debounce and _.throttle.

This commit is contained in:
John-David Dalton
2015-07-09 19:51:11 -07:00
parent d58cda122d
commit 8cc19d908e
2 changed files with 27 additions and 48 deletions

View File

@@ -7093,6 +7093,7 @@
timeoutId, timeoutId,
trailingCall, trailingCall,
lastCalled = 0, lastCalled = 0,
leading = false,
maxWait = false, maxWait = false,
trailing = true; trailing = true;
@@ -7100,10 +7101,7 @@
throw new TypeError(FUNC_ERROR_TEXT); throw new TypeError(FUNC_ERROR_TEXT);
} }
wait = wait < 0 ? 0 : (+wait || 0); wait = wait < 0 ? 0 : (+wait || 0);
if (options === true) { if (isObject(options)) {
var leading = true;
trailing = false;
} else if (isObject(options)) {
leading = !!options.leading; leading = !!options.leading;
maxWait = 'maxWait' in options && nativeMax(+options.maxWait || 0, wait); maxWait = 'maxWait' in options && nativeMax(+options.maxWait || 0, wait);
trailing = 'trailing' in options ? !!options.trailing : trailing; trailing = 'trailing' in options ? !!options.trailing : trailing;
@@ -7686,9 +7684,7 @@
if (typeof func != 'function') { if (typeof func != 'function') {
throw new TypeError(FUNC_ERROR_TEXT); throw new TypeError(FUNC_ERROR_TEXT);
} }
if (options === false) { if (isObject(options)) {
leading = false;
} else if (isObject(options)) {
leading = 'leading' in options ? !!options.leading : leading; leading = 'leading' in options ? !!options.leading : leading;
trailing = 'trailing' in options ? !!options.trailing : trailing; trailing = 'trailing' in options ? !!options.trailing : trailing;
} }

View File

@@ -2841,48 +2841,40 @@
} }
}); });
asyncTest('should support a `leading` option', 7, function() { asyncTest('should support a `leading` option', 5, function() {
if (!(isRhino && isModularize)) { if (!(isRhino && isModularize)) {
var withLeading, var callCounts = [0, 0];
callCounts = [0, 0, 0];
_.each([true, { 'leading': true }], function(options, index) { var withLeading = _.debounce(function(value) {
var debounced = _.debounce(function(value) { callCounts[0]++;
callCounts[index]++; return value;
return value; }, 32, { 'leading': true });
}, 32, options);
if (index == 1) { strictEqual(withLeading('x'), 'x');
withLeading = debounced;
}
strictEqual(debounced('x'), 'x');
});
_.each([false, { 'leading': false }], function(options) { var withoutLeading = _.debounce(_.identity, 32, { 'leading': false });
var withoutLeading = _.debounce(_.identity, 32, options); strictEqual(withoutLeading('x'), undefined);
strictEqual(withoutLeading('x'), undefined);
});
var withLeadingAndTrailing = _.debounce(function() { var withLeadingAndTrailing = _.debounce(function() {
callCounts[2]++; callCounts[1]++;
}, 32, { 'leading': true }); }, 32, { 'leading': true });
withLeadingAndTrailing(); withLeadingAndTrailing();
withLeadingAndTrailing(); withLeadingAndTrailing();
strictEqual(callCounts[2], 1); strictEqual(callCounts[1], 1);
setTimeout(function() { setTimeout(function() {
deepEqual(callCounts, [1, 1, 2]); deepEqual(callCounts, [1, 2]);
withLeading('x'); withLeading('x');
strictEqual(callCounts[1], 2); strictEqual(callCounts[0], 2);
QUnit.start(); QUnit.start();
}, 64); }, 64);
} }
else { else {
skipTest(7); skipTest(5);
QUnit.start(); QUnit.start();
} }
}); });
@@ -15225,26 +15217,17 @@
} }
}); });
test('should support a `leading` option', 4, function() { test('should support a `leading` option', 2, function() {
_.each([true, { 'leading': true }], function(options) { if (!(isRhino && isModularize)) {
if (!(isRhino && isModularize)) { var withLeading = _.throttle(_.identity, 32, { 'leading': true });
var withLeading = _.throttle(_.identity, 32, options); strictEqual(withLeading('a'), 'a');
strictEqual(withLeading('a'), 'a');
}
else {
skipTest();
}
});
_.each([false, { 'leading': false }], function(options) { var withoutLeading = _.throttle(_.identity, 32, { 'leading': false });
if (!(isRhino && isModularize)) { strictEqual(withoutLeading('a'), undefined);
var withoutLeading = _.throttle(_.identity, 32, options); }
strictEqual(withoutLeading('a'), undefined); else {
} skipTest(2);
else { }
skipTest();
}
});
}); });
asyncTest('should support a `trailing` option', 6, function() { asyncTest('should support a `trailing` option', 6, function() {