awesome-string
Version:
The ultimate JavaScript string library
39 lines (38 loc) • 1.27 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` starts with `start`.
*
* @function startsWith
* @static
* @since 1.0.0
* @memberOf Query
* @param {string} [subject=''] The string to verify.
* @param {string} start The starting string.
* @param {number} [position=0] The position to start searching.
* @return {boolean} Returns `true` if `subject` starts with `start` or `false` otherwise.
* @example
* as.startsWith('say hello to my little friend', 'say hello');
* // => true
*
* as.startsWith('tony', 'on', 1);
* // => true
*
* as.startsWith('the world is yours', 'world');
* // => false
*/
export default function startsWith(subject, start, position) {
const subjectString = coerceToString(subject);
const startString = toString(start);
if (startString === null) {
return false;
}
if (startString === '') {
return true;
}
position = isNil(position) ? 0 : clipNumber(toInteger(position), 0, subjectString.length);
return subjectString.substr(position, startString.length) === startString;
}