@newdash/newdash
Version:
javascript/typescript utility library
47 lines (46 loc) • 1.52 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.endsWith = void 0;
const baseClamp_1 = __importDefault(require("./.internal/baseClamp"));
const baseToString_1 = __importDefault(require("./.internal/baseToString"));
const toInteger_1 = __importDefault(require("./toInteger"));
const toString_1 = __importDefault(require("./toString"));
/**
* Checks if `string` ends with the given target string.
*
* @since 5.7.0
* @category String
* @param str The string to inspect.
* @param target The string to search for.
* @param position The position to search up to.
* @returns Returns `true` if `string` ends with `target`, else `false`.
* @see [[includes]],[[startsWith]]
* @example
*
* ```js
* endsWith('abc', 'c')
* // => true
*
* endsWith('abc', 'b')
* // => false
*
* endsWith('abc', 'b', 2)
* // => true
* ```
*/
function endsWith(str, target, position = str.length) {
const string = (0, toString_1.default)(str);
target = (0, baseToString_1.default)(target);
const length = string.length;
position = position === undefined
? length
: (0, baseClamp_1.default)((0, toInteger_1.default)(position), 0, length);
const end = position;
position -= target.length;
return position >= 0 && string.slice(position, end) == target;
}
exports.endsWith = endsWith;
exports.default = endsWith;