diff --git a/CHANGELOG.md b/CHANGELOG.md index 9dfe40061..2a76bc98e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ Changes: - Add `arrayUnzip` as reverse of `arrayZip` +- Adjust `isAscii` check to disallow `\t`, `\n`, & `\r` characters ## 10.0.2 Jul 10, 2022 diff --git a/packages/util/src/is/ascii.spec.ts b/packages/util/src/is/ascii.spec.ts index 51ecedc27..4b60c9de9 100644 --- a/packages/util/src/is/ascii.spec.ts +++ b/packages/util/src/is/ascii.spec.ts @@ -7,7 +7,11 @@ import { isAscii } from '.'; describe('isAscii', (): void => { it('returns true for an ASCII string', (): void => { - expect(isAscii('Hello\tWorld!\n\rTesting')).toEqual(true); + expect(isAscii('Hello World! Testing')).toEqual(true); + }); + + it('returns false for an ASCII string (with formatters)', (): void => { + expect(isAscii('Hello\tWorld!\n\rTesting')).toEqual(false); }); it('returns false on an non-ascii string', (): void => { @@ -15,7 +19,7 @@ describe('isAscii', (): void => { }); it('returns true for ASCII bytes', (): void => { - expect(isAscii(new Uint8Array([0x31, 0x32, 0x20, 10]))).toEqual(true); + expect(isAscii(stringToU8a('Hello World! Testing'))).toEqual(true); }); it('returns true for empty string inputs', (): void => { @@ -38,6 +42,6 @@ describe('isAscii', (): void => { expect(isAscii('0x010203')).toEqual(false); }); - perf('isAscii (str)', 2_000_000, [['Hello\tWorld!\n\rTesting']], isAscii); - perf('isAscii (u8a)', 200_000, [[[stringToU8a('Hello\tWorld!\n\rTesting')]]], isAscii); + perf('isAscii (str)', 2_000_000, [['Hello World! Testing']], isAscii); + perf('isAscii (u8a)', 100_000, [[[stringToU8a('Hello World! Testing')]]], isAscii); }); diff --git a/packages/util/src/is/ascii.ts b/packages/util/src/is/ascii.ts index ed6685c96..7bded4f87 100644 --- a/packages/util/src/is/ascii.ts +++ b/packages/util/src/is/ascii.ts @@ -15,7 +15,7 @@ function isAsciiStr (str: AnyString): boolean { const b = str.charCodeAt(i); // check is inlined here, it is faster than making a call - if (!((b < 127) && ((b >= 32) || (b === 10) || (b === 9) || (b === 13)))) { + if ((b < 32) || (b > 126)) { return false; } } @@ -31,7 +31,7 @@ function isAsciiBytes (u8a: Uint8Array | Buffer | number[]): boolean { const b = u8a[i]; // check is inlined here, it is faster than making a call - if (!((b < 127) && ((b >= 32) || (b === 10) || (b === 9) || (b === 13)))) { + if ((b < 32) || (b > 126)) { return false; } }