forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfill.js
128 lines (103 loc) · 4.05 KB
/
fill.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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import assert from 'assert';
import lodashStable from 'lodash';
import { falsey } from './utils.js';
import fill from '../fill.js';
describe('fill', function() {
it('should use a default `start` of `0` and a default `end` of `length`', function() {
var array = [1, 2, 3];
assert.deepStrictEqual(fill(array, 'a'), ['a', 'a', 'a']);
});
it('should use `undefined` for `value` if not given', function() {
var array = [1, 2, 3],
actual = fill(array);
assert.deepStrictEqual(actual, Array(3));
assert.ok(lodashStable.every(actual, function(value, index) {
return index in actual;
}));
});
it('should work with a positive `start`', function() {
var array = [1, 2, 3];
assert.deepStrictEqual(fill(array, 'a', 1), [1, 'a', 'a']);
});
it('should work with a `start` >= `length`', function() {
lodashStable.each([3, 4, Math.pow(2, 32), Infinity], function(start) {
var array = [1, 2, 3];
assert.deepStrictEqual(fill(array, 'a', start), [1, 2, 3]);
});
});
it('should treat falsey `start` values as `0`', function() {
var expected = lodashStable.map(falsey, lodashStable.constant(['a', 'a', 'a']));
var actual = lodashStable.map(falsey, function(start) {
var array = [1, 2, 3];
return fill(array, 'a', start);
});
assert.deepStrictEqual(actual, expected);
});
it('should work with a negative `start`', function() {
var array = [1, 2, 3];
assert.deepStrictEqual(fill(array, 'a', -1), [1, 2, 'a']);
});
it('should work with a negative `start` <= negative `length`', function() {
lodashStable.each([-3, -4, -Infinity], function(start) {
var array = [1, 2, 3];
assert.deepStrictEqual(fill(array, 'a', start), ['a', 'a', 'a']);
});
});
it('should work with `start` >= `end`', function() {
lodashStable.each([2, 3], function(start) {
var array = [1, 2, 3];
assert.deepStrictEqual(fill(array, 'a', start, 2), [1, 2, 3]);
});
});
it('should work with a positive `end`', function() {
var array = [1, 2, 3];
assert.deepStrictEqual(fill(array, 'a', 0, 1), ['a', 2, 3]);
});
it('should work with a `end` >= `length`', function() {
lodashStable.each([3, 4, Math.pow(2, 32), Infinity], function(end) {
var array = [1, 2, 3];
assert.deepStrictEqual(fill(array, 'a', 0, end), ['a', 'a', 'a']);
});
});
it('should treat falsey `end` values, except `undefined`, as `0`', function() {
var expected = lodashStable.map(falsey, function(value) {
return value === undefined ? ['a', 'a', 'a'] : [1, 2, 3];
});
var actual = lodashStable.map(falsey, function(end) {
var array = [1, 2, 3];
return fill(array, 'a', 0, end);
});
assert.deepStrictEqual(actual, expected);
});
it('should work with a negative `end`', function() {
var array = [1, 2, 3];
assert.deepStrictEqual(fill(array, 'a', 0, -1), ['a', 'a', 3]);
});
it('should work with a negative `end` <= negative `length`', function() {
lodashStable.each([-3, -4, -Infinity], function(end) {
var array = [1, 2, 3];
assert.deepStrictEqual(fill(array, 'a', 0, end), [1, 2, 3]);
});
});
it('should coerce `start` and `end` to integers', function() {
var positions = [[0.1, 1.6], ['0', 1], [0, '1'], ['1'], [NaN, 1], [1, NaN]];
var actual = lodashStable.map(positions, function(pos) {
var array = [1, 2, 3];
return fill.apply(_, [array, 'a'].concat(pos));
});
assert.deepStrictEqual(actual, [['a', 2, 3], ['a', 2, 3], ['a', 2, 3], [1, 'a', 'a'], ['a', 2, 3], [1, 2, 3]]);
});
it('should work as an iteratee for methods like `_.map`', function() {
var array = [[1, 2], [3, 4]],
actual = lodashStable.map(array, fill);
assert.deepStrictEqual(actual, [[0, 0], [1, 1]]);
});
it('should return a wrapped value when chaining', function() {
var array = [1, 2, 3],
wrapped = _(array).fill('a'),
actual = wrapped.value();
assert.ok(wrapped instanceof _);
assert.strictEqual(actual, array);
assert.deepEqual(actual, ['a', 'a', 'a']);
});
});