forked from lodash/lodash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtoString.js
44 lines (41 loc) · 1.05 KB
/
toString.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import isSymbol from './isSymbol.js'
/** Used as references for various `Number` constants. */
const INFINITY = 1 / 0
/**
* 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 `${value.map((other) => other == null ? other : toString(other))}`
}
if (isSymbol(value)) {
return value.toString()
}
const result = `${value}`
return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result
}
export default toString