npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

elv

v2.2.0

Published

Elvis operator functionality for JavaScript.

Downloads

853

Readme

elv

Elvis operator functionality for JavaScript.

With the absence of an Elvis (existential) operator in JavaScript, I often find myself writing the same checks to see if something is undefined over and over and over again. The || operator in JavaScript is generally inadequate for this purpose, because it fails to account for Booleans. So, I decided to create a small module to clean up the redundant code.

API

elv(val)

Determines whether or not the argument val is defined. Returns false if val is undefined or null, or will otherwise returns true.

Parameters

  • val: (required) the value on which to perform an existential check.

Example:

const elv = require('elv');

const foo = { foo: 'bar' };
console.log(elv(foo)); // true

const bar = undefined;
console.log(elv(bar)); // false

const baz = false;
console.log(elv(baz)); // true

const qux = null;
console.log(elv(qux)); // false

elv.behavior.enableFalse

Gets or sets whether or not existential checks should check if value is not false. Defaults to false (not enabled).

Example:

const elv = require('elv');

console.log(elv(false)); // true

elv.behavior.enableFalse = true;

console.log(elv(false)); // false

elv.behavior.enableNaN

Gets or sets whether or not existential checks should check if value is not Number.NaN. Defaults to false (not enabled).

Example:

const elv = require('elv');

console.log(elv(Number.NaN)); // true

elv.behavior.enableNaN = true;

console.log(elv(Number.NaN)); // false

elv.behavior.enableNull

Gets or sets whether or not existential checks should check if value is not null. Defaults to true (enabled).

Example:

const elv = require('elv');

console.log(elv(null)); // false

elv.behavior.enableNull = false;

console.log(elv(null)); // true

elv.behavior.enableUndefined

Gets or sets whether or not existential checks should check if value is not undefined. Defaults to true (enabled).

Example:

const elv = require('elv');

console.log(elv(undefined)); // false

elv.behavior.enableUndefined = false;

console.log(elv(undefined)); // true

elv.coalesce(...val)

Accepts a series of parameters, and returns the first argument that is defined.

Parameters

  • ...val: (required) the values to coalesce.

Example:

const elv = require('elv');
const coalesce = elv.coalesce;

const foo = undefined;
const bar = null;
const baz = 'hello world';
const qux = true;

const result = coalesce(foo, bar, baz, qux);
console.log(result); // hello world

Deferred Function Execution

If the final argument passed to elv.coalesce() is reached, and it is a function, then it will evaluate that function and return its result. The idea is to ensure that potentially expensive functions to execute are not run unless absolutely necessary.

Example

const elv = require('elv');
const coalesce = elv.coalesce;

const getFoo = function() {
  // do something expensive to compute theValueOfFoo
  return theValueOfFoo;
};

class Bar {
  constructor(foo) {
    // the getFoo function will only be executed if foo is not truthy
    this._foo = coalesce(foo, getFoo);
  }
}

elv.ncoalesce(...val)

Accepts a series of parameters, and returns the first argument that is defined. Works just like elv.coalesce(), but it does not lazily execute functions.

Parameters

  • ...val: (required) the values to coalesce.

Example:

const elv = require('elv');
const coalesce = elv.coalesce;

const foo = undefined;
const bar = null;
const baz = 'hello world';
const qux = true;

const result = coalesce(foo, bar, baz, qux);
console.log(result); // hello world

elv.populated(val)

In addition to performing an existential check, determines if a given string, array or object is not empty. An empty object is one that has no properties.

Parameters

  • val: (required) the value on which to perform an existential and populated check.

Examples:

const elv = require('elv');

console.log(elv.populated('foo')); //true
console.log(elv.populated(['foo', 'bar'])); //true
console.log(elv.populated({ foo: 'bar' })); //true
console.log(elv.populated(null)); // false
console.log(elv.populated('')); // false
console.log(elv.populated([])); // false
console.log(elv.populated({})); // false

elv.tryGet(val, index [, default])

Attempts to get an entry from an array at the given index. If the given index is out of the array's bounds, then a given default value is returned.

Parameters

  • val: (required) the array from which an entry is being fetched.

  • index: (required) the index being referenced in the array.

  • default: (optional) the default value if index is not found.

Examples

const elv = require('elv');

const val = ['foo', 'bar', 'baz', 'qux'];

console.log(elv.tryGet(val, 5)); // undefined
console.log(elv.tryGet(val, 5, 42)); // 42
console.log(elv.tryGet(val, 2)); // baz