UNPKG

pfn

Version:

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.

9 lines (6 loc) 257 B
'use strict' const f = x => typeof x === 'function' const nil = require('is-nil') const toss = err => { throw err } module.exports = (x, or = a => a) => f(x) ? x : nil(x) ? (f(or) ? or : () => or) : toss(new TypeError('Value cannot be a non-function'))