Giter VIP home page Giter VIP logo

darky / rocket-pipes Goto Github PK

View Code? Open in Web Editor NEW
25.0 2.0 0.0 1.11 MB

Powerful pipes for TypeScript, that chain Promise and ADT for you ๐ŸšŒ -> โ›ฐ๏ธ -> ๐Ÿš  -> ๐Ÿ‚ -> ๐Ÿš€

License: MIT License

TypeScript 99.87% JavaScript 0.13%
pipe pipeline kleisli ramda compose composition maybe promise either validation adt monet ts typescript chain-promise fp-libraries context mock aop exit

rocket-pipes's Introduction

Rocket pipes

Powerful pipes for TypeScript, that chain Promise and ADT like Maybe or Either from popular FP libraries.

Features

  • ๐Ÿฌ 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.

Library support

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

Examples

Basic
const resp = await p(
  () => 123,
  (n) => n + 1
)();
expect(resp + 1).toEqual(125);
Context (โ— mutable)
const resp = await p(
  () => 123,
  pc((ctx: {n: number}) => n => n + ctx.n),
  n => n + 1
).context({n: 1})();
expect(resp + 1).toEqual(126);
Exit pipeline
const resp = await p(
  () => 123,
  (n) => ep(n + 1),
  (n) => "qwe"
)();
iep(resp) && expect(resp.r + 1).toEqual(125);
Replace pipeline (โ— mutable)
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);
AOP beforeAll/afterAll hooks
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);
AOP clear hooks
clearAfterAll();
clearBeforeAll();
Promise basic
const resp = await p(
  () => Promise.resolve(123),
  (n) => n + 1
)();
expect(resp + 1).toEqual(125);
Either right
const resp = await p(
  () => Either.right(123),
  (n) => n + 1
)();
expect(resp + 1).toEqual(125);
Promise can include anything supported
const resp = await p(
  () => Promise.resolve(Either.right(123)),
  (n) => n + 1
)();
expect(resp + 1).toEqual(125);
Either left
const resp = await p(
  () => Either.left(123),
  (_, l) => l + 1
)();
expect(resp + 1).toEqual(125);
Maybe some
const resp = await p(
  () => Maybe.some(123),
  (n) => n + 1
)();
expect(resp + 1).toEqual(125);
Maybe none
const resp = await p(
  () => Maybe.none(),
  (s, n) => s || n
)();
expect(resp).toEqual(void 0);
Validation success
const resp = await p(
  () => Validation.success(123),
  (n) => n + 1
)();
expect(resp + 1).toEqual(125);
Validation fail
const resp = await p(
  () => Validation.fail(123),
  (_, l) => l + 1
)();
expect(resp + 1).toEqual(125);

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.