Skip to content

Commit 6d0840c

Browse files
committed
v3
1 parent 3ff1e9d commit 6d0840c

30 files changed

Lines changed: 4213 additions & 2351 deletions

.eslintignore

Lines changed: 0 additions & 3 deletions
This file was deleted.

.eslintrc

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ jobs:
77
runs-on: ubuntu-latest
88
strategy:
99
matrix:
10-
node-version: [20.x]
10+
node-version: [24.x]
1111
steps:
1212
- uses: actions/checkout@v1
1313
- name: Use Node.js ${{ matrix.node-version }}

.npmrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
registry=https://registry.npmjs.org/

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Changelog
22

3+
## v3.0.0
4+
Переработано API для возможности использовать в браузере.
5+
36
## v2.5.6
47
В небезопасный словарь перенесены слова #22, #26:
58
- протёкший;

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
The MIT License (MIT)
22

3-
Copyright (c) 2023 Denis Seleznev
3+
Copyright (c) 2025 Denis Seleznev
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,28 @@
1717
Отсутствуют.
1818

1919
## Использование
20+
21+
### Node.js
2022
```js
21-
const Eyo = require('eyo-kernel');
23+
import { Eyo } from 'eyo-kernel';
24+
import { loadSafeDictionary, loadNotSafeDictionary } from 'eyo-kernel/loadDictionary';
25+
2226
const text = 'Мой текст...';
2327

2428
// Работа с безопасным встроенным словарём.
2529
const safeEyo = new Eyo();
26-
safeEyo.dictionary.loadSafeSync(); // ./dict/safe.txt.gz
30+
const safeDictionary = await loadSafeDictionary() // ./dict/safe.txt
31+
safeEyo.dictionary.set(safeDictionary);
2732
console.log(safeEyo.restore(text));
2833
console.log(safeEyo.lint(text));
2934

3035
// Работа с небезопасным встроенным словарём.
3136
const notSafeEyo = new Eyo();
32-
notSafeEyo.dictionary.loadNotSafeSync(); // ./dict/not_safe.txt.gz
37+
const notSafeDictionary = loadNotSafeDictionary(); // ./dict/not_safe.txt
38+
notSafeEyo.dictionary.set(notSafeDictionary);
3339
console.log(notSafeEyo.restore(text));
3440
console.log(notSafeEyo.lint(text));
3541

36-
// Загрузка собственного словаря.
37-
const eyo = new Eyo();
38-
// Также поддерживаются словари, сжатые с помощью gzip, *.txt.gz
39-
eyo.dictionary.loadSync('./my_eyo_dict.txt');
40-
console.log(eyo.restore(text));
41-
console.log(eyo.lint(text));
42-
4342
// Создание собственного словаря.
4443
const eyo = new Eyo();
4544
// Добавить слово в свой словарь.
@@ -50,6 +49,27 @@ eyo.dictionary.removeWord('словоСБуквойЁ');
5049
eyo.dictionary.clear();
5150
```
5251

