Skip to content

Commit

Permalink
Fixed BigInt as Object
Browse files Browse the repository at this point in the history
  • Loading branch information
WebReflection committed Oct 30, 2021
1 parent 49942dd commit f8d539a
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions cjs/deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ const _deserialize = (index, $, _) => {
}
case BIGINT:
return as(BigInt(value));
case 'BigInt':
return as(Object(BigInt(value)));
}
return as(new env[type](value));
};
Expand Down
2 changes: 2 additions & 0 deletions cjs/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const _serialize = (value, $, _) => {
case 'Number':
case 'String':
return as([type, value.valueOf()]);
case 'BigInt':
return as([type, value.toString()]);
}

if (type.includes('Array'))
Expand Down
2 changes: 2 additions & 0 deletions esm/deserialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ const _deserialize = (index, $, _) => {
}
case BIGINT:
return as(BigInt(value));
case 'BigInt':
return as(Object(BigInt(value)));
}
return as(new env[type](value));
};
Expand Down
2 changes: 2 additions & 0 deletions esm/serialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ const _serialize = (value, $, _) => {
case 'Number':
case 'String':
return as([type, value.valueOf()]);
case 'BigInt':
return as([type, value.toString()]);
}

if (type.includes('Array'))
Expand Down
9 changes: 4 additions & 5 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const obj = {
Str: new String(''),
re: new RegExp('test', 'gim'),
error: new Error('test'),
BI: Object(1n),
date
};

Expand Down Expand Up @@ -52,11 +53,7 @@ function test(firstRun = false) {
console.timeEnd('serialized in');

assert(JSON.stringify(serialized), [
`[[2,[[1,2],[3,4],[5,6],[7,8],[9,10],[11,12],[13,14],[15,16],[17,18],[20,21],[23,24],[25,26],[27,28],[29,30],[31,32],[33,34]]]`,
`[0,"arr"],[1,[0,0,0]],[0,"bigint"],[8,"1"],[0,"boolean"],[0,true],[0,"number"],[0,123],[0,"string"],[0,""],[0,"undefined"]`,
`[0,null],[0,"null"],[0,null],[0,"int"],["Uint32Array",[1,2,3]],[0,"map"],[5,[[19,8]]],[0,"a"],[0,"set"],[6,[19,22]],[0,"b"]`,
`[0,"Bool"],["Boolean",false],[0,"Num"],["Number",0],[0,"Str"],["String",""],[0,"re"],[4,{"source":"test","flags":"gim"}]`,
`[0,"error"],[7,{"name":"Error","message":"test"}],[0,"date"],[3,"${date.toISOString()}"]]`
`[[2,[[1,2],[3,4],[5,6],[7,8],[9,10],[11,12],[13,14],[15,16],[17,18],[20,21],[23,24],[25,26],[27,28],[29,30],[31,32],[33,34],[35,36]]],[0,"arr"],[1,[0,0,0]],[0,"bigint"],[8,"1"],[0,"boolean"],[0,true],[0,"number"],[0,123],[0,"string"],[0,""],[0,"undefined"],[0,null],[0,"null"],[0,null],[0,"int"],["Uint32Array",[1,2,3]],[0,"map"],[5,[[19,8]]],[0,"a"],[0,"set"],[6,[19,22]],[0,"b"],[0,"Bool"],["Boolean",false],[0,"Num"],["Number",0],[0,"Str"],["String",""],[0,"re"],[4,{"source":"test","flags":"gim"}],[0,"error"],[7,{"name":"Error","message":"test"}],[0,"BI"],["BigInt","1"],[0,"date"],[3,"${date.toISOString()}"]]`
].join(','));

// firstRun && console.log(serialized);
Expand Down Expand Up @@ -95,6 +92,8 @@ function test(firstRun = false) {
assert(deserialized.re.flags, 'gim');
assert(deserialized.error instanceof Error, true);
assert(deserialized.error.message, 'test');
assert(deserialized.BI instanceof BigInt, true);
assert(deserialized.BI.valueOf(), 1n);
assert(deserialized.date instanceof Date, true);
assert(deserialized.date.toISOString(), date.toISOString());

Expand Down

0 comments on commit f8d539a

Please sign in to comment.