@umatch/pluralize-ptbr
Version:
Obtenha o plural de palavras em português brasileiro
49 lines (48 loc) • 1.7 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.plural = exports.ordinal = void 0;
const ordinal_1 = require("./ordinal");
Object.defineProperty(exports, "ordinal", { enumerable: true, get: function () { return ordinal_1.ordinal; } });
const plural_1 = require("./plural");
/**
* Returns the plural form of the word.
*
* The word is only pluralized if quantity is undefined or not 1. Uses
* the custom plural form if provided.
*
* *Warning*: expects single or compound words, not whole sentences.
* @throws {Error} if splitting the string by spaces or dashes produces more than 3 words.
*/
function plural(word, quantity, customPlural) {
const shouldPluralize = quantity === undefined || Math.abs(quantity) !== 1;
if (shouldPluralize) {
if (customPlural !== undefined)
return customPlural;
const [lowerCase, caps] = uncapitalize(word);
const pluralized = (0, plural_1.getPluralForm)(lowerCase);
return capitalize(pluralized, caps);
}
else {
return word;
}
}
exports.plural = plural;
/**
* Returns the lowercase version of the string and an array of booleans
* indicating whether the letter at each position was uppercase.
*/
function uncapitalize(string) {
const lower = string.toLowerCase();
const diff = lower.split('').map((letter, i) => letter !== string[i]);
return [lower, diff];
}
/**
* Returns the original casing of the string by applying uppercase at
* the correct positions.
*/
function capitalize(string, positions) {
return string
.split('')
.map((letter, i) => (positions[i] ? letter.toUpperCase() : letter))
.join('');
}