Expose _.toString.

This commit is contained in:
John-David Dalton
2015-09-25 19:37:12 -07:00
parent 9c35db38a0
commit 8e207196d3
2 changed files with 44 additions and 44 deletions

View File

@@ -112,9 +112,6 @@
var reRegExpChar = /[\\^$.*+?()[\]{}|]/g, var reRegExpChar = /[\\^$.*+?()[\]{}|]/g,
reHasRegExpChar = RegExp(reRegExpChar.source); reHasRegExpChar = RegExp(reRegExpChar.source);
/** Used to match [combining diacritical marks](https://en.wikipedia.org/wiki/Combining_Diacritical_Marks). */
var reComboMark = /[\u0300-\u036f\ufe20-\ufe23]/g;
/** Used to match backslashes in property paths. */ /** Used to match backslashes in property paths. */
var reEscapeChar = /\\(\\)?/g; var reEscapeChar = /\\(\\)?/g;
@@ -792,22 +789,6 @@
return result; return result;
} }
/**
* Converts `value` to a string if it's not one.
* An empty string is returned for `null` and `undefined` values.
*
* @private
* @param {*} value The value to process.
* @returns {string} Returns the string.
*/
function baseToString(value) {
// Exit early for strings to avoid a performance hit in some environments.
if (typeof value == 'string') {
return value;
}
return value == null ? '' : (value + '');
}
/** /**
* The base implementation of `_.values` and `_.valuesIn` which creates an * The base implementation of `_.values` and `_.valuesIn` which creates an
* array of `object` property values corresponding to the property names * array of `object` property values corresponding to the property names
@@ -1468,8 +1449,8 @@
* `reduceRight`, `repeat`, `result`, `round`, `runInContext`, `shift`, `size`, * `reduceRight`, `repeat`, `result`, `round`, `runInContext`, `shift`, `size`,
* `snakeCase`, `some`, `sortedIndex`, `sortedIndexBy`, `sortedLastIndex`, * `snakeCase`, `some`, `sortedIndex`, `sortedIndexBy`, `sortedLastIndex`,
* `sortedLastIndexBy`, `startCase`, `startsWith`, `sum`, `sumBy`, `template`, * `sortedLastIndexBy`, `startCase`, `startsWith`, `sum`, `sumBy`, `template`,
* `toInteger`, `trim`, `trimLeft`, `trimRight`, `trunc`, `unescape`, `uniqueId`, * `toInteger`, `toString`, `trim`, `trimLeft`, `trimRight`, `trunc`, `unescape`,
* `value`, and `words` * `uniqueId`, `value`, and `words`
* *
* The wrapper method `sample` will return a wrapped value when `n` is provided, * The wrapper method `sample` will return a wrapped value when `n` is provided,
* otherwise an unwrapped value is returned. * otherwise an unwrapped value is returned.
@@ -4517,7 +4498,7 @@
*/ */
function stringToPath(string) { function stringToPath(string) {
var result = []; var result = [];
baseToString(string).replace(rePropName, function(match, number, quote, string) { toString(string).replace(rePropName, function(match, number, quote, string) {
result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match)); result.push(quote ? string.replace(reEscapeChar, '$1') : (number || match));
}); });
return result; return result;
@@ -6215,7 +6196,7 @@
* // => '1,2,3' * // => '1,2,3'
*/ */
function wrapperToString() { function wrapperToString() {
return (this.value() + ''); return toString(this.value());
} }
/** /**
@@ -9221,6 +9202,24 @@
return copyObject(value, keysIn(value)); return copyObject(value, keysIn(value));
} }
/**
* Converts `value` to a string if it's not one.
* An empty string is returned for `null` and `undefined` values.
*
* @static
* @memberOf _
* @category Lang
* @param {*} value The value to process.
* @returns {string} Returns the string.
*/
function toString(value) {
// Exit early for strings to avoid a performance hit in some environments.
if (typeof value == 'string') {
return value;
}
return value == null ? '' : (value + '');
}
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
/** /**
@@ -10477,7 +10476,7 @@
* // => 'Fred' * // => 'Fred'
*/ */
function capitalize(string) { function capitalize(string) {
string = baseToString(string); string = toString(string);
if (!string) { if (!string) {
return string; return string;
} }
@@ -10503,7 +10502,7 @@
* // => 'deja vu' * // => 'deja vu'
*/ */
function deburr(string) { function deburr(string) {
string = baseToString(string); string = toString(string);
return string && string.replace(reLatin1, deburrLetter).replace(reComboMark, ''); return string && string.replace(reLatin1, deburrLetter).replace(reComboMark, '');
} }
@@ -10529,7 +10528,7 @@
* // => true * // => true
*/ */
function endsWith(string, target, position) { function endsWith(string, target, position) {
string = baseToString(string); string = toString(string);
target = typeof target == 'string' ? target : (target + ''); target = typeof target == 'string' ? target : (target + '');
var length = string.length; var length = string.length;
@@ -10574,7 +10573,7 @@
* // => 'fred, barney, & pebbles' * // => 'fred, barney, & pebbles'
*/ */
function escape(string) { function escape(string) {
string = baseToString(string); string = toString(string);
return (string && reHasUnescapedHtml.test(string)) return (string && reHasUnescapedHtml.test(string))
? string.replace(reUnescapedHtml, escapeHtmlChar) ? string.replace(reUnescapedHtml, escapeHtmlChar)
: string; : string;
@@ -10595,7 +10594,7 @@
* // => '\[lodash\]\(https://lodash\.com/\)' * // => '\[lodash\]\(https://lodash\.com/\)'
*/ */
function escapeRegExp(string) { function escapeRegExp(string) {
string = baseToString(string); string = toString(string);
return (string && reHasRegExpChar.test(string)) return (string && reHasRegExpChar.test(string))
? string.replace(reRegExpChar, '\\$&') ? string.replace(reRegExpChar, '\\$&')
: string; : string;
@@ -10647,7 +10646,7 @@
* // => 'abc' * // => 'abc'
*/ */
function pad(string, length, chars) { function pad(string, length, chars) {
string = baseToString(string); string = toString(string);
length = toInteger(length); length = toInteger(length);
var strLength = stringSize(string); var strLength = stringSize(string);
@@ -10684,7 +10683,7 @@
* // => 'abc' * // => 'abc'
*/ */
function padLeft(string, length, chars) { function padLeft(string, length, chars) {
string = baseToString(string); string = toString(string);
return createPadding(string, length, chars) + string; return createPadding(string, length, chars) + string;
} }
@@ -10711,7 +10710,7 @@
* // => 'abc' * // => 'abc'
*/ */
function padRight(string, length, chars) { function padRight(string, length, chars) {
string = baseToString(string); string = toString(string);
return string + createPadding(string, length, chars); return string + createPadding(string, length, chars);
} }
@@ -10771,7 +10770,7 @@
* // => '' * // => ''
*/ */
function repeat(string, n) { function repeat(string, n) {
string = baseToString(string); string = toString(string);
n = toInteger(n); n = toInteger(n);
var result = ''; var result = '';
@@ -10859,7 +10858,7 @@
* // => true * // => true
*/ */
function startsWith(string, target, position) { function startsWith(string, target, position) {
string = baseToString(string); string = toString(string);
position = nativeMin(nativeMax(toInteger(position), 0), string.length); position = nativeMin(nativeMax(toInteger(position), 0), string.length);
return string.lastIndexOf(target, position) == position; return string.lastIndexOf(target, position) == position;
} }
@@ -10968,7 +10967,7 @@
if (otherOptions && isIterateeCall(string, options, otherOptions)) { if (otherOptions && isIterateeCall(string, options, otherOptions)) {
options = otherOptions = undefined; options = otherOptions = undefined;
} }
string = baseToString(string); string = toString(string);
options = extendWith({}, otherOptions || options, settings, extendDefaults); options = extendWith({}, otherOptions || options, settings, extendDefaults);
var imports = extendWith({}, options.imports, settings.imports, extendDefaults), var imports = extendWith({}, options.imports, settings.imports, extendDefaults),
@@ -11088,7 +11087,7 @@
* // => ['foo', 'bar'] * // => ['foo', 'bar']
*/ */
function trim(string, chars, guard) { function trim(string, chars, guard) {
string = baseToString(string); string = toString(string);
if (!string) { if (!string) {
return string; return string;
} }
@@ -11124,7 +11123,7 @@
* // => 'abc-_-' * // => 'abc-_-'
*/ */
function trimLeft(string, chars, guard) { function trimLeft(string, chars, guard) {
string = baseToString(string); string = toString(string);
if (!string) { if (!string) {
return string; return string;
} }
@@ -11158,7 +11157,7 @@
* // => '-_-abc' * // => '-_-abc'
*/ */
function trimRight(string, chars, guard) { function trimRight(string, chars, guard) {
string = baseToString(string); string = toString(string);
if (!string) { if (!string) {
return string; return string;
} }
@@ -11216,9 +11215,9 @@
if (isObject(options)) { if (isObject(options)) {
var separator = 'separator' in options ? options.separator : separator; var separator = 'separator' in options ? options.separator : separator;
length = 'length' in options ? toInteger(options.length) : length; length = 'length' in options ? toInteger(options.length) : length;
omission = 'omission' in options ? baseToString(options.omission) : omission; omission = 'omission' in options ? toString(options.omission) : omission;
} }
string = baseToString(string); string = toString(string);
var strLength = string.length; var strLength = string.length;
if (reAdvSymbol.test(string)) { if (reAdvSymbol.test(string)) {
@@ -11248,7 +11247,7 @@
substring = result; substring = result;
if (!separator.global) { if (!separator.global) {
separator = RegExp(separator.source, baseToString(reFlags.exec(separator)) + 'g'); separator = RegExp(separator.source, toString(reFlags.exec(separator)) + 'g');
} }
separator.lastIndex = 0; separator.lastIndex = 0;
while ((match = separator.exec(substring))) { while ((match = separator.exec(substring))) {
@@ -11284,7 +11283,7 @@
* // => 'fred, barney, & pebbles' * // => 'fred, barney, & pebbles'
*/ */
function unescape(string) { function unescape(string) {
string = baseToString(string); string = toString(string);
return (string && reHasEscapedHtml.test(string)) return (string && reHasEscapedHtml.test(string))
? string.replace(reEscapedHtml, unescapeHtmlChar) ? string.replace(reEscapedHtml, unescapeHtmlChar)
: string; : string;
@@ -11309,7 +11308,7 @@
* // => ['fred', 'barney', '&', 'pebbles'] * // => ['fred', 'barney', '&', 'pebbles']
*/ */
function words(string, pattern, guard) { function words(string, pattern, guard) {
string = baseToString(string); string = toString(string);
pattern = guard ? undefined : guard; pattern = guard ? undefined : guard;
return string.match(pattern || reWord) || []; return string.match(pattern || reWord) || [];
} }
@@ -11833,7 +11832,7 @@
*/ */
function uniqueId(prefix) { function uniqueId(prefix) {
var id = ++idCounter; var id = ++idCounter;
return baseToString(prefix) + id; return toString(prefix) + id;
} }
/*------------------------------------------------------------------------*/ /*------------------------------------------------------------------------*/
@@ -12207,6 +12206,7 @@
lodash.toArray = toArray; lodash.toArray = toArray;
lodash.toPath = toPath; lodash.toPath = toPath;
lodash.toPlainObject = toPlainObject; lodash.toPlainObject = toPlainObject;
lodash.toString = toString;
lodash.transform = transform; lodash.transform = transform;
lodash.union = union; lodash.union = union;
lodash.uniq = uniq; lodash.uniq = uniq;

View File

@@ -20990,7 +20990,7 @@
var acceptFalsey = _.difference(allMethods, rejectFalsey); var acceptFalsey = _.difference(allMethods, rejectFalsey);
QUnit.test('should accept falsey arguments', function(assert) { QUnit.test('should accept falsey arguments', function(assert) {
assert.expect(240); assert.expect(241);
var emptyArrays = _.map(falsey, _.constant([])); var emptyArrays = _.map(falsey, _.constant([]));