typescript(option): feat: added the 'values' helper
Signed-off-by: prescientmoon <git@moonythm.dev>
This commit is contained in:
parent
bf3fc154e3
commit
08b28804e0
|
@ -26,3 +26,4 @@ export * from './unpack'
|
||||||
export * from './unwrap'
|
export * from './unwrap'
|
||||||
export * from './withDefault'
|
export * from './withDefault'
|
||||||
export * from './withDefaultLazy'
|
export * from './withDefaultLazy'
|
||||||
|
export * from './values'
|
||||||
|
|
19
typescript/option/src/helpers/values.test.ts
Normal file
19
typescript/option/src/helpers/values.test.ts
Normal 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
|
||||||
|
})
|
||||||
|
})
|
11
typescript/option/src/helpers/values.ts
Normal file
11
typescript/option/src/helpers/values.ts
Normal 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)
|
||||||
|
}
|
Loading…
Reference in a new issue