mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 06:27:49 +00:00
Simplify toString (#4441)
* Assume Symbol exists in toString tests * Use native array map instead of custom one in toString * Assume Symbol.prototype.toString exists in toString
This commit is contained in:
committed by
John-David Dalton
parent
3ebb38d389
commit
abb54cc49a
@@ -28,24 +28,12 @@ describe('toString', function() {
|
||||
assert.deepStrictEqual(toString(values), '-0,-0,0,0');
|
||||
});
|
||||
|
||||
it('should not error on symbols', function() {
|
||||
if (Symbol) {
|
||||
try {
|
||||
assert.strictEqual(toString(symbol), 'Symbol(a)');
|
||||
} catch (e) {
|
||||
assert.ok(false, e.message);
|
||||
}
|
||||
}
|
||||
it('should handle symbols', function() {
|
||||
assert.strictEqual(toString(symbol), 'Symbol(a)');
|
||||
});
|
||||
|
||||
it('should not error on an array of symbols', function() {
|
||||
if (Symbol) {
|
||||
try {
|
||||
assert.strictEqual(toString([symbol]), 'Symbol(a)');
|
||||
} catch (e) {
|
||||
assert.ok(false, e.message);
|
||||
}
|
||||
}
|
||||
it('should handle an array of symbols', function() {
|
||||
assert.strictEqual(toString([symbol]), 'Symbol(a)');
|
||||
});
|
||||
|
||||
it('should return the `toString` result of the wrapped value', function() {
|
||||
|
||||
@@ -1,12 +1,8 @@
|
||||
import map from './map.js'
|
||||
import isSymbol from './isSymbol.js'
|
||||
|
||||
/** Used as references for various `Number` constants. */
|
||||
const INFINITY = 1 / 0
|
||||
|
||||
/** Used to convert symbols to primitives and strings. */
|
||||
const symbolToString = Symbol.prototype.toString
|
||||
|
||||
/**
|
||||
* Converts `value` to a string. An empty string is returned for `null`
|
||||
* and `undefined` values. The sign of `-0` is preserved.
|
||||
@@ -36,10 +32,10 @@ function toString(value) {
|
||||
}
|
||||
if (Array.isArray(value)) {
|
||||
// Recursively convert values (susceptible to call stack limits).
|
||||
return `${map(value, (other) => other == null ? other : toString(other))}`
|
||||
return `${value.map((other) => other == null ? other : toString(other))}`
|
||||
}
|
||||
if (isSymbol(value)) {
|
||||
return symbolToString ? symbolToString.call(value) : ''
|
||||
return value.toString()
|
||||
}
|
||||
const result = `${value}`
|
||||
return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result
|
||||
|
||||
Reference in New Issue
Block a user