time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

contains.js (3204B)


      1 /**
      2 * @license Apache-2.0
      3 *
      4 * Copyright (c) 2018 The Stdlib Authors.
      5 *
      6 * Licensed under the Apache License, Version 2.0 (the "License");
      7 * you may not use this file except in compliance with the License.
      8 * You may obtain a copy of the License at
      9 *
     10 *    http://www.apache.org/licenses/LICENSE-2.0
     11 *
     12 * Unless required by applicable law or agreed to in writing, software
     13 * distributed under the License is distributed on an "AS IS" BASIS,
     14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     15 * See the License for the specific language governing permissions and
     16 * limitations under the License.
     17 */
     18 
     19 'use strict';
     20 
     21 // MODULES //
     22 
     23 var isCollection = require( './../../is-collection' );
     24 var isInteger = require( './../../is-integer' ).isPrimitive;
     25 var isString = require( './../../is-string' ).isPrimitive;
     26 var isnan = require( './../../is-nan' ).isPrimitive;
     27 
     28 
     29 // MAIN //
     30 
     31 /**
     32 * Tests if an array-like value contains a search value.
     33 *
     34 * @param {(Collection|string)} val - input value
     35 * @param {*} searchValue - search value
     36 * @param {integer} [position=0] - position at which to start searching for `searchValue`
     37 * @throws {TypeError} first argument must be array-like
     38 * @throws {Error} must provide a search value
     39 * @throws {TypeError} second argument must be a primitive string primitive when the first argument is a string
     40 * @throws {TypeError} third argument must be an integer
     41 * @returns {boolean} boolean indicating whether one value contains another
     42 *
     43 * @example
     44 * var bool = contains( 'last man standing', 'stand' );
     45 * // returns true
     46 *
     47 * @example
     48 * var bool = contains( [ 1, 2, 3, 4 ], 2 );
     49 * // returns true
     50 *
     51 * @example
     52 * var bool = contains( 'presidential election', 'president' );
     53 * // returns true
     54 *
     55 * @example
     56 * var bool = contains( [ NaN, 2, 3, 4 ], NaN );
     57 * // returns true
     58 *
     59 * @example
     60 * var bool = contains( 'javaScript', 'js' );
     61 * // returns false
     62 *
     63 * @example
     64 * var bool = contains( [ 1, 2, 3, {} ], {} );
     65 * // returns false
     66 *
     67 * @example
     68 * var bool = contains( 'Hidden Treasures', '' );
     69 * // returns true
     70 */
     71 function contains( val, searchValue, position ) {
     72 	var len;
     73 	var pos;
     74 	var i;
     75 	if ( !isCollection( val ) && !isString( val ) ) {
     76 		throw new TypeError( 'invalid argument. First argument must be array-like. Value: `' + val + '`.' );
     77 	}
     78 	if ( arguments.length < 2 ) {
     79 		throw new Error( 'insufficient input arguments. Must provide a search value.' );
     80 	}
     81 	if ( arguments.length > 2 ) {
     82 		if ( !isInteger( position ) ) {
     83 			throw new TypeError( 'invalid argument. Third argument must be an integer. Value: `' + position + '`.' );
     84 		}
     85 		pos = position;
     86 		if ( pos < 0 ) {
     87 			pos = 0;
     88 		}
     89 	} else {
     90 		pos = 0;
     91 	}
     92 	if ( isString( val ) ) {
     93 		if ( !isString( searchValue ) ) {
     94 			throw new TypeError( 'invalid argument. Second argument must be a string primitive. Value: `' + searchValue + '`.' );
     95 		}
     96 		return val.indexOf( searchValue, pos ) !== -1;
     97 	}
     98 	len = val.length;
     99 	if ( isnan( searchValue ) ) {
    100 		for ( i = pos; i < len; i++ ) {
    101 			if ( isnan( val[ i ] ) ) {
    102 				return true;
    103 			}
    104 		}
    105 		return false;
    106 	}
    107 	for ( i = pos; i < len; i++ ) {
    108 		if ( val[ i ] === searchValue ) {
    109 			return true;
    110 		}
    111 	}
    112 	return false;
    113 }
    114 
    115 
    116 // EXPORTS //
    117 
    118 module.exports = contains;