mirror of
https://github.com/trezor/trezor-suite.git
synced 2026-03-02 21:45:14 +01:00
feat(product-components): add utils to detect non-ASCII chars
This commit is contained in:
committed by
Jiri Zbytovsky
parent
7ae24e9615
commit
b1d9fb0d49
@@ -0,0 +1,17 @@
|
||||
import { useEffect, useMemo, useState } from 'react';
|
||||
import { getNonAsciiChars } from '@trezor/utils';
|
||||
|
||||
export const useNonAsciiChars = (value: string) => {
|
||||
const [showAsciiBanner, setShowAsciiBanner] = useState(false);
|
||||
|
||||
const nonAsciiChars = useMemo(() => getNonAsciiChars(value), [value]);
|
||||
|
||||
useEffect(() => {
|
||||
if (nonAsciiChars !== null) {
|
||||
// If the banner was displayed once, we don't hide it again
|
||||
setShowAsciiBanner(true);
|
||||
}
|
||||
}, [nonAsciiChars]);
|
||||
|
||||
return { nonAsciiChars, showAsciiBanner };
|
||||
};
|
||||
14
packages/utils/src/asciiUtils.ts
Normal file
14
packages/utils/src/asciiUtils.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
// Regular expression to match non-ASCII characters
|
||||
const nonAsciiPattern = /[^\x00-\x7F]/g;
|
||||
|
||||
export function isAscii(value?: string): boolean {
|
||||
if (!value) return true;
|
||||
|
||||
return !nonAsciiPattern.test(value);
|
||||
}
|
||||
|
||||
export function getNonAsciiChars(value?: string): RegExpMatchArray | null {
|
||||
if (!value) return null;
|
||||
|
||||
return value.match(nonAsciiPattern);
|
||||
}
|
||||
@@ -26,7 +26,6 @@ export * from './getWeakRandomId';
|
||||
export * from './getWeakRandomInt';
|
||||
export * from './hasUppercaseLetter';
|
||||
export * from './isArrayMember';
|
||||
export * from './isAscii';
|
||||
export * from './isHex';
|
||||
export * from './isNotUndefined';
|
||||
export * from './isUrl';
|
||||
@@ -49,3 +48,4 @@ export * from './bigNumber';
|
||||
export * from './throttler';
|
||||
export * from './extractUrlsFromText';
|
||||
export * from './isFullPath';
|
||||
export * from './asciiUtils';
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
export function isAscii(value?: string): boolean {
|
||||
if (!value) return true;
|
||||
|
||||
return /^[\x00-\x7F]*$/.test(value);
|
||||
}
|
||||
34
packages/utils/tests/asciUtils.test.ts
Normal file
34
packages/utils/tests/asciUtils.test.ts
Normal file
@@ -0,0 +1,34 @@
|
||||
import { isAscii, getNonAsciiChars } from '../src/asciiUtils';
|
||||
|
||||
describe('isAscii', () => {
|
||||
it('should return true for ASCII only string', () => {
|
||||
expect(isAscii('this is only ascii')).toEqual(true);
|
||||
});
|
||||
|
||||
it('should return true when called without parameter', () => {
|
||||
expect(isAscii()).toEqual(true);
|
||||
});
|
||||
|
||||
it('should return false strings with non ASCII chars', () => {
|
||||
expect(isAscii('¥')).toEqual(false);
|
||||
expect(isAscii('železniční přejezd')).toEqual(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getNonAsciiChars', () => {
|
||||
test('should return null for an empty string', () => {
|
||||
expect(getNonAsciiChars('')).toBeNull();
|
||||
});
|
||||
|
||||
test('should return null for ASCII-only characters', () => {
|
||||
expect(getNonAsciiChars('Hello, World!')).toBeNull();
|
||||
});
|
||||
|
||||
test('should return all non-ASCII characters for a mixed string', () => {
|
||||
expect(getNonAsciiChars('Čau světe!')).toEqual(['Č', 'ě']);
|
||||
});
|
||||
|
||||
test('should return all instances of repeating non-ASCII characters', () => {
|
||||
expect(getNonAsciiChars('Přátelé přátelé')).toEqual(['ř', 'á', 'é', 'ř', 'á', 'é']);
|
||||
});
|
||||
});
|
||||
@@ -1,16 +0,0 @@
|
||||
import { isAscii } from '../src/isAscii';
|
||||
|
||||
describe('isAscii', () => {
|
||||
it('should return true for ASCII only string', () => {
|
||||
expect(isAscii('this is only ascii')).toEqual(true);
|
||||
});
|
||||
|
||||
it('should return true when called without parameter', () => {
|
||||
expect(isAscii()).toEqual(true);
|
||||
});
|
||||
|
||||
it('should return false strings with non ASCII chars', () => {
|
||||
expect(isAscii('¥')).toEqual(false);
|
||||
expect(isAscii('železniční přejezd')).toEqual(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user