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

wfn

v1.0.0

Published

Gives a wrapper function the properties and string value of its underlying function.

Downloads

7,960

Readme

wfn

A module that prepares Wrapper Functions.

Ensures your wrapper has the same properties as the underlying function, such as name and length. When toString() is called on the wrapper function, the code of the original function is shown, together with a comment identifying the wrapper(s).

Installation

Requires Node.js 6.0.0 or above.

npm i wfn

API

The module exports a single function.

Parameters

  1. fn (Function): The underlying function
  2. wrapper (Function): The wrapper function that calls fn

Return Value

The modified wrapper function

Usage

const wfn = require('wfn')

function func (a, b, c) { return 'result' }
function wrapper () { return func() }

wrapper() // 'result'

// Before
wrapper.name // 'wrapper'
wrapper.length // 0
wrapper.toString() // 'function wrapper () { return func() }'

// Apply the module
wfn(func, wrapper)

// After
wrapper.name // 'func'
wrapper.length // 3
wrapper.toString() // 'function func (a, b, c) { return 'result' } /* [wfn] Wrapped with wrapper() */'

Wrapper Function Stringification

In the above example, note that the wrapper’s toString() method produces the code of the underlying function — not of the wrapper — but with a comment notation that the function has been wrapped.

If you wrap a function more than once, wfn will recognize this and will generate a comment notation that mentions all the wrappers. Consider the following example:

function original () {
  return 'value'
}

const wrapper1 = wfn(original, function wrapper1 () { return original() })
const wrapper2 = wfn(wrapper1, () => wrapper1())
const wrapper3 = wfn(wrapper2, function wrapper3 () { return wrapper2() })

If you call wrapper3.toString(), you will get the following:

function original () {
  /* [wfn] Wrapped with wrapper1(), wrapper3(), and 1 other unnamed function */
  return 'value'
}

If this is something you absolutely don’t care about, and you only want properties like name and length copied, consider swapping out this module for copy-own so you have less overhead.

Related Modules

For more projects like this, check out the xfn family of modules.