ecmason
TypeScript icon, indicating that this package has built-in type declarations

0.3.1 • Public • Published

ecmason

JSON parse/stringify for modern ES objects

NPM JavaScript Style Guide

Install

npm install --save ecmason
yarn add ecmason

Usage

import { parse, stringify, setup } from 'ecmason';

// Call setup first
setup();

// Support for Map
const a = new Map([
  // Support for Dates
  ['hello', new Date()],
  // Support for Regex
  ['world', /Hello World/],
]);

// Support for plain objects
const object = {
  a,
  // Support for Set
  b: new Set([
    // Support for NaN
    NaN,
    // Support for undefined
    undefined,
    // Support for infinity
    Infinity,
    -Infinity,
  ]),
  c: [
    // Support for signed zeroes
    +0,
    -0,
  ],
};

// Support for recursion
a.set('recursive', object);

const value = ecmason.stringify(object);

// Output:
// {
//   "tag": "RECURSIVE(OBJECT)",
//   "value": {
//     "id": 1,
//     "value": {
//       "a": {
//         "tag": "RECURSIVE(MAP)",
//         "value": {
//           "id": 2,
//           "value": [
//             [{
//               "tag": "PRIMITIVE",
//               "value": "hello"
//             }, {
//               "tag": "DATE",
//               "value": "2021-04-23T17:34:08.630Z"
//             }],
//             [{
//               "tag": "PRIMITIVE",
//               "value": "world"
//             }, {
//               "tag": "REGEXP",
//               "value": ["Hello World", ""]
//             }],
//             [{
//               "tag": "PRIMITIVE",
//               "value": "recursive"
//             }, {
//               "tag": "RECURSIVE(OBJECT)",
//               "value": 1
//             }]
//           ]
//         }
//       },
//       "b": {
//         "tag": "RECURSIVE(SET)",
//         "value": {
//           "id": 3,
//           "value": [{
//             "tag": "NAN",
//             "value": null
//           }, {
//             "tag": "UNDEFINED",
//             "value": null
//           }, {
//             "tag": "INF",
//             "value": null
//           }, {
//             "tag": "-INF",
//             "value": null
//           }]
//         }
//       },
//       "c": {
//         "tag": "RECURSIVE(ARRAY)",
//         "value": {
//           "id": 4,
//           "value": [{
//             "tag": "+0",
//             "value": 0
//           }, {
//             "tag": "-0",
//             "value": 0
//           }]
//         }
//       }
//     }
//   }
// }
console.log(value);

// Output (Node)
// <ref *1> {
//   a: Map(3) {
//     'hello' => 2021-04-23T17:34:08.630Z,
//     'world' => /Hello World/,
//     'recursive' => [Circular *1]
//   },
//   b: Set(4) { NaN, undefined, Infinity, -Infinity },
//   c: [ 0, -0 ]
// }
console.log(ecmason.parse(value));

Features

ECMAScript globals

The following are values and classes supported by ecmason, ordered from highest priority:

  • Literal Transformers
    • NaN
    • Number.POSITIVE_INFINITY or Infinity
    • Number.NEGATIVE_INFINITY or -Infinity
    • -0
  • Primitive Transformers
    • BigInt
    • Symbol (disabled; needs ES2019)
    • number, string, boolean and null
    • undefined
  • Object Transformers
    • RegExp
    • Date
    • Map
    • Set
    • Array
  • Final Transformers
    • Object

Custom transformers

To be documented

Recursive references

Recursive references in Map, Set, Array and Object are handled as well.

Cons

To preserve data safety-ness, the output result of stringify is verbose, which is done in an AST-like implementation, which allows us to preserve contextual data. Plain parsing will be inconveniently make the data inaccessible.

License

MIT © lxsmnsyc

Readme

Keywords

Package Sidebar

Install

npm i ecmason

Weekly Downloads

1

Version

0.3.1

License

MIT

Unpacked Size

127 kB

Total Files

17

Last publish

Collaborators

  • lxsmnsyc