voca
Version:
The ultimate JavaScript string library
46 lines (39 loc) • 1.31 kB
JavaScript
import { i as isNil } from './internal/is_nil.js';
import './is_string.js';
import { c as coerceToString } from './internal/coerce_to_string.js';
import { t as toString } from './internal/to_string.js';
import { c as clipNumber, t as toInteger } from './internal/to_integer.js';
/**
* 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
* v.startsWith('say hello to my little friend', 'say hello');
* // => true
*
* v.startsWith('tony', 'on', 1);
* // => true
*
* v.startsWith('the world is yours', 'world');
* // => false
*/
function startsWith(subject, start, position) {
var subjectString = coerceToString(subject);
var 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;
}
export default startsWith;