@nestledjs/helpers
Version:
A collection of TypeScript utility types and helper functions for common development tasks
66 lines • 2.23 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.getPluralName = getPluralName;
const tslib_1 = require("tslib");
const pluralize_1 = tslib_1.__importDefault(require("pluralize"));
/**
* List of uncountable nouns that the pluralize library incorrectly pluralizes.
* These words should remain the same in plural form, so we append "List" instead.
*/
const UNCOUNTABLE_OVERRIDES = new Set([
'luggage',
'furniture',
'advice',
'research',
'progress',
'homework',
'housework',
'software',
'hardware',
'traffic',
'weather',
'news',
'evidence',
'feedback',
'knowledge',
'music',
'art',
'love',
'happiness',
'anger',
'beauty',
'courage',
'wisdom'
]);
/**
* Pluralizes a word using the pluralize library, with special handling for words
* where the singular and plural forms are the same (like "data", "sheep").
* In those cases, appends "List" to make it clear it's a collection.
* Also includes overrides for known uncountable nouns that the pluralize library
* incorrectly pluralizes.
*
* @param name - The word to pluralize
* @returns The pluralized form, or the word + "List" if singular equals plural or is uncountable
*
* @example
* ```typescript
* getPluralName('user') // 'users'
* getPluralName('category') // 'categories'
* getPluralName('data') // 'dataList' (because 'data' plural is also 'data')
* getPluralName('sheep') // 'sheepList' (because 'sheep' plural is also 'sheep')
* getPluralName('luggage') // 'luggageList' (override: pluralize incorrectly returns 'luggages')
* getPluralName('furniture') // 'furnitureList' (override: pluralize incorrectly returns 'furnitures')
* ```
*/
function getPluralName(name) {
if (!name || typeof name !== 'string') {
throw new Error('getPluralName: name must be a non-empty string');
}
// Check if this is a known uncountable noun that pluralize incorrectly handles
if (UNCOUNTABLE_OVERRIDES.has(name.toLowerCase())) {
return name + 'List';
}
const plural = (0, pluralize_1.default)(name);
return plural === name ? name + 'List' : plural;
}
//# sourceMappingURL=helpers.js.map
;