/
isIterableObject-test.ts
71 lines (55 loc) · 2.42 KB
/
isIterableObject-test.ts
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
import { expect } from 'chai';
import { describe, it } from 'mocha';
import { identityFunc } from '../identityFunc';
import { isIterableObject } from '../isIterableObject';
describe('isIterableObject', () => {
it('should return `true` for collections', () => {
expect(isIterableObject([])).to.equal(true);
expect(isIterableObject(new Int8Array(1))).to.equal(true);
// eslint-disable-next-line no-new-wrappers
expect(isIterableObject(new String('ABC'))).to.equal(true);
function getArguments() {
return arguments;
}
expect(isIterableObject(getArguments())).to.equal(true);
const iterable = { [Symbol.iterator]: identityFunc };
expect(isIterableObject(iterable)).to.equal(true);
// istanbul ignore next (Never called and use just as a placeholder)
function* generatorFunc() {
/* do nothing */
}
expect(isIterableObject(generatorFunc())).to.equal(true);
// But generator function itself is not iterable
expect(isIterableObject(generatorFunc)).to.equal(false);
});
it('should return `false` for non-collections', () => {
expect(isIterableObject(null)).to.equal(false);
expect(isIterableObject(undefined)).to.equal(false);
expect(isIterableObject('ABC')).to.equal(false);
expect(isIterableObject('0')).to.equal(false);
expect(isIterableObject('')).to.equal(false);
expect(isIterableObject(1)).to.equal(false);
expect(isIterableObject(0)).to.equal(false);
expect(isIterableObject(NaN)).to.equal(false);
// eslint-disable-next-line no-new-wrappers
expect(isIterableObject(new Number(123))).to.equal(false);
expect(isIterableObject(true)).to.equal(false);
expect(isIterableObject(false)).to.equal(false);
// eslint-disable-next-line no-new-wrappers
expect(isIterableObject(new Boolean(true))).to.equal(false);
expect(isIterableObject({})).to.equal(false);
expect(isIterableObject({ iterable: true })).to.equal(false);
const iteratorWithoutSymbol = { next: identityFunc };
expect(isIterableObject(iteratorWithoutSymbol)).to.equal(false);
const invalidIterable = {
[Symbol.iterator]: { next: identityFunc },
};
expect(isIterableObject(invalidIterable)).to.equal(false);
const arrayLike: { [key: string]: unknown } = {};
arrayLike[0] = 'Alpha';
arrayLike[1] = 'Bravo';
arrayLike[2] = 'Charlie';
arrayLike.length = 3;
expect(isIterableObject(arrayLike)).to.equal(false);
});
});