1 var charsEndIndex = require('./_charsEndIndex'),
2 charsStartIndex = require('./_charsStartIndex'),
3 stringToArray = require('./_stringToArray'),
4 toString = require('./toString');
6 /** Used to match leading and trailing whitespace. */
7 var reTrim = /^\s+|\s+$/g;
10 * Removes leading and trailing whitespace or specified characters from `string`.
15 * @param {string} [string=''] The string to trim.
16 * @param {string} [chars=whitespace] The characters to trim.
17 * @param- {Object} [guard] Enables use as an iteratee for functions like `_.map`.
18 * @returns {string} Returns the trimmed string.
24 * _.trim('-_-abc-_-', '_-');
27 * _.map([' foo ', ' bar '], _.trim);
28 * // => ['foo', 'bar']
30 function trim(string, chars, guard) {
31 string = toString(string);
35 if (guard || chars === undefined) {
36 return string.replace(reTrim, '');
42 var strSymbols = stringToArray(string),
43 chrSymbols = stringToArray(chars);
45 return strSymbols.slice(charsStartIndex(strSymbols, chrSymbols), charsEndIndex(strSymbols, chrSymbols) + 1).join('');
48 module.exports = trim;