Skip to content

Latest commit

 

History

History

contains

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

Contains

Test if a value is found in an array-like object.

Usage

var contains = require( '@stdlib/utils/contains' );

contains( val, searchValue[, position=0] )

Tests if a search value is found in val. When val is a string, the function checks whether the characters of the search string are found in the input string.

var v = contains( 'Hello World', 'World' );
// returns true

Notice that the search is case-sensitive.

var v = contains( 'Hello World', 'world' );
// returns false

When val is an array-like object, but not a string, the function checks whether the input array contains an element equal to the specified search value. Equality is determined via the strict equality operator ===.

var arr = [ null, NaN, 2, 'abc', {} ];

var v = contains( arr, NaN );
// returns true

v = contains( arr, {} );
// returns false

v = contains( arr, 'ab' );
// returns false

To start the search at a specified position of val, supply the position parameter.

var v = contains( 'Hello World', 'Hello', 6 );
// returns false

v = contains( [ true, NaN, false ], true, 1 );
// returns false

If not provided an array-like object for val, the function throws an error.

v = contains( false, 'abc' );
// throws TypeError

The position parameter expects an integer value. Otherwise, the function throws an error.

var v = contains( 'hello', 'e', 2.5 );
// throws TypeError

Notes

  • For strings, this function is modeled after String.prototype.includes, part of the ECMAScript 6 specification. This function is different from a call to String.prototype.includes.call insofar as type-checking is performed for all arguments.
  • If position < 0, the search is performed for the entire input array or string.

Examples

var contains = require( '@stdlib/utils/contains' );

var bool = contains( 'last man standing', 'stand' );
// returns true

bool = contains( [ 1, 2, 3, 4 ], 2 );
// returns true

bool = contains( 'presidential election', 'president' );
// returns true

bool = contains( [ NaN, 2, 3, 4 ], NaN );
// returns true

bool = contains( 'javaScript', 'js' );
// returns false

bool = contains( 'Hidden Treasures', '' );
// returns true