Skip to content

Commit

Permalink
Better regexin
Browse files Browse the repository at this point in the history
  • Loading branch information
CoenWarmer committed Jan 5, 2024
1 parent 35514f7 commit 928909b
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* Side Public License, v 1.
*/
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/typescript-estree';
import { cleanString, lowerCaseFirstLetter, upperCaseFirstLetter } from './utils';
import { cleanIdentifier, lowerCaseFirstLetter, upperCaseFirstLetter } from './utils';

const EXEMPTED_TAG_NAMES = ['EuiCode', 'EuiBetaBadge', 'FormattedMessage'];

Expand All @@ -15,7 +15,7 @@ export function getIntentFromNode(
parent: TSESTree.Node | undefined
): string | false {
const processedValue = lowerCaseFirstLetter(
cleanString(value)
cleanIdentifier(value)
.split(' ')
.filter((_, i) => i < 4)
.map(upperCaseFirstLetter)
Expand Down
22 changes: 19 additions & 3 deletions packages/kbn-eslint-plugin-i18n/helpers/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,26 @@ describe('Utils', () => {
).toBe('hello great');
});

it('should remove all non alphabet characters', () => {
expect(cleanString('abc123!@#')).toBe('abc');
it('should remove all non alphabet characters unless they are incorporated in a sentence', () => {
expect(cleanString('1')).toBe('');
expect(cleanString('12')).toBe('');
expect(cleanString('123')).toBe('');
expect(cleanString('?')).toBe('');
expect(cleanString('!')).toBe('');
expect(cleanString('!!')).toBe('');
expect(cleanString('!!!')).toBe('');
expect(cleanString('!!!!')).toBe('');
expect(cleanString('@')).toBe('');
expect(cleanString('!@#$%^&*()_+{}|')).toBe('');
expect(cleanString(' Hey, this is great! Success. ')).toBe('Hey this is great Success');

expect(cleanString('Hey, you.')).toBe('Hey, you.');
expect(cleanString(' Hey, you. ')).toBe('Hey, you.');
expect(cleanString(' Hey? ')).toBe('Hey?');
expect(cleanString('Hey?')).toBe('Hey?');
expect(cleanString('Hey, this is great! Success.')).toBe('Hey, this is great! Success.');
expect(cleanString(' Hey, this is great! Success. ')).toBe(
'Hey, this is great! Success.'
);
});

it('should leave markdown alone', () => {
Expand Down
27 changes: 26 additions & 1 deletion packages/kbn-eslint-plugin-i18n/helpers/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,35 @@ function isUpperCase(val: string) {
return /^[A-Z]+$/.test(val);
}

export function cleanString(str: string) {
export function cleanIdentifier(str: string) {
return str
.replace(/```\w*```/g, '')
.replace(/\s+/g, ' ')
.replace(/[^a-zA-Z\s]*/g, '')
.trim();
}

export function cleanString(str: string) {
const strTrimmed = str.trim();

if (strTrimmed.length === 1) {
return '';
}

// Numbers
if (strTrimmed.replace(/[0-9]+/g, '').length === 0) {
return '';
}

// Markdown
if (strTrimmed.replace(/```\w*```/g, '').length === 0) {
return '';
}

// Special characters
if (strTrimmed.replace(/[!\@\#\$\%\^\&\*\(\)\_\+\{\}\|]+/g, '').length === 0) {
return '';
}

return strTrimmed;
}
3 changes: 3 additions & 0 deletions packages/kbn-eslint-plugin-i18n/jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ module.exports = {
preset: '@kbn/test/jest_node',
rootDir: '../..',
roots: ['<rootDir>/packages/kbn-eslint-plugin-i18n'],
globals: {
structuredClone: {},
},
};

0 comments on commit 928909b

Please sign in to comment.