-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main-v2' into release/v2.8.23
- Loading branch information
Showing
5 changed files
with
92 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// ... existing imports ... | ||
|
||
import { isValidNumber } from "./numbers"; | ||
|
||
describe("isValidNumber", () => { | ||
it("should return true for valid numbers", () => { | ||
expect(isValidNumber(0)).toBe(true); | ||
expect(isValidNumber(1)).toBe(true); | ||
expect(isValidNumber(1.5)).toBe(true); | ||
expect(isValidNumber("0")).toBe(true); | ||
expect(isValidNumber("1")).toBe(true); | ||
expect(isValidNumber("1.5")).toBe(true); | ||
expect(isValidNumber("0x0")).toBe(true); | ||
expect(isValidNumber("0x1")).toBe(true); | ||
expect(isValidNumber("0xF")).toBe(true); | ||
expect(isValidNumber("0x1A")).toBe(true); | ||
}); | ||
|
||
it("should return false for invalid numbers", () => { | ||
expect(isValidNumber(-1)).toBe(false); | ||
expect(isValidNumber("-1")).toBe(false); | ||
expect(isValidNumber("abc")).toBe(false); | ||
expect(isValidNumber("")).toBe(false); | ||
expect(isValidNumber(null)).toBe(false); | ||
expect(isValidNumber(undefined)).toBe(false); | ||
expect(isValidNumber(NaN)).toBe(false); | ||
expect(isValidNumber(Infinity)).toBe(false); | ||
expect(isValidNumber({})).toBe(false); | ||
}); | ||
|
||
it("should handle large numbers correctly", () => { | ||
expect(isValidNumber(Number.MAX_SAFE_INTEGER)).toBe(true); | ||
expect(isValidNumber(Number.MAX_SAFE_INTEGER.toString())).toBe(true); | ||
expect(isValidNumber("0x" + Number.MAX_SAFE_INTEGER.toString(16))).toBe(true); | ||
}); | ||
|
||
it("should handle edge cases", () => { | ||
expect(isValidNumber("0x0")).toBe(true); | ||
expect(isValidNumber("0x00")).toBe(true); | ||
expect(isValidNumber("0x")).toBe(false); | ||
expect(isValidNumber("0xG")).toBe(false); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { getNumber, isHexString } from "ethers"; | ||
|
||
/** | ||
* Checks if a value is a valid number. | ||
* Handles both numeric and string inputs, including hex strings. | ||
* | ||
* @param {any} value - The value to check. | ||
* @returns {boolean} True if the value is a valid, non-negative, finite number; false otherwise. | ||
*/ | ||
export const isValidNumber = (value: any): boolean => { | ||
try { | ||
if (value === null || value === undefined || value === "") { | ||
return false; | ||
} | ||
|
||
// Handle hex strings | ||
if (typeof value === "string" && isHexString(value)) { | ||
value = getNumber(value); | ||
} | ||
|
||
const num = Number(value); | ||
return !isNaN(num) && isFinite(num) && num >= 0; | ||
} catch { | ||
return false; | ||
} | ||
}; |