decline-word@1.4.0
Helps you to inflect words of Russian, Ukrainian and English languages.
It may work for some other languages, who knows.
Installation
decline-word
is available via NPM:
$ npm i decline-word@1.4.0
Usage
For Russian and Ukrainian languages:
Parameter | Required | Default value | Value |
---|---|---|---|
1st | true | amount of items | |
2nd | true | main part of the word (unchangeable beginning of each form) | |
3rd | false | '' |
word ending for 1 item |
4th | false | '' |
word ending for 2 items |
5th | false | '' |
word ending for 5 items |
import declineWord from 'decline-word';
console.log(`12 ${declineWord(12, 'ΡΠ±Π»ΠΎΠΊ', 'ΠΎ', 'Π°')}`); // 12 ΡΠ±Π»ΠΎΠΊ
console.log(`22 ${declineWord(22, 'ΡΠ±Π»ΠΎΠΊ', 'ΠΎ', 'Π°')}`); // 22 ΡΠ±Π»ΠΎΠΊΠ°
console.log(`159 ${declineWord(159, 'ΡΠ±Π»ΠΎΠΊ', 'ΠΎ', 'Π°')}`); // 159 ΡΠ±Π»ΠΎΠΊ
console.log(`1151 ${declineWord(1151, 'ΡΠ±Π»ΠΎΠΊ', 'ΠΎ', 'Π°')}`); // 1151 ΡΠ±Π»ΠΎΠΊΠΎ
console.log(`1 ${declineWord(1, 'Π°ΠΏΠ΅Π»ΡΡΠΈΠ½', '', 'ΠΈ', 'ΡΠ²')}`); // 1 Π°ΠΏΠ΅Π»ΡΡΠΈΠ½
console.log(`11 ${declineWord(11, 'Π°ΠΏΠ΅Π»ΡΡΠΈΠ½', '', 'ΠΈ', 'ΡΠ²')}`); // 11 Π°ΠΏΠ΅Π»ΡΡΠΈΠ½ΡΠ²
console.log(`84 ${declineWord(84, 'Π°ΠΏΠ΅Π»ΡΡΠΈΠ½', '', 'ΠΈ', 'ΡΠ²')}`); // 84 Π°ΠΏΠ΅Π»ΡΡΠΈΠ½ΠΈ
console.log(`147 ${declineWord(147, 'Π°ΠΏΠ΅Π»ΡΡΠΈΠ½', '', 'ΠΈ', 'ΡΠ²')}`); // 147 Π°ΠΏΠ΅Π»ΡΡΠΈΠ½ΡΠ²
console.log(`0 ${declineWord(0, '', 'Π³ΡΠ°', 'Π³ΡΠΈ', 'ΡΠ³ΠΎΡ')}`); // 0 ΡΠ³ΠΎΡ
console.log(`1 ${declineWord(1, '', 'Π³ΡΠ°', 'Π³ΡΠΈ', 'ΡΠ³ΠΎΡ')}`); // 1 Π³ΡΠ°
console.log(`4 ${declineWord(4, '', 'Π³ΡΠ°', 'Π³ΡΠΈ', 'ΡΠ³ΠΎΡ')}`); // 4 Π³ΡΠΈ
console.log(`25 ${declineWord(25, '', 'Π³ΡΠ°', 'Π³ΡΠΈ', 'ΡΠ³ΠΎΡ')}`); // 25 ΡΠ³ΠΎΡ
For English language:
There are some differences of parameter default values comparing to Russian and Ukrainian languages.
Parameter | Required | Default value | Value |
---|---|---|---|
1st | true | amount of items | |
2nd | true | main part of the word (unchangeable beginning of each form) | |
3rd | false | '' |
word ending for 1 item |
4th | false | 's' |
word ending for 2 items |
5th | false | <4th parameter> |
word ending for 5 items |
This difference makes more convenience for using English words.
Note: decline-word
declines English words incorrect according to language rules. Just because of if not equal to one
is very simple check algorithm (and I'm too lazy to change already made one) this package declines them according to Russian/Ukrainian language rules. Do not use decline-word
for English language if you want to decline words correctly.
import declineWord from 'decline-word';
console.log(`1 ${declineWord(1, 'door')}`); // 1 door
console.log(`10 ${declineWord(10, 'door')}`); // 10 doors
console.log(`11 ${declineWord(11, 'hero', '', 'es')}`); // 11 heroes
console.log(`21 ${declineWord(21, 'hero', '', 'es')}`); // 21 hero (but correct is "heros")
console.log(`5 ${declineWord(5, 'cand', 'y', 'ies')}`); // 5 candies
console.log(`101 ${declineWord(101, 'cand', 'y', 'ies')}`); // 101 candy (but correct is "candies")
// declineWord(31, 't', 'ooth', 'eeth') or...
console.log(`31 ${declineWord(31, '', 'tooth', 'teeth')}`); // 31 tooth (but correct is "teeth")
console.log(`32 ${declineWord(32, '', 'tooth', 'teeth')}`); // 32 teeth
Advanced usage
import { declineWrapper } from 'decline-word';
const declApples = declineWrapper('ΡΠ±Π»ΠΎΠΊ', 'ΠΎ', 'Π°');
const declOranges = declineWrapper('Π°ΠΏΠ΅Π»ΡΡΠΈΠ½', '', 'Π°', 'ΠΎΠ²');
const declPears = declineWrapper('Π³ΡΡΡ', 'Π°', 'ΠΈ');
for(let i = 0; i <= 6; i++) {
console.log(`${i} ${declApples(i)} | ${i} ${declOranges(i)} | ${i} ${declPears(i)}`);
}
/*
1st iteration: '0 ΡΠ±Π»ΠΎΠΊ | 0 Π°ΠΏΠ΅Π»ΡΡΠΈΠ½ΠΎΠ² | 0 Π³ΡΡΡ'
2nd iteration: '1 ΡΠ±Π»ΠΎΠΊΠΎ | 1 Π°ΠΏΠ΅Π»ΡΡΠΈΠ½ | 1 Π³ΡΡΡΠ°'
3rd iteration: '2 ΡΠ±Π»ΠΎΠΊΠ° | 2 Π°ΠΏΠ΅Π»ΡΡΠΈΠ½Π° | 2 Π³ΡΡΡΠΈ'
4th iteration: '3 ΡΠ±Π»ΠΎΠΊΠ° | 3 Π°ΠΏΠ΅Π»ΡΡΠΈΠ½Π° | 3 Π³ΡΡΡΠΈ'
5th iteration: '4 ΡΠ±Π»ΠΎΠΊΠ° | 4 Π°ΠΏΠ΅Π»ΡΡΠΈΠ½Π° | 4 Π³ΡΡΡΠΈ'
6th iteration: '5 ΡΠ±Π»ΠΎΠΊ | 5 Π°ΠΏΠ΅Π»ΡΡΠΈΠ½ΠΎΠ² | 5 Π³ΡΡΡ'
7th iteration: '6 ΡΠ±Π»ΠΎΠΊ | 6 Π°ΠΏΠ΅Π»ΡΡΠΈΠ½ΠΎΠ² | 6 Π³ΡΡΡ'
*/
Declination of cases:
import { declineWrapper } from 'decline-word';
// Nominative case, ΠΈΠΌΠ΅Π½ΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠ°Π΄Π΅ΠΆ, Π½Π°Π·ΠΈΠ²Π½ΠΈΠΉ Π²ΡΠ΄ΠΌΡΠ½ΠΎΠΊ
const nomCase = declineWrapper('ΠΏΠ°Π΄Π΅ΠΆ', '', 'Π°', 'Π΅ΠΉ');
console.log('ΠΎΠ΄ΠΈΠ½', nomCase(1)); // ΠΎΠ΄ΠΈΠ½ ΠΏΠ°Π΄Π΅ΠΆ
console.log('Π΄Π²Π°', nomCase(2)); // Π΄Π²Π° ΠΏΠ°Π΄Π΅ΠΆΠ°
console.log('ΠΏΡΡΡ', nomCase(5)); // ΠΏΡΡΡ ΠΏΠ°Π΄Π΅ΠΆΠ΅ΠΉ
// Genitive case, ΡΠΎΠ΄ΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠ°Π΄Π΅ΠΆ, ΡΠΎΠ΄ΠΎΠ²ΠΈΠΉ Π²ΡΠ΄ΠΌΡΠ½ΠΎΠΊ
const genCase = declineWrapper('ΠΏΠ°Π΄Π΅ΠΆ', 'Π°', 'Π΅ΠΉ', 'Π΅ΠΉ');
console.log('ΠΎΠ΄Π½ΠΎΠ³ΠΎ', genCase(1)); // ΠΎΠ΄Π½ΠΎΠ³ΠΎ ΠΏΠ°Π΄Π΅ΠΆΠ°
console.log('Π΄Π²ΡΡ
', genCase(2)); // Π΄Π²ΡΡ
ΠΏΠ°Π΄Π΅ΠΆΠ΅ΠΉ
console.log('ΠΏΡΡΠΈ', genCase(5)); // ΠΏΡΡΠΈ ΠΏΠ°Π΄Π΅ΠΆΠ΅ΠΉ
// Dative case, Π΄Π°ΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠ°Π΄Π΅ΠΆ, Π΄Π°Π²Π°Π»ΡΠ½ΠΈΠΉ Π²ΡΠ΄ΠΌΡΠ½ΠΎΠΊ
const datCase = declineWrapper('ΠΏΠ°Π΄Π΅ΠΆ', 'Ρ', 'Π°ΠΌ', 'Π°ΠΌ');
console.log('ΠΎΠ΄Π½ΠΎΠΌΡ', datCase(1)); // ΠΎΠ΄Π½ΠΎΠΌΡ ΠΏΠ°Π΄Π΅ΠΆΡ
console.log('Π΄Π²ΡΠΌ', datCase(2)); // Π΄Π²ΡΠΌ ΠΏΠ°Π΄Π΅ΠΆΠ°ΠΌ
console.log('ΠΏΡΡΠΈ', datCase(5)); // ΠΏΡΡΠΈ ΠΏΠ°Π΄Π΅ΠΆΠ°ΠΌ
// Accusative case, Π²ΠΈΠ½ΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠ°Π΄Π΅ΠΆ, Π·Π½Π°Ρ
ΡΠ΄Π½ΠΈΠΉ Π²ΡΠ΄ΠΌΡΠ½ΠΎΠΊ
const accCase = declineWrapper('ΠΏΠ°Π΄Π΅ΠΆ', '', 'Π°', 'Π΅ΠΉ');
console.log('ΠΎΠ΄ΠΈΠ½', accCase(1)); // ΠΎΠ΄ΠΈΠ½ ΠΏΠ°Π΄Π΅ΠΆ
console.log('Π΄Π²Π°', accCase(2)); // Π΄Π²Π° ΠΏΠ°Π΄Π΅ΠΆΠ°
console.log('ΠΏΡΡΡ', accCase(5)); // ΠΏΡΡΡ ΠΏΠ°Π΄Π΅ΠΆΠ΅ΠΉ
// Instrumental case, ΡΠ²ΠΎΡΠΈΡΠ΅Π»ΡΠ½ΡΠΉ ΠΏΠ°Π΄Π΅ΠΆ, ΠΎΡΡΠ΄Π½ΠΈΠΉ Π²ΡΠ΄ΠΌΡΠ½ΠΎΠΊ
const insCase = declineWrapper('ΠΏΠ°Π΄Π΅ΠΆ', 'ΠΎΠΌ', 'Π°ΠΌΠΈ', 'Π°ΠΌΠΈ');
console.log('ΠΎΠ΄Π½ΠΈΠΌ', insCase(1)); // ΠΎΠ΄Π½ΠΈΠΌ ΠΏΠ°Π΄Π΅ΠΆΠΎΠΌ
console.log('Π΄Π²ΡΠΌΡ', insCase(2)); // Π΄Π²ΡΠΌΡ ΠΏΠ°Π΄Π΅ΠΆΠ°ΠΌΠΈ
console.log('ΠΏΡΡΡΡ', insCase(5)); // ΠΏΡΡΡΡ ΠΏΠ°Π΄Π΅ΠΆΠ°ΠΌΠΈ
// Prepositional case, ΠΏΡΠ΅Π΄Π»ΠΎΠΆΠ½ΡΠΉ ΠΏΠ°Π΄Π΅ΠΆ, ΠΌΡΡΡΠ΅Π²ΠΈΠΉ Π²ΡΠ΄ΠΌΡΠ½ΠΎΠΊ
const prepCase = declineWrapper('ΠΏΠ°Π΄Π΅ΠΆ', 'Π΅', 'Π°Ρ
', 'Π°Ρ
');
console.log('Π½Π° ΠΎΠ΄Π½ΠΎΠΌ', prepCase(1)); // Π½Π° ΠΎΠ΄Π½ΠΎΠΌ ΠΏΠ°Π΄Π΅ΠΆΠ΅
console.log('Π½Π° Π΄Π²ΡΡ
', prepCase(2)); // Π½Π° Π΄Π²ΡΡ
ΠΏΠ°Π΄Π΅ΠΆΠ°Ρ
console.log('Π½Π° ΠΏΡΡΠΈ', prepCase(5)); // Π½Π° ΠΏΡΡΠΈ ΠΏΠ°Π΄Π΅ΠΆΠ°Ρ
Testing
$ npm test
See also
Your improve suggestions and bug reports are welcome any time.