UNPKG

1.18 kBJavaScriptView Raw
1import Symbol from './_Symbol';
2import isSymbol from './isSymbol';
3
4/** Used as references for various `Number` constants. */
5var INFINITY = 1 / 0;
6
7/** Used to convert symbols to primitives and strings. */
8var symbolProto = Symbol ? Symbol.prototype : undefined,
9 symbolToString = symbolProto ? symbolProto.toString : undefined;
10
11/**
12 * Converts `value` to a string if it's not one. An empty string is returned
13 * for `null` and `undefined` values. The sign of `-0` is preserved.
14 *
15 * @static
16 * @memberOf _
17 * @since 4.0.0
18 * @category Lang
19 * @param {*} value The value to process.
20 * @returns {string} Returns the string.
21 * @example
22 *
23 * _.toString(null);
24 * // => ''
25 *
26 * _.toString(-0);
27 * // => '-0'
28 *
29 * _.toString([1, 2, 3]);
30 * // => '1,2,3'
31 */
32function toString(value) {
33 // Exit early for strings to avoid a performance hit in some environments.
34 if (typeof value == 'string') {
35 return value;
36 }
37 if (value == null) {
38 return '';
39 }
40 if (isSymbol(value)) {
41 return symbolToString ? symbolToString.call(value) : '';
42 }
43 var result = (value + '');
44 return (result == '0' && (1 / value) == -INFINITY) ? '-0' : result;
45}
46
47export default toString;