🦕 An opposite function of nullish coalescing operator

Overview

unnullish

npm deno land deno doc Test npm version

unnullish returns undefined if value is nullish, otherwise it executes callback and returns the result. It is an opposite function of the nullish coalescing operator (??).

Usage

unnullish

  • unnullish<T, R>(value: T | null | undefined, callback(v: T) => R): R | undefined

The function is useful when you want to apply some transformation functions to optional values. For example,

import { unnullish } from "./mod.ts";

type Options = {
  foo?: string;
  bar?: number;
};

function sayHello(v: string): string {
  return `Hello ${v}`;
}

const options: Options = {
  foo: unnullish(Deno.env.get("foo"), (v) => sayHello(v)),
  // instead of
  //foo: Deno.env.get("foo") != null
  //  ? sayHello(Deno.env.get("foo"))
  //  : undefined,

  bar: unnullish(Deno.env.get("bar"), (v) => parseInt(v, 10)),
  // instead of
  //bar: Deno.env.get("bar") != null
  //  ? parseInt(Deno.env.get("bar"), 10)
  //  : undefined,
};

Note that the function returns undefined even the input is null, mean that you may need to use nullish coalescing operator to normalize the result. For example,

import { unnullish } from "./mod.ts";

console.log(unnullish(null, () => 0));
// -> undefined
console.log(unnullish(undefined, () => 0));
// -> undefined

console.log(unnullish(null, () => 0) ?? null);
// -> null
console.log(unnullish(undefined, () => 0) ?? null);
// -> null
Deno

Use import { unnullish } from "https://deno.land/x/unnullish/mod.ts";

Node

Use import { unnullish } from "unnullish";

License

The code follows MIT license written in LICENSE. Contributors need to agree that any modifications sent in this repository follow the license.

You might also like...

This is an VanillaJS SPA example with function based rendering.

This is an VanillaJS SPA example with function based rendering.

Function-Based-Rendering This is an VanillaJS SPA example with function based rendering. Here's how to create Views by function composition and how to

Oct 16, 2021

This simple project, show how work with async Fetch, function component and class component

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

Feb 17, 2022

Aergo Timer Service schedule smart contract function calls

Aergo Timer Service ⏰ Create timers to call functions on your smart contracts Schedule calls based on time interval or on specific date-times For a sm

Mar 10, 2022

A data-binding function for the DOM.

Alert: this library is now deprecated. s2 is its successor. It implements what simulacra does in a better way (using Proxy), and more. Simulacra.js Si

Nov 18, 2022

Interactive 3D plotting with a simple function call using Three.js

Interactive 3D plotting with a simple function call using Three.js

About Generate interactive 3d plots with a simple function call. Function returns a Three.js scene which can be customized as needed. Basic function c

Oct 20, 2022

A simple Todo App with check complete function and filter todo. Made with React and TypeScript.

A simple Todo App with check complete function and filter todo. Made with React and TypeScript.

A simple Todo App with check complete function and filter todo. Made with React and TypeScript. The app can Add, Delete and Edit todo. Todo can be sorted by Complete or Active.

Dec 6, 2022

Avoid use of dangerouslySetInnerHTML with this lightweight (2KB) function.

Avoid use of dangerouslySetInnerHTML with this lightweight (2KB) function. Can parse HTML strings into VDom trees, ready to render in your Preact components

May 14, 2022

Logs the output, time, arguments, and stacktrace of any function when it's called in a gorgeous way.

Function.prototype.log Logs the output, time, arguments, and stacktrace of any function when it's called. How to use: Like this: function yourFunction

Apr 9, 2022

Some process handle JavaScript function parameter.

