-
Notifications
You must be signed in to change notification settings - Fork 137
Expand file tree
/
Copy pathincludes.js
More file actions
36 lines (35 loc) · 1.16 KB
/
includes.js
File metadata and controls
36 lines (35 loc) · 1.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import clipNumber from 'helper/number/clip_number';
import coerceToString from 'helper/string/coerce_to_string';
import isNil from 'helper/object/is_nil';
import toInteger from 'helper/number/to_integer';
import toString from 'helper/string/to_string';
/**
* Checks whether `subject` includes `search` starting from `position`.
*
* @function includes
* @static
* @since 1.0.0
* @memberOf Query
* @param {string} [subject=''] The string where to search.
* @param {string} search The string to search.
* @param {number} [position=0] The position to start searching.
* @return {boolean} Returns `true` if `subject` includes `search` or `false` otherwise.
* @example
* v.includes('starship', 'star');
* // => true
*
* v.includes('galaxy', 'g', 1);
* // => false
*/
export default function includes(subject, search, position) {
const subjectString = coerceToString(subject);
const searchString = toString(search);
if (searchString === null) {
return false;
}
if (searchString === '') {
return true;
}
position = isNil(position) ? 0 : clipNumber(toInteger(position), 0, subjectString.length);
return subjectString.indexOf(searchString, position) !== -1;
}