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 {
//! 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.