统一转换方式名称&显示排序;统一转换函数;项目目录结构微调;补全测试用例并全部测试通过
This commit is contained in:
115
src/main-code/conversion.ts
Normal file
115
src/main-code/conversion.ts
Normal file
@@ -0,0 +1,115 @@
|
||||
import { EOL } from '../type-definition/EOLType';
|
||||
import { SupportCase } from '../type-definition/SupportCaseType';
|
||||
import { TransformTextResult } from '../type-definition/TransformTextResultType';
|
||||
import { transformMutliLineText, transformText } from './transform';
|
||||
|
||||
/**
|
||||
* 统一文本转换函数
|
||||
*
|
||||
* @param {SupportCase} targetCase 目标文本情况
|
||||
* @param {string} str 用户选择的文本 user selection
|
||||
* @param {EOL} eol 行结束符
|
||||
* @returns 转换后的文本
|
||||
* @since 2024-04-04
|
||||
*/
|
||||
export function caseConversion(targetCase: SupportCase, str: string, eol: EOL): string {
|
||||
let spaceCharacter: '-' | '_' | ' ' | undefined = undefined;
|
||||
switch (targetCase) {
|
||||
default:
|
||||
case SupportCase.CAMEL_CASE: // 小驼峰(驼峰)命名
|
||||
case SupportCase.PASCAL_CASE: // 大驼峰(帕斯卡)命名
|
||||
spaceCharacter = undefined;
|
||||
break;
|
||||
case SupportCase.SNAKE_CASE: // 下划线(蛇形)命名
|
||||
case SupportCase.SNAKE_CAMEL_CASE: // 下划线(蛇形) + 小驼峰(驼峰)命名
|
||||
case SupportCase.SNAKE_PASCAL_CASE: // 下划线(蛇形) + 大驼峰(帕斯卡)命名
|
||||
case SupportCase.SNAKE_UPPER_CASE: // 下划线(蛇形) + 全大写命名
|
||||
spaceCharacter = '_';
|
||||
break;
|
||||
case SupportCase.KEBAB_CASE: // 连字符(脊柱式)命名
|
||||
case SupportCase.KEBAB_CAMEL_CASE: // 连字符(脊柱式) + 小驼峰(驼峰)命名
|
||||
case SupportCase.KEBAB_PASCAL_CASE: // 连字符(脊柱式) + 大驼峰(帕斯卡)命名
|
||||
case SupportCase.KEBAB_UPPER_CASE: // 连字符(脊柱式) + 全大写命名
|
||||
spaceCharacter = '-';
|
||||
break;
|
||||
case SupportCase.LOWER_CASE: // 全小写
|
||||
return str.toLowerCase();
|
||||
case SupportCase.UPPER_CASE: // 全大写
|
||||
return str.toUpperCase();
|
||||
}
|
||||
|
||||
// Cut text 切割文本
|
||||
const results: Array<TransformTextResult> = transformMutliLineText(str);
|
||||
// console.log('results', results);
|
||||
|
||||
const transformedLines: Array<string> = [];
|
||||
for (const result of results) {
|
||||
// Post Process 后置处理
|
||||
const words = result.trimResult.split('|');
|
||||
|
||||
let isFirstWord: boolean = true;// [驼峰写法] 用于判断首词小写
|
||||
let isPreviousWordSpecial = true; // 用于判断上一个词是 单词 or 特殊字符
|
||||
const transformedWords: Array<string> = [];
|
||||
for (let index = 0; index < words.length; index++) {
|
||||
const word = words[index];
|
||||
|
||||
const firstLetter = word.charAt(0);
|
||||
const pascalCaseWord = firstLetter.toUpperCase() + word.slice(1);
|
||||
|
||||
// 当前 word 是否是特殊单词
|
||||
// const isCurrentWordNormal = firstLetter !== firstLetter.toUpperCase(); // 是小写 a-z (不是特殊字符)
|
||||
const isCurrentWordNormal = /^[A-Za-z]+$/.test(word);
|
||||
// /^[A-Za-z]+$/.test("") false
|
||||
// /^[A-Za-z]+$/.test("苹果") true
|
||||
// /^[A-Za-z]+$/.test("apple") true
|
||||
const isCurrentWordSpecial = !isCurrentWordNormal;
|
||||
|
||||
// 添加连字符
|
||||
if (!isPreviousWordSpecial && !isCurrentWordSpecial) {
|
||||
spaceCharacter !== undefined
|
||||
&& transformedWords.push(spaceCharacter);
|
||||
}
|
||||
|
||||
// 根据目标情况转换单词
|
||||
switch (targetCase) {
|
||||
case SupportCase.CAMEL_CASE: // 小驼峰(驼峰)命名
|
||||
case SupportCase.SNAKE_CAMEL_CASE: // 下划线(蛇形) + 小驼峰(驼峰)命名
|
||||
case SupportCase.KEBAB_CAMEL_CASE: // 连字符(脊柱式) + 小驼峰(驼峰)命名
|
||||
if (isFirstWord) {
|
||||
transformedWords.push(word);
|
||||
if (isCurrentWordNormal) {
|
||||
isFirstWord = false;
|
||||
}
|
||||
} else {
|
||||
transformedWords.push(pascalCaseWord);
|
||||
}
|
||||
break;
|
||||
case SupportCase.PASCAL_CASE: // 大驼峰(帕斯卡)命名
|
||||
case SupportCase.SNAKE_PASCAL_CASE: // 下划线(蛇形) + 大驼峰(帕斯卡)命名
|
||||
case SupportCase.KEBAB_PASCAL_CASE: // 连字符(脊柱式) + 大驼峰(帕斯卡)命名
|
||||
transformedWords.push(pascalCaseWord);
|
||||
break;
|
||||
case SupportCase.SNAKE_CASE: // 下划线(蛇形)命名
|
||||
case SupportCase.KEBAB_CASE: // 转连字符 / 脊柱式命名
|
||||
transformedWords.push(word);
|
||||
break;
|
||||
case SupportCase.SNAKE_UPPER_CASE: // 下划线(蛇形) + 全大写命名
|
||||
case SupportCase.KEBAB_UPPER_CASE: // 连字符(脊柱式) + 全大写命名
|
||||
transformedWords.push(word.toUpperCase());
|
||||
break;
|
||||
default:
|
||||
throw new Error(`Unsupported case: ${targetCase}`);
|
||||
}
|
||||
|
||||
if (word === '\n' || word === '\r\n') {
|
||||
isFirstWord = true; // 换行后,重新计算首词
|
||||
}
|
||||
|
||||
isPreviousWordSpecial = isCurrentWordSpecial;
|
||||
}
|
||||
|
||||
const transformedLine = result.leadingSpace + transformedWords.join('') + result.trailingSpace;
|
||||
transformedLines.push(transformedLine);
|
||||
}
|
||||
return transformedLines.join(eol);
|
||||
}
|
@@ -1,4 +1,4 @@
|
||||
import { TransformTextResult } from "../type-definition/variable-transform-type";
|
||||
import { TransformTextResult } from "../type-definition/TransformTextResultType";
|
||||
|
||||
const logDebugInfo = false;
|
||||
|
@@ -1,177 +0,0 @@
|
||||
import { ConvertFunction, EOL } from '../type-definition/convert-function-type';
|
||||
import { TransformTextResult } from '../type-definition/variable-transform-type';
|
||||
import { transformMutliLineText, transformText } from './variable-transform';
|
||||
|
||||
/**
|
||||
* 转小驼峰 to Camel Case
|
||||
*
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
* @since 2024-03-28
|
||||
*/
|
||||
export const toCamelCase: 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) {
|
||||
// Post Process 后置处理
|
||||
const words = result.trimResult.split('|');
|
||||
let isFirstWord: boolean = true;// 用于判断首词小写
|
||||
const camelCaseWords: Array<string> = [];
|
||||
for (let index = 0; index < words.length; index++) {
|
||||
const word = words[index];
|
||||
const firstLetter = word.charAt(0);
|
||||
const pascalCaseWord = firstLetter.toUpperCase() + word.slice(1);
|
||||
if (isFirstWord) {
|
||||
camelCaseWords.push(word);
|
||||
if (firstLetter !== firstLetter.toUpperCase()) {
|
||||
// 是大写 (A-Z), 再后面的词句不再是首词
|
||||
isFirstWord = false;
|
||||
}
|
||||
} else {
|
||||
camelCaseWords.push(pascalCaseWord);
|
||||
}
|
||||
|
||||
if (word === '\n' || word === '\r\n') {
|
||||
isFirstWord = true; // 换行后,重新计算首词
|
||||
}
|
||||
}
|
||||
const transformedLine = result.leadingSpace + camelCaseWords.join('') + result.trailingSpace;
|
||||
transformedLines.push(transformedLine);
|
||||
}
|
||||
return transformedLines.join(eol);
|
||||
// return str.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
|
||||
};
|
||||
|
||||
/**
|
||||
* 转大驼峰 to Pascal Case
|
||||
*
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
* @since 2024-03-28
|
||||
*/
|
||||
export const toPascalCase: 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('|');
|
||||
const pascalCaseWords = words.map((word, index) => {
|
||||
// 首字母大写
|
||||
return word.charAt(0).toUpperCase() + word.slice(1);
|
||||
});
|
||||
const transformedLine = result.leadingSpace + pascalCaseWords.join('') + result.trailingSpace;
|
||||
transformedLines.push(transformedLine);
|
||||
}
|
||||
return transformedLines.join(eol);
|
||||
|
||||
// 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
|
||||
*
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
* @since 2024-03-28
|
||||
*/
|
||||
export const toUpperCase: ConvertFunction = (str: string, eol: EOL): string => {
|
||||
return str.toUpperCase();
|
||||
};
|
||||
|
||||
/**
|
||||
* 转小写 to Lower Case
|
||||
*
|
||||
* @param {string} str user selection
|
||||
* @returns
|
||||
* @since 2024-03-28
|
||||
*/
|
||||
export const toLowerCase: ConvertFunction = (str: string, eol: EOL): string => {
|
||||
return str.toLowerCase();
|
||||
};
|
Reference in New Issue
Block a user