UNPKG

kira-crud

Version:

Intelligent CRUD Generator for Laravel and Angular

133 lines (122 loc) 3.01 kB
/** * String utility functions for case conversion */ /** * Convert a string to PascalCase * @param {string} str - The input string * @returns {string} The PascalCase string */ function pascalCase(str) { return str .split(/[-_\s]+/) .map(word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase()) .join(''); } /** * Convert a string to camelCase * @param {string} str - The input string * @returns {string} The camelCase string */ function camelCase(str) { const pascal = pascalCase(str); return pascal.charAt(0).toLowerCase() + pascal.slice(1); } /** * Convert a string to kebab-case * @param {string} str - The input string * @returns {string} The kebab-case string */ function kebabCase(str) { return str .replace(/([a-z])([A-Z])/g, '$1-$2') .replace(/[\s_]+/g, '-') .toLowerCase(); } /** * Convert a string to snake_case * @param {string} str - The input string * @returns {string} The snake_case string */ function snakeCase(str) { return str .replace(/([a-z])([A-Z])/g, '$1_$2') .replace(/[\s-]+/g, '_') .toLowerCase(); } /** * Convert a string to CONSTANT_CASE * @param {string} str - The input string * @returns {string} The CONSTANT_CASE string */ function constantCase(str) { return snakeCase(str).toUpperCase(); } /** * Convert a singular word to plural * @param {string} str - The singular word * @returns {string} The plural form */ function pluralize(str) { // Very basic pluralization rules if (str.endsWith('y')) { return str.slice(0, -1) + 'ies'; } else if (str.endsWith('s') || str.endsWith('x') || str.endsWith('z') || str.endsWith('ch') || str.endsWith('sh')) { return str + 'es'; } else { return str + 's'; } } /** * Convert a plural word to singular * @param {string} str - The plural word * @returns {string} The singular form */ function singularize(str) { // Very basic singularization rules if (str.endsWith('ies')) { return str.slice(0, -3) + 'y'; } else if (str.endsWith('es')) { return str.slice(0, -2); } else if (str.endsWith('s') && str.length > 1) { return str.slice(0, -1); } else { return str; } } /** * Capitalize the first letter of a string * @param {string} str - The input string * @returns {string} The capitalized string */ function capitalize(str) { return str.charAt(0).toUpperCase() + str.slice(1); } /** * Decapitalize the first letter of a string * @param {string} str - The input string * @returns {string} The decapitalized string */ function decapitalize(str) { return str.charAt(0).toLowerCase() + str.slice(1); } /** * Convert a string to Title Case * @param {string} str - The input string * @returns {string} The Title Case string */ function titleCase(str) { return str.split(/\s+/).map(capitalize).join(' '); } module.exports = { pascalCase, camelCase, kebabCase, snakeCase, constantCase, pluralize, singularize, capitalize, decapitalize, titleCase };