UNPKG

@technobuddha/library

Version:
60 lines (50 loc) 1.62 kB
import { empty } from './unicode.ts'; /** * Converts an unknown value to its string representation. * * - Returns an empty string if the value is `null` or `undefined`. * - Returns the value itself if it is a string. * - Converts booleans to `'true'` or `'false'`. * - Converts symbols and bigints using their respective `toString` methods. * - Returns a string representation for functions in the format `function <name>();`. * - Converts numbers using their `toString` method. * - For all other types, returns the result of `Object.prototype.toString`. * @param value - The value to convert to a string. * @returns The string representation of the input value. * @group Object * @category Conversion */ export function toString(value: unknown): string { if (value == null) { return empty; } if (typeof value === 'string') { return value; } if (typeof value === 'boolean') { return value ? 'true' : 'false'; } if (typeof value === 'symbol') { return value.toString(); } if (typeof value === 'bigint') { return value.toString(); } if (typeof value === 'function') { return `function ${value.name}();`; } if (typeof value === 'number') { return value.toString(); } if (typeof value === 'object') { if (Object.hasOwn(value, 'toString')) { // eslint-disable-next-line @typescript-eslint/no-base-to-string return value.toString(); } const proto = Object.getPrototypeOf(value); if (proto && 'toString' in proto) { return proto.toString.call(value); } } return Object.prototype.toString.call(value); }