@iov/encoding
Advanced tools
Comparing version
@@ -7,2 +7,5 @@ "use strict"; | ||
const bn_js_1 = __importDefault(require("bn.js")); | ||
// Too large values lead to massive memory usage. Limit to something sensible. | ||
// The largest value we need is 18 (Ether). | ||
const maxFractionalDigits = 100; | ||
/** | ||
@@ -59,2 +62,5 @@ * A type for arbitrary precision, non-negative decimals. | ||
throw new Error("Fractional digits must not be negative"); | ||
if (fractionalDigits > maxFractionalDigits) { | ||
throw new Error(`Fractional digits must not exceed ${maxFractionalDigits}`); | ||
} | ||
} | ||
@@ -64,2 +70,5 @@ get atomics() { | ||
} | ||
get fractionalDigits() { | ||
return this.data.fractionalDigits; | ||
} | ||
constructor(atomics, fractionalDigits) { | ||
@@ -84,4 +93,25 @@ this.data = { | ||
} | ||
/** | ||
* Returns an approximation as a float type. Only use this if no | ||
* exact calculation is required. | ||
*/ | ||
toFloatApproximation() { | ||
const out = Number(this.toString()); | ||
if (Number.isNaN(out)) | ||
throw new Error("Conversion to number failed"); | ||
return out; | ||
} | ||
/** | ||
* a.plus(b) returns a+b. | ||
* | ||
* Both values need to have the same fractional digits. | ||
*/ | ||
plus(b) { | ||
if (this.fractionalDigits !== b.fractionalDigits) | ||
throw new Error("Fractional digits do not match"); | ||
const sum = this.data.atomics.add(new bn_js_1.default(b.atomics)); | ||
return new Decimal(sum.toString(), this.fractionalDigits); | ||
} | ||
} | ||
exports.Decimal = Decimal; | ||
//# sourceMappingURL=decimal.js.map |
@@ -16,3 +16,3 @@ "use strict"; | ||
for (let i = 0; i < bytes.length; ++i) { | ||
if (bytes[i] > 255 || bytes[i] < 0 || Number.isNaN(bytes[i])) { | ||
if (!Number.isInteger(bytes[i]) || bytes[i] > 255 || bytes[i] < 0) { | ||
throw new Error("Invalid value in byte. Found: " + bytes[i]); | ||
@@ -109,3 +109,3 @@ } | ||
for (let i = 0; i < bytes.length; ++i) { | ||
if (bytes[i] > 255 || bytes[i] < 0 || Number.isNaN(bytes[i])) { | ||
if (!Number.isInteger(bytes[i]) || bytes[i] > 255 || bytes[i] < 0) { | ||
throw new Error("Invalid value in byte. Found: " + bytes[i]); | ||
@@ -112,0 +112,0 @@ } |
{ | ||
"name": "@iov/encoding", | ||
"version": "0.17.7", | ||
"version": "0.17.8", | ||
"description": "Encoding helpers for IOV projects", | ||
@@ -48,3 +48,3 @@ "author": "IOV SAS <admin@iov.one>", | ||
}, | ||
"gitHead": "69743bbaf8b5aadb93f199b8aecc7a740253954e" | ||
"gitHead": "722c7ed3539806384a1b02c6d6777aadd9eeb6b4" | ||
} |
@@ -11,5 +11,17 @@ /** | ||
readonly atomics: string; | ||
readonly fractionalDigits: number; | ||
private readonly data; | ||
private constructor(); | ||
toString(): string; | ||
/** | ||
* Returns an approximation as a float type. Only use this if no | ||
* exact calculation is required. | ||
*/ | ||
toFloatApproximation(): number; | ||
/** | ||
* a.plus(b) returns a+b. | ||
* | ||
* Both values need to have the same fractional digits. | ||
*/ | ||
plus(b: Decimal): Decimal; | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
60519
3.74%782
5.68%