]> git.codecow.com Git - libnemo.git/commitdiff
Add hex typing.
authorChris Duncan <chris@codecow.com>
Sun, 2 Aug 2026 21:27:04 +0000 (14:27 -0700)
committerChris Duncan <chris@codecow.com>
Sun, 2 Aug 2026 21:27:04 +0000 (14:27 -0700)
src/index.ts
src/lib/convert/hex.ts

index bbc9941cb856d89d32f3e4c7c8d8d99425110aca..5170ad17f2eb8d8402c61c009418e03347387534 100644 (file)
@@ -12,6 +12,9 @@ import { Wallet } from './lib/wallet'
 
 declare global {
        type Bytes = Uint8Array<ArrayBuffer> | Buffer<ArrayBuffer>
+       type Hex = [HexByte]
+       type HexByte = `${HexChar}${HexChar}`
+       type HexChar = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f'
 }
 
 export {
index f37d4debd058518b35f4ceb9ca9506541a32cf55..f148e607699611c1f8a5b1c43fc481bfe90f2aa8 100644 (file)
@@ -2,6 +2,16 @@
 //! SPDX-License-Identifier: GPL-3.0-or-later
 
 export const hex = Object.freeze({
+       /**
+        * Check if input is a valid hexadecimal string.
+        *
+        * @param {unknown} input - Variable to check
+        * @returns True if input is hex, else false
+        */
+       is (input: unknown): input is Hex {
+               return typeof input === 'string' && /([0-9a-f]{2})+/.test(input)
+       },
+
        /**
         * Convert a hexadecimal string to an ArrayBuffer. The result must be at least
         * one byte and no more than 4 GiB.