-
Notifications
You must be signed in to change notification settings - Fork 2
/
endsWith.ts
44 lines (40 loc) · 1.08 KB
/
endsWith.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import baseClamp from "./.internal/baseClamp";
import baseToString from "./.internal/baseToString";
import toInteger from "./toInteger";
import toString from "./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
* ```
*/
export function endsWith(str: string, target: string, position = str.length): boolean {
const string = toString(str);
target = baseToString(target);
const length = string.length;
position = position === undefined
? length
: baseClamp(toInteger(position), 0, length);
const end = position;
position -= target.length;
return position >= 0 && string.slice(position, end) == target;
}
export default endsWith;