Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions packages/math/src/integers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ export class Uint32 implements Integer, WithByteConverters {
*/
public static fromBytes(bytes: ArrayLike<number>, endianess: "be" | "le" = "be"): Uint32 {
if (bytes.length !== 4) {
throw new Error("Invalid input length. Expected 4 bytes.");
throw new Error(`Invalid input length. Expected 4 bytes, has ${bytes.length} bytes.`);
}

for (let i = 0; i < bytes.length; ++i) {
if (!Number.isInteger(bytes[i]) || bytes[i] > 255 || bytes[i] < 0) {
throw new Error("Invalid value in byte. Found: " + bytes[i]);
throw new Error(`Invalid value in byte. Found: '${bytes[i]}'`);
}
}

Expand All @@ -55,7 +55,7 @@ export class Uint32 implements Integer, WithByteConverters {

public static fromString(str: string): Uint32 {
if (!str.match(/^[0-9]+$/)) {
throw new Error("Invalid string format");
throw new Error(`Invalid string format. Found: '${str}'`);
}
return new Uint32(Number.parseInt(str, 10));
}
Expand All @@ -64,15 +64,15 @@ export class Uint32 implements Integer, WithByteConverters {

public constructor(input: number) {
if (Number.isNaN(input)) {
throw new Error("Input is not a number");
throw new Error(`Input is not a number. Found: '${input}'`);
}

if (!Number.isInteger(input)) {
throw new Error("Input is not an integer");
throw new Error(`Input is not an integer. Found: '${input}'`);
}

if (input < 0 || input > 4294967295) {
throw new Error("Input not in uint32 range: " + input.toString());
throw new Error(`Input not in uint32 range. Found: '${input.toString()}'`);
}

this.data = input;
Expand Down Expand Up @@ -116,7 +116,7 @@ export class Uint32 implements Integer, WithByteConverters {
export class Int53 implements Integer {
public static fromString(str: string): Int53 {
if (!str.match(/^-?[0-9]+$/)) {
throw new Error("Invalid string format");
throw new Error(`Invalid string format. Found: '${str}'`);
}

return new Int53(Number.parseInt(str, 10));
Expand All @@ -126,15 +126,15 @@ export class Int53 implements Integer {

public constructor(input: number) {
if (Number.isNaN(input)) {
throw new Error("Input is not a number");
throw new Error(`Input is not a number. Found: '${input}'`);
}

if (!Number.isInteger(input)) {
throw new Error("Input is not an integer");
throw new Error(`Input is not an integer. Found: '${input}'`);
}

if (input < Number.MIN_SAFE_INTEGER || input > Number.MAX_SAFE_INTEGER) {
throw new Error("Input not in int53 range: " + input.toString());
throw new Error(`Input not in int53 range. Found: '${input.toString()}'`);
}

this.data = input;
Expand Down Expand Up @@ -164,7 +164,7 @@ export class Uint53 implements Integer {
public constructor(input: number) {
const signed = new Int53(input);
if (signed.toNumber() < 0) {
throw new Error("Input is negative");
throw new Error(`Input is negative. Found: '${input}'`);
}
this.data = signed;
}
Expand Down Expand Up @@ -196,12 +196,12 @@ export class Uint64 implements Integer, WithByteConverters {
*/
public static fromBytes(bytes: ArrayLike<number>, endianess: "be" | "le" = "be"): Uint64 {
if (bytes.length !== 8) {
throw new Error("Invalid input length. Expected 8 bytes.");
throw new Error(`Invalid input length. Expected 8 bytes, has ${bytes.length} bytes.`);
}

for (let i = 0; i < bytes.length; ++i) {
if (!Number.isInteger(bytes[i]) || bytes[i] > 255 || bytes[i] < 0) {
throw new Error("Invalid value in byte. Found: " + bytes[i]);
throw new Error(`Invalid value in byte. Found: '${bytes[i]}'`);
}
}

Expand All @@ -211,25 +211,25 @@ export class Uint64 implements Integer, WithByteConverters {

public static fromString(str: string): Uint64 {
if (!str.match(/^[0-9]+$/)) {
throw new Error("Invalid string format");
throw new Error(`Invalid string format. Found: '${str}'`);
}
return new Uint64(new BN(str, 10, "be"));
}

public static fromNumber(input: number): Uint64 {
if (Number.isNaN(input)) {
throw new Error("Input is not a number");
throw new Error(`Input is not a number. Found: '${input}'`);
}

if (!Number.isInteger(input)) {
throw new Error("Input is not an integer");
throw new Error(`Input is not an integer. Found: '${input}'`);
}

let bigint: BN;
try {
bigint = new BN(input);
} catch {
throw new Error("Input is not a safe integer");
throw new Error(`Input is not a safe integer. Found: '${input}'`);
}
return new Uint64(bigint);
}
Expand All @@ -238,10 +238,10 @@ export class Uint64 implements Integer, WithByteConverters {

private constructor(data: BN) {
if (data.isNeg()) {
throw new Error("Input is negative");
throw new Error(`Input is negative. Found: '${data.toString(10)}'`);
}
if (data.gt(uint64MaxValue)) {
throw new Error("Input exceeds uint64 range");
throw new Error(`Input exceeds uint64 range. Found: '${data.toString(10)}'`);
}
this.data = data;
}
Expand Down