Skip to content

keidrun/fantasy-utils

Repository files navigation

fantasy-utils NPM version Build Status Dependency Status

Small utils for Functional Programming.

Description

This provides small utilities to complement the lack of a functional library like ramda or lodash. It includes both pure and impure functions and fullfills an easy point-free style programming. Of course, all utilities are curried.

An example is below.

const R = require('ramda');
const F = require('fantasy-utils');

const dashrize = R.compose(
  R.join('-'),
  R.map(R.toLower),
  F.trace('debug:'), // print middle data for debugging
  R.split(' '),
  R.replace(/\s{2,}/gi, '')
);
console.log('result:', dashrize('Do you know our world is small?'));
//=> debug: [ 'Do', 'you', 'know', 'our', 'world', 'is', 'small?' ]
//=> result: do-you-know-our-world-is-small?

Other examples are below.

const R = require('ramda');
const F = require('fantasy-utils');

// Promise way
const promiseDashrize = R.compose(
  F.promiseBind(R.join('-')),
  F.promiseBind(R.map(R.toLower)),
  F.promiseBind(R.split(' ')),
  F.promiseBind(R.replace(/\s{2,}/gi, ''))
);
promiseDashrize(F.promiseOf('Do you know our limit is sky?'))
  .then(console.log)
  .catch(console.error);
//=> do-you-know-our-limit-is-sky?

// Promise point-free style
const calc = R.compose(
  F.promiseReduce((x, y) => x + y, 0),
  F.promiseMap(x => x * 3),
  F.promiseFilter(x => x % 2 === 0)
);
calc(F.promiseOf([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]))
  .then(console.log)
  .catch(console.error);
//=> 90

Implementations

Pure functions

  • promiseOf :: Any -> Promise Any
  • promiseBind :: (Function, Promise Any) -> Promise Any
  • promiseMap :: (Function, Promise Array) -> Promise Array
  • promiseFind :: (Function, Promise Array) -> Promise Any
  • promiseFilter :: (Function, Promise Array) -> Promise Array
  • promiseReduce :: (Function, Any, Promise Array) -> Promise Any
  • promiseConcat :: (Array, Promise Array) -> Promise Array
  • promiseSlice :: (Integer, Integer, Promise Array) -> Promise Array
  • promiseSplice :: (Integer, Promise Array) -> Promise Array

Impure functions

  • trace :: (String, Any) -> Any

Releases

No releases published

Packages

No packages published