UNPKG

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