1
Fork 0

typescript(option): feat: added the 'values' helper

Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
Matei Adriel 2019-12-26 16:53:20 +02:00 committed by prescientmoon
parent bf3fc154e3
commit 08b28804e0
Signed by: prescientmoon
SSH key fingerprint: SHA256:UUF9JT2s8Xfyv76b8ZuVL7XrmimH4o49p4b+iexbVH4
3 changed files with 31 additions and 0 deletions

View file

@ -26,3 +26,4 @@ export * from './unpack'
export * from './unwrap'
export * from './withDefault'
export * from './withDefaultLazy'
export * from './values'

View file

@ -0,0 +1,19 @@
import { expect } from 'chai'
import { values } from './values'
import { Some, None } from '../types'
describe('The values helper', () => {
it('should ignore all None values', () => {
// arrange
const items = Array(50)
.fill(1)
.map((_, i) => (i % 2 ? Some(i) : None))
// act
const result = values(items)
// assert
expect(result).to.not.contain(None)
expect(result, "ensure it didn't clear everything").to.not.be.empty
})
})

View file

@ -0,0 +1,11 @@
import { Option } from '../types'
import { toArray } from './toArray'
/**
* Take all the values that are present, throwing away any None
*
* @param iterable The iterable to collect the values from.
*/
export const values = <T>(iterable: Iterable<Option<T>>) => {
return Array.from(iterable).flatMap(toArray)
}