UNPKG

naming-conventions-modeler

Version:
244 lines 6.69 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.convention = convention; exports.toNoCase = toNoCase; exports.isNoCase = isNoCase; exports.toSnakeCase = toSnakeCase; exports.isSnakeCase = isSnakeCase; exports.toCamelCase = toCamelCase; exports.isCamelCase = isCamelCase; exports.toPascalCase = toPascalCase; exports.isPascalCase = isPascalCase; exports.toMacroCase = toMacroCase; exports.isMacroCase = isMacroCase; exports.toKebabCase = toKebabCase; exports.isKebabCase = isKebabCase; exports.toTrainCase = toTrainCase; exports.isTrainCase = isTrainCase; exports.toFlatCase = toFlatCase; exports.isFlatCase = isFlatCase; /** * It takes a naming convention and returns an object with two functions: `to` and `is` * * @param {NamingConvention} name - NamingConvention * * @returns An object with two properties: to and is. */ function convention(name) { let to; let is; switch (name) { case 'snake_case': to = toSnakeCase; is = isSnakeCase; break; case 'camelCase': to = toCamelCase; is = isCamelCase; break; case 'PascalCase': to = toPascalCase; is = isPascalCase; break; case 'kebab-case': to = toKebabCase; is = isKebabCase; break; case 'MACRO_CASE': to = toMacroCase; is = isMacroCase; break; case 'Train-Case': to = toTrainCase; is = isTrainCase; break; case 'flatcase': to = toFlatCase; is = isFlatCase; break; case 'no case': to = toNoCase; is = isNoCase; break; default: throw new Error('Naming convention is not defined'); } return { to, is }; } /** * It replaces all capital letters with a space and the capital letter, replaces all capital letter * sequences with a space and the capital letter sequence, replaces all spaces, underscores, and * hyphens with a space, converts the string to lowercase, and trims the string * * @param {string} str - The string to convert. * * @returns A string with no case. */ function toNoCase(str) { return str .replace(/[A-Z][a-z]+/g, (m) => ' ' + m) .replace(/[A-Z][A-Z]+/g, (m) => ' ' + m) .replace(/[\s-_]+/g, ' ') .trim(); } /** * If the string is already in no case, return true, otherwise return false. * * @param {string} str - The string to check. * * @returns A boolean value. */ function isNoCase(str) { return toNoCase(str) === str; } /** * It converts a string to snake case by converting it to no case and replacing all spaces with * underscores * * @param {string} str - The string to convert. * * @returns A function that takes a string and returns a string with all the spaces replaced with * underscores. */ function toSnakeCase(str) { return toNoCase(str).toLowerCase().replace(/\s+/g, '_'); } /** * If the string is already in snake case, then return true. * * @param {string} str - The string to check. * * @returns A boolean value. */ function isSnakeCase(str) { return toSnakeCase(str) === str; } /** * Replace all non-word characters with a space, then replace all spaces with nothing, then replace all * lowercase letters that follow a non-word character with an uppercase letter, then replace all spaces * with nothing. * * @param {string} str - The string to convert. * * @returns A string with the first letter capitalized. */ function toCamelCase(str) { return toNoCase(str) .toLowerCase() .replace(/[^\w][a-z]/g, (m) => m.toUpperCase()) .replace(/\s+/g, ''); } /** * If the string is already camelCase, then return true, otherwise return false. * * @param {string} str - The string to check. * * @returns A boolean value. */ function isCamelCase(str) { return toCamelCase(str) === str; } /** * Convert a string to pascal case. * * @param {string} str - The string to convert to PascalCase. * * @returns A string with the first letter capitalized. */ function toPascalCase(str) { return (' ' + toNoCase(str)).replace(/[^\w][a-z]/g, (m) => m.toUpperCase()).replace(/\s+/g, ''); } /** * It returns true if the string is already in PascalCase * * @param {string} str - The string to check. * * @returns A boolean value. */ function isPascalCase(str) { return toPascalCase(str) === str; } /** * It converts a string to a macro case string * * @param {string} str - The string to convert. * * @returns A string with all lowercase letters and spaces replaced with underscores. */ function toMacroCase(str) { return toNoCase(str).toLowerCase().replace(/\s+/g, '_').toUpperCase(); } /** * Convert a string to MacroCase, and return true if the result is the same as the input. * * @param {string} str - The string to convert to MacroCase. * * @returns A boolean value. */ function isMacroCase(str) { return toMacroCase(str) === str; } /** * It takes a string, converts it to no case, and replaces all spaces with dashes * * @param {string} str - The string to convert. * * @returns A function that takes a string and returns a string. */ function toKebabCase(str) { return toNoCase(str).toLowerCase().replace(/\s+/g, '-'); } /** * It returns true if the input string is already in kebab case * * @param {string} str - The string to check. * * @returns true */ function isKebabCase(str) { return toKebabCase(str) === str; } /** * It takes a string, converts it to train case * * @param {string} str - The string to convert. * * @returns A function that takes a string and returns a string. */ function toTrainCase(str) { return (' ' + toNoCase(str)) .replace(/[^\w][a-z]/g, (m) => m.toUpperCase()) .trim() .replace(/\s+/g, '-'); } /** * It returns true if the input string is already in train case * * @param {string} str - The string to check. * * @returns true */ function isTrainCase(str) { return toTrainCase(str) === str; } /** * It takes a string, converts it to flat case, and replaces all spaces with nothing * * @param {string} str - The string to convert. * * @returns A function that takes a string and returns a string. */ function toFlatCase(str) { return toNoCase(str).toLowerCase().replace(/\s+/g, ''); } /** * It returns true if the input string is already in flat case * * @param {string} str - The string to check. * * @returns true */ function isFlatCase(str) { return toFlatCase(str) === str; } //# sourceMappingURL=tools.js.map