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.
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:
constelv=require('elv');constfoo={foo: 'bar'};console.log(elv(foo));// trueconstbar=undefined;console.log(elv(bar));// falseconstbaz=false;console.log(elv(baz));// trueconstqux=null;console.log(elv(qux));// falseGets or sets whether or not existential checks should check if value is not false. Defaults to false (not enabled).
Example:
constelv=require('elv');console.log(elv(false));// trueelv.behavior.enableFalse=true;console.log(elv(false));// falseGets or sets whether or not existential checks should check if value is not Number.NaN. Defaults to false (not enabled).
Example:
constelv=require('elv');console.log(elv(Number.NaN));// trueelv.behavior.enableNaN=true;console.log(elv(Number.NaN));// falseGets or sets whether or not existential checks should check if value is not null. Defaults to true (enabled).
Example:
constelv=require('elv');console.log(elv(null));// falseelv.behavior.enableNull=false;console.log(elv(null));// trueGets or sets whether or not existential checks should check if value is not undefined. Defaults to true (enabled).
Example:
constelv=require('elv');console.log(elv(undefined));// falseelv.behavior.enableUndefined=false;console.log(elv(undefined));// trueAccepts a series of parameters, and returns the first argument that is defined.
Parameters
...val: (required) the values to coalesce.
Example:
constelv=require('elv');constcoalesce=elv.coalesce;constfoo=undefined;constbar=null;constbaz='hello world';constqux=true;constresult=coalesce(foo,bar,baz,qux);console.log(result);// hello worldIf 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
constelv=require('elv');constcoalesce=elv.coalesce;constgetFoo=function(){// do something expensive to compute theValueOfFooreturntheValueOfFoo;};classBar{constructor(foo){// the getFoo function will only be executed if foo is not truthythis._foo=coalesce(foo,getFoo);}}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:
constelv=require('elv');constcoalesce=elv.coalesce;constfoo=undefined;constbar=null;constbaz='hello world';constqux=true;constresult=coalesce(foo,bar,baz,qux);console.log(result);// hello worldIn 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:
constelv=require('elv');console.log(elv.populated('foo'));//trueconsole.log(elv.populated(['foo','bar']));//trueconsole.log(elv.populated({foo: 'bar'}));//trueconsole.log(elv.populated(null));// falseconsole.log(elv.populated(''));// falseconsole.log(elv.populated([]));// falseconsole.log(elv.populated({}));// falseAttempts 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
constelv=require('elv');constval=['foo','bar','baz','qux'];console.log(elv.tryGet(val,5));// undefinedconsole.log(elv.tryGet(val,5,42));// 42console.log(elv.tryGet(val,2));// baz