refactor: stricter check for non-negative integers

This commit is contained in:
Tien Nguyen Khac 2022-06-25 23:13:09 +12:00
parent 6f22e00488
commit 048169fd3f
2 changed files with 6 additions and 6 deletions

View File

@ -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);
});
});

View File

@ -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,
};
}