cbk-functional-library

0.0.20 • Public • Published

cbk-functional-library

This library offers a lot of functional helpers I use a lot in my applications. Mainly I want to allow most of Javascripts functionality to be used in a function pipeline. A function pipeline replaces the use of functions this way

function doSomething(input) {
    const preparation = prepareTheData(input)
    const data = getDataFromAPI(preparation)
    const result = validateTheData(data)
    return result
}

by this way

const doSomething = flow(
    prepareTheData,
    getDataFromAPI,
    validateTheData
)

which I think is way easier to read. However, it also brings some challenges in terms of error handling and async calls. In the coming weeks I will add some descriptions how to handle those using the library.

Getting Started

Install the library using npm:

npm install cbk-functional-library --save

Use the library by importing it:

import { flow, ifThenElse, log, isDevEnvironment, addMinutes } from "cbk-functional-library"

const addHoursToDate = (date, hours) => flow(
    ifThenElse(isDevEnvironment, log("Called addHoursToDate")),
    (date) => addMinutes(date, hours*60),
    ifThenElse(isDevEnvironment, log("Result of addHoursToDate")),
)(date)

const inOneHour = addHoursToDate(new Date(), 1)

I think it is useful to just import the functions you want to use in your code as shown in the example above.

Documentation

Find the documentation for the library here.

Contribute and raise issues

Feel free to contribute to the library and send me a pull request in the Github repo. Also, feel free to raise an issue in the repo.

Package Sidebar

Install

npm i cbk-functional-library

Weekly Downloads

0

Version

0.0.20

License

MIT

Unpacked Size

22.6 kB

Total Files

37

Last publish

Collaborators

  • cabcookie