Checks whether a collection contains a value.
Requires Node.js 8.3.0 or above.
npm i vhas
The module exports a has()
function that has one other function attached to it as a method: has.any()
.
- Bindable:
collection
(Array, Iterator, Map, Object, Set, string, Typed Array, or WeakSet): The collection from which to retrieve a value. valueToCheck
(any): The value whose presence in the collection is in question.- 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).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 identify 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.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
.
- Returns
true
ifvalueToCheck
matches an element in thecollection
. The match can be strict or loose depending on the configured options. - Otherwise, returns
false
.
const has = require('vhas')
const emptyObj = {}
const collection = [emptyObj]
has(collection, {}, {loose: true}) // true
Use this function if you want to check whether a collection contains any one of a set of values. The signature is the same as the main function except that the second parameter is called valuesToCheck
and takes an iterable (such as an array or string).
const has = require('vhas')
const vowels = 'aeiou'
has.any('test', vowels) // true
has.any('xyz', vowels) // false
The “k” family of modules works on keyed/indexed collections.
The “v” family of modules works on any collection of values.