-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from RobinMalfait/feature/async
feature/async
- Loading branch information
Showing
59 changed files
with
1,450 additions
and
152 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,26 @@ | ||
import { pipe } from './pipe'; | ||
import { average } from './average'; | ||
import { delay } from './delay'; | ||
|
||
it('should be possible to get an average of all the values', () => { | ||
const program = pipe(average()); | ||
|
||
expect(program([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).toEqual(5); | ||
expect(program([10, 10, 10])).toEqual(10); | ||
}); | ||
|
||
it('should be possible to get an average of all the values (async)', async () => { | ||
const program = pipe(delay(0), average()); | ||
|
||
expect(await program([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])).toEqual(5); | ||
expect(await program([10, 10, 10])).toEqual(10); | ||
}); | ||
|
||
it('should be possible to get an average of all the values (Promise async)', async () => { | ||
const program = pipe(average()); | ||
|
||
expect( | ||
await program(Promise.resolve([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) | ||
).toEqual(5); | ||
expect(await program(Promise.resolve([10, 10, 10]))).toEqual(10); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,64 @@ | ||
import { isAsyncIterable } from './utils/iterator'; | ||
import { LazyIterable } from './shared-types'; | ||
|
||
export function chunk<T>(size: number) { | ||
return function* chunkFn(data: Iterable<T>) { | ||
// Let's have a placeholder for our current chunk | ||
let chunk = []; | ||
|
||
// Loop over our data | ||
for (let datum of data) { | ||
// Add item to our current chunk | ||
chunk.push(datum); | ||
|
||
if (chunk.length === size) { | ||
// Our current chunk is full, let's yield it | ||
yield chunk; | ||
|
||
// Let's also clear our chunk for the next chunk | ||
chunk = []; | ||
} | ||
} | ||
return function chunkFn(data: LazyIterable<T>) { | ||
if (isAsyncIterable(data) || data instanceof Promise) { | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
const stream = data instanceof Promise ? await data : data; | ||
|
||
// Let's have a placeholder for our current chunk | ||
let chunk = []; | ||
|
||
// Loop over our data | ||
for await (let datum of stream) { | ||
// Add item to our current chunk | ||
chunk.push(datum); | ||
|
||
if (chunk.length === size) { | ||
// Our current chunk is full, let's yield it | ||
yield chunk; | ||
|
||
// Let's also clear our chunk for the next chunk | ||
chunk = []; | ||
} | ||
} | ||
|
||
// When the chunk is not full yet, but when we are at the end of the data we | ||
// have to ensure that this one is also yielded | ||
if (chunk.length > 0) { | ||
yield chunk; | ||
// When the chunk is not full yet, but when we are at the end of the data we | ||
// have to ensure that this one is also yielded | ||
if (chunk.length > 0) { | ||
yield chunk; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
*[Symbol.iterator]() { | ||
// Let's have a placeholder for our current chunk | ||
let chunk = []; | ||
|
||
// Loop over our data | ||
for (let datum of data) { | ||
// Add item to our current chunk | ||
chunk.push(datum); | ||
|
||
if (chunk.length === size) { | ||
// Our current chunk is full, let's yield it | ||
yield chunk; | ||
|
||
// Let's also clear our chunk for the next chunk | ||
chunk = []; | ||
} | ||
} | ||
|
||
// When the chunk is not full yet, but when we are at the end of the data we | ||
// have to ensure that this one is also yielded | ||
if (chunk.length > 0) { | ||
yield chunk; | ||
} | ||
}, | ||
}; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,41 @@ | ||
import { pipe } from './pipe'; | ||
import { compact } from './compact'; | ||
import { toArray } from './toArray'; | ||
import { delay } from './delay'; | ||
|
||
it('should remove all falsey values', () => { | ||
const program = pipe(compact(), toArray()); | ||
|
||
expect( | ||
program([0, 1, true, false, null, undefined, '', 'test', NaN]) | ||
).toEqual([1, true, 'test']); | ||
expect( | ||
program([0, 1, true, false, null, undefined, '', 'test', NaN]) | ||
).toEqual([1, true, 'test']); | ||
}); | ||
|
||
it('should remove all falsey values (async)', async () => { | ||
const program = pipe(delay(0), compact(), toArray()); | ||
|
||
expect( | ||
await program([0, 1, true, false, null, undefined, '', 'test', NaN]) | ||
).toEqual([1, true, 'test']); | ||
expect( | ||
await program([0, 1, true, false, null, undefined, '', 'test', NaN]) | ||
).toEqual([1, true, 'test']); | ||
}); | ||
|
||
it('should remove all falsey values (Promise async)', async () => { | ||
const program = pipe(compact(), toArray()); | ||
|
||
expect( | ||
await program( | ||
Promise.resolve([0, 1, true, false, null, undefined, '', 'test', NaN]) | ||
) | ||
).toEqual([1, true, 'test']); | ||
expect( | ||
await program( | ||
Promise.resolve([0, 1, true, false, null, undefined, '', 'test', NaN]) | ||
) | ||
).toEqual([1, true, 'test']); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,25 @@ | ||
export function* concat<T>(...data: Iterable<T>[]) { | ||
for (let datum of data) { | ||
yield* datum; | ||
import { isAsyncIterable } from './utils/iterator'; | ||
import { LazyIterable } from './shared-types'; | ||
|
||
export function concat<T>(...data: LazyIterable<T>[]) { | ||
if ( | ||
data.some(isAsyncIterable) || | ||
data.some(datum => datum instanceof Promise) | ||
) { | ||
return { | ||
async *[Symbol.asyncIterator]() { | ||
for await (let datum of await Promise.all(data)) { | ||
yield* datum; | ||
} | ||
}, | ||
}; | ||
} | ||
|
||
return { | ||
*[Symbol.iterator]() { | ||
for (let datum of data as Iterable<T>[]) { | ||
yield* datum; | ||
} | ||
}, | ||
}; | ||
} |
Oops, something went wrong.