UNPKG

voca

Version:

The ultimate JavaScript string library

47 lines (40 loc) 1.32 kB
import { i as isNil } from './internal/is_nil.js'; import './is_string.js'; import { c as coerceToString } from './internal/coerce_to_string.js'; import { c as clipNumber, t as toInteger } from './internal/to_integer.js'; /** * Checks whether `subject` ends with `end`. * * @function endsWith * @static * @since 1.0.0 * @memberOf Query * @param {string} [subject=''] The string to verify. * @param {string} end The ending string. * @param {number} [position=subject.length] Search within `subject` as if the string were only `position` long. * @return {boolean} Returns `true` if `subject` ends with `end` or `false` otherwise. * @example * v.endsWith('red alert', 'alert'); * // => true * * v.endsWith('metro south', 'metro'); * // => false * * v.endsWith('Murphy', 'ph', 5); * // => true */ function endsWith(subject, end, position) { if (isNil(end)) { return false; } var subjectString = coerceToString(subject); var endString = coerceToString(end); if (endString === '') { return true; } position = isNil(position) ? subjectString.length : clipNumber(toInteger(position), 0, subjectString.length); position -= endString.length; var lastIndex = subjectString.indexOf(endString, position); return lastIndex !== -1 && lastIndex === position; } export default endsWith;