vget
Gets a value from a collection that strictly or loosely equals another value.
Installation
Requires Node.js 8.3.0 or above.
npm i vget
API
The module exports a get()
function that has one other function attached to it as a method: get.any()
.
get()
Parameters
- Bindable:
collection
(Array, Iterator, Map, Object, Set, string, Typed Array, or WeakSet): The collection from which to retrieve a value. valueToMatch
(any): The value to retrieve from the collection.- Optional: Object argument:
arrays
/maps
/sets
/weakSets
(arrays of classes/strings): Arrays of classes and/or string names of classes that should be treated as equivalent toArray
/Map
/Set
/WeakSet
(respectively).elseReturn
(any): A value to return ifvalueToMatch
is not found. Defaults toundefined
.inObj
(boolean): Whether or not to search inherited properties ifcollection
is an Object (i.e. not another recognized type). Defaults tofalse
.loose
(boolean): Whether or not to compare values loosely (as defined bylooselyEquals
). Defaults tofalse
.looselyEquals
(function): A callback that accepts two values and returnstrue
if they are to be considered equivalent orfalse
otherwise. This argument is only used ifloose
istrue
. If omitted, the default behavior will, among other things, consider arrays/objects to be equal if they have the same entries.preferStrict
(boolean): Only applies ifloose
istrue
. Iftrue
, then strictly-identical values will be preferred over loosely-equivalent values. Otherwise, the first loosely-equivalent key found will be used, even if a strictly-identical one comes later. Defaults tofalse
.reflectObj
(boolean): Whether or not to use reflection to include non-enumerable Object property values. Only takes effect ifcollection
is an Object (i.e. not another recognized type). Defaults tofalse
.
Return Values
- Returns a value from the collection that matches the search value either strictly or loosely (depending on the configured options).
- If no such value exists, returns
elseReturn
if provided; otherwiseundefined
.
Example
const get = const emptyObj = {}const collection = emptyObj === emptyObj // true
get.any()
Use this function if you want to get the first value that matches any one of a set of values. The signature is the same as that of the main function except that the second parameter is called valuesToMatch
and takes an iterable (such as an array or string).
Related
The “k” family of modules works on keyed/indexed collections.
The “v” family of modules works on any collection of values.