refactor: stricter check for non-negative integers
This commit is contained in:
parent
6f22e00488
commit
048169fd3f
@ -32,7 +32,7 @@ describe("Decimal", () => {
|
||||
expect(Decimal.fromAtomics("44", 4).toString()).toEqual("0.0044");
|
||||
});
|
||||
|
||||
it("throws for atomics that are not non-negative", () => {
|
||||
it("throws for atomics that are not non-negative integers", () => {
|
||||
expect(() => Decimal.fromAtomics("-1", 0)).toThrowError(/atomics must not be negative/i);
|
||||
});
|
||||
});
|
||||
|
||||
@ -107,14 +107,14 @@ export class Decimal {
|
||||
};
|
||||
|
||||
private constructor(atomics: string, fractionalDigits: number) {
|
||||
const _atomics = new BN(atomics);
|
||||
|
||||
if (_atomics.isNeg()) {
|
||||
throw new Error("Atomics must not be negative");
|
||||
if (!atomics.match(/^[0-9]+$/)) {
|
||||
throw new Error(
|
||||
"Invalid string format. Only non-negative integers in decimal representation suppored.",
|
||||
);
|
||||
}
|
||||
|
||||
this.data = {
|
||||
atomics: _atomics,
|
||||
atomics: new BN(atomics),
|
||||
fractionalDigits: fractionalDigits,
|
||||
};
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user