* Check if input is a valid hexadecimal string.
*
* @param {unknown} input - Variable to check
+ * @param {number} [length] - Exact even-parity length of the input
* @returns True if input is hex, else false
*/
- is (input: unknown): input is Hex {
- return typeof input === 'string' && /^([0-9A-F]{2})+$/i.test(input)
+ is (input: unknown, length?: number): input is Hex {
+ if (typeof length !== 'undefined') {
+ if (!Number.isSafeInteger(length) || length < 0 || length & 1) {
+ throw new TypeError(`Invalid "length" argument`)
+ }
+ }
+ const quantifier = length ? `{${length >> 1}}` : '+'
+ const pattern = new RegExp(`^([0-9A-F]{2})${quantifier}$`, 'i')
+ return typeof input === 'string' && pattern.test(input)
},
/**