@diagramers/cli
Version:
Diagramers CLI - Command-line tools for managing Diagramers projects
183 lines • 5.88 kB
JavaScript
"use strict";
var _a;
Object.defineProperty(exports, "__esModule", { value: true });
exports.NamingUtils = void 0;
// Comprehensive naming utilities for proper table/file naming
class NamingUtils {
/**
* Convert singular to plural with proper rules
*/
static pluralize(word) {
if (!word)
return word;
const lowerWord = word.toLowerCase();
// Check irregular plurals first
if (this.irregularPlurals[lowerWord]) {
return this.preserveCase(word, this.irregularPlurals[lowerWord]);
}
// Handle compound words with underscores
if (word.includes('_')) {
const parts = word.split('_');
const lastPart = parts[parts.length - 1];
const pluralizedLast = this.pluralize(lastPart);
return [...parts.slice(0, -1), pluralizedLast].join('_');
}
// Regular pluralization rules
if (lowerWord.endsWith('y') && !this.isVowel(lowerWord[lowerWord.length - 2])) {
return word.slice(0, -1) + 'ies';
}
if (lowerWord.endsWith('s') || lowerWord.endsWith('ss') ||
lowerWord.endsWith('sh') || lowerWord.endsWith('ch') ||
lowerWord.endsWith('x') || lowerWord.endsWith('z')) {
return word + 'es';
}
if (lowerWord.endsWith('f')) {
return word.slice(0, -1) + 'ves';
}
if (lowerWord.endsWith('fe')) {
return word.slice(0, -2) + 'ves';
}
if (lowerWord.endsWith('o') && !this.isVowel(lowerWord[lowerWord.length - 2])) {
return word + 'es';
}
// Default: just add 's'
return word + 's';
}
/**
* Convert plural to singular with proper rules
*/
static singularize(word) {
if (!word)
return word;
const lowerWord = word.toLowerCase();
// Check irregular singulars first
if (this.irregularSingulars[lowerWord]) {
return this.preserveCase(word, this.irregularSingulars[lowerWord]);
}
// Handle compound words with underscores
if (word.includes('_')) {
const parts = word.split('_');
const lastPart = parts[parts.length - 1];
const singularizedLast = this.singularize(lastPart);
return [...parts.slice(0, -1), singularizedLast].join('_');
}
// Regular singularization rules
if (lowerWord.endsWith('ies') && lowerWord.length > 3) {
return word.slice(0, -3) + 'y';
}
if (lowerWord.endsWith('ves')) {
if (lowerWord.endsWith('ives')) {
return word.slice(0, -3) + 'fe';
}
return word.slice(0, -3) + 'f';
}
if (lowerWord.endsWith('ses') || lowerWord.endsWith('shes') ||
lowerWord.endsWith('ches') || lowerWord.endsWith('xes') ||
lowerWord.endsWith('zes')) {
return word.slice(0, -2);
}
if (lowerWord.endsWith('oes') && lowerWord.length > 3) {
return word.slice(0, -2);
}
// Default: remove 's'
if (lowerWord.endsWith('s') && lowerWord.length > 1) {
return word.slice(0, -1);
}
return word;
}
/**
* Convert to camelCase
*/
static toCamelCase(str) {
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
}
/**
* Convert to PascalCase
*/
static toPascalCase(str) {
const camelCase = this.toCamelCase(str);
return camelCase.charAt(0).toUpperCase() + camelCase.slice(1);
}
/**
* Convert to snake_case
*/
static toSnakeCase(str) {
return str.replace(/[A-Z]/g, letter => `_${letter.toLowerCase()}`);
}
/**
* Convert to kebab-case
*/
static toKebabCase(str) {
return str.replace(/[A-Z]/g, letter => `-${letter.toLowerCase()}`);
}
/**
* Check if character is a vowel
*/
static isVowel(char) {
return 'aeiou'.includes(char?.toLowerCase() || '');
}
/**
* Preserve the case pattern of the original word
*/
static preserveCase(original, transformed) {
if (original === original.toUpperCase()) {
return transformed.toUpperCase();
}
if (original === original.toLowerCase()) {
return transformed.toLowerCase();
}
if (original[0] === original[0].toUpperCase()) {
return transformed.charAt(0).toUpperCase() + transformed.slice(1).toLowerCase();
}
return transformed;
}
}
exports.NamingUtils = NamingUtils;
_a = NamingUtils;
// Irregular plural mappings
NamingUtils.irregularPlurals = {
'person': 'people',
'man': 'men',
'woman': 'women',
'child': 'children',
'tooth': 'teeth',
'foot': 'feet',
'mouse': 'mice',
'goose': 'geese',
'ox': 'oxen',
'deer': 'deer',
'fish': 'fish',
'sheep': 'sheep',
'series': 'series',
'species': 'species',
'datum': 'data',
'medium': 'media',
'criterion': 'criteria',
'phenomenon': 'phenomena',
'analysis': 'analyses',
'basis': 'bases',
'crisis': 'crises',
'diagnosis': 'diagnoses',
'thesis': 'theses',
'index': 'indices',
'matrix': 'matrices',
'vertex': 'vertices',
'appendix': 'appendices',
'cactus': 'cacti',
'focus': 'foci',
'fungus': 'fungi',
'nucleus': 'nuclei',
'radius': 'radii',
'stimulus': 'stimuli',
'alumnus': 'alumni',
'syllabus': 'syllabi'
};
// Irregular singular mappings (reverse of above)
NamingUtils.irregularSingulars = {};
(() => {
// Build reverse mapping
for (const [singular, plural] of Object.entries(_a.irregularPlurals)) {
_a.irregularSingulars[plural] = singular;
}
})();
//# sourceMappingURL=naming-utils.js.map