Narnia's Psychedelic Mushrooms
Wondering what’s next for npm?Check out our public roadmap! »

parameter-reduction

1.3.3 • Public • Published

Build Status

parameter-reduction

Tired of defining parameter for resolvers? This might be your friend.

FEATURING

Calling array methods

  • array methods
    • map
    • filter
    • sort
    • reduce
    • reduceRight
    • forEach
    • join
    • concat
  • optional methods
    • map
    • filter
    • forEach
    • concat

Optional

wraps single values and enables you to handle them like arrays.

const { wrap, map, filter, unwrap } = require("parameter-reduction"),
  add = increment => num => num + increment,
  byBeingOdd = num => num % 2;
 
Promise.resolve(5) //now, concerns are separated.
  .then(wrap)
  .then(filter(byBeingOdd))
  .then(map(add(1)))
  .then(unwrap);

Utility methods

flatten

flattens array of arrays to array (reduces 1 dimension)

const { flatten } = require("parameter-reduction");
flatten([[1, 2, 3], [4, 5, 6], [7, 8, 9]]); //returns [1, 2, 3, 4, 5, 6, 7, 8, 9]

sift

sift elements with multiple criterias. Plus, you can get the rests.

const { sift } = require("parameter-reduction");
sift([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).by(
  n => n > 5,
  n => n < 0,
  n => !(% 2),
  n => n === 5
);
/*
 * returns
 * [
 *   [6, 7, 8, 9, 10],
 *   [],
 *   [2, 4],
 *   [5],
 *   [1, 3] // rests
 * ]
 */

siftBy

callback-friendly implement of sift, you can give criterias first.

const { siftBy } = require("parameter-reduction");
Promise.resolve([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]).then(
  siftBy(n => n > 5, n => n < 0, n => !(% 2), n => n === 5)
);
/*
 * resolves
 * [
 *   [6, 7, 8, 9, 10],
 *   [],
 *   [2, 4],
 *   [5],
 *   [1, 3]
 * ]
 */

head, tail, last, init

returns

  • head: the first element or undefined
  • tail: array of elements other than the first one or []
  • last: the last element or undefined
  • init: array of elements other than the last one or []

EXAMPLE

You can make this

const add = increment => num => num + increment,
  byBeingOdd = num => num % 2,
  sum = (soFar, current) => soFar + current;
 
Promise.resolve([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  .then(arr => arr.map(add(1)))
  .then(arr => arr.filter(byBeingOdd))
  .then(arr => arr.reduce(sum));

Like this!

const { map, filter, reduce } = require("parameter-reduction"),
  add = increment => num => num + increment,
  byBeingOdd = num => num % 2,
  sum = (soFar, current) => soFar + current;
 
Promise.resolve([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
  .then(map(add(1)))
  .then(filter(byBeingOdd))
  .then(reduce(sum));

Install

npm i parameter-reduction

DownloadsWeekly Downloads

36

Version

1.3.3

License

MIT

Unpacked Size

27.7 kB

Total Files

9

Last publish

Collaborators

  • avatar