decline-word

Declines words in Russian, Ukrainian and English languages.


Keywords
decline-word, word-decliner, word-declination, word-declension, decline, inflect, singular, plural, склонСниС-слов, падСТ, падСТи
License
ISC
Install
npm install decline-word@1.3.1

Documentation

All dependencies Reported vulnerabilities Commits NPM-version Total downloads Developed by
Publish size Install size Minified size Minified + gzipped size

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.