Function parameter handle or paremeter error control Example 1: Just checking if all arguments were passed / defined. const required = (name) = {

Mar 14, 2022
Comments
  • :package: Update Deno dependencies

    :package: Update Deno dependencies

    The output of make update is

    /home/runner/work/deno-unnullish/deno-unnullish/unnullish.ts
    
    /home/runner/work/deno-unnullish/deno-unnullish/README.md
    
    /home/runner/work/deno-unnullish/deno-unnullish/mod.ts
    
    /home/runner/work/deno-unnullish/deno-unnullish/scripts/build_npm.ts
    [1/1] Looking for releases: https://deno.land/x/[email protected]/mod.ts
    [1/1] Attempting update: https://deno.land/x/[email protected]/mod.ts -> 0.31.0
    [1/1] Update successful: https://deno.land/x/[email protected]/mod.ts -> 0.31.0
    
    /home/runner/work/deno-unnullish/deno-unnullish/unnullish_test.ts
    [1/2] Looking for releases: https://deno.land/[email protected]/testing/asserts.ts
    [1/2] Attempting update: https://deno.land/[email protected]/testing/asserts.ts -> 0.164.0
    [1/2] Update successful: https://deno.land/[email protected]/testing/asserts.ts -> 0.164.0
    [2/2] Looking for releases: https://deno.land/x/[email protected]/mod.ts
    [2/2] Using latest: https://deno.land/x/[email protected]/mod.ts
    
    Already latest version:
    https://deno.land/x/[email protected]/mod.ts == v0.1.0
    
    Successfully updated:
    https://deno.land/x/[email protected]/mod.ts 0.21.0 -> 0.31.0
    https://deno.land/[email protected]/testing/asserts.ts 0.150.0 -> 0.164.0
    make[1]: Entering directory '/home/runner/work/deno-unnullish/deno-unnullish'
    make[1]: Leaving directory '/home/runner/work/deno-unnullish/deno-unnullish'
    
    
    automation 
    opened by github-actions[bot] 1
Releases(v1.0.0)
  • v1.0.0(Nov 14, 2022)

    What's Changed

    • :up: Upgrade dependencies and supported Deno version by @lambdalisue in https://github.com/lambdalisue/deno-unnullish/pull/2

    New Contributors

    • @lambdalisue made their first contribution in https://github.com/lambdalisue/deno-unnullish/pull/2

    Full Changelog: https://github.com/lambdalisue/deno-unnullish/compare/v0.2.0...v1.0.0

    Source code(tar.gz)
    Source code(zip)
  • v0.2.0(Aug 7, 2022)

Owner
Alisue
Vim, Python, Rust, Deno
Alisue
Dark theme for VSCode with italics support (good for Dank Mono, Operator Mono)

Omni Owl for Visual Studio Code Dark theme for Visual Studio Code (with Italics) Install • Team • Imitate Preview • License Install All instructions c

Guilherme Rodz 59 Dec 23, 2022
A Kubernates Cloud-Shell (Web Terminal) Operator

A Kubernates Cloud-Shell (Web Terminal) Operator. English | Simplified_Chinese Why cloudtty ? Existing project ttyd already provides great feature to

cloudtty-io 307 Dec 27, 2022
The operator CLI for CDK apps.

cdk-app The operator CLI for CDK apps. Experimental. cdk-app lets you associate commands with CDK constructs so that you can quickly invoke functions,

CDK Labs at AWS 42 Dec 8, 2022
Trivy-Operator Lens Extension

Trivy-Operator Lens Extension This is a Lens extension for trivy-operator which provides visibility into vulnerability,misconfiguration and secrets as

Aqua Security 18 Dec 5, 2022
ESLint plugin to disallow the optional-call operator

ESLint Plugin: no-optional-call Overview The no-optional-call ESLint plugin provides a single rule (non-configurable) that disallows any usage of the

Kyle Simpson 15 Sep 24, 2022
A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM deffiniation and appropriate file structure.

Welcome to function-stencil ?? A quickstart AWS Lambda function code generator. Downloads a template function code file, test harness file, sample SAM

Ben Smith 21 Jun 20, 2022
Tries to execute sync/async function, returns a specified default value if the function throws

good-try Tries to execute sync/async function, returns a specified default value if the function throws. Why Why not nice-try with it's 70+ million do

Antonio Stoilkov 14 Dec 8, 2022
Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called

bun-livereload Wrap a function with bun-livereload to automatically reload any imports inside the function the next time it is called. import liveRelo

Jarred Sumner 19 Dec 19, 2022
⚖️ Limit an async function's concurrency with ease!

limit-concur Limit an async function's concurrency with ease! Install $ npm i limit-concur Usage import got from 'got' import limitConcur from 'limit-

Tomer Aberbach 19 Apr 8, 2022
An adapter where you can define which function to run

Switch Functions An adapter where you can define which function to run Installation This is a Node.js module available through the npm registry. Befor

Badass Team 2 Jun 17, 2022