Skip to content

Commit

Permalink
fix: tests for concat and collect and missing exported type
Browse files Browse the repository at this point in the history
  • Loading branch information
reconbot committed May 13, 2018
1 parent ba16541 commit 26bcf34
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 14 deletions.
15 changes: 8 additions & 7 deletions lib/collect-test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
import { assert } from 'chai'
import { collect } from '../lib/collect'
import { fromIterable } from '../lib/from-iterable'
import { collect, fromIterable } from './'

function promiseImmediate (data?) {
return new Promise(resolve => setImmediate(() => resolve(data)))
async function* asyncIterable (arr) {
for (const val of arr) {
yield val
}
}

describe('collect', () => {
it('collects async data', async () => {
assert.deepEqual(await collect(fromIterable([1, 2, 3, 4])), [1, 2, 3, 4])
it('collects async iterator data', async () => {
assert.deepEqual(await collect(asyncIterable([1, 2, 3, 4])), [1, 2, 3, 4])
})
it('collects sync data', async () => {
it('collects sync iterable data', async () => {
assert.deepEqual(await collect([1, 2, 3, 4]), [1, 2, 3, 4])
})
})
5 changes: 3 additions & 2 deletions lib/collect.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export async function collect (iterable: Iterable<any>) {
import { Iterableish } from './types'
export async function collect<T> (iterable: Iterableish<T>) {
const values = []
for await (const value of iterable) {
for await (const value of iterable as AsyncIterable<T>) {
values.push(value)
}
return values
Expand Down
18 changes: 18 additions & 0 deletions lib/concat-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { assert } from 'chai'
import { collect, concat, fromIterable } from './'

describe('concat', () => {
it('concatenates multiple async iterators', async () => {
async function* one () { yield 1 }
async function* two () { yield 2 }
assert.deepEqual(await collect(concat(one(), two())), [1, 2])
})
it('concatenates multiple sync iterators', async () => {
function* one () { yield 1 }
function* two () { yield 2 }
assert.deepEqual(await collect(concat(one(), two())), [1, 2])
})
it('concatenates sync iterables', async () => {
assert.deepEqual(await collect(concat([1], [2], [3], [4])), [1, 2, 3, 4])
})
})
5 changes: 3 additions & 2 deletions lib/concat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export async function* concat (...iterables: Array<Iterable<any>>) {
import { Iterableish } from './types'
export async function* concat (...iterables: Array<Iterableish<any>>) {
for await (const iterable of iterables) {
for await (const value of iterable) {
for await (const value of iterable as AsyncIterable<any>) {
yield value
}
}
Expand Down
5 changes: 4 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ if ((Symbol as any).asyncIterator === undefined) {

export { buffer } from './buffer'
export { collect } from './collect'
export { concat } from './concat'
export { combine } from './combine'
export { consume } from './consume'
export { fromIterable } from './from-iterable'
export { generate } from './generate'
export { Iterableish } from './types'
export { map } from './map'
export { merge } from './merge'
export { parallelMap } from './parallel-map'
export { parallelMerge } from './parallelMerge'
export { parallelMerge } from './parallel-merge'
export { reduce } from './reduce'
export { take } from './take'
10 changes: 9 additions & 1 deletion lib/map.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { Iterableish } from './types'
async function* _map (func, iterable) {
for await (const val of iterable) {
yield await func(val)
}
}

export function map (func: (data: any) => any, iterable?: Iterable<any>|Iterator<any>) {
export function map<T, B> (
func: (data: T) => B|Promise<B>,
): (iterable: Iterableish<T>) => AsyncIterator<B>
export function map<T, B> (
func: (data: T) => B|Promise<B>,
iterable: Iterableish<T>,
): AsyncIterator<B>
export function map (func, iterable?) {
if (iterable === undefined) {
return curriedIterable => _map(func, curriedIterable)
}
Expand Down
2 changes: 1 addition & 1 deletion lib/take-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async function* asyncNumbers () {
yield 3
}

describe.only('take', () => {
describe('take', () => {
it('Returns the first n elements of the given async iterable', async () => {
const values = []
for await (const val of take(2, asyncNumbers())) {
Expand Down
1 change: 1 addition & 0 deletions lib/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export type Iterableish<T> = Iterable<T>|Iterator<T>|AsyncIterable<T>|AsyncIterator<T>

0 comments on commit 26bcf34

Please sign in to comment.