Skip to content

Commit

Permalink
Fixes #18. Библиотека utfstring заменена на Intl.Segmenter
Browse files Browse the repository at this point in the history
  • Loading branch information
erickskrauch committed Apr 14, 2024
1 parent 5957cdf commit 40092f5
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 18 deletions.
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@
"discord.js": "^14",
"dotenv": "^16",
"node-graceful": "^3",
"node-telegram-bot-api": "^0.65",
"utfstring": "^3.1.3"
"node-telegram-bot-api": "^0.65"
},
"devDependencies": {
"@types/jest": "^29",
Expand Down
1 change: 1 addition & 0 deletions src/answerProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ describe('getResponse', () => {
it('Да́', () => expect(getResponse('Да́')).toBe('Пизда́'));
it('ДА̀', () => expect(getResponse('ДА̀')).toBe('ПИЗДА̀'));
it('Д𐌀', () => expect(getResponse('Д𐌀')).toBe('ПИЗД𐌀'));
it('д̌ӓ̄', () => expect(getResponse('д̌ӓ̄')).toBe('пизд̌ӓ̄')); // https://github.com/erickskrauch/da-pizda-bot/issues/18
});

describe('should be forgiven if the answer is more detailed', () => {
Expand Down
7 changes: 3 additions & 4 deletions src/answerProvider.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { UtfString } from 'utfstring';
import { normalizeString } from './unicode';
import { splitByGlyph, normalizeString } from './unicode';

const P = 0;
const I = 1;
Expand Down Expand Up @@ -44,8 +43,8 @@ export function getResponse(message: string): string | undefined {
const { prefix, d, delimiter, a, postfix } = match.groups;

let letters = ['п', 'и', 'з', 'д', 'а'];
letters[D] = new UtfString(message.substring(prefix.length)).charAt(0).toLowerCase().toString();
letters[A] = new UtfString(message.substring(0, message.length - postfix.length)).substr(-a.length).toString();
letters[D] = splitByGlyph(message.substring(prefix.length))[0].toLowerCase();
letters[A] = splitByGlyph(message.substring(0, message.length - postfix.length)).slice(-a.length).join('');

if (isLatin(d[0]) && isLatin(a[0])) {
letters[P] = 'p';
Expand Down
16 changes: 9 additions & 7 deletions src/unicode.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { UtfString } from 'utfstring';
const segmenter = new Intl.Segmenter(['ru', 'en']);

export function splitByGlyph(str: string): Array<string> {
return Array.from(segmenter.segment(str)).map(({ segment }) => segment);
}

const normalizationMap = new Map<string, string>();
// Please note that this dictionary is adjusted to needs of this project and doesn't match Unicode confusion dictionary
Expand All @@ -15,17 +19,15 @@ const normalizationDictionary: Record<string, string> = {
'а': 'а́а̂а̄ӓӑа̊а̃ӓ̄ӕа̨ѧ',
};
for (const normalLetter in normalizationDictionary) {
const utfDict = new UtfString(normalizationDictionary[normalLetter]);
for (const confusingChar of utfDict) {
normalizationMap.set(confusingChar.toString(), normalLetter);
for (const confusingChar of splitByGlyph(normalizationDictionary[normalLetter])) {
normalizationMap.set(confusingChar, normalLetter);
}
}

export function normalizeString(str: string): string {
const utfString = new UtfString(str);
let result = '';
for (const char of utfString) {
result += normalizationMap.get(char.toString()) || char;
for (const char of splitByGlyph(str)) {
result += normalizationMap.get(char) || char;
}

return result;
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3769,11 +3769,6 @@ url-parse@^1.5.3:
querystringify "^2.1.1"
requires-port "^1.0.0"

utfstring@^3.1.3:
version "3.1.3"
resolved "https://registry.yarnpkg.com/utfstring/-/utfstring-3.1.3.tgz#9663ba2093dbc73fcc135087e3f7282ec14f9e75"
integrity sha512-hliroryYsgiY2YWcnkDfn7vUzKf9+oL/wmf2fokEtN4zDfYUzeptNayk/4IkxBCLssezin3KkgBwRWgj5wCRKA==

util-deprecate@~1.0.1:
version "1.0.2"
resolved "https://registry.yarnpkg.com/util-deprecate/-/util-deprecate-1.0.2.tgz#450d4dc9fa70de732762fbd2d4a28981419a0ccf"
Expand Down

0 comments on commit 40092f5

Please sign in to comment.