unnullish
v1.0.1
Published
[![npm](http://img.shields.io/badge/available%20on-npm-lightgrey.svg?logo=npm&logoColor=white)](https://www.npmjs.com/package/unnullish) [![deno land](http://img.shields.io/badge/available%20on-deno.land/x-lightgrey.svg?logo=deno)](https://deno.land/x/unn
Downloads
6
Readme
unnullish
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.