1
0
Code Issues Pull Requests Packages Projects Releases Wiki Activity GitHub Gitee

实现 连字符(脊柱式命名) Kebab Case / Spinal Case,驼峰脊柱式命名 Camel Kebab Case,连字符命名大写 Kebab Upper Case;完善测试用例;通过测试用例

This commit is contained in:
2024-04-03 23:54:38 +08:00
parent e7b62379c9
commit 723d9148b9
8 changed files with 261 additions and 17 deletions

View File

@@ -72,6 +72,88 @@ export const toPascalCase: ConvertFunction = (str: string, eol: EOL): string =>
// return str.replace(/(^\w|_\w)/g, (g) => g.toUpperCase().replace('_', ''));
};
/**
* 转连字符 / 脊柱式命名 to Kebab Case / Spinal Case
*
* @param {string} str user selection
* @returns
* @since 2024-04-03
*/
export const toKebabCase: ConvertFunction = (str: string, eol: EOL): string => {
// Cut text 切割文本
const results: Array<TransformTextResult> = transformMutliLineText(str);
// console.log('results', results);
const transformedLines: Array<string> = [];
for (const result of results) {
const words = result.trimResult.split('|');
let isPreviousWordSpecial = true;
const transformedWords = [];
for (let index = 0; index < words.length; index++) {
const word = words[index];
const isCurrentWordSpecial = !/^[A-Za-z]+$/.test(word);
if (!isPreviousWordSpecial && !isCurrentWordSpecial) {
transformedWords.push('-');
}
transformedWords.push(word);
isPreviousWordSpecial = isCurrentWordSpecial;
}
const transformedLine = result.leadingSpace + transformedWords.join('') + result.trailingSpace;
transformedLines.push(transformedLine);
}
return transformedLines.join(eol);
};
/**
* 转驼峰脊柱式命名 to Camel Kebab Case
*
* @param {string} str user selection
* @returns
* @since 2024-04-03
*/
export const toCamelKebabCase: ConvertFunction = (str: string, eol: EOL): string => {
// Cut text 切割文本
const results: Array<TransformTextResult> = transformMutliLineText(str);
// console.log('results', results);
const transformedLines: Array<string> = [];
for (const result of results) {
const words = result.trimResult.split('|');
let isPreviousWordSpecial = true;
const transformedWords = [];
for (let index = 0; index < words.length; index++) {
const word = words[index];
const isCurrentWordSpecial = !/^[A-Za-z]+$/.test(word);
if (!isPreviousWordSpecial && !isCurrentWordSpecial) {
transformedWords.push('-');
}
const pascalCaseWord = word.charAt(0).toUpperCase() + word.slice(1);
transformedWords.push(pascalCaseWord);
isPreviousWordSpecial = isCurrentWordSpecial;
}
const transformedLine = result.leadingSpace + transformedWords.join('') + result.trailingSpace;
transformedLines.push(transformedLine);
}
return transformedLines.join(eol);
};
/**
* 转连字符命名大写 to Kebab Upper Case
*
* @param {string} str user selection
* @returns
* @since 2024-04-03
*/
export const toKebabUpperCase: ConvertFunction = (str: string, eol: EOL): string => {
return toKebabCase(str, eol).toUpperCase();
};
/**
* 转大写 to Upper Case
*