decline-word
Version:
Declines words in Russian, Ukrainian and English languages.
39 lines (38 loc) • 1.42 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.declineWrapper = exports.wrap = void 0;
function declineWord(n, mainPart, endingForOne, endingForTwo, endingForFive) {
// eslint-disable-next-line no-param-reassign
n = Math.abs(n % 100);
const n1 = Math.floor(n / 10);
const n2 = n % 10;
/* eslint-disable no-param-reassign */
if (!(mainPart || endingForOne || '').match(/[a-z]/i)) {
endingForOne = endingForOne || '';
endingForTwo = endingForTwo || '';
endingForFive = endingForFive || '';
}
else {
endingForOne = endingForOne != null ? endingForOne : '';
endingForTwo = endingForTwo != null ? endingForTwo : 's';
}
/* eslint-enasble no-param-reassign */
endingForFive = endingForFive != null ? endingForFive : endingForTwo;
if (n1 !== 1) {
if (n2 === 1) {
return `${mainPart}${endingForOne}`;
}
if (n2 >= 2 && n2 <= 4) {
return `${mainPart}${endingForTwo}`;
}
}
return `${mainPart}${endingForFive}`;
}
exports.default = declineWord;
const wrap = (mainPart, ...args) => (n) => declineWord(n, mainPart, ...args);
exports.wrap = wrap;
exports.declineWrapper = exports.wrap;
if (typeof exports.default === 'function') {
module.exports = exports.default;
Object.assign(module.exports, exports);
}
;