README.md (3452B)
1 <!-- 2 3 @license Apache-2.0 4 5 Copyright (c) 2018 The Stdlib Authors. 6 7 Licensed under the Apache License, Version 2.0 (the "License"); 8 you may not use this file except in compliance with the License. 9 You may obtain a copy of the License at 10 11 http://www.apache.org/licenses/LICENSE-2.0 12 13 Unless required by applicable law or agreed to in writing, software 14 distributed under the License is distributed on an "AS IS" BASIS, 15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 16 See the License for the specific language governing permissions and 17 limitations under the License. 18 19 --> 20 21 # Contains 22 23 > Test if an array-like value contains a search value. 24 25 <section class="intro"> 26 27 </section> 28 29 <!-- /.intro --> 30 31 <section class="usage"> 32 33 ## Usage 34 35 ```javascript 36 var contains = require( '@stdlib/assert/contains' ); 37 ``` 38 39 #### contains( val, searchValue\[, position] ) 40 41 Tests if `val` contains a search value. When `val` is a `string`, the function checks whether the characters of a search string are found in the input string. 42 43 ```javascript 44 var v = contains( 'Hello World', 'World' ); 45 // returns true 46 ``` 47 48 When `val` is an `array-like` object, but not a `string`, the function checks whether the input value contains an element strictly equal to the specified search value. 49 50 ```javascript 51 var arr = [ null, NaN, 2, 'abc', {} ]; 52 53 var v = contains( arr, NaN ); // NaNs are considered equal 54 // returns true 55 56 v = contains( arr, {} ); 57 // returns false 58 59 v = contains( arr, 'ab' ); 60 // returns false 61 ``` 62 63 Search is case-sensitive. 64 65 ```javascript 66 var v = contains( 'Hello World', 'world' ); 67 // returns false 68 ``` 69 70 To start searching at a specified index, provide a `position` argument. 71 72 ```javascript 73 var v = contains( 'Hello World', 'Hello', 6 ); 74 // returns false 75 76 v = contains( [ true, NaN, false ], true, 1 ); 77 // returns false 78 ``` 79 80 If not provided an `array-like` object, the function throws an error. 81 82 <!-- run throws: true --> 83 84 ```javascript 85 var v = contains( false, 'abc' ); 86 // throws <TypeError> 87 ``` 88 89 If not provided an integer-valued `position` argument, the function throws an error. 90 91 <!-- run throws: true --> 92 93 ```javascript 94 var v = contains( 'hello', 'e', 2.5 ); 95 // throws <TypeError> 96 ``` 97 98 </section> 99 100 <!-- /.usage --> 101 102 <section class="notes"> 103 104 * * * 105 106 ## Notes 107 108 - For `strings`, the function is modeled after [String.prototype.includes][mdn-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. 109 - The function does **not** distinguish between positive and negative zero. 110 - If `position < 0`, the search is performed for the entire input array or string. 111 112 </section> 113 114 <!-- /.notes --> 115 116 <section class="examples"> 117 118 ## Examples 119 120 <!-- eslint no-undef: "error" --> 121 122 ```javascript 123 var contains = require( '@stdlib/assert/contains' ); 124 125 var bool = contains( 'last man standing', 'stand' ); 126 // returns true 127 128 bool = contains( [ 1, 2, 3, 4 ], 2 ); 129 // returns true 130 131 bool = contains( 'presidential election', 'president' ); 132 // returns true 133 134 bool = contains( [ NaN, 2, 3, 4 ], NaN ); 135 // returns true 136 137 bool = contains( 'javaScript', 'js' ); 138 // returns false 139 140 bool = contains( 'Hidden Treasures', '' ); 141 // returns true 142 ``` 143 144 </section> 145 146 <!-- /.examples --> 147 148 <section class="links"> 149 150 [mdn-includes]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/includes 151 152 </section> 153 154 <!-- /.links -->