UNPKG

1.23 kBJavaScriptView Raw
1var baseToString = require('./_baseToString'),
2 castSlice = require('./_castSlice'),
3 charsEndIndex = require('./_charsEndIndex'),
4 stringToArray = require('./_stringToArray'),
5 toString = require('./toString');
6
7/** Used to match leading and trailing whitespace. */
8var reTrimEnd = /\s+$/;
9
10/**
11 * Removes trailing whitespace or specified characters from `string`.
12 *
13 * @static
14 * @memberOf _
15 * @since 4.0.0
16 * @category String
17 * @param {string} [string=''] The string to trim.
18 * @param {string} [chars=whitespace] The characters to trim.
19 * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
20 * @returns {string} Returns the trimmed string.
21 * @example
22 *
23 * _.trimEnd(' abc ');
24 * // => ' abc'
25 *
26 * _.trimEnd('-_-abc-_-', '_-');
27 * // => '-_-abc'
28 */
29function trimEnd(string, chars, guard) {
30 string = toString(string);
31 if (string && (guard || chars === undefined)) {
32 return string.replace(reTrimEnd, '');
33 }
34 if (!string || !(chars = baseToString(chars))) {
35 return string;
36 }
37 var strSymbols = stringToArray(string),
38 end = charsEndIndex(strSymbols, stringToArray(chars)) + 1;
39
40 return castSlice(strSymbols, 0, end).join('');
41}
42
43module.exports = trimEnd;