Skip to content

Commit

Permalink
null,NaN,undefiend will be treated as undedined
Browse files Browse the repository at this point in the history
  • Loading branch information
heliosnaut committed Sep 16, 2021
1 parent e540d47 commit c4694bf
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ Solve the problem of **big number** precision loss, using string representation.

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.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "json-enhancer",
"version": "1.0.3",
"version": "1.0.4",
"description": "Extension JavaScript native parser for ES new features.",
"main": "lib/json-enhancer.cjs",
"unpkg": "dist/json-enhancer.js",
Expand Down
12 changes: 11 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const Types = [
'regexp',
'set',
'symbol',
'undefined',
].reduce((acc, cur) => {
acc[cur] = `[@@EnhancerJSON/${cur.toUpperCase()}]`;
return acc;
Expand Down Expand Up @@ -63,6 +64,8 @@ export function decodeJSON(param) {
const result = JSON.parse(
_innerParam,
function (key, value) {
if (value === null) return undefined;

if (typeof value !== 'string') return value;

if (value.startsWith(Types.function)) {
Expand All @@ -89,6 +92,10 @@ export function decodeJSON(param) {
return Symbol(value.slice(Types.symbol.length));
}

if (value.startsWith(Types.undefined)) {
return undefined;
}

return value;
});

Expand All @@ -102,7 +109,10 @@ export function encodeJSON(p) {

return JSON.stringify(param, function (key, value) {
if (typeof value === 'function') return Types['function'] + value.toString();

if (
value === null ||
value === undefined ||
Number.isNaN(value)) return value = Types['undefined'] + 'undefined';
if (isISOString(value)) return Types['date'] + Date.parse(value);

if (typeof value === 'bigint') return String(value);
Expand Down
8 changes: 8 additions & 0 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ describe('combination:encodeJSON + decodeJSON', () => {
).toEqual(targetObj);
});

test('null, undefined, NaN are unified undefined', () => {
const arr = [null, NaN, undefined, 'null', 1, 'NaN', 'undefined'];

expect(
decodeJSON(encodeJSON(arr))
).toEqual([undefined, undefined, undefined, 'null', 1, 'NaN', 'undefined']);
});

test('Regular expression', () => {
const reg = new RegExp('ab+c', 'i');
const regCopy = decodeJSON(encodeJSON(reg));
Expand Down

0 comments on commit c4694bf

Please sign in to comment.