forked from zloirock/core-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
es.array.for-each.js
47 lines (45 loc) · 1.33 KB
/
es.array.for-each.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
import { STRICT } from '../helpers/constants';
import forEach from 'core-js-pure/features/array/for-each';
QUnit.test('Array#forEach', assert => {
assert.isFunction(forEach);
let array = [1];
const context = {};
forEach(array, function (value, key, that) {
assert.same(arguments.length, 3, 'correct number of callback arguments');
assert.same(value, 1, 'correct value in callback');
assert.same(key, 0, 'correct index in callback');
assert.same(that, array, 'correct link to array in callback');
assert.same(this, context, 'correct callback context');
}, context);
let result = '';
forEach([1, 2, 3], it => {
result += it;
});
assert.ok(result === '123');
result = '';
forEach([1, 2, 3], (value, key) => {
result += key;
});
assert.ok(result === '012');
result = '';
forEach([1, 2, 3], (value, key, that) => {
result += that;
});
assert.ok(result === '1,2,31,2,31,2,3');
result = '';
forEach([1, 2, 3], function () {
result += this;
}, 1);
assert.ok(result === '111');
result = '';
array = [];
array[5] = '';
forEach(array, (value, key) => {
result += key;
});
assert.ok(result === '5');
if (STRICT) {
assert.throws(() => forEach(null, () => { /* empty */ }), TypeError);
assert.throws(() => forEach(undefined, () => { /* empty */ }), TypeError);
}
});