@newdash/newdash
Version:
javascript/typescript utility library
61 lines (60 loc) • 1.68 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toString = void 0;
const isSymbol_1 = __importDefault(require("./isSymbol"));
const isArray_1 = __importDefault(require("./isArray"));
const arrayMap_1 = __importDefault(require("./.internal/arrayMap"));
/**
* @ignore
* @private
*/
const symbolToString = Symbol?.prototype?.toString;
/**
* Used as references for various `Number` constants.
* @ignore
*/
const INFINITY = Infinity;
/**
* Converts `value` to a string. An empty string is returned for `null`
* and `undefined` values. The sign of `-0` is preserved.
*
* @since 5.5.0
* @category Lang
* @param value The value to convert.
* @returns Returns the converted string.
* @example
*
* ```js
* 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 ((0, isArray_1.default)(value)) {
// Recursively convert values (susceptible to call stack limits).
return `${(0, arrayMap_1.default)(value, toString)}`;
}
if ((0, isSymbol_1.default)(value)) {
return symbolToString ? symbolToString.call(value) : "";
}
const result = (`${value}`);
return (result == "0" && (1 / value) == -INFINITY) ? "-0" : result;
}
exports.toString = toString;
exports.default = toString;