mirror of https://github.com/lukechilds/node.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
40 lines
1.0 KiB
40 lines
1.0 KiB
9 years ago
|
var baseClamp = require('./_baseClamp'),
|
||
|
toInteger = require('./toInteger'),
|
||
|
toString = require('./toString');
|
||
10 years ago
|
|
||
|
/**
|
||
|
* Checks if `string` ends with the given target string.
|
||
|
*
|
||
|
* @static
|
||
|
* @memberOf _
|
||
|
* @category String
|
||
|
* @param {string} [string=''] The string to search.
|
||
|
* @param {string} [target] The string to search for.
|
||
|
* @param {number} [position=string.length] The position to search from.
|
||
|
* @returns {boolean} Returns `true` if `string` ends with `target`, else `false`.
|
||
|
* @example
|
||
|
*
|
||
|
* _.endsWith('abc', 'c');
|
||
|
* // => true
|
||
|
*
|
||
|
* _.endsWith('abc', 'b');
|
||
|
* // => false
|
||
|
*
|
||
|
* _.endsWith('abc', 'b', 2);
|
||
|
* // => true
|
||
|
*/
|
||
|
function endsWith(string, target, position) {
|
||
9 years ago
|
string = toString(string);
|
||
|
target = typeof target == 'string' ? target : (target + '');
|
||
10 years ago
|
|
||
|
var length = string.length;
|
||
|
position = position === undefined
|
||
|
? length
|
||
9 years ago
|
: baseClamp(toInteger(position), 0, length);
|
||
10 years ago
|
|
||
|
position -= target.length;
|
||
|
return position >= 0 && string.indexOf(target, position) == position;
|
||
|
}
|
||
|
|
||
|
module.exports = endsWith;
|