UNPKG

1.1 kBJavaScriptView Raw
1var baseClamp = require('./_baseClamp'),
2 baseToString = require('./_baseToString'),
3 toInteger = require('./toInteger'),
4 toString = require('./toString');
5
6/**
7 * Checks if `string` ends with the given target string.
8 *
9 * @static
10 * @memberOf _
11 * @since 3.0.0
12 * @category String
13 * @param {string} [string=''] The string to inspect.
14 * @param {string} [target] The string to search for.
15 * @param {number} [position=string.length] The position to search up to.
16 * @returns {boolean} Returns `true` if `string` ends with `target`,
17 * else `false`.
18 * @example
19 *
20 * _.endsWith('abc', 'c');
21 * // => true
22 *
23 * _.endsWith('abc', 'b');
24 * // => false
25 *
26 * _.endsWith('abc', 'b', 2);
27 * // => true
28 */
29function endsWith(string, target, position) {
30 string = toString(string);
31 target = baseToString(target);
32
33 var length = string.length;
34 position = position === undefined
35 ? length
36 : baseClamp(toInteger(position), 0, length);
37
38 var end = position;
39 position -= target.length;
40 return position >= 0 && string.slice(position, end) == target;
41}
42
43module.exports = endsWith;