fix: edge case constructing from string '0' (#121)

* fix: edge case constructing from string '0'
This commit is contained in:
Nathan Nichols
2021-03-10 11:36:01 -06:00
parent 9bd9ebfc09
commit 53c72b7045
2 changed files with 35 additions and 0 deletions

View File

@@ -54,6 +54,10 @@ class UInt64 extends UInt {
} }
if (typeof val === "string") { if (typeof val === "string") {
if (val === "0") {
return UInt64.from(0);
}
if (!HEX_REGEX.test(val)) { if (!HEX_REGEX.test(val)) {
throw new Error(`${val} is not a valid hex-string`); throw new Error(`${val} is not a valid hex-string`);
} }

View File

@@ -1,6 +1,32 @@
const { coreTypes } = require("../dist/types"); const { coreTypes } = require("../dist/types");
const { UInt8, UInt64 } = coreTypes; const { UInt8, UInt64 } = coreTypes;
const { encode } = require("../dist");
const binary =
"11007222000300003700000000000000003800000000000000006280000000000000000000000000000000000000005553440000000000000000000000000000000000000000000000000166D5438D7EA4C680000000000000000000000000005553440000000000AE123A8556F3CF91154711376AFB0F894F832B3D67D5438D7EA4C680000000000000000000000000005553440000000000F51DFC2A09D62CBBA1DFBDD4691DAC96AD98B90F";
const json = {
Balance: {
currency: "USD",
issuer: "rrrrrrrrrrrrrrrrrrrrBZbvji",
value: "0",
},
Flags: 196608,
HighLimit: {
currency: "USD",
issuer: "rPMh7Pi9ct699iZUTWaytJUoHcJ7cgyziK",
value: "1000",
},
HighNode: "0",
LedgerEntryType: "RippleState",
LowLimit: {
currency: "USD",
issuer: "rG1QQv2nh2gr7RCZ1P8YYcBUKCCN633jCn",
value: "1000",
},
LowNode: "0",
};
test("compareToTests[0]", () => { test("compareToTests[0]", () => {
expect(UInt8.from(124).compareTo(UInt64.from(124))).toBe(0); expect(UInt8.from(124).compareTo(UInt64.from(124))).toBe(0);
}); });
@@ -33,6 +59,11 @@ test("compareToTest[7]", () => {
expect(UInt8.from(124).compareTo(13)).toBe(1); expect(UInt8.from(124).compareTo(13)).toBe(1);
}); });
test("UInt64 from string zero", () => {
expect(UInt64.from("0")).toEqual(UInt64.from(0));
expect(encode(json)).toEqual(binary);
});
test("valueOfTests", () => { test("valueOfTests", () => {
let val = UInt8.from(1); let val = UInt8.from(1);
val |= 0x2; val |= 0x2;