Skip to content

Commit

Permalink
Add toSet feature (#34)
Browse files Browse the repository at this point in the history
* add `toSet` feature

* update changelog
  • Loading branch information
RobinMalfait committed Apr 21, 2023
1 parent 36bc2b0 commit 607913c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Add `includes` feature ([#24](https://github.com/RobinMalfait/lazy-collections/pull/24), [#29](https://github.com/RobinMalfait/lazy-collections/pull/29))
- Add `at` feature ([#25](https://github.com/RobinMalfait/lazy-collections/pull/25), [#30](https://github.com/RobinMalfait/lazy-collections/pull/30))
- Add `product` feature ([#33](https://github.com/RobinMalfait/lazy-collections/pull/33))
- Add `toSet` feature ([#34](https://github.com/RobinMalfait/lazy-collections/pull/34))

### Fixed

Expand Down
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ program(range(0, 1000000))
- [`takeWhile`](#takewhile)
- [`tap`](#tap)
- [`toArray`](#toarray)
- [`toSet`](#toset)
- [`unique`](#unique)
- [`wait`](#wait)
- [`where`](#where)
Expand Down Expand Up @@ -865,6 +866,21 @@ program()
// [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 ]
```

#### `toSet`

[Table of contents](#table-of-contents)

Converts an array or an iterator to Set.

```js
import { pipe, range, toSet } from 'lazy-collections'

let program = pipe(range(0, 10), toSet())

program()
// Set (11) { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
```

#### `unique`

[Table of contents](#table-of-contents)
Expand Down
1 change: 1 addition & 0 deletions src/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ exports[`should export all the things 1`] = `
"takeWhile": [Function],
"tap": [Function],
"toArray": [Function],
"toSet": [Function],
"unique": [Function],
"wait": [Function],
"where": [Function],
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export * from './take'
export * from './takeWhile'
export * from './tap'
export * from './toArray'
export * from './toSet'
export * from './unique'
export * from './where'
export * from './windows'
Expand Down
29 changes: 29 additions & 0 deletions src/toSet.test.ts
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]))
})
12 changes: 12 additions & 0 deletions src/toSet.ts
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)
}
}

0 comments on commit 607913c

Please sign in to comment.