forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
endsWith.js
37 lines (36 loc) · 939 Bytes
/
endsWith.js
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
/**
* 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