52+
### Браузер
53+
```js
54+
import { Eyo } from 'eyo-kernel';
55+
56+
const text = 'Мой текст...';
57+
58+
// Работа с безопасным встроенным словарём.
59+
const safeEyo = new Eyo();
60+
const safeDictionary = await fetch('./dictionary/safe.txt').then(response => response.text());
61+
safeEyo.dictionary.set(safeDictionary);
62+
console.log(safeEyo.restore(text));
63+
console.log(safeEyo.lint(text));
64+
65+
// Работа с небезопасным встроенным словарём.
66+
const notSafeEyo = new Eyo();
67+
const notSafeDictionary = await fetch('./dictionary/not_safe.txt').then(response => response.text());
68+
notSafeEyo.dictionary.set(notSafeDictionary);
69+
console.log(notSafeEyo.restore(text));
70+
console.log(notSafeEyo.lint(text));
71+
```
72+
5373
## Словарь
5474
Первоначально словарь взят из проекта [php-yoficator](https://github.com/rin-nas/php-yoficator/tree/master/Text). По доработкам словаря см. [CHANGELOG.md](./CHANGELOG.md).
5575

dist/dictionary.d.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
export declare class Dictionary {
2+
private dict;
3+
constructor();
4+
/**
5+
* Очищает словарь.
6+
*/
7+
clear(): void;
8+
/**
9+
* Восстанавливает в слове букву «ё».
10+
*/
11+
restoreWord(word: string): string;
12+
/**
13+
* Добавляет слово в словарь.
14+
*/
15+
addWord(rawWord: string): void;
16+
/**
17+
* Удаляет слово из словаря.
18+
*/
19+
removeWord(word: string): void;
20+
/**
21+
* Установить словарь.
22+
*/
23+
set(dict: string | string[]): void;
24+
/**
25+
* Получить словарь.
26+
*/
27+
get(): Record<string, string>;
28+
private addWordInner;
29+
private capitalize;
30+
private replaceYo;
31+
}

dist/dictionary.js

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
export class Dictionary {
2+
dict = {};
3+
constructor() {
4+
this.dict = {};
5+
}
6+
/**
7+
* Очищает словарь.
8+
*/
9+
clear() {
10+
this.dict = {};
11+
}
12+
/**
13+
* Восстанавливает в слове букву «ё».
14+
*/
15+
restoreWord(word) {
16+
return this.dict[this.replaceYo(word)] || word;
17+
}
18+
/**
19+
* Добавляет слово в словарь.
20+
*/
21+
addWord(rawWord) {
22+
let word = rawWord;
23+
if (rawWord.search('#') > -1) {
24+
word = word.split('#')[0].trim();
25+
}
26+
if (word.search(/\(/) > -1) {
27+
const parts = word.split(/[(|)]/);
28+
for (let i = 1, len = parts.length - 1; i < len; i++) {
29+
this.addWordInner(parts[0] + parts[i]);
30+
}
31+
}
32+
else {
33+
this.addWordInner(word);
34+
}
35+
}
36+
/**
37+
* Удаляет слово из словаря.
38+
*/
39+
removeWord(word) {
40+
const wordE = this.replaceYo(word);
41+
delete this.dict[wordE];
42+
if (word.search(/^[А-ЯЁ]/) === -1) {
43+
delete this.dict[this.capitalize(wordE)];
44+
}
45+
}
46+
/**
47+
* Установить словарь.
48+
*/
49+
set(dict) {
50+
this.clear();
51+
if (!dict) {
52+
return;
53+
}
54+
const buffer = Array.isArray(dict) ? dict : dict.trim().split(/\r?\n/);
55+
buffer.forEach(word => this.addWord(word));
56+
}
57+
/**
58+
* Получить словарь.
59+
*/
60+
get() {
61+
return this.dict;
62+
}
63+
addWordInner(word) {
64+
// Слово может использоваться только со строчной буквы.
65+
// Пример: _киёв. Киев и только киёв.
66+
const hasUnderscore = word.search('_') === 0;
67+
word = word.replace(/^_/, '');
68+
const key = this.replaceYo(word);
69+
this.dict[key] = word;
70+
if (word.search(/^[А-ЯЁ]/) === -1 && !hasUnderscore) {
71+
this.dict[this.capitalize(key)] = this.capitalize(word);
72+
}
73+
}
74+
capitalize(text) {
75+
return text.substr(0, 1).toUpperCase() + text.substr(1);
76+
}
77+
replaceYo(word) {
78+
return word.replace(/Ё/g, 'Е').replace(/ё/g, 'е');
79+
}
80+
}
81+
;

dist/index.d.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Dictionary } from './dictionary';
2+
type Position = {
3+
line: number;
4+
column: number;
5+
index: number;
6+
};
7+
type Replacement = {
8+
before: string;
9+
after: string;
10+
position: Position[];
11+
};
12+
export declare class Eyo {
13+
dictionary: Dictionary;
14+
/**
15+
* Ищет варианты замены буквы «е» на «ё».
16+
*/
17+
lint(text: string, groupByWords?: boolean): Replacement[];
18+
/**
19+
* Восстанавливает букву «ё» в тексте.
20+
*/
21+
restore(text: string): string;
22+
private hasEYo;
23+
private getPosition;
24+
private delDuplicates;
25+
private sort;
26+
}
27+
export {};

0 commit comments

Comments
 (0)