Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/lambdalisue/deno-unnullish
🦕 An opposite function of nullish coalescing operator
https://github.com/lambdalisue/deno-unnullish
deno jsr nullish
Last synced: 12 days ago
JSON representation
🦕 An opposite function of nullish coalescing operator
- Host: GitHub
- URL: https://github.com/lambdalisue/deno-unnullish
- Owner: lambdalisue
- License: mit
- Created: 2022-08-05T02:28:39.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2024-04-06T08:29:40.000Z (7 months ago)
- Last Synced: 2024-10-04T15:31:55.886Z (about 1 month ago)
- Topics: deno, jsr, nullish
- Language: TypeScript
- Homepage: https://jsr.io/@lambdalisue/unnullish
- Size: 18.6 KB
- Stars: 15
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# unnullish
[![jsr](https://img.shields.io/jsr/v/%40lambdalisue/unnullish?logo=javascript&logoColor=white)](https://jsr.io/@lambdalisue/unnullish)
[![denoland](https://img.shields.io/github/v/release/lambdalisue/deno-unnullish?logo=deno&label=denoland)](https://github.com/lambdalisue/deno-unnullish/releases)
[![npm](http://img.shields.io/badge/available%20on-npm-lightgrey.svg?logo=npm&logoColor=white)](https://www.npmjs.com/package/unnullish)
[![deno doc](https://doc.deno.land/badge.svg)](https://doc.deno.land/https/deno.land/x/unnullish/mod.ts)
[![Test](https://github.com/lambdalisue/deno-unnullish/workflows/Test/badge.svg)](https://github.com/lambdalisue/deno-unnullish/actions?query=workflow%3ATest)
[![npm version](https://badge.fury.io/js/unnullish.svg)](https://badge.fury.io/js/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](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator)
(`??`).[nullish coalescing operator]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Nullish_coalescing_operator
## Usage
### unnullish
- `unnullish(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,```typescript
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,```typescript
import { unnullish } from "https://deno.land/x/unnullish@$MODULE_VERSION/mod.ts";console.log(unnullish(null, () => 0));
// -> undefined
console.log(unnullish(undefined, () => 0));
// -> undefinedconsole.log(unnullish(null, () => 0) ?? null);
// -> null
console.log(unnullish(undefined, () => 0) ?? null);
// -> null
```## License
The code follows MIT license written in [LICENSE](./LICENSE). Contributors need
to agree that any modifications sent in this repository follow the license.