1.0.5-beta.2-canary-57a252d-1587304604912 • Published 6 years ago

@iadvize-oss/foldable-helpers v1.0.5-beta.2-canary-57a252d-1587304604912

Weekly downloads
1,293
License
MIT
Repository
github
Last release
6 years ago

@iadvize-oss/foldable-helpers

Continuous integration

Helpers to fold on things

While we recommend using @iadvize-oss/foldable-helpers with Typescript, it can be used in standard Javascript apps.

💻 Usage

First, install the library:

npm add @iadvize-oss/foldable-helpers

📖 Documentation

Named fold - createFoldObject

You have a sum type and the type guards for each of the types. For example:

type T = A | B | C | D;

function isA(t: T): t is A { ... }
function isB(t: T): t is B { ... }
function isC(t: T): t is C { ... }
function isD(t: T): t is D { ... }

To create a named fold function to fold on T, use createFoldObject. You choose the name of each fold function by passing an object.

import { pipe } from 'fp-ts/es6/pipeable';

import { createFoldObject } from '@iadvize-oss/foldable-helpers';

const foldOnT = createFoldObject({
  onA: isA,
  onB: isB,
  onC: isC,
  onD: isD,
});

const t: T = ...;

pipe(
  t,
  foldOnT({
    onA: (tbis) => console.log('executed when t is A', { tbis }),
    onB: (tbis) => console.log('executed when t is B', { tbis }),
    onC: (tbis) => console.log('executed when t is C', { tbis }),
    onD: (tbis) => console.log('executed when t is D', { tbis }),
  }),
);

Default clause

You can use _ as a "catch all" clause if you don't want to handle all types independently.

pipe(
  t,
  foldOnT({
    onA: (tbis) => console.log('executed when t is A', { tbis }),
    onB: (tbis) => console.log('executed when t is B', { tbis }),
    _:   (tbis) => console.log('executed when t is not A nor B', { tbis }),
  }),
);

Classic fold - createFold

You have a sum type and the type guards for each of the types. For example:

type T = A | B | C;

function isA(t: T): t is A { ... }
function isB(t: T): t is B { ... }
function isC(t: T): t is C { ... }

To create a fold function to fold on T, use createFold

import { pipe } from 'fp-ts/es6/pipeable';

import { createFold } from '@iadvize-oss/foldable-helpers';

const foldOnT = createFold(isA, isB, isC);

const t: T = ...;

pipe(
  t,
  foldOnT(
    (tbis) => console.log('executed when t is A', { tbis }),
    (tbis) => console.log('executed when t is B', { tbis }),
    (tbis) => console.log('executed when t is C', { tbis }),
  ),
);

Classic fold is very useful but could become hard to read when we have more than 3-4 types to fold on. You probably want to use createFoldObject in that case.

combineGuards

When using fold you will probably encounter cases where a type is a combination (union) of different guards. Oo reduce the boilerplate of having to write each combination by hand you can use the combineGuards helper.

type A = { a: string };
type B = { b: number };

const isTypeA = (value: any): value is A =>
  value != null && typeof value.a === 'string';

const isTypeB = (value: any): value is B =>
  value != null && typeof value.b === 'number';

const oldIsTypeAAndB = (value: any): value is A & B =>
    isTypeA(value) && isTypeB(value);

const isTypeAAndB = combineGuards(isTypeA, isTypeB);
// (t: any): t is A & B => boolean

not

When using createFold you need to make sure that each guard mutually excludes the others but it can sometimes be painfull if one type depends on another, therefore we let you use the not operator to exclude a guard

type TypeA = { a: string };
type TypeB = { a: 'test' };

const isTypeA = (value: {a: unknown}): value is TypeA => typeof value.a === 'string';
const isTypeB = (value: TypeA): value is TypeB => value.a === 'test';

const fold = createFoldObject({
  onTypeA: combineGuards(isTypeA, not(isTypeB)),
  onTypeB: isTypeB,
});
2.2.0

6 years ago

2.1.1

6 years ago

2.1.0

6 years ago

2.0.0

6 years ago

1.0.5

6 years ago

1.0.5-beta.2

6 years ago

1.0.5-beta.1

6 years ago

1.0.5-beta.0

6 years ago

1.0.4

6 years ago

1.0.4-beta.1

6 years ago

1.0.4-beta.0

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.0.1-beta.1

6 years ago

0.0.1-beta.0

6 years ago