Files
common/packages/util/src/is/ascii.ts
T
2022-01-01 09:26:11 +02:00

41 lines
962 B
TypeScript

// 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)
: u8aToU8a(value).every(isAsciiByte);
}
return isStringIn;
}