-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplurEn.ts
80 lines (72 loc) · 2.63 KB
/
plurEn.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import irregularPlurals from "irregular-plurals";
const getIrregularVerb = require('irregular');
export const EnglishNouns = {
Case: {
Base: "BF",
PastSimple: "PS",
PastParticiple: "PP",
ThirdPersonSingular: "TPS",
PresentParticipleGerund: "PPG"
}
}
export type EnglishNounsCases = typeof EnglishNouns.Case.Base | typeof EnglishNouns.Case.PastSimple
| typeof EnglishNouns.Case.PastParticiple | typeof EnglishNouns.Case.ThirdPersonSingular
| typeof EnglishNouns.Case.PresentParticipleGerund;
export function plurEn(word: string, plural?: string, count?: number) {
if (typeof plural === 'number') {
count = plural;
}
if (irregularPlurals.has(word.toLowerCase())) {
plural = irregularPlurals.get(word.toLowerCase());
const firstLetter = word.charAt(0);
const isFirstLetterUpperCase = firstLetter === firstLetter.toUpperCase();
if (isFirstLetterUpperCase) {
plural = firstLetter + plural?.slice(1);
}
const isWholeWordUpperCase = word === word.toUpperCase();
if (isWholeWordUpperCase) {
plural = plural?.toUpperCase();
}
} else if (typeof plural !== 'string') {
plural = (word.replace(/(?:s|x|z|ch|sh)$/i, '$&e').replace(/([^aeiou])y$/i, '$1ie') + 's')
.replace(/i?e?s$/i, match => {
const isTailLowerCase = word.slice(-1) === word.slice(-1).toLowerCase();
return isTailLowerCase ? match.toLowerCase() : match.toUpperCase();
});
}
return Math.abs(count || 0) === 1 ? word : plural;
}
export function decline(word: string, form: EnglishNounsCases) {
let result = getIrregularVerb(word)?.[form];
if (!result) {
if ([EnglishNouns.Case.PastSimple, EnglishNouns.Case.PastParticiple].includes(form)) {
if (word.endsWith("e")) {
result = word + 'd';
} else if (word.endsWith('ry') || word.endsWith('ty') || word.endsWith('fy')) {
result = word.slice(0, -1) + 'ied';
} else if (word.endsWith('am')) {
result = word + 'med';
} else if (word.endsWith('eg')) {
result = word + 'ged';
} else if (word.endsWith('an')) {
result = word + 'ned';
} else if (word.endsWith('ip')) {
result = word + 'ped';
} else if (word.endsWith('er')) {
result = word + 'red';
} else if (word.endsWith('et')) {
result = word + 'ted';
} else if (word.endsWith('ip')) {
result = word + 'ped';
} else if (word.endsWith('el')) {
result = word + 'led';
} else {
result = word + 'ed';
}
} else {
// TODO: доделать всякие случаи типа come -> coming
result = word + 'ing';
}
}
return result;
}