New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

elv

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

elv

Elvis operator functionality for JavaScript.

  • 1.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
35
decreased by-78.66%
Maintainers
1
Weekly downloads
 
Created
Source

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(obj)

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

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.coalesce(obj[, obj1, obj2 ...,objN])

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

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

The elv.coalesce() function will execute function arguments, and return its value if it is found to be the first truthy argument in the series. 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);
  }
}

Keywords

FAQs

Package last updated on 28 Aug 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc