Skip to content

Highly performant implementation of SQL `COALESCE` for JavaScript. Return the first value that is not null or undefined.

License

Notifications You must be signed in to change notification settings

exbotanical/coalesce

Repository files navigation

coalesce-x

Highly performant implementation of SQL COALESCE for JavaScript. Return the first value that is not null or undefined

Build Status npm version License: 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

coalesce-x currently supports UMD, CommonJS, and ESM build-targets.

Installation + Usage

npm install coalesce-x

# OR

yarn add coalesce-x
const { coalesce } = require('coalesce-x');

(function t (num) {
  num = coalesce(num, 9);
  for (let i = 0; i < num; i++) {
    console.log({ i });
  }
})(null);
import { coalesce } from 'coalesce-x';

const fallback = 9;

const v = coalesce(maybeVal1, maybeVal2, maybeVal3, fallback);

Customization

You can also utilize the coalescent function and supply your own list of values to coalesce against.

import { coalescent } from 'coalesce-x';

const coalesceOdds = coalescent(1,3,5,7,9);

const firstEven = coalesceOdds(1,3,6,3); // 6

About

Highly performant implementation of SQL `COALESCE` for JavaScript. Return the first value that is not null or undefined.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published