From ab5e468375be6a3c4a194821232a27b4135e6513 Mon Sep 17 00:00:00 2001 From: Matei Adriel Date: Thu, 26 Dec 2019 16:36:05 +0200 Subject: [PATCH] typescript(option): feat: added the 'unpack' helper Signed-off-by: prescientmoon --- typescript/option/src/helpers/external.ts | 1 + typescript/option/src/helpers/unpack.test.ts | 61 ++++++++++++++++++++ typescript/option/src/helpers/unpack.ts | 20 +++++++ 3 files changed, 82 insertions(+) create mode 100644 typescript/option/src/helpers/unpack.test.ts create mode 100644 typescript/option/src/helpers/unpack.ts diff --git a/typescript/option/src/helpers/external.ts b/typescript/option/src/helpers/external.ts index d607755..851a146 100644 --- a/typescript/option/src/helpers/external.ts +++ b/typescript/option/src/helpers/external.ts @@ -22,6 +22,7 @@ export * from './or' export * from './orLazy' export * from './toArray' export * from './toNullable' +export * from './unpack' export * from './unwrap' export * from './withDefault' export * from './withDefaultLazy' diff --git a/typescript/option/src/helpers/unpack.test.ts b/typescript/option/src/helpers/unpack.test.ts new file mode 100644 index 0000000..f3ebec1 --- /dev/null +++ b/typescript/option/src/helpers/unpack.test.ts @@ -0,0 +1,61 @@ +import { expect } from 'chai' +import { None } from '../types' +import { alwaysX, x, someX } from '../../test/constants' +import { constantly } from '@thi.ng/compose' +import { spy } from 'sinon' +import { unpack } from './unpack' + +describe('The unpack helper', () => { + describe('When given None', () => { + it('should return the default when given None', () => { + // act + const result = unpack(constantly(0), constantly(1), None) + + // assert + expect(result).to.equal(0) + }) + + it('should call the lazy default', () => { + // arrange + const func = spy(alwaysX) + + // act + unpack(func, alwaysX, None) + + // assert + expect(func.called).to.be.true + }) + }) + + describe('When given Some', () => { + it('should return the return of the mapper', () => { + // act + const result = unpack(constantly(0), constantly(1), someX) + + // assert + expect(result).to.equal(1) + }) + + it('should not call the lazy default', () => { + // arrange + const func = spy(constantly(x)) + + // act + unpack(func, constantly(x), someX) + + // assert + expect(func.called).to.be.false + }) + + it('should pass the inner value to the mapper', () => { + // arrange + const mapper = spy(alwaysX) + + // act + unpack(alwaysX, mapper, someX) + + // assert + expect(mapper.calledWith(x)).to.be.true + }) + }) +}) diff --git a/typescript/option/src/helpers/unpack.ts b/typescript/option/src/helpers/unpack.ts new file mode 100644 index 0000000..07c2562 --- /dev/null +++ b/typescript/option/src/helpers/unpack.ts @@ -0,0 +1,20 @@ +import { Lazy, Mapper } from '../internalTypes' +import { Option } from '../types' +import { withDefaultLazy } from './withDefaultLazy' +import { map } from './map' + +/** + * Like unwrap, but the default value is lazy, + * and will only be computed if the Option is None. + * + * @param _default The lazy value to use in case option is None. + * @param mapper The function to pass the inner value to. + * @param option The option to unpack. + */ +export const unpack = ( + _default: Lazy, + mapper: Mapper, + option: Option +) => { + return withDefaultLazy(_default, map(mapper, option)) +}