myex-cli
Version:
Opinionated Express.js framework with CLI tools
121 lines (107 loc) • 2.74 kB
JavaScript
/**
* Convert a string to pascal case (MyVariable)
* @param {string} str - Input string
* @returns {string} Pascal cased string
*/
export function pascalCase(str) {
return str
.split(/[-_\s]+/)
.map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase())
.join('');
}
/**
* Convert a string to camel case (myVariable)
* @param {string} str - Input string
* @returns {string} Camel cased string
*/
export function camelCase(str) {
const pascal = pascalCase(str);
return pascal.charAt(0).toLowerCase() + pascal.slice(1);
}
/**
* Convert a string to kebab case (my-variable)
* @param {string} str - Input string
* @returns {string} Kebab cased string
*/
export function kebabCase(str) {
return str
.replace(/([a-z])([A-Z])/g, '$1-$2')
.replace(/[\s_]+/g, '-')
.toLowerCase();
}
/**
* Convert a string to snake case (my_variable)
* @param {string} str - Input string
* @returns {string} Snake cased string
*/
export function snakeCase(str) {
return str
.replace(/([a-z])([A-Z])/g, '$1_$2')
.replace(/[\s-]+/g, '_')
.toLowerCase();
}
/**
* Pluralize a word using common English rules
* @param {string} str - Input string
* @returns {string} Pluralized string
*/
export function pluralize(str) {
// Handle some irregular plurals
const irregulars = {
'person': 'people',
'child': 'children',
'man': 'men',
'woman': 'women',
'tooth': 'teeth',
'foot': 'feet',
'mouse': 'mice',
'goose': 'geese'
};
if (irregulars[str.toLowerCase()]) {
return irregulars[str.toLowerCase()];
}
// Handle words ending in y
if (str.match(/[^aeiou]y$/i)) {
return str.replace(/y$/i, 'ies');
}
// Handle words ending in s, x, z, ch, sh
if (str.match(/s$|x$|z$|ch$|sh$/i)) {
return str + 'es';
}
// Default: just add s
return str + 's';
}
/**
* Singularize a word using common English rules (approximate)
* @param {string} str - Input string
* @returns {string} Singularized string
*/
export function singularize(str) {
// Handle some irregular plurals
const irregulars = {
'people': 'person',
'children': 'child',
'men': 'man',
'women': 'woman',
'teeth': 'tooth',
'feet': 'foot',
'mice': 'mouse',
'geese': 'goose'
};
if (irregulars[str.toLowerCase()]) {
return irregulars[str.toLowerCase()];
}
// Handle words ending in ies
if (str.match(/ies$/i)) {
return str.replace(/ies$/i, 'y');
}
// Handle words ending in es
if (str.match(/ses$|xes$|zes$|ches$|shes$/i)) {
return str.replace(/es$/i, '');
}
// Default: just remove s
if (str.match(/s$/i) && !str.match(/ss$/i)) {
return str.replace(/s$/i, '');
}
return str;
}