Security News
Weekly Downloads Now Available in npm Package Search Results
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Possible Function. Wraps what might be a function, with fallback behavior in case it’s not. Perfect for use in functions that accept optional callback arguments.
Wraps what might be a function, with fallback behavior in case it’s not. Perfect for use in functions that accept optional callback arguments.
Requires Node.js 6.0.0 or above.
npm i pfn
There are two ways you can import the module: require('pfn')
or require('pfn/strict')
. Each exposes a single function with the same signature. The difference is that strict mode will throw an error if fn
is anything other than a function, null
, or undefined
. Normal mode will silently defer to or
if fn
is of an unexpected type.
fn
(any): The value that may or may not be a function.or
(any): The function that will be called, or the value that will be returned, if fn
is not a function. Defaults to a passthrough function (a => a
).Always returns a Function.
fn
, if fn
is a Functionor
, if or
is a Functionor
, if neither fn
nor or
is a functionpfn
wraps a value that may or may not be a function. If the underlying value is not a function, then pfn
will execute one of the following fallback behaviors.
If the value turns out to not be a function, pfn
will, by default, pass through whatever is given as the first argument. This is useful for optional filters.
const pfn = require('pfn')
function sayHello (name, filter) {
filter = pfn(filter)
return filter('Hello, ' + name)
}
// No filter is provided, so the hello message is returned without change:
sayHello('world') // 'Hello, world'
// A filter is provided which changes the hello message:
sayHello('world', m => m + '!!') // 'Hello, world!!'
If the value turns out to not be a function, pfn
can be configured to return a value of your choosing.
const pfn = require('pfn')
function sayHello (nameCallback) {
nameCallback = pfn(nameCallback, 'world')
return 'Hello, ' + nameCallback()
}
sayHello() // 'Hello, world'
sayHello(() => 'Dolly') // 'Hello, Dolly'
If you provide the possible function as its own fallback, then you can accept either a value or a function as an argument for your code. For example, the sayHello
function in the following example can accept either a string or a function.
Don’t use the module’s strict mode (require('pfn/strict')
) if you want self-fallback behavior, because strict mode will throw an error if the first parameter is anything other than a function, null
, or undefined
.
const pfn = require('pfn')
function sayHello (name) {
name = pfn(name, name)
return 'Hello, ' + name()
}
sayHello('world') // 'Hello, world'
sayHello(() => 'world') // 'Hello, world'
If the wrapped value turns out to not be a function, the wrapping function can execute a custom fallback function instead:
const pfn = require('pfn')
const mightBeAFunction = null
const callback = pfn(mightBeAFunction, (...args) => args.length)
callback('arg 1', 'arg 2') // 2
This module is part of the fn
family of modules.
FAQs
Possible Function. Wraps what might be a function, with fallback behavior in case it’s not. Perfect for use in functions that accept optional callback arguments.
The npm package pfn receives a total of 1,914 weekly downloads. As such, pfn popularity was classified as popular.
We found that pfn demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.
Security News
A Stanford study reveals 9.5% of engineers contribute almost nothing, costing tech $90B annually, with remote work fueling the rise of "ghost engineers."
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.