awesome-string
Version:
The ultimate JavaScript string library
36 lines (35 loc) • 1.19 kB
JavaScript
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
* as.includes('starship', 'star');
* // => true
*
* as.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;
}