mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-06 01:47:48 +00:00
Ensure after n parameter is coerced from NaN to 0 (#4430)
* Enable after tests * Ensure after n parameter is coerced from NaN to 0
This commit is contained in:
committed by
John-David Dalton
parent
8b441a506b
commit
2cf08be625
1
after.js
1
after.js
@@ -19,6 +19,7 @@ function after(n, func) {
|
|||||||
if (typeof func != 'function') {
|
if (typeof func != 'function') {
|
||||||
throw new TypeError('Expected a function')
|
throw new TypeError('Expected a function')
|
||||||
}
|
}
|
||||||
|
n = n || 0
|
||||||
return function(...args) {
|
return function(...args) {
|
||||||
if (--n < 1) {
|
if (--n < 1) {
|
||||||
return func.apply(this, args)
|
return func.apply(this, args)
|
||||||
|
|||||||
@@ -1,31 +0,0 @@
|
|||||||
import assert from 'assert';
|
|
||||||
import lodashStable from 'lodash';
|
|
||||||
import { _ } from './utils.js';
|
|
||||||
|
|
||||||
describe('after', function() {
|
|
||||||
function after(n, times) {
|
|
||||||
var count = 0;
|
|
||||||
lodashStable.times(times, _.after(n, function() { count++; }));
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
it('should create a function that invokes `func` after `n` calls', function() {
|
|
||||||
assert.strictEqual(after(5, 5), 1, 'after(n) should invoke `func` after being called `n` times');
|
|
||||||
assert.strictEqual(after(5, 4), 0, 'after(n) should not invoke `func` before being called `n` times');
|
|
||||||
assert.strictEqual(after(0, 0), 0, 'after(0) should not invoke `func` immediately');
|
|
||||||
assert.strictEqual(after(0, 1), 1, 'after(0) should invoke `func` when called once');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should coerce `n` values of `NaN` to `0`', function() {
|
|
||||||
assert.strictEqual(after(NaN, 1), 1);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('should use `this` binding of function', function() {
|
|
||||||
var after = _.after(1, function() { return ++this.count; }),
|
|
||||||
object = { 'after': after, 'count': 0 };
|
|
||||||
|
|
||||||
object.after();
|
|
||||||
assert.strictEqual(object.after(), 2);
|
|
||||||
assert.strictEqual(object.count, 2);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
31
test/after.test.js
Normal file
31
test/after.test.js
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
import assert from 'assert';
|
||||||
|
import lodashStable from 'lodash';
|
||||||
|
import after from '../after.js';
|
||||||
|
|
||||||
|
describe('after', function() {
|
||||||
|
function testAfter(n, times) {
|
||||||
|
var count = 0;
|
||||||
|
lodashStable.times(times, after(n, function() { count++; }));
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
it('should create a function that invokes `func` after `n` calls', function() {
|
||||||
|
assert.strictEqual(testAfter(5, 5), 1, 'after(n) should invoke `func` after being called `n` times');
|
||||||
|
assert.strictEqual(testAfter(5, 4), 0, 'after(n) should not invoke `func` before being called `n` times');
|
||||||
|
assert.strictEqual(testAfter(0, 0), 0, 'after(0) should not invoke `func` immediately');
|
||||||
|
assert.strictEqual(testAfter(0, 1), 1, 'after(0) should invoke `func` when called once');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should coerce `n` values of `NaN` to `0`', function() {
|
||||||
|
assert.strictEqual(testAfter(NaN, 1), 1);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should use `this` binding of function', function() {
|
||||||
|
var afterFn = after(1, function() { return ++this.count; }),
|
||||||
|
object = { 'after': afterFn, 'count': 0 };
|
||||||
|
|
||||||
|
object.after();
|
||||||
|
assert.strictEqual(object.after(), 2);
|
||||||
|
assert.strictEqual(object.count, 2);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in New Issue
Block a user