// Copyright 2017-2022 @polkadot/util authors & contributors // SPDX-License-Identifier: Apache-2.0 import type { U8aLike } from '../types'; import { u8aToU8a } from '../u8a/toU8a'; import { isHex } from './hex'; import { isString } from './string'; const FORMAT = [9, 10, 13]; /** @internal */ function isAsciiByte (b: number): boolean { return (b < 127) && ( (b >= 32) || FORMAT.includes(b) ); } function isAsciiChar (s: string): boolean { return isAsciiByte(s.charCodeAt(0)); } /** * @name isAscii * @summary Tests if the input is printable ASCII * @description * Checks to see if the input string or Uint8Array is printable ASCII, 32-127 + formatters */ export function isAscii (value?: U8aLike | null): boolean { const isStringIn = isString(value); if (value) { return isStringIn && !isHex(value) ? value.toString().split('').every(isAsciiChar) : isAsciiStrict(u8aToU8a(value)); } return isStringIn; } /** * @name isAsciiStrict * @description A strict version of [[isAscii]], accepting only Uint8Array inputs */ export function isAsciiStrict (u8a: Uint8Array): boolean { return u8a.every(isAsciiByte); }