|
| 1 | +import { expect } from 'chai'; |
| 2 | +import { describe, it } from 'mocha'; |
| 3 | + |
| 4 | +import identityFunc from '../identityFunc'; |
| 5 | +import safeArrayFrom from '../safeArrayFrom'; |
| 6 | + |
| 7 | +describe('safeArrayFrom', () => { |
| 8 | + it('should convert collections into arrays', () => { |
| 9 | + expect(safeArrayFrom([])).to.deep.equal([]); |
| 10 | + expect(safeArrayFrom(new Set([1, 2, 3]))).to.deep.equal([1, 2, 3]); |
| 11 | + expect(safeArrayFrom(new Int8Array([1, 2, 3]))).to.deep.equal([1, 2, 3]); |
| 12 | + |
| 13 | + // eslint-disable-next-line no-new-wrappers |
| 14 | + expect(safeArrayFrom(new String('ABC'))).to.deep.equal(['A', 'B', 'C']); |
| 15 | + |
| 16 | + function getArguments() { |
| 17 | + return arguments; |
| 18 | + } |
| 19 | + expect(safeArrayFrom(getArguments())).to.deep.equal([]); |
| 20 | + |
| 21 | + const arrayLike = {}; |
| 22 | + arrayLike[0] = 'Alpha'; |
| 23 | + arrayLike[1] = 'Bravo'; |
| 24 | + arrayLike[2] = 'Charlie'; |
| 25 | + arrayLike.length = 3; |
| 26 | + |
| 27 | + expect(safeArrayFrom(arrayLike)).to.deep.equal([ |
| 28 | + 'Alpha', |
| 29 | + 'Bravo', |
| 30 | + 'Charlie', |
| 31 | + ]); |
| 32 | + |
| 33 | + const iteratable = { |
| 34 | + [Symbol.iterator]() { |
| 35 | + const values = [1, 2, 3]; |
| 36 | + return { |
| 37 | + next() { |
| 38 | + const done = values.length === 0; |
| 39 | + const value = values.shift(); |
| 40 | + |
| 41 | + return { done, value }; |
| 42 | + }, |
| 43 | + }; |
| 44 | + }, |
| 45 | + }; |
| 46 | + expect(safeArrayFrom(iteratable)).to.deep.equal([1, 2, 3]); |
| 47 | + |
| 48 | + // istanbul ignore next (Never called and use just as a placeholder) |
| 49 | + function* generatorFunc() { |
| 50 | + yield 1; |
| 51 | + yield 2; |
| 52 | + yield 3; |
| 53 | + } |
| 54 | + expect(safeArrayFrom(generatorFunc())).to.deep.equal([1, 2, 3]); |
| 55 | + |
| 56 | + // But generator function itself is not iteratable |
| 57 | + expect(safeArrayFrom(generatorFunc)).to.equal(null); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should return `null` for non-collections', () => { |
| 61 | + expect(safeArrayFrom(null)).to.equal(null); |
| 62 | + expect(safeArrayFrom(undefined)).to.equal(null); |
| 63 | + |
| 64 | + expect(safeArrayFrom('ABC')).to.equal(null); |
| 65 | + expect(safeArrayFrom('0')).to.equal(null); |
| 66 | + expect(safeArrayFrom('')).to.equal(null); |
| 67 | + |
| 68 | + expect(safeArrayFrom(1)).to.equal(null); |
| 69 | + expect(safeArrayFrom(0)).to.equal(null); |
| 70 | + expect(safeArrayFrom(NaN)).to.equal(null); |
| 71 | + // eslint-disable-next-line no-new-wrappers |
| 72 | + expect(safeArrayFrom(new Number(123))).to.equal(null); |
| 73 | + |
| 74 | + expect(safeArrayFrom(true)).to.equal(null); |
| 75 | + expect(safeArrayFrom(false)).to.equal(null); |
| 76 | + // eslint-disable-next-line no-new-wrappers |
| 77 | + expect(safeArrayFrom(new Boolean(true))).to.equal(null); |
| 78 | + |
| 79 | + expect(safeArrayFrom({})).to.equal(null); |
| 80 | + expect(safeArrayFrom({ length: 3 })).to.equal(null); |
| 81 | + expect(safeArrayFrom({ iterable: true })).to.equal(null); |
| 82 | + |
| 83 | + const iteratorWithoutSymbol = { next: identityFunc }; |
| 84 | + expect(safeArrayFrom(iteratorWithoutSymbol)).to.equal(null); |
| 85 | + |
| 86 | + const invalidIteratable = { |
| 87 | + [Symbol.iterator]: { next: identityFunc }, |
| 88 | + }; |
| 89 | + expect(safeArrayFrom(invalidIteratable)).to.equal(null); |
| 90 | + }); |
| 91 | +}); |
0 commit comments