@@ -13,7 +13,7 @@ export type Hex = `0x${string}`;
1313export type HexLike = BytesLike ;
1414
1515/**
16- * Determines whether a given string is a properly formatted hexadecimal string (ccc.Hex).
16+ * Determines whether a given value is a properly formatted hexadecimal string (ccc.Hex).
1717 *
1818 * A valid hexadecimal string:
1919 * - Has at least two characters.
@@ -24,20 +24,14 @@ export type HexLike = BytesLike;
2424 * @param s - The string to validate as a hexadecimal (ccc.Hex) string.
2525 * @returns True if the string is a valid hex string, false otherwise.
2626 */
27- export function isHex ( s : string ) : s is Hex {
28- if (
29- s . length < 2 ||
30- s . charCodeAt ( 0 ) !== 48 || // ascii code for '0'
31- s . charCodeAt ( 1 ) !== 120 || // ascii code for 'x'
32- s . length % 2 !== 0
33- ) {
27+ export function isHex ( s : unknown ) : s is Hex {
28+ if ( ! ( typeof s === "string" && s . length % 2 === 0 && s . startsWith ( "0x" ) ) ) {
3429 return false ;
3530 }
3631
3732 for ( let i = 2 ; i < s . length ; i ++ ) {
38- const c = s . charCodeAt ( i ) ;
39- // Allow characters '0'-'9' and 'a'-'f'
40- if ( ! ( ( c >= 48 && c <= 57 ) || ( c >= 97 && c <= 102 ) ) ) {
33+ const c = s . charAt ( i ) ;
34+ if ( ! ( ( "0" <= c && c <= "9" ) || ( "a" <= c && c <= "f" ) ) ) {
4135 return false ;
4236 }
4337 }
@@ -58,7 +52,7 @@ export function isHex(s: string): s is Hex {
5852 */
5953export function hexFrom ( hex : HexLike ) : Hex {
6054 // Passthru an already normalized hex. V8 optimization: maintain existing hidden string fields.
61- if ( typeof hex === "string" && isHex ( hex ) ) {
55+ if ( isHex ( hex ) ) {
6256 return hex ;
6357 }
6458
0 commit comments