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

vfn

v1.1.0

Published

Creates a variadic function, with a “rest parameter” that’s NOT at the end.

Downloads

8,621

Readme

Variadic Function (vfn)

Lets you specify a “rest parameter” that’s not at the end.

Installation

Requires Node.js 6.0.0 or above.

npm i vfn

API

The module exports a single function.

Parameters

  1. Object argument (or a value for arg):
    • Optional: arg (positive integer): The zero-based index of which parameter in fn’s parameters list should be the “rest parameter.” Defaults to 0.
    • Optional: oo (boolean): If true, an optional options argument at the end of the parameter list will be ignored. (This will only work if you can be sure that none of your other arguments will be plain objects.) Defaults to false.
  2. fn (function): The function that has a parameter you want to convert into a “rest parameter.”

Return Value

A wrapper function that turns all excess arguments into an array which is passed to fn at parameter index arg.

Example

JavaScript supports “rest parameters,” but only at the end. Anything else will throw an error:

function func (...a, b, c) {} // Uncaught SyntaxError: Rest parameter must be last formal parameter

The vfn module lets you accomplish this. The index of a in the parameters list is 0, so we pass 0 as the first argument to vfn():

const vfn = require('vfn')

const func = vfn(0, function (a, b, c) {})

func(1, 2, 3, 4, 5) // a = [1, 2, 3]; b = 4; c = 5
func(1, 2, 3) // a = [1]; b = 2; c = 3
func('test', 'example') // a = []; b = 'test'; c = 'example'
func('hello world') // a = []; b = 'hello world'; c = undefined

With Optional Options Argument

Normally, vfn does not support optional parameters, because there’s no way to tell which arguments go to the optional parameter and which go to the variadic parameter. Therefore, vfn ignores optional parameters altogether. Take for example the common design pattern of including an optional options parameter at the end of the parameter list:

const vfn = require('vfn')

const func = vfn(0, function (a, b, c, {option} = {}) {})

func(1, 2, 3, 4, 5) // a = [1, 2, 3]; b = 4; c = 5; option = undefined
func(1, 2, 3, 4, 5, {option: 123}) // a = [1, 2, 3, 4]; b = 5; c = {option: 123}; option = undefined

As you can see, the first call works as expected, because the optional options parameter is ignored completely. But it continues to be ignored when we introduce the options object in the second call. vfn collapses one too many arguments into a and puts the options object into c.

To make use of an optional options argument, set the oo flag when calling vfn:

const vfn = require('vfn')

const func = vfn({arg: 0, oo: true}, function (a, b, c, {option} = {}) {})

func(1, 2, 3, 4, 5) // a = [1, 2, 3]; b = 4; c = 5; option = undefined
func(1, 2, 3, 4, 5, {option: 123}) // a = [1, 2, 3]; b = 4; c = 5; option = 123

With oo set, vfn will recognize that a plain object at the end belongs to an optional ending parameter.

This only works if your options parameter is optional and is at the end, and if you can be sure that none of your other arguments will be plain objects.

Related

For more projects like this, check out @lamansky/fn.