https://github.com/exbotanical/coalesce
Highly performant implementation of SQL `COALESCE` for JavaScript. Return the first value that is not null or undefined.
https://github.com/exbotanical/coalesce
coalesce helper-functions micro-library sql validation-library
Last synced: 4 months ago
JSON representation
Highly performant implementation of SQL `COALESCE` for JavaScript. Return the first value that is not null or undefined.
- Host: GitHub
- URL: https://github.com/exbotanical/coalesce
- Owner: exbotanical
- License: mit
- Created: 2021-04-29T05:20:05.000Z (about 4 years ago)
- Default Branch: master
- Last Pushed: 2021-04-30T04:23:11.000Z (about 4 years ago)
- Last Synced: 2025-02-02T07:03:53.445Z (5 months ago)
- Topics: coalesce, helper-functions, micro-library, sql, validation-library
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/coalesce-x
- Size: 129 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# coalesce-x
## Highly performant implementation of SQL `COALESCE` for JavaScript. Return the first value that is not null or undefined
[](https://travis-ci.com/MatthewZito/coalesce)
[](https://badge.fury.io/js/coalesce-x)
[](https://opensource.org/licenses/MIT)### Why?
JavaScript evaluates 'falsy' values such as 0 and '' as false, ergo the logical OR `||` operator cannot be reliably employed to tender fallback values. `coalesce` provides a fast interface for resolving fallback values in a single line of code.
## Table of Contents
- [Supported Environments](#builds)
- [Installation + Usage](#usage)
- [Supply Your Own Skip Value](#skip)`coalesce-x` currently supports UMD, CommonJS, and ESM build-targets.
```bash
npm install coalesce-x# OR
yarn add coalesce-x
``````js
const { coalesce } = require('coalesce-x');(function t (num) {
num = coalesce(num, 9);
for (let i = 0; i < num; i++) {
console.log({ i });
}
})(null);
``````js
import { coalesce } from 'coalesce-x';const fallback = 9;
const v = coalesce(maybeVal1, maybeVal2, maybeVal3, fallback);
```You can also utilize the `coalescent` function and supply your own list of values to coalesce against.
```js
import { coalescent } from 'coalesce-x';const coalesceOdds = coalescent(1,3,5,7,9);
const firstEven = coalesceOdds(1,3,6,3); // 6
```