Skip to content
/ klubok Public

Do notation pipes for Promise-based or pure functions which easy to mock

Notifications You must be signed in to change notification settings

darky/klubok

Repository files navigation

Klubok

logo

Do notation pipes for Promise-based or pure functions which easy to mock
Inspired by fp-ts/Effect bind Do-notation, but much more small and simple
Primarly created for easy mocking of functions, which allows to write tons of unit tests

Example

import { pure, eff, klubok } from 'klubok';

const catsBirthdays = klubok(
  eff('cats', async ({ ids }: { ids: number[] }) => { /* fetch cats from DB */ }),

  pure('catsOneYearOld', ({ cats }) => cats.map(cat => ({ ...cat, age: cat.age + 1 })),

  eff('saved', async ({ catsOneYearOld }) => { /* save to DB */ })
)

// production usage
catsBirthdays({ ids: [1, 2, 3] })

// in tests usage
catsBirthdays(
  { ids: [1, 2, 3] },

  {
    // DB response mock
    cats: () => [
      { name: 'Barsik', age: 10 },
      { name: 'Marfa', age: 7 }
    ]
  },

  // call only this functions
  ['catsOneYearOld']

) // Promise<{ ..., catsOneYearOld: [{ name: 'Barsik', age: 11 }, { name: 'Marfa', age: 8 }] }>

About

Do notation pipes for Promise-based or pure functions which easy to mock

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published