bignumberify
Advanced tools
Comparing version 0.0.3 to 0.0.4
29
index.js
const ethers = require("ethers"); | ||
function bigNumberify(obj) { | ||
function convert(bn) { | ||
try { | ||
bn = ethers.BigNumber.from(bn.hex); | ||
} catch(e) { | ||
// if it looks like a BigNumber but it is not | ||
} | ||
return bn; | ||
} | ||
function bigNumberify(key, value) { | ||
if (typeof key === 'object') { | ||
return scan(key) | ||
} else { | ||
return (typeof value === "object" && value.type === "BigNumber" && !!value.hex ? convert(value) : value); | ||
} | ||
} | ||
function scan(obj) { | ||
const isObject = (o) => { | ||
@@ -10,10 +27,2 @@ return typeof o === "object" && o !== null && !!Object.keys(o).length; | ||
}; | ||
const convert = (bn) => { | ||
try { | ||
bn = ethers.BigNumber.from(bn.hex); | ||
} catch(e) { | ||
// if it looks like a BigNumber but it is not | ||
} | ||
return bn; | ||
} | ||
const manage = (item, i) => { | ||
@@ -47,2 +56,4 @@ if (isObject(item[i])) { | ||
module.exports = bigNumberify; |
{ | ||
"name": "bignumberify", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"description": "A parser to correctly recreate BigNumber items in a JSON", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/sullof/bignumberify#readme", |
@@ -36,11 +36,18 @@ # bigNumberify | ||
Use as | ||
To parse an object | ||
``` | ||
const obj = bigNumberify(JSON.parse(jsonStr)); | ||
const obj = bigNumberify(inputObj); | ||
``` | ||
or as a reviver function | ||
``` | ||
const obj = JSON.parse(jsonStr, bigNumberify); | ||
``` | ||
## History | ||
**0.0.4** | ||
- Can now be used as a reviver function | ||
**0.0.3** | ||
- Ignore object that look like a BigNumber but they are not | ||
- Ignore objects that look like a BigNumber, but they are not | ||
@@ -53,2 +60,6 @@ **0.0.2** | ||
## TODO | ||
- Add support for other formats | ||
## License | ||
@@ -55,0 +66,0 @@ |
@@ -72,2 +72,15 @@ const { assert } = require("chai"); | ||
}); | ||
it("should recover if used as a reviver function", async function () { | ||
let jsonStr = JSON.stringify(obj); | ||
let good = JSON.parse(jsonStr, bigNumberify); | ||
assert.isTrue(ethers.BigNumber.isBigNumber(good.h)); | ||
assert.equal(good.b.c.toString(), "10"); | ||
assert.equal(good.e[0].toString(), "20"); | ||
assert.equal(good.e[1].f.toString(), "30"); | ||
assert.equal(good.e[1].g[5].toString(), "40"); | ||
assert.equal(good.h.toString(), "50"); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16051
121
70
13