mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-02-05 01:17:50 +00:00
wip: migrate to bun
This commit is contained in:
36
src/endsWith.ts
Normal file
36
src/endsWith.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Checks if `string` ends with the given target string.
|
||||
*
|
||||
* @since 3.0.0
|
||||
* @category String
|
||||
* @param {string} [string=''] The string to inspect.
|
||||
* @param {string} [target] The string to search for.
|
||||
* @param {number} [position=string.length] The position to search up to.
|
||||
* @returns {boolean} Returns `true` if `string` ends with `target`,
|
||||
* else `false`.
|
||||
* @see includes, startsWith
|
||||
* @example
|
||||
*
|
||||
* endsWith('abc', 'c')
|
||||
* // => true
|
||||
*
|
||||
* endsWith('abc', 'b')
|
||||
* // => false
|
||||
*
|
||||
* endsWith('abc', 'b', 2)
|
||||
* // => true
|
||||
*/
|
||||
function endsWith(string, target, position) {
|
||||
const { length } = string;
|
||||
position = position === undefined ? length : +position;
|
||||
if (position < 0 || position != position) {
|
||||
position = 0;
|
||||
} else if (position > length) {
|
||||
position = length;
|
||||
}
|
||||
const end = position;
|
||||
position -= target.length;
|
||||
return position >= 0 && string.slice(position, end) == target;
|
||||
}
|
||||
|
||||
export default endsWith;
|
||||
Reference in New Issue
Block a user