forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextremum-methods.js
More file actions
64 lines (49 loc) · 1.93 KB
/
extremum-methods.js
File metadata and controls
64 lines (49 loc) · 1.93 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import assert from 'assert';
import lodashStable from 'lodash';
import { _ } from './utils.js';
describe('extremum methods', function() {
lodashStable.each(['max', 'maxBy', 'min', 'minBy'], function(methodName) {
var func = _[methodName],
isMax = /^max/.test(methodName);
it('`_.' + methodName + '` should work with Date objects', function() {
var curr = new Date,
past = new Date(0);
assert.strictEqual(func([curr, past]), isMax ? curr : past);
});
it('`_.' + methodName + '` should work with extremely large arrays', function() {
var array = lodashStable.range(0, 5e5);
assert.strictEqual(func(array), isMax ? 499999 : 0);
});
it('`_.' + methodName + '` should work when chaining on an array with only one value', function() {
var actual = _([40])[methodName]();
assert.strictEqual(actual, 40);
});
});
lodashStable.each(['maxBy', 'minBy'], function(methodName) {
var array = [1, 2, 3],
func = _[methodName],
isMax = methodName == 'maxBy';
it('`_.' + methodName + '` should work with an `iteratee`', function() {
var actual = func(array, function(n) {
return -n;
});
assert.strictEqual(actual, isMax ? 1 : 3);
});
it('should work with `_.property` shorthands', function() {
var objects = [{ 'a': 2 }, { 'a': 3 }, { 'a': 1 }],
actual = func(objects, 'a');
assert.deepStrictEqual(actual, objects[isMax ? 1 : 2]);
var arrays = [[2], [3], [1]];
actual = func(arrays, 0);
assert.deepStrictEqual(actual, arrays[isMax ? 1 : 2]);
});
it('`_.' + methodName + '` should work when `iteratee` returns +/-Infinity', function() {
var value = isMax ? -Infinity : Infinity,
object = { 'a': value };
var actual = func([object, { 'a': value }], function(object) {
return object.a;
});
assert.strictEqual(actual, object);
});
});
});