Skip to content

Commit

Permalink
fix: Improve regex patterns. (#175)
Browse files Browse the repository at this point in the history
* Update regex.

* Go back.

* Add more tests.

* Fix.
  • Loading branch information
milesj committed Jun 9, 2024
1 parent e93eabf commit f835b53
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 3 deletions.
11 changes: 11 additions & 0 deletions bin/generateRegex.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env node

import { generateRegex } from 'emojibase-generator';

function handleError(error) {
console.error(error.message, error.stack);
}

process.on('unhandledRejection', handleError);

generateRegex().catch(handleError);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"generate": "yarn run generate:emojis && yarn run generate:shortcodes && node ./bin/buildFilesizeTable.mjs && yarn run generate:tests",
"generate:emojis": "rm -rf ./cache/final && node ./bin/generateEmoji.mjs",
"generate:po": "node ./bin/generatePo.mjs",
"generate:regex": "node ./bin/generateRegex.mjs",
"generate:shortcodes": "node ./bin/generateShortcodes.mjs",
"generate:tests": "cp ./packages/data/en/data.json ./packages/test-utils/test-data.json && cp ./packages/data/en/shortcodes/emojibase.json ./packages/test-utils/test-shortcodes.json && cp ./packages/data/en/messages.json ./packages/test-utils/test-messages.json",
"generate:types": "node ./bin/generateTypes.mjs",
Expand Down
31 changes: 28 additions & 3 deletions packages/regex/tests/regex.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ describe('regex', () => {
tests.push({
type: 'emoji',
unicode: emoji.emoji,
pass: [...BASE_PATTERNS, 'emojiLoose', 'emojiCodepoint', 'emojiCodepointLoose'],
pass: [...BASE_PATTERNS, 'emoji', 'emojiLoose', 'emojiCodepoint', 'emojiCodepointLoose'],
// fail: ['text', 'textLoose', 'textCodepoint', 'textCodepointLoose'],
});
}

Expand All @@ -81,11 +82,12 @@ describe('regex', () => {
tests.push({
type: 'text',
unicode: emoji.text,
pass: [...BASE_PATTERNS, 'textLoose', 'textCodepoint', 'textCodepointLoose'],
pass: [...BASE_PATTERNS, 'text', 'textLoose', 'textCodepoint', 'textCodepointLoose'],
// fail: ['emoji', 'emojiLoose', 'emojiCodepoint', 'emojiCodepointLoose'],
});
}

tests.forEach(({ unicode, type, pass }) => {
tests.forEach(({ unicode, type, pass, fail }) => {
describe(`${VARIATION_DESCRIPTIONS[type]}`, () => {
pass.forEach((passType) => {
const pattern = PATTERNS[passType];
Expand Down Expand Up @@ -113,6 +115,17 @@ describe('regex', () => {
});
});
});

fail?.forEach((passType) => {
const pattern = PATTERNS[passType];

describe(`fails ${PATTERN_DESCRIPTIONS[passType]}`, () => {
it(`doesnt match unicode by itself for ${unicode}`, () => {
const match = unicode.match(pattern)!;
expect(match).toBeNull();
});
});
});
});
});

Expand Down Expand Up @@ -194,4 +207,16 @@ describe('regex', () => {
});
});
});

it('manual cases', () => {
expect('👍🏻'.match(PATTERNS.combo)).not.toBeNull();
expect('👍🏻'.match(PATTERNS.emoji)).not.toBeNull();
expect('👍🏻'.match(PATTERNS.emojiLoose)).not.toBeNull();
// expect('👍🏻'.match(PATTERNS.text)).toBeNull();

expect('👍'.match(PATTERNS.combo)).not.toBeNull();
expect('👍'.match(PATTERNS.emoji)).toBeNull();
expect('👍'.match(PATTERNS.emojiLoose)).not.toBeNull();
// expect('👍'.match(PATTERNS.text)).toBeNull();
});
});

0 comments on commit f835b53

Please sign in to comment.