1.1.0 • Published 3 years ago

json-enhancer v1.1.0

Weekly downloads
-
License
ISC
Repository
github
Last release
3 years ago

json-enhancer

Extension JavaScript native parser for ES new features.

Build Status Coverage Status Version npm bundle size dependencies-logo last-commit-date-logo

Installation

The json-enhancer package lives in npm. To install the latest stable version, run the following command:

npm i json-enhancer

Or if you're using yarn:

yarn add json-enhancer

Usage

import {decodeJSON, encodeJSON} from 'json-enhancer';

/* big number */
decodeJSON('9007199254740997')  //'9007199254740997'

/* date */
const d = new Date()
const dCopy = decodeJSON(encodeJSON(d));

dCopy.toISOString() === d.toISOString();  //true

/* Omit null, undefined, NaN */
const obj = {
    prop0:0,
    prop1:null,
    prop2:undefined,
    prop3:NaN,
};

decodeJSON(encodeJSON(obj)) //{prop0: 0}

/* Regular expression */
const reg = new RegExp('ab+c', 'i');
const regCopy = decodeJSON(encodeJSON(reg));

reg.toString() === regCopy.toString() //true
regCopy instanceof RegExp //true

/* bigint */
const b = 10n;
const obj = {b};

decodeJSON(encodeJSON(obj));  //{b: '10'}

/* Set */
const b = 10n;
const s = new Set([b,'a',1,'10',2,10,]);

//If the set contains bigInt, json-enhancer parses the value as a numeric string. If the set contains the same numeric string, only one value is retained.
decodeJSON(encodeJSON(s));  //Set(5) {'10', 'a', 1, 2, 10}


/* Map */
let myMap = new Map();

let keyObj = {};
let keyString = 'a string';
let keyNull = null;

myMap.set(keyString, "value associated with 'a string'");
myMap.set(keyObj, 'value associated with keyObj');
myMap.set(keyNull, 'value associated with keyNull');

const targetMap = decodeJSON(encodeJSON(myMap));
 
/*     
targetMap:
    Map(3) {
      'a string' => "value associated with 'a string'",
      {} => 'value associated with keyObj',
      undefined => 'value associated with keyNull'
    } 
*/

/* Symbol */
const sym = Symbol('foo');
const obj = {sym};

decodeJSON(encodeJSON(obj));  //{sym: Symbol(foo)}

Objective

Solve the problem of big number precision loss, using string representation.

Methods in an object are not lost after parsing.

Date objects are typed unchanged after parsing.

If the value of plain object is null / undefiend / NaN, it will be discarded.

null,undefiend,NaN will be treated as undefined.

Regular expression parsing is supported.

Bigint parsing is supported, using string representation.

Support Set and Map data type resolution.

Symbol is passed to json-enhancer as a value that will be redeclared after parsing.

Notice

WeakMap and WeakSet will not be handled.

Symbol as the key in the plain object will be omitted.