forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexit-early.js
37 lines (30 loc) · 1.1 KB
/
exit-early.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
import assert from 'assert';
import lodashStable from 'lodash';
import { _ } from './utils.js';
describe('exit early', function() {
lodashStable.each(['_baseEach', 'forEach', 'forEachRight', 'forIn', 'forInRight', 'forOwn', 'forOwnRight', 'transform'], function(methodName) {
var func = _[methodName];
it('`_.' + methodName + '` can exit early when iterating arrays', function() {
if (func) {
var array = [1, 2, 3],
values = [];
func(array, function(value, other) {
values.push(lodashStable.isArray(value) ? other : value);
return false;
});
assert.deepStrictEqual(values, [lodashStable.endsWith(methodName, 'Right') ? 3 : 1]);
}
});
it('`_.' + methodName + '` can exit early when iterating objects', function() {
if (func) {
var object = { 'a': 1, 'b': 2, 'c': 3 },
values = [];
func(object, function(value, other) {
values.push(lodashStable.isArray(value) ? other : value);
return false;
});
assert.strictEqual(values.length, 1);
}
});
});
});