Powerful pipes for TypeScript, that chain Promise and ADT like Maybe or Either from popular FP libraries.
- ๐ฌ Sugar pipes. No worries about promises or ADT itself. Work with resolved values directly.
- ๐ก Type inference. No worries about manual typing work. Types of resolved values inferred automatically.
- โ๏ธ FP libraries friendly. Understand Catamorphism/Foldable libraries.
- ๐๏ธ Mix of Promise with FP library. Yes! Catamorphism/Foldable can be included in Promise.
- ๐ Context. Easy pass context through all pipes.
- ๐ช Pipeline exit (even nested exit). You can exit from any place of pipeline with result value (it's also have proper type inference ๐ค)
- ๐น Pipeline replace. You can replace function on pipeline to another on the fly. Useful for mock testing.
- โฐ AOP. Use beforeAll/afterAll hooks for your pipelines.
- ๐ฆฅ Lazy. Pipeline returns function, that can be used later. It's friendly with Ramda or Sanctuary.
Vanilla | Monet | Purify | fp-ts | RxJS / IxJS |
---|---|---|---|---|
Promise | Either | Either | Either | pipe |
Maybe | Maybe | Promise<Either> | ||
Validation | EitherAsync | |||
Promise<Either> | MaybeAsync | |||
Promise<Maybe> | Promise<Either> | |||
Promise<Validation> | Promise<Maybe> | |||
Promise<EitherAsync> | ||||
Promise<MaybeAsync> |
if you want slim version without libraries support, install slim version npm install rocket-pipes-slim
const resp = await p(
() => 123,
(n) => n + 1
)();
expect(resp + 1).toEqual(125);
const resp = await p(
() => 123,
pc((ctx: {n: number}) => n => n + ctx.n),
n => n + 1
).context({n: 1})();
expect(resp + 1).toEqual(126);
const resp = await p(
() => 123,
(n) => ep(n + 1),
(n) => "qwe"
)();
iep(resp) && expect(resp.r + 1).toEqual(125);
const fn = p(
() => 123,
(n) => n + 1
);
const resp = await fn.replace([[0, () => 124]])();
expect(resp + 1).toEqual(126);
fn.replaceUndo();
expect(await fn()).toEqual(125);
beforeAll((label, n) => {
expect(label).toEqual("(n) => n + 1\n(n) => n + 1");
expect(n).toEqual(123);
});
afterAll((label, n) => {
expect(label).toEqual("(n) => n + 1\n(n) => n + 1");
expect(n).toEqual(125);
});
p(
(n: number) => n + 1,
(n) => n + 1
)(123);
clearAfterAll();
clearBeforeAll();
const resp = await p(
() => Promise.resolve(123),
(n) => n + 1
)();
expect(resp + 1).toEqual(125);
const resp = await p(
() => Either.right(123),
(n) => n + 1
)();
expect(resp + 1).toEqual(125);
const resp = await p(
() => Promise.resolve(Either.right(123)),
(n) => n + 1
)();
expect(resp + 1).toEqual(125);
const resp = await p(
() => Either.left(123),
(_, l) => l + 1
)();
expect(resp + 1).toEqual(125);
const resp = await p(
() => Maybe.some(123),
(n) => n + 1
)();
expect(resp + 1).toEqual(125);
const resp = await p(
() => Maybe.none(),
(s, n) => s || n
)();
expect(resp).toEqual(void 0);
const resp = await p(
() => Validation.success(123),
(n) => n + 1
)();
expect(resp + 1).toEqual(125);
const resp = await p(
() => Validation.fail(123),
(_, l) => l + 1
)();
expect(resp + 1).toEqual(125);