mirror of
https://github.com/whoisclebs/lodash.git
synced 2026-01-29 14:37:49 +00:00
49 lines
1.2 KiB
JavaScript
49 lines
1.2 KiB
JavaScript
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.
|
|
*
|
|
* @since 4.0.0
|
|
* @category Lang
|
|
* @param {*} value The value to convert.
|
|
* @returns {string} Returns the converted string.
|
|
* @example
|
|
*
|
|
* toString(null)
|
|
* // => ''
|
|
*
|
|
* toString(-0)
|
|
* // => '-0'
|
|
*
|
|
* toString([1, 2, 3])
|
|
* // => '1,2,3'
|
|
*/
|
|
function toString(value) {
|
|
if (value == null) {
|
|
return ''
|
|
}
|
|
// Exit early for strings to avoid a performance hit in some environments.
|
|
if (typeof value == 'string') {
|
|
return value
|
|
}
|
|
if (Array.isArray(value)) {
|
|
// Recursively convert values (susceptible to call stack limits).
|
|
return `${map(value, (other) => other == null ? other : toString(other))}`
|
|
}
|
|
if (isSymbol(value)) {
|
|
return symbolToString ? symbolToString.call(value) : ''
|
|
}
|
|
const result = `${value}`
|
|
return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result
|
|
}
|
|
|
|
export default toString
|