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
(??
).
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 "https://deno.land/x/unnullish@$MODULE_VERSION/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 "https://deno.land/x/unnullish@$MODULE_VERSION/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
The code follows MIT license written in LICENSE. Contributors need to agree that any modifications sent in this repository follow the license.