Rework charsLeftIndex and charsRightIndex to pass tests.

This commit is contained in:
John-David Dalton
2015-09-14 23:45:18 -07:00
parent 925c489cf9
commit 55ef0f12cc

View File

@@ -806,39 +806,35 @@
} }
/** /**
* Used by `_.trim` and `_.trimLeft` to get the index of the first character * Used by `_.trim` and `_.trimLeft` to get the index of the first string symbol
* of `string` that is not found in `chars`. * that is not found in the character symbols.
* *
* @private * @private
* @param {string} string The string to inspect. * @param {Array} strSymbols The string symbols to inspect.
* @param {string} chars The characters to find. * @param {Array} chrSymbols The character symbols to find.
* @returns {number} Returns the index of the first character not found in `chars`. * @returns {number} Returns the index of the first unmatched string symbol.
*/ */
function charsLeftIndex(string, chars) { function charsLeftIndex(strSymbols, chrSymbols) {
var chrArray = chars.match(reStrSymbol), var index = -1,
strArray = string.match(reStrSymbol), length = strSymbols.length;
index = -1,
length = strArray.length;
while (++index < length && baseIndexOf(chrArray, strArray[index], 0) > -1) {} while (++index < length && baseIndexOf(chrSymbols, strSymbols[index], 0) > -1) {}
return index; return index;
} }
/** /**
* Used by `_.trim` and `_.trimRight` to get the index of the last character * Used by `_.trim` and `_.trimRight` to get the index of the last string symbol
* of `string` that is not found in `chars`. * that is not found in the character symbols.
* *
* @private * @private
* @param {string} string The string to inspect. * @param {Array} strSymbols The string symbols to inspect.
* @param {string} chars The characters to find. * @param {Array} chrSymbols The character symbols to find.
* @returns {number} Returns the index of the last character not found in `chars`. * @returns {number} Returns the index of the last unmatched string symbol.
*/ */
function charsRightIndex(string, chars) { function charsRightIndex(strSymbols, chrSymbols) {
var chrArray = chars.match(reStrSymbol), var index = strSymbols.length;
strArray = string.match(reStrSymbol),
index = strArray.length;
while (index-- && baseIndexOf(chrArray, strArray[index], 0) > -1) {} while (index-- && baseIndexOf(chrSymbols, strSymbols[index], 0) > -1) {}
return index; return index;
} }
@@ -10924,7 +10920,13 @@
return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1); return string.slice(trimmedLeftIndex(string), trimmedRightIndex(string) + 1);
} }
chars = (chars + ''); chars = (chars + '');
return chars ? string.slice(charsLeftIndex(string, chars), charsRightIndex(string, chars) + 1) : string; if (!chars) {
return string;
}
var strSymbols = string.match(reStrSymbol),
chrSymbols = chars.match(reStrSymbol);
return strSymbols.slice(charsLeftIndex(strSymbols, chrSymbols), charsRightIndex(strSymbols, chrSymbols) + 1).join('');
} }
/** /**
@@ -10954,7 +10956,11 @@
return string.slice(trimmedLeftIndex(string)); return string.slice(trimmedLeftIndex(string));
} }
chars = (chars + ''); chars = (chars + '');
return chars ? string.slice(charsLeftIndex(string, chars)) : string; if (!chars) {
return string;
}
var strSymbols = string.match(reStrSymbol);
return strSymbols.slice(charsLeftIndex(strSymbols, chars.match(reStrSymbol))).join('');
} }
/** /**
@@ -10984,7 +10990,11 @@
return string.slice(0, trimmedRightIndex(string) + 1); return string.slice(0, trimmedRightIndex(string) + 1);
} }
chars = (chars + ''); chars = (chars + '');
return chars ? string.slice(0, charsRightIndex(string, chars) + 1) : string; if (!chars) {
return string;
}
var strSymbols = string.match(reStrSymbol);
return strSymbols.slice(0, charsRightIndex(strSymbols, chars.match(reStrSymbol)) + 1).join('');
} }
/** /**