A utility to convert stringifiable values to a string; this will use native toString()
capabilities, if available. Invalid values will cause a ZeroDepError
to be thrown.
Full documentation is available at the zerodep.app page.
All @zerodep packages support both ESM and CJS.
import { toString } from '@zerodep/to-string';
// or
const { toString } = require('@zerodep/to-string');
// booleans
toString(true); // "true"
toString(false); // "false"
// numbers
toString(42); // "42"
toString(3.14); // "3.14"
toString(100e10); // "1000000000000"
toString(Number.NaN); // "NaN"
toString(Number.POSITIVE_INFINITY); // "Infinity"
// bigint
toString(8675309n); // "8675309"
// strings
toString('Some string'); // "Some string"
// null + undefined
toString(null); // ""
toString(undefined); // ""
// objects
toString({}); // "{}";
toString({ a: 'a', b: 'b' }); // "{\"a\":\"a\",\"b\":\"b\"}"
// arrays
toString([]); // "[]"
toString(['a', 'b', 'c']); // "[\"a\",\"b\",\"c\"]"
// dates
toString(new Date('2022-02-24')); // "2022-02-24T00:00:00.000Z"
// Sets
toString(new Set()); // "[]"
toString(new Set([1, 2, 3])); // "[1, 2, 3]"
// Maps
toString(new Map()); // "{}"
toString(
new Map([
['a', 1],
['b', 2],
])
); // "{\"a\":1, \"b\":2 }
// invalid values
toString(new Promise()); // throws ZeroDepError: Cannot convert to JSON