UNPKG

836 BJavaScriptView Raw
1// Copyright 2017-2022 @polkadot/util authors & contributors
2// SPDX-License-Identifier: Apache-2.0
3import { u8aToU8a } from "../u8a/toU8a.js";
4import { isHex } from "./hex.js";
5import { isString } from "./string.js";
6const FORMAT = [9, 10, 13];
7/** @internal */
8
9function isAsciiByte(b) {
10 return b < 127 && (b >= 32 || FORMAT.includes(b));
11}
12
13function isAsciiChar(s) {
14 return isAsciiByte(s.charCodeAt(0));
15}
16/**
17 * @name isAscii
18 * @summary Tests if the input is printable ASCII
19 * @description
20 * Checks to see if the input string or Uint8Array is printable ASCII, 32-127 + formatters
21 */
22
23
24export function isAscii(value) {
25 const isStringIn = isString(value);
26
27 if (value) {
28 return isStringIn && !isHex(value) ? value.toString().split('').every(isAsciiChar) : u8aToU8a(value).every(isAsciiByte);
29 }
30
31 return isStringIn;
32}
\No newline at end of file