{"id":15917595,"url":"https://github.com/dsfields/elv","last_synced_at":"2025-03-24T07:32:06.052Z","repository":{"id":82746525,"uuid":"66777985","full_name":"dsfields/elv","owner":"dsfields","description":"Elvis operator functionality for JavaScript.","archived":false,"fork":false,"pushed_at":"2018-09-13T17:23:51.000Z","size":35,"stargazers_count":7,"open_issues_count":1,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-19T03:13:18.738Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dsfields.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-28T16:14:25.000Z","updated_at":"2023-04-19T20:11:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"d18c5838-4c30-4d95-909b-270adeb41fed","html_url":"https://github.com/dsfields/elv","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsfields%2Felv","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsfields%2Felv/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsfields%2Felv/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dsfields%2Felv/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dsfields","download_url":"https://codeload.github.com/dsfields/elv/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245227523,"owners_count":20580894,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-06T18:11:47.572Z","updated_at":"2025-03-24T07:32:05.632Z","avatar_url":"https://github.com/dsfields.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# elv\n\nElvis operator functionality for JavaScript.\n\nWith the absence of an [Elvis (existential) operator](https://en.wikipedia.org/wiki/Elvis_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.\n\n## API\n\n### `elv(val)`\n\nDetermines whether or not the argument `val` is _defined_.  Returns `false` if `val` is `undefined` or `null`, or will otherwise returns `true`.\n\n__Parameters__\n\n* `val`: _(required)_ the value on which to perform an existential check.\n\n__Example:__\n```js\nconst elv = require('elv');\n\nconst foo = { foo: 'bar' };\nconsole.log(elv(foo)); // true\n\nconst bar = undefined;\nconsole.log(elv(bar)); // false\n\nconst baz = false;\nconsole.log(elv(baz)); // true\n\nconst qux = null;\nconsole.log(elv(qux)); // false\n```\n\n### `elv.behavior.enableFalse`\n\nGets or sets whether or not existential checks should check if value is not `false`.  Defaults to `false` (not enabled).\n\n__Example:__\n\n```js\nconst elv = require('elv');\n\nconsole.log(elv(false)); // true\n\nelv.behavior.enableFalse = true;\n\nconsole.log(elv(false)); // false\n```\n\n### `elv.behavior.enableNaN`\n\nGets or sets whether or not existential checks should check if value is not `Number.NaN`.  Defaults to `false` (not enabled).\n\n__Example:__\n\n```js\nconst elv = require('elv');\n\nconsole.log(elv(Number.NaN)); // true\n\nelv.behavior.enableNaN = true;\n\nconsole.log(elv(Number.NaN)); // false\n```\n\n### `elv.behavior.enableNull`\n\nGets or sets whether or not existential checks should check if value is not `null`.  Defaults to `true` (enabled).\n\n__Example:__\n\n```js\nconst elv = require('elv');\n\nconsole.log(elv(null)); // false\n\nelv.behavior.enableNull = false;\n\nconsole.log(elv(null)); // true\n```\n\n### `elv.behavior.enableUndefined`\n\nGets or sets whether or not existential checks should check if value is not `undefined`.  Defaults to `true` (enabled).\n\n__Example:__\n\n```js\nconst elv = require('elv');\n\nconsole.log(elv(undefined)); // false\n\nelv.behavior.enableUndefined = false;\n\nconsole.log(elv(undefined)); // true\n```\n\n### `elv.coalesce(...val)`\n\nAccepts a series of parameters, and returns the first argument that is _defined_.\n\n__Parameters__\n\n* `...val`: _(required)_ the values to coalesce.\n\n__Example:__\n```js\nconst elv = require('elv');\nconst coalesce = elv.coalesce;\n\nconst foo = undefined;\nconst bar = null;\nconst baz = 'hello world';\nconst qux = true;\n\nconst result = coalesce(foo, bar, baz, qux);\nconsole.log(result); // hello world\n```\n\n#### Deferred Function Execution\n\nIf 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.\n\n__Example__\n```js\nconst elv = require('elv');\nconst coalesce = elv.coalesce;\n\nconst getFoo = function() {\n  // do something expensive to compute theValueOfFoo\n  return theValueOfFoo;\n};\n\nclass Bar {\n  constructor(foo) {\n    // the getFoo function will only be executed if foo is not truthy\n    this._foo = coalesce(foo, getFoo);\n  }\n}\n```\n\n### `elv.ncoalesce(...val)`\n\nAccepts a series of parameters, and returns the first argument that is _defined_.  Works just like [elv.coalesce()](#elvcoalesceval), but it does not lazily execute functions.\n\n__Parameters__\n\n* `...val`: _(required)_ the values to coalesce.\n\n__Example:__\n```js\nconst elv = require('elv');\nconst coalesce = elv.coalesce;\n\nconst foo = undefined;\nconst bar = null;\nconst baz = 'hello world';\nconst qux = true;\n\nconst result = coalesce(foo, bar, baz, qux);\nconsole.log(result); // hello world\n```\n\n### `elv.populated(val)`\n\nIn 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.\n\n__Parameters__\n\n* `val`: _(required)_ the value on which to perform an existential and populated check.\n\n__Examples:__\n\n```js\nconst elv = require('elv');\n\nconsole.log(elv.populated('foo')); //true\nconsole.log(elv.populated(['foo', 'bar'])); //true\nconsole.log(elv.populated({ foo: 'bar' })); //true\nconsole.log(elv.populated(null)); // false\nconsole.log(elv.populated('')); // false\nconsole.log(elv.populated([])); // false\nconsole.log(elv.populated({})); // false\n```\n\n### `elv.tryGet(val, index [, default])`\n\nAttempts 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.\n\n__Parameters__\n\n* `val`: _(required)_ the array from which an entry is being fetched.\n\n* `index`: _(required)_ the index being referenced in the array.\n\n* `default`: _(optional)_ the default value if index is not found.\n\n__Examples__\n\n```js\nconst elv = require('elv');\n\nconst val = ['foo', 'bar', 'baz', 'qux'];\n\nconsole.log(elv.tryGet(val, 5)); // undefined\nconsole.log(elv.tryGet(val, 5, 42)); // 42\nconsole.log(elv.tryGet(val, 2)); // baz\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdsfields%2Felv","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdsfields%2Felv","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdsfields%2Felv/lists"}