diff --git a/typescript/option/src/helpers/external.ts b/typescript/option/src/helpers/external.ts index 851a146..9cddd76 100644 --- a/typescript/option/src/helpers/external.ts +++ b/typescript/option/src/helpers/external.ts @@ -26,3 +26,4 @@ export * from './unpack' export * from './unwrap' export * from './withDefault' export * from './withDefaultLazy' +export * from './values' diff --git a/typescript/option/src/helpers/values.test.ts b/typescript/option/src/helpers/values.test.ts new file mode 100644 index 0000000..ceb78c1 --- /dev/null +++ b/typescript/option/src/helpers/values.test.ts @@ -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 + }) +}) diff --git a/typescript/option/src/helpers/values.ts b/typescript/option/src/helpers/values.ts new file mode 100644 index 0000000..c973b65 --- /dev/null +++ b/typescript/option/src/helpers/values.ts @@ -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 = (iterable: Iterable>) => { + return Array.from(iterable).flatMap(toArray) +}