|
| 1 | + |
| 2 | +import tap from 'tap'; |
| 3 | +import { mapMode, isIterator, isIterable, isGenerator, ensureIterable, MAPMODE, map } from '../src/index'; |
| 4 | + |
| 5 | +const array = [ 1, 2, 3 ]; |
| 6 | +const object = { a: 1, b: 2, c: 3 }; |
| 7 | +const generator = function* () { |
| 8 | + let i = 3; |
| 9 | + while (i) yield i--; |
| 10 | +}; |
| 11 | + |
| 12 | +const emptyIterator = { next: () => ({ done: true }) }; |
| 13 | +const emptyIterable = { [Symbol.iterator]: () => emptyIterator }; |
| 14 | + |
| 15 | +tap.equal(mapMode(array), MAPMODE.ARRAY); |
| 16 | +tap.equal(mapMode(generator), false); |
| 17 | +tap.equal(mapMode(generator()), MAPMODE.ITERATOR); |
| 18 | +tap.equal(mapMode(emptyIterator), MAPMODE.ITERATOR); |
| 19 | +tap.equal(mapMode(emptyIterable), MAPMODE.ITERABLE); |
| 20 | +tap.equal(mapMode(object), MAPMODE.OBJECT); |
| 21 | + |
| 22 | +tap.equal(isIterator(emptyIterable), false); |
| 23 | +tap.equal(isIterator(emptyIterator), true); |
| 24 | +tap.equal(isIterator(array), false); |
| 25 | +tap.equal(isIterator(generator), false); |
| 26 | +tap.equal(isIterator(generator()), true); |
| 27 | +tap.equal(isIterator(object), false); |
| 28 | + |
| 29 | +tap.equal(isIterable(emptyIterable), true); |
| 30 | +tap.equal(isIterable(emptyIterator), false); |
| 31 | +tap.equal(isIterable(array), true); |
| 32 | +tap.equal(isIterable(generator), false); |
| 33 | +tap.equal(isIterable(generator()), true); |
| 34 | +tap.equal(isIterable(object), false); |
| 35 | + |
| 36 | +tap.equal(isGenerator(emptyIterable), false); |
| 37 | +tap.equal(isGenerator(emptyIterator), false); |
| 38 | +tap.equal(isGenerator(array), false); |
| 39 | +tap.equal(isGenerator(generator), true); |
| 40 | +tap.equal(isGenerator(generator()), false); |
| 41 | +tap.equal(isGenerator(object), false); |
| 42 | + |
| 43 | +tap.ok(isIterable(ensureIterable(emptyIterator))); |
| 44 | +tap.ok(isIterable(ensureIterable(emptyIterable))); |
| 45 | +tap.ok(isIterator(ensureIterable(emptyIterator))); |
| 46 | +tap.notOk(isIterator(ensureIterable(emptyIterable))); |
| 47 | +tap.notOk(isGenerator(ensureIterable(emptyIterator))); |
| 48 | + |
| 49 | +const mappedGenerator = map(generator()); |
| 50 | +tap.type(mappedGenerator, Array); |
| 51 | +tap.same(mappedGenerator, [ 3, 2, 1 ]); |
| 52 | + |
0 commit comments