@zerodep/to-string
TypeScript icon, indicating that this package has built-in type declarations

2.0.7 • Public • Published

@zerodep/to-string

version language types license

CodeFactor Known Vulnerabilities

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.

Examples

All @zerodep packages support both ESM and CJS.

import { toString } from '@zerodep/to-string';
// or
const { toString } = require('@zerodep/to-string');

Use Cases

// 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

Readme

Keywords

Package Sidebar

Install

npm i @zerodep/to-string

Homepage

zerodep.app

Weekly Downloads

0

Version

2.0.7

License

MIT

Unpacked Size

12.5 kB

Total Files

7

Last publish

Collaborators

  • cdepage