1 var baseToString = require('./_baseToString'),
2 castSlice = require('./_castSlice'),
3 charsEndIndex = require('./_charsEndIndex'),
4 charsStartIndex = require('./_charsStartIndex'),
5 stringToArray = require('./_stringToArray'),
6 toString = require('./toString');
8 /** Used to match leading and trailing whitespace. */
9 var reTrim = /^\s+|\s+$/g;
12 * Removes leading and trailing whitespace or specified characters from `string`.
18 * @param {string} [string=''] The string to trim.
19 * @param {string} [chars=whitespace] The characters to trim.
20 * @param- {Object} [guard] Enables use as an iteratee for methods like `_.map`.
21 * @returns {string} Returns the trimmed string.
27 * _.trim('-_-abc-_-', '_-');
30 * _.map([' foo ', ' bar '], _.trim);
31 * // => ['foo', 'bar']
33 function trim(string, chars, guard) {
34 string = toString(string);
35 if (string && (guard || chars === undefined)) {
36 return string.replace(reTrim, '');
38 if (!string || !(chars = baseToString(chars))) {
41 var strSymbols = stringToArray(string),
42 chrSymbols = stringToArray(chars),
43 start = charsStartIndex(strSymbols, chrSymbols),
44 end = charsEndIndex(strSymbols, chrSymbols) + 1;
46 return castSlice(strSymbols, start, end).join('');
49 module.exports = trim;