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

2.0.7 • Public • Published

@zerodep/to-pojo

version language types license

CodeFactor Known Vulnerabilities

A utility to convert serializable objects or arrays of serializable values or objects to a Plain Old Javascript Object (POJO); this will use native toJSON() 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 { toPojo } from '@zerodep/to-pojo';
// or
const { toPojo } = require('@zerodep/to-pojo');

Use Cases

toPojo({}); // {}
toPojo({ a: 'one', b: 'two' }); // { "a": 'one', "b": 'two' }

toPojo([]); // []
toPojo([1, 2, 3]); // [1, 2, 3]
toPojo(['a', 'b', 'c']); // ["a", "b", "c"]

// Sets are converted to arrays
toPojo(new Set()); // []
toPojo(new Set([1, 2, 3])); // [1, 2, 3]

// Maps are converted to nested arrays
toPojo(new Map()); // []
toPojo(new Map([['a', 1]])); // [["a", 1]]

toPojo(null); // null
toPojo(undefined); // null

toPojo({
  string: 'a string',
  date: new Date('2022-02-24'),
  int: 42,
  float: 3.14,
  bigInt: 8675309n,
  boolT: true,
  boolF: false,
});
// {
//   "string": "a string",
//   "date": "2022-02-24T00:00:00.000Z", <-- CAUTION: Dates are converted to ISO-8601 format
//   "int": 42,
//   "float": 3.14,
//   "bigInt": "8675309", <-- CAUTION: BigInts are converted to strings
//   "boolT": true,
//   "boolF": false,
// }

// invalid values
toPojo('a string'); // throws ZeroDepError: Cannot convert to JSON

Readme

Keywords

Package Sidebar

Install

npm i @zerodep/to-pojo

Homepage

zerodep.app

Weekly Downloads

1

Version

2.0.7

License

MIT

Unpacked Size

10.8 kB

Total Files

7

Last publish

Collaborators

  • cdepage