UNPKG

es-toolkit

Version:

A state-of-the-art, high-performance JavaScript utility library with a small bundle size and strong type annotations.

28 lines (27 loc) 803 B
//#region src/compat/util/toString.ts /** * Converts `value` to a string. * * An empty string is returned for `null` and `undefined` values. * The sign of `-0` is preserved. * * @param value - The value to convert. * @returns Returns the converted string. * * @example * toString(null) // returns '' * toString(undefined) // returns '' * toString(-0) // returns '-0' * toString([1, 2, -0]) // returns '1,2,-0' * toString([Symbol('a'), Symbol('b')]) // returns 'Symbol(a),Symbol(b)' */ function toString(value) { if (value == null) return ""; if (typeof value === "string") return value; if (Array.isArray(value)) return value.map(toString).join(","); const result = String(value); if (result === "0" && Object.is(Number(value), -0)) return "-0"; return result; } //#endregion export { toString };