From 738d4ee9049e55a27e52543f738f163c472e850e Mon Sep 17 00:00:00 2001 From: cateland Date: Fri, 25 Oct 2019 11:56:50 +0200 Subject: [PATCH] feat(foldable): add fp-ts foldable interface --- src/main.ts | 10 +++++++--- test/remoteDataTestFoldable.test.ts | 24 ++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 test/remoteDataTestFoldable.test.ts diff --git a/src/main.ts b/src/main.ts index e3b678e..360ca79 100644 --- a/src/main.ts +++ b/src/main.ts @@ -5,6 +5,7 @@ import { Either, isLeft } from 'fp-ts/lib/Either'; import { Option, isNone } from 'fp-ts/lib/Option'; import { Monad2 } from 'fp-ts/lib/Monad'; +import { Foldable2 } fromĀ 'fp-ts/lib/Foldable'; import { pipeable } from 'fp-ts/lib/pipeable'; import { Eq } from 'fp-ts/lib/Eq'; @@ -378,7 +379,7 @@ export const getEq = (EE: Eq, EA: Eq): Eq> => { } } } -export const dataway: Monad2 = { +export const dataway: Monad2 & Foldable2 = { /** * @ignore */ @@ -480,8 +481,11 @@ validate(loading) * ``` */ chain: (monadA, func) => (isSuccess(monadA) ? func(monadA.success) : monadA), + reduce: (functorA, accumulator, func) => (isSuccess(functorA) ? func(accumulator, functorA.success) : accumulator), + foldMap: M => (functorA, func) => (!isSuccess(functorA) ? M.empty : func(functorA.success)), + reduceRight: (functorA, accumulator, func) => (isSuccess(functorA) ? func(functorA.success, accumulator) : accumulator), }; -const { ap, map, chain } = pipeable(dataway); +const { ap, map, chain, reduce, foldMap, reduceRight } = pipeable(dataway); -export { ap, map, chain }; +export { ap, map, chain, reduce, foldMap, reduceRight }; diff --git a/test/remoteDataTestFoldable.test.ts b/test/remoteDataTestFoldable.test.ts new file mode 100644 index 0000000..aa10e27 --- /dev/null +++ b/test/remoteDataTestFoldable.test.ts @@ -0,0 +1,24 @@ +import { reduce, success, foldMap, failure, loading, notAsked, reduceRight } from '../src/main'; +import { monoidString } from 'fp-ts/lib/Monoid'; +import { identity } from 'fp-ts/lib/function'; + +describe('Dataway', () => { + it('reduce', () => { + expect(reduce([], (b, a) => [...b, a] )(success('test'))).toEqual(['test']) + expect(reduce([], (b, a) => [...b, a] )(failure('message'))).toEqual([]) + expect(reduce([], (b, a) => [...b, a] )(loading)).toEqual([]) + expect(reduce([], (b, a) => [...b, a] )(notAsked)).toEqual([]) + }); + it('reduceRight', () => { + expect(reduceRight([], (a, b) => [...b, a] )(success('test'))).toEqual(['test']) + expect(reduceRight([], (a, b) => [...b, a] )(failure('message'))).toEqual([]) + expect(reduceRight([], (a, b) => [...b, a] )(loading)).toEqual([]) + expect(reduceRight([], (a, b) => [...b, a] )(notAsked)).toEqual([]) + }); + it('foldMap', () => { + expect(foldMap(monoidString)(identity)(success('test'))).toEqual('test'); + expect(foldMap(monoidString)(identity)(failure('message'))).toEqual(''); + expect(foldMap(monoidString)(identity)(loading)).toEqual(''); + expect(foldMap(monoidString)(identity)(notAsked)).toEqual(''); + }); +}) \ No newline at end of file