-
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.
Showing
6 changed files
with
60 additions
and
0 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
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 |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { pipe, range, flatMap, toSet, delay } from './' | ||
|
||
it('should convert an iterator to an array', () => { | ||
let program = pipe(range(0, 10), toSet()) | ||
|
||
expect(program()).toEqual(new Set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) | ||
}) | ||
|
||
it('should remove duplicate items (default Set behaviour)', () => { | ||
let program = pipe( | ||
range(0, 10), | ||
flatMap<number, number[]>((value) => [value, value, value + 1]), | ||
toSet() | ||
) | ||
|
||
expect(program()).toEqual(new Set([0, 1, 10, 11, 2, 3, 4, 5, 6, 7, 8, 9])) | ||
}) | ||
|
||
it('should convert an iterator to an array (async)', async () => { | ||
let program = pipe(range(0, 10), delay(0), toSet()) | ||
|
||
expect(await program()).toEqual(new Set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])) | ||
}) | ||
|
||
it('should convert an iterator to an array (Promise async)', async () => { | ||
let program = pipe(Promise.resolve(range(0, 10)), toSet()) | ||
|
||
expect(await program()).toEqual(new Set([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { reduce } from './' | ||
|
||
import { LazyIterable } from './shared-types' | ||
|
||
export function toSet<T>() { | ||
return (data: LazyIterable<T>) => { | ||
return reduce<Set<T>, T>((acc, current) => { | ||
acc.add(current) | ||
return acc | ||
}, new Set<T>())(data) | ||
} | ||
} |