nick-offerman
Version:
You know how you always have optional values.
20 lines (19 loc) • 914 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.of = exports.Of = void 0;
/** Given a single-parameter constructor, return a function that will accept either
* null, undefined or that single parameter, and return either null, undefined or a
* new instance of the corresponding class.
* @param Ctor the constructor
* @return an optional factory method
*/
const Of = (Ctor) => (param) => (param == null ? param : new Ctor(param));
exports.Of = Of;
/** Given a function, return a new function with the same shape but an optional first
* parameter, that returns either null or undefined if that first parameter is
* null or undefined, or else the result of invoking the wrapped function.
* @param func the function to wrap
* @return a new more optional function
*/
const of = (func) => (param, ...rest) => (param == null ? param : func(param, ...rest));
exports.of = of;