npm

npm’s 2019 JavaScript ecosystem survey analysis is now available!Get your copy here »

wraperizer

1.2.2 • Public • Published

Wraperizer

chainable wraperizer for a function

Examples

const Wraperizer = require('wraperizer').default
const fn = (a) => a;
const wraperizer = new Wraperizer(fn);
wraperizer.$ === fn; // true

$

Pointer to fn function

Type: function

add

Adds wrapper as a method into instance. fn is bound as the first argument of a wrapping function

Parameters

Examples

const fn = (argA) => argA;
const wraperer = new Wraperizer(fn);
wraperer[Wraperizer.add]('increment', (fn, inc) => (argA) => fn(argA) + inc)
const incremented2 = wraperer.increment(2).$
const incremented5 = wraperer.increment(5).$
incremented2(2) === 4; // true
incremented5(2) === 7; // true

Returns Function.Wraperizer

del

Removes method from instance

Parameters

Returns Function.Wraperizer

bare Wraperizer (without wrappers) can be obtained via require('wraperizer/source/Wraperizer');

Build-in wrappers usage

Examples

const fn = (a) => a;
const wraperizer = new Wraperizer(fn);
const wrapered = wraperizer.pipe((a) => a + 1).nextTick().before((a) => a * 2).$;
wrapered(1); // Promise<3>

after

Postprocesses fn results with wrapper

Parameters

Examples

const fn = (a, b) => a + b
const fnAfter = (a) => a * 2
after(fn, fnAfter)(1, 2); // Promise<6>

Returns function (): Promise

before

Preprocesses arguments with wrapper before fn

Parameters

Examples

const fn = (a) => a + 1
const fnBefore = (a, b) => a * b
before(fn, fnBefore)(1, 2); // Promise<7>

Returns function (): Promise

bottleneck

Creates queue for fn with concurrency limitation

Parameters

  • fn function
  • concurrency number count of concurrently running fn (optional, default 1)

Returns function (): Promise

bunching

Creates function to bunching data of multiply bunching function result invokes

Parameters

  • handler function (Array<arguments>): any function for processing `bunch` of data
  • checker function (resolve: function) function to invoke handler
  • separator function function to parse result from handler into chunks

Examples

let timer;
const invoke = bunching(
  (...args) => args.map(({0: item}) => item),
  (resolve, bunch) => {
    timer = timer ? timer : setTimeout(() => {
      timer = undefined;
      resolve();
    }, 50);
  }
);
 
for (let i = 0; i < 3; i++) {
  setTimeout(() => invoke(i).then((payload) => expect(payload).to.equal(i)), 0);
}

Returns function (): Promise function to set chunk of data to process bunchly

compose

Compose functions

Examples

const fn1 = (a) => a * 2;
const fn2 = (a) => a + 1;
const fn3 = (a) => a ** 2;
compose(fn1, fn2, fn3)(2); // 10;

Returns function

curry

Curries function

Parameters

Examples

const fn = (a, b, c) => a + b + c;
const curried = curry(fn);
curried(1, 2, 3); // 6;
curried(1)(2, 3); // 6;
curried(1, 2)(3); // 6;
curried(1)(2)(3); // 6;

Returns function

length

Returns function with given length (required for curry)

Parameters

Examples

const fn = (a, b) => a + b;
fn.length; // 2
length(fn, 5).length; // 5

Returns function

limit

Limits fn invokes

Parameters

Examples

const fn = (a, b) => a + b;
const limited = limit(fn, 2);
limited(1, 2); // 3
limited(1, 2); // 3
limited(1, 2); // undefined

Returns (function (): any | undefined)

memo

Map based memoization for function

Parameters

Returns function function with memoization

nextTick

Postpones invoke of fn to nextTick

Parameters

Returns function (): Promise

pipe

Pipe functions

Examples

const fn1 = (a) => a * 2;
const fn2 = (a) => a + 1;
const fn3 = (a) => a ** 2;
pipe(fn1, fn2, fn3)(2); // 25

Returns function

promisify

Wrap function with a Promise and call it in async manner

Parameters

Examples

const fn = (a, b) => a + b;
promisify(fn)(1, 2); // Promise<3>

Returns function (): Promise

promisifyCb

Wrap node-style function with a Promise

Parameters

  • fn function (cb: function (error, response)) node-style function (last argument is callback)

Examples

function fn(payload, cb) {
 cb(null, payload + 1);
}
 
promisifyCb(fn)(1); // Promise<2>

Returns function (): Promise function returns Promise for fn invoking

redefine

Creates function wrapper with self-redefine ability

Parameters

  • fn function function which will be invoke on next call of returned function init

Examples

const fn3 = (a, b, redefiner) => a ** b;
const fn2 = (a, b, redefiner) => (redefiner(fn3), a * b);
const fn1 = (a, b, redefiner) => (redefiner(fn2), a + b);
const wrapped = redefine(fn1);
wrapped(2, 3); // 5
wrapped(2, 3); // 6
wrapped(2, 3); // 8
wrapped(2, 3); // 8

Returns function (any, redefiner: redefiner) init - function which will invoke fn with adding redefiner callback as last argument

redefiner

Set function for next call

Type: Function

Parameters

Returns function fn

retry

Retries fn call if error occurs

Parameters

  • fn function
  • retries number (optional, default 1)
  • handler function (optional, default (error,argsObject)->argsObject)

Returns function (): Promise

throttle

Creates function throttling wrapper

Parameters

  • fn function
  • stay function (): Boolean function which define must fn be invoked or not (optional, default ()->false)

Examples

const wrapped = throttle((a, b, c) => b + c, (a) => !!a);
wrapped(0, 1, 2); // 3
wrapped(1, 1, 2); // undefined
wrapped(false, 1, 2); // 3

Returns function

throttleAsync

Creates function throttling wrapper

Parameters

  • fn function
  • stay function (): Boolean function which define must fn be invoked or not (optional, default ()->false)

Returns function (): Promise

timeout

Delay function invoke

Parameters

  • fn function function which will be invoke on next call of returned function init
  • delay number delay interval in milliseconds

Examples

const wrapped = wrapper((a) => a + 1, 100);
wrapped(11); // Promise<2> will resolve in 100 milliseconds

Returns function (): Promise

install

npm i wraperizer

Downloadsweekly downloads

5

version

1.2.2

license

ISC

homepage

github.com

repository

Gitgithub

last publish

collaborators

  • avatar
Report a vulnerability