Nutritious Pumpkin Meal

    @hugoalh/undefinish
    TypeScript icon, indicating that this package has built-in type declarations

    1.0.15 • Public • Published

    Undefinish (NodeJS)

    Undefinish.NodeJS

    NodeJS License GitHub Stars GitHub Contributors GitHub Issues GitHub Pull Requests GitHub Discussions CodeFactor Grade

    Releases Latest (GitHub Latest Release Date) Pre (GitHub Latest Pre-Release Date)
    GitHub GitHub Total Downloads GitHub Latest Release Version GitHub Latest Pre-Release Version
    NPM NPM Total Downloads NPM Latest Release Version NPM Latest Pre-Release Version

    📝 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

    Install

    • NodeJS >= v6.9.0
    npm install @hugoalh/undefinish

    Use

    CommonJS
    const undefinish = require("@hugoalh/undefinish");
    ModuleJS
    import undefinish from "@hugoalh/undefinish";

    API

    Function

    undefinish(...inputs: any[]): any

    Example

    let input = {
      displayName: null,
      age: 8
    };
    
    input.username ?? input.name ?? input.displayName ?? "owl";
    //=> "owl"
    
    undefinish(input.username, input.name, input.displayName, "owl");
    //=> null

    Install

    npm i @hugoalh/undefinish

    DownloadsWeekly Downloads

    199

    Version

    1.0.15

    License

    MIT

    Unpacked Size

    9.47 kB

    Total Files

    9

    Last publish

    Collaborators

    • hugoalh