npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

wraperizer

v1.2.2

Published

chainable wraperizer for a function

Downloads

12

Readme

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