UNPKG

1.13 kBJavaScriptView Raw
1import { u8aToU8a } from '../u8a/toU8a.js';
2import { isHex } from './hex.js';
3import { isString } from './string.js';
4/** @internal */
5function isAsciiStr(str) {
6 for (let i = 0, count = str.length; i < count; i++) {
7 const b = str.charCodeAt(i);
8 // check is inlined here, it is faster than making a call
9 if (b < 32 || b > 126) {
10 return false;
11 }
12 }
13 return true;
14}
15/** @internal */
16function isAsciiBytes(u8a) {
17 for (let i = 0, count = u8a.length; i < count; i++) {
18 const b = u8a[i] | 0;
19 // check is inlined here, it is faster than making a call
20 if (b < 32 || b > 126) {
21 return false;
22 }
23 }
24 return true;
25}
26/**
27 * @name isAscii
28 * @summary Tests if the input is printable ASCII
29 * @description
30 * Checks to see if the input string or Uint8Array is printable ASCII, 32-127 + formatters
31 */
32export function isAscii(value) {
33 return isString(value)
34 ? isHex(value)
35 ? isAsciiBytes(u8aToU8a(value))
36 : isAsciiStr(value)
37 : value
38 ? isAsciiBytes(value)
39 : false;
40}