Undefinish (NodeJS)
Releases | Latest () | Pre () |
---|---|---|
📝 Description
A NodeJS module to provide a better and easier coalescing, similar to the function default parameter.
Although the nullish coalescing operator (??
) is an improved operator from the OR operator (||
), it is still not good enough due to it considers null
is an undefined value, even though this is defined and/or as expected.
The conditional (ternary) operator (?:
) maybe good:
(typeof a === "undefined") ? 1 : a;
But it is not that good when need to have many:
(typeof a === "undefined") ? (
(typeof b === "undefined") ? (
(typeof c === "undefined") ? (
(typeof d === "undefined") ? (
(typeof e === "undefined") ? 1 : e
) : d
) : c
) : b
) : a;
Much cleaner with Undefinish:
undefinish(a, b, c, d, e, 1);
📚 Documentation
Getting Started
- NodeJS >= v6.9.0
npm install @hugoalh/undefinish
/* Either */
const undefinish = require("@hugoalh/undefinish");// [CommonJS] Require
import undefinish from "@hugoalh/undefinish";// [ModuleJS] Default Import
API
Function
undefinish(...inputs: unknown[]): unknown;
Example
let input = {
name: null,
age: 8
};
input.name ?? "owl";
//=> "owl"
undefinish(input.name, "owl");
//=> null