Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
80742 views
1
var baseToString = require('../internal/baseToString');
2
3
/* Native method references for those with the same name as other `lodash` methods. */
4
var nativeMin = Math.min;
5
6
/**
7
* Checks if `string` ends with the given target string.
8
*
9
* @static
10
* @memberOf _
11
* @category String
12
* @param {string} [string=''] The string to search.
13
* @param {string} [target] The string to search for.
14
* @param {number} [position=string.length] The position to search from.
15
* @returns {boolean} Returns `true` if `string` ends with `target`, else `false`.
16
* @example
17
*
18
* _.endsWith('abc', 'c');
19
* // => true
20
*
21
* _.endsWith('abc', 'b');
22
* // => false
23
*
24
* _.endsWith('abc', 'b', 2);
25
* // => true
26
*/
27
function endsWith(string, target, position) {
28
string = baseToString(string);
29
target = (target + '');
30
31
var length = string.length;
32
position = position === undefined
33
? length
34
: nativeMin(position < 0 ? 0 : (+position || 0), length);
35
36
position -= target.length;
37
return position >= 0 && string.indexOf(target, position) == position;
38
}
39
40
module.exports = endsWith;
41
42