diff --git a/GPT.md b/GPT.md index 84003a9..bc8f4d3 100644 --- a/GPT.md +++ b/GPT.md @@ -272,4 +272,20 @@ vsce publish (4) vscode-更换图标主题VSCode Icons._vscode更改不了icon-CSDN博客. https://blog.csdn.net/xiaomizhou66a/article/details/81260347. (5) 【VSCode插件】之插件图标设置 - 掘金. https://juejin.cn/post/7315373969833754624. +========== + +我正在用 typescript 开发 VSCode 插件,我想做一个命名方式转换工具,我已经实现了转换的逻辑(就是传入一个字符串,然后可以将其转换成各种命名方式比如下划线、小驼峰、连字符等),我想实现用户选中一个单词,然后用户多次按下快捷键,实现按照 小驼峰、大驼峰、连字符、下划线、全大写、全小写 的顺序循环转换(或者光标处在一个单词的位置,按下快捷键的时候自动选中这部分,然后再和前面一样的操作),选中区域取消选中,或者选择其他区域,或者用户手动编辑后,这个循环转换顺序就取消。但是我想知道如何记录用户初始的选中内容以便转换呢? + +比如 foo-bar 按顺序就应该是按下 Shift + T 依次转换为 fooBar FooBar foo-bar foo_bar FOO-BAR foo-bar + +如果我直接每次都根据上一次转换结果调用转换函数,那么就变成 fooBar FooBar foo-bar foo_bar FOO_BAR foo_bar(最后面两个的间隔符不对了) + +所以应该每次都需要从用户的输入进行转换 + +这块逻辑应该如何实现呢》特别是当用户按下快捷键后,选中内容已经替换成上次转换的结果了,我怎么知道用户之前的选中以进行下次转换呢 + + +我就是不知道如何通过vscode提供的api来实现,可以把代码在写具体点吗(convertText代码我已经有了,这个不用写了) + +我还是有点不太明白,可以帮我继续完善这个代码吗,谢谢你! diff --git a/package-comment.jsonc b/package-comment.jsonc index 7b14963..e0f8703 100644 --- a/package-comment.jsonc +++ b/package-comment.jsonc @@ -45,37 +45,45 @@ // group-1-camel { "command": "variable-conversion.toCamelCase", - "title": "小驼峰 / 驼峰命名 (Camel Case) [ fooBar ]" + "title": "小驼峰(驼峰)命名 (Camel Case) [ fooBar ]" }, { "command": "variable-conversion.toPascalCase", - "title": "大驼峰 / 帕斯卡命名 (Pascal Case) [ FooBar ]" + "title": "大驼峰(帕斯卡)命名 (Pascal Case) [ FooBar ]" }, // group-2-snake { "command": "variable-conversion.toSnakeCase", - "title": "下划线 / 蛇形命名 (Snake Case) [ foo_bar ]" + "title": "下划线(蛇形)命名 (Snake Case) [ foo_bar ]" }, { "command": "variable-conversion.toSnakeUpperCase", - "title": "下划线大写命名 (Snake Upper Case) [ FOO_BAR ]" + "title": "下划线(蛇形) + 全大写命名 (Snake Upper Case) [ FOO_BAR ]" }, { - "command": "variable-conversion.toCamelSnakeCase", - "title": "驼峰蛇形命名 (Camel Snake Case) [ Foo_Bar ]" + "command": "variable-conversion.toSnakePascalCase", + "title": "下划线(蛇形) + 大驼峰(帕斯卡)命名 (Snake Pascal Case) [ Foo_Bar ]" + }, + { + "command": "variable-conversion.toSnakeCamelCase", + "title": "下划线(蛇形) + 小驼峰(驼峰)命名 (Snake Camel Case) [ foo_Bar ]" }, // group-3-kebab { "command": "variable-conversion.toKebabCase", - "title": "连字符 / 脊柱式命名 (Kebab Case / Spinal Case) [ foo-bar ]" + "title": "连字符(脊柱式)命名 (Kebab/Spinal Case) [ foo-bar ]" }, { "command": "variable-conversion.toKebabUpperCase", - "title": "连字符大写命名 (Kebab Upper Case) [ FOO-BAR ]" + "title": "连字符(脊柱式) + 全大写命名 (Kebab Upper Case) [ FOO-BAR ]" }, { - "command": "variable-conversion.toCamelKebabCase", - "title": "驼峰脊柱式命名 (Kebab Upper Case) [ Foo-Bar ]" + "command": "variable-conversion.toKebabPascalCase", + "title": "连字符(脊柱式) + 大驼峰(帕斯卡)命名 (Kebab Pascal Case) [ Foo-Bar ]" + }, + { + "command": "variable-conversion.toKebabCamelCase", + "title": "连字符(脊柱式) + 小驼峰(驼峰)命名 (Kebab Camel Case) [ foo-Bar ]" }, // group-4-upper-lower { @@ -163,9 +171,13 @@ "group": "group-2-snake@2" }, { - "command": "variable-conversion.toCamelSnakeCase", + "command": "variable-conversion.toSnakePascalCase", "group": "group-2-snake@3" }, + { + "command": "variable-conversion.toSnakeCamelCase", + "group": "group-2-snake@4" + }, { "command": "variable-conversion.toKebabCase", "group": "group-3-kebab@1" @@ -175,9 +187,13 @@ "group": "group-3-kebab@2" }, { - "command": "variable-conversion.toCamelKebabCase", + "command": "variable-conversion.toKebabPascalCase", "group": "group-3-kebab@3" }, + { + "command": "variable-conversion.toKebabCamelCase", + "group": "group-3-kebab@4" + }, { "command": "variable-conversion.toLowerCase", "group": "group-4-upper-lower@1" diff --git a/package.json b/package.json index 5299fb4..ba6a6fe 100644 --- a/package.json +++ b/package.json @@ -35,35 +35,43 @@ }, { "command": "variable-conversion.toCamelCase", - "title": "小驼峰 / 驼峰命名 (Camel Case) [ fooBar ]" + "title": "小驼峰(驼峰)命名 (Camel Case) [ fooBar ]" }, { "command": "variable-conversion.toPascalCase", - "title": "大驼峰 / 帕斯卡命名 (Pascal Case) [ FooBar ]" + "title": "大驼峰(帕斯卡)命名 (Pascal Case) [ FooBar ]" }, { "command": "variable-conversion.toSnakeCase", - "title": "下划线 / 蛇形命名 (Snake Case) [ foo_bar ]" + "title": "下划线(蛇形)命名 (Snake Case) [ foo_bar ]" }, { "command": "variable-conversion.toSnakeUpperCase", - "title": "下划线大写命名 (Snake Upper Case) [ FOO_BAR ]" + "title": "下划线(蛇形) + 全大写命名 (Snake Upper Case) [ FOO_BAR ]" }, { - "command": "variable-conversion.toCamelSnakeCase", - "title": "驼峰蛇形命名 (Camel Snake Case) [ Foo_Bar ]" + "command": "variable-conversion.toSnakePascalCase", + "title": "下划线(蛇形) + 大驼峰(帕斯卡)命名 (Snake Pascal Case) [ Foo_Bar ]" + }, + { + "command": "variable-conversion.toSnakeCamelCase", + "title": "下划线(蛇形) + 小驼峰(驼峰)命名 (Snake Camel Case) [ foo_Bar ]" }, { "command": "variable-conversion.toKebabCase", - "title": "连字符 / 脊柱式命名 (Kebab Case / Spinal Case) [ foo-bar ]" + "title": "连字符(脊柱式)命名 (Kebab/Spinal Case) [ foo-bar ]" }, { "command": "variable-conversion.toKebabUpperCase", - "title": "连字符大写命名 (Kebab Upper Case) [ FOO-BAR ]" + "title": "连字符(脊柱式) + 全大写命名 (Kebab Upper Case) [ FOO-BAR ]" }, { - "command": "variable-conversion.toCamelKebabCase", - "title": "驼峰脊柱式命名 (Kebab Upper Case) [ Foo-Bar ]" + "command": "variable-conversion.toKebabPascalCase", + "title": "连字符(脊柱式) + 大驼峰(帕斯卡)命名 (Kebab Pascal Case) [ Foo-Bar ]" + }, + { + "command": "variable-conversion.toKebabCamelCase", + "title": "连字符(脊柱式) + 小驼峰(驼峰)命名 (Kebab Camel Case) [ foo-Bar ]" }, { "command": "variable-conversion.toLowerCase", @@ -112,9 +120,13 @@ "group": "group-2-snake@2" }, { - "command": "variable-conversion.toCamelSnakeCase", + "command": "variable-conversion.toSnakePascalCase", "group": "group-2-snake@3" }, + { + "command": "variable-conversion.toSnakeCamelCase", + "group": "group-2-snake@4" + }, { "command": "variable-conversion.toKebabCase", "group": "group-3-kebab@1" @@ -124,9 +136,13 @@ "group": "group-3-kebab@2" }, { - "command": "variable-conversion.toCamelKebabCase", + "command": "variable-conversion.toKebabPascalCase", "group": "group-3-kebab@3" }, + { + "command": "variable-conversion.toKebabCamelCase", + "group": "group-3-kebab@4" + }, { "command": "variable-conversion.toLowerCase", "group": "group-4-upper-lower@1" diff --git a/src/extension-handler/editor-submenu-handler.ts b/src/extension-handler/editor-submenu-handler.ts index 077f974..622d2ff 100644 --- a/src/extension-handler/editor-submenu-handler.ts +++ b/src/extension-handler/editor-submenu-handler.ts @@ -1,5 +1,7 @@ import * as vscode from 'vscode'; -import { ConvertFunction, EOL } from '../type-definition/convert-function-type'; +import { EOL } from '../type-definition/EOLType'; +import { caseConversion } from '../main-code/conversion'; +import { SupportCase } from '../type-definition/SupportCaseType'; /** * 编辑器右键菜单 @@ -7,7 +9,7 @@ import { ConvertFunction, EOL } from '../type-definition/convert-function-type'; * @param convertFunction * @returns */ -const handleEditorReplace = (convertFunction: ConvertFunction) => { +const handleEditorReplace = (targetCase: SupportCase) => { // 获取当前编辑器 let editor = vscode.window.activeTextEditor; if (!editor) { @@ -23,7 +25,7 @@ const handleEditorReplace = (convertFunction: ConvertFunction) => { let text = document.getText(selection); // 转换文本 - const converted = convertFunction(text, eol); + const converted = caseConversion(targetCase, text, eol); console.log('converted', converted); // 无法转换时,跳过转换 diff --git a/src/extension-handler/quick-pick-handler.ts b/src/extension-handler/quick-pick-handler.ts index 391edc1..4ec738b 100644 --- a/src/extension-handler/quick-pick-handler.ts +++ b/src/extension-handler/quick-pick-handler.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import QuickPickItemEx from "../type-definition/extended-quick-pick-item-type"; +import QuickPickItemEx from "../type-definition/QuickPickItemExType"; /** * 弹出的提示 diff --git a/src/extension.ts b/src/extension.ts index f3bbd2b..c3ae5d6 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,10 +1,9 @@ // The module 'vscode' contains the VS Code extensibility API // Import the module and reference it with the alias vscode in your code below import * as vscode from 'vscode'; -import * as TextConversion from './main-code/variable-conversion'; -import { ConvertFunction, EOL } from './type-definition/convert-function-type'; import handleEditorReplace from './extension-handler/editor-submenu-handler'; import { handleQuickPick } from './extension-handler/quick-pick-handler'; +import { SupportCase } from './type-definition/SupportCaseType'; // This method is called when your extension is activated // Your extension is activated the very first time the command is executed @@ -42,22 +41,24 @@ export function activate(context: vscode.ExtensionContext) { vscode.window.showInformationMessage('editor is undefined'); } - const commands: Array<{ command: string; convertFunction: ConvertFunction }> = [ - { command: 'variable-conversion.toCamelCase', convertFunction: TextConversion.toCamelCase }, - { command: 'variable-conversion.toPascalCase', convertFunction: TextConversion.toPascalCase }, - { command: 'variable-conversion.toKebabCase', convertFunction: TextConversion.toKebabCase }, - { command: 'variable-conversion.toCamelKebabCase', convertFunction: TextConversion.toCamelKebabCase }, - { command: 'variable-conversion.toKebabUpperCase', convertFunction: TextConversion.toKebabUpperCase }, - // { command: 'variable-conversion.toSnakeCase', convertFunction: TextConversion.toSnakeCase }, - // { command: 'variable-conversion.toCamelSnakeCase', convertFunction: TextConversion.toCamelSnakeCase }, - // { command: 'variable-conversion.toSnakeUpperCase', convertFunction: TextConversion.toSnakeUpperCase }, - { command: 'variable-conversion.toUpperCase', convertFunction: TextConversion.toUpperCase }, - { command: 'variable-conversion.toLowerCase', convertFunction: TextConversion.toLowerCase }, + const commands: Array<{ command: string; targetCase: SupportCase }> = [ + { command: 'variable-conversion.toCamelCase', targetCase: SupportCase.CAMEL_CASE }, + { command: 'variable-conversion.toPascalCase', targetCase: SupportCase.PASCAL_CASE }, + { command: 'variable-conversion.toSnakeCase', targetCase: SupportCase.SNAKE_CASE }, + { command: 'variable-conversion.toSnakeUpperCase', targetCase: SupportCase.SNAKE_UPPER_CASE }, + { command: 'variable-conversion.toSnakePascalCase', targetCase: SupportCase.SNAKE_PASCAL_CASE }, + { command: 'variable-conversion.toSnakeCamelCase', targetCase: SupportCase.SNAKE_CAMEL_CASE }, + { command: 'variable-conversion.toKebabCase', targetCase: SupportCase.KEBAB_CASE }, + { command: 'variable-conversion.toKebabUpperCase', targetCase: SupportCase.KEBAB_UPPER_CASE }, + { command: 'variable-conversion.toKebabPascalCase', targetCase: SupportCase.KEBAB_PASCAL_CASE }, + { command: 'variable-conversion.toKebabCamelCase', targetCase: SupportCase.KEBAB_CAMEL_CASE }, + { command: 'variable-conversion.toLowerCase', targetCase: SupportCase.LOWER_CASE }, + { command: 'variable-conversion.toUpperCase', targetCase: SupportCase.UPPER_CASE }, ]; - for (const { command, convertFunction } of commands) { + for (const { command, targetCase } of commands) { let disposable = vscode.commands.registerCommand(command, () => { - handleEditorReplace(convertFunction); + handleEditorReplace(targetCase); }); context.subscriptions.push(disposable); } diff --git a/src/main-code/conversion.ts b/src/main-code/conversion.ts new file mode 100644 index 0000000..f414b81 --- /dev/null +++ b/src/main-code/conversion.ts @@ -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 = transformMutliLineText(str); + // console.log('results', results); + + const transformedLines: Array = []; + for (const result of results) { + // Post Process 后置处理 + const words = result.trimResult.split('|'); + + let isFirstWord: boolean = true;// [驼峰写法] 用于判断首词小写 + let isPreviousWordSpecial = true; // 用于判断上一个词是 单词 or 特殊字符 + const transformedWords: Array = []; + 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); +} diff --git a/src/main-code/variable-transform.ts b/src/main-code/transform.ts similarity index 96% rename from src/main-code/variable-transform.ts rename to src/main-code/transform.ts index d1f961e..f9a848d 100644 --- a/src/main-code/variable-transform.ts +++ b/src/main-code/transform.ts @@ -1,4 +1,4 @@ -import { TransformTextResult } from "../type-definition/variable-transform-type"; +import { TransformTextResult } from "../type-definition/TransformTextResultType"; const logDebugInfo = false; diff --git a/src/main-code/variable-conversion.ts b/src/main-code/variable-conversion.ts deleted file mode 100644 index 169cb18..0000000 --- a/src/main-code/variable-conversion.ts +++ /dev/null @@ -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 = transformMutliLineText(str); - // console.log('results', results); - - const transformedLines: Array = []; - for (const result of results) { - // Post Process 后置处理 - const words = result.trimResult.split('|'); - let isFirstWord: boolean = true;// 用于判断首词小写 - const camelCaseWords: Array = []; - 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 = transformMutliLineText(str); - // console.log('results', results); - - const transformedLines: Array = []; - 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 = transformMutliLineText(str); - // console.log('results', results); - - const transformedLines: Array = []; - 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 = transformMutliLineText(str); - // console.log('results', results); - - const transformedLines: Array = []; - 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(); -}; diff --git a/src/test/extension.test.ts b/src/test/extension.test.ts index 9006287..a4366b5 100644 --- a/src/test/extension.test.ts +++ b/src/test/extension.test.ts @@ -4,10 +4,11 @@ import * as assert from 'assert'; // as well as import your extension to test it import * as vscode from 'vscode'; import testGroups from './test-case'; -import { TestCase, TestCaseGroup } from '../type-definition/test-case-type'; -import { transformMutliLineText, transformText } from '../main-code/variable-transform'; -import { toCamelCase, toCamelKebabCase, toKebabCase, toKebabUpperCase, toLowerCase, toPascalCase, toUpperCase } from '../main-code/variable-conversion'; -import { TransformTextResult } from '../type-definition/variable-transform-type'; +import { TestCase, TestCaseGroup } from '../type-definition/TestCaseType'; +import { transformMutliLineText, transformText } from '../main-code/transform'; +import { caseConversion } from '../main-code/conversion'; +import { SupportCase } from '../type-definition/SupportCaseType'; +import { TransformTextResult } from '../type-definition/TransformTextResultType'; // import * as myExtension from '../../extension'; /* @@ -24,9 +25,6 @@ suite('Extension Test Suite', () => { suite('Extension Test: run test case', () => { vscode.window.showInformationMessage('Start all tests.'); - // transform to camel case - - const groups: Array = testGroups; for (const testGroup of groups) { const testTitle = testGroup.testTitle; @@ -51,17 +49,22 @@ suite('Extension Test: run test case', () => { } // 验证转换 for (let eol of eolList) { - assert.strictEqual(testCase.output.camelCase, toCamelCase(input, eol)); - assert.strictEqual(testCase.output.pascalCase, toPascalCase(input, eol)); - if (testCase.output.upperCase !== undefined) { - assert.strictEqual(testCase.output.upperCase, toUpperCase(input, eol)); - } + assert.strictEqual(testCase.output.camelCase, caseConversion(SupportCase.CAMEL_CASE, input, eol)); + assert.strictEqual(testCase.output.pascalCase, caseConversion(SupportCase.PASCAL_CASE, input, eol)); + assert.strictEqual(testCase.output.snakeCase, caseConversion(SupportCase.SNAKE_CASE, input, eol)); + assert.strictEqual(testCase.output.snakeCamelCase, caseConversion(SupportCase.SNAKE_CAMEL_CASE, input, eol)); + assert.strictEqual(testCase.output.snakePascalCase, caseConversion(SupportCase.SNAKE_PASCAL_CASE, input, eol)); + assert.strictEqual(testCase.output.snakeUpperCase, caseConversion(SupportCase.SNAKE_UPPER_CASE, input, eol)); + assert.strictEqual(testCase.output.kebabCase, caseConversion(SupportCase.KEBAB_CASE, input, eol)); + assert.strictEqual(testCase.output.kebabCamelCase, caseConversion(SupportCase.KEBAB_CAMEL_CASE, input, eol)); + assert.strictEqual(testCase.output.kebabPascalCase, caseConversion(SupportCase.KEBAB_PASCAL_CASE, input, eol)); + assert.strictEqual(testCase.output.kebabUpperCase, caseConversion(SupportCase.KEBAB_UPPER_CASE, input, eol)); if (testCase.output.lowerCase !== undefined) { - assert.strictEqual(testCase.output.lowerCase, toLowerCase(input, eol)); + assert.strictEqual(testCase.output.lowerCase, caseConversion(SupportCase.LOWER_CASE, input, eol)); + } + if (testCase.output.upperCase !== undefined) { + assert.strictEqual(testCase.output.upperCase, caseConversion(SupportCase.UPPER_CASE, input, eol)); } - assert.strictEqual(testCase.output.kebabCase, toKebabCase(input, eol)); - assert.strictEqual(testCase.output.camelkebabCase, toCamelKebabCase(input, eol)); - assert.strictEqual(testCase.output.kebabUpperCase, toKebabUpperCase(input, eol)); } } }); diff --git a/src/test/test-case.ts b/src/test/test-case.ts index 424c509..80b5001 100644 --- a/src/test/test-case.ts +++ b/src/test/test-case.ts @@ -1,4 +1,4 @@ -import { TestCaseGroup } from "../type-definition/test-case-type"; +import { TestCaseGroup } from "../type-definition/TestCaseType"; const LF = '\n'; const CRLF = '\r\n'; @@ -19,11 +19,16 @@ const testGroups: Array = [ output: { camelCase: '', pascalCase: '', - upperCase: '', - lowerCase: '', + snakeCase: '', + snakeCamelCase: '', + snakePascalCase: '', + snakeUpperCase: '', kebabCase: '', - camelkebabCase: '', + kebabCamelCase: '', + kebabPascalCase: '', kebabUpperCase: '', + lowerCase: '', + upperCase: '', }, }, { @@ -36,11 +41,16 @@ const testGroups: Array = [ output: { camelCase: ' ', pascalCase: ' ', - upperCase: ' ', - lowerCase: ' ', + snakeCase: ' ', + snakeCamelCase: ' ', + snakePascalCase: ' ', + snakeUpperCase: ' ', kebabCase: ' ', - camelkebabCase: ' ', + kebabCamelCase: ' ', + kebabPascalCase: ' ', kebabUpperCase: ' ', + lowerCase: ' ', + upperCase: ' ', }, }, { @@ -54,11 +64,16 @@ const testGroups: Array = [ output: { camelCase: ' \r\n ', pascalCase: ' \r\n ', - upperCase: ' \r\n ', - lowerCase: ' \r\n ', + snakeCase: ' \r\n ', + snakeCamelCase: ' \r\n ', + snakePascalCase: ' \r\n ', + snakeUpperCase: ' \r\n ', kebabCase: ' \r\n ', - camelkebabCase: ' \r\n ', + kebabCamelCase: ' \r\n ', + kebabPascalCase: ' \r\n ', kebabUpperCase: ' \r\n ', + lowerCase: ' \r\n ', + upperCase: ' \r\n ', }, }, { @@ -72,11 +87,16 @@ const testGroups: Array = [ output: { camelCase: ' x \r\ny ', pascalCase: ' X \r\nY ', - upperCase: ' X \r\nY ', - lowerCase: ' x \r\ny ', + snakeCase: ' x \r\ny ', + snakeCamelCase: ' x \r\ny ', + snakePascalCase: ' X \r\nY ', + snakeUpperCase: ' X \r\nY ', kebabCase: ' x \r\ny ', - camelkebabCase: ' X \r\nY ', + kebabCamelCase: ' x \r\ny ', + kebabPascalCase: ' X \r\nY ', kebabUpperCase: ' X \r\nY ', + lowerCase: ' x \r\ny ', + upperCase: ' X \r\nY ', }, }, { @@ -90,11 +110,16 @@ const testGroups: Array = [ output: { camelCase: ' \n ', pascalCase: ' \n ', - upperCase: ' \n ', - lowerCase: ' \n ', + snakeCase: ' \n ', + snakeCamelCase: ' \n ', + snakePascalCase: ' \n ', + snakeUpperCase: ' \n ', kebabCase: ' \n ', - camelkebabCase: ' \n ', + kebabCamelCase: ' \n ', + kebabPascalCase: ' \n ', kebabUpperCase: ' \n ', + lowerCase: ' \n ', + upperCase: ' \n ', }, }, { @@ -108,11 +133,16 @@ const testGroups: Array = [ output: { camelCase: ' a\nb ', pascalCase: ' A\nB ', - upperCase: ' A\nB ', - lowerCase: ' a\nb ', + snakeCase: ' a\nb ', + snakeCamelCase: ' a\nb ', + snakePascalCase: ' A\nB ', + snakeUpperCase: ' A\nB ', kebabCase: ' a\nb ', - camelkebabCase: ' A\nB ', + kebabCamelCase: ' a\nb ', + kebabPascalCase: ' A\nB ', kebabUpperCase: ' A\nB ', + lowerCase: ' a\nb ', + upperCase: ' A\nB ', }, }, // 输入长文本 @@ -132,21 +162,36 @@ const testGroups: Array = [ pascalCase: "China'SFactoryActivityExpandedInMarchAfterFiveConsecutiveMonthsOfContraction,AnOfficialSurveyRevealedOnSunday,AddingToARunOfIndicatorsThatSuggestTheStabilizationOfTheWorld'SSecondLargestEconomy." , - upperCase: - "CHINA'S FACTORY ACTIVITY EXPANDED IN MARCH AFTER FIVE CONSECUTIVE MONTHS OF CONTRACTION, AN OFFICIAL SURVEY REVEALED ON SUNDAY, ADDING TO A RUN OF INDICATORS THAT SUGGEST THE STABILIZATION OF THE WORLD'S SECOND-LARGEST ECONOMY." + snakeCase: + "china's_factory_activity_expanded_in_march_after_five_consecutive_months_of_contraction,an_official_survey_revealed_on_sunday,adding_to_a_run_of_indicators_that_suggest_the_stabilization_of_the_world's_second_largest_economy." , - lowerCase: - "china's factory activity expanded in march after five consecutive months of contraction, an official survey revealed on sunday, adding to a run of indicators that suggest the stabilization of the world's second-largest economy." + snakeCamelCase: + "china'S_Factory_Activity_Expanded_In_March_After_Five_Consecutive_Months_Of_Contraction,An_Official_Survey_Revealed_On_Sunday,Adding_To_A_Run_Of_Indicators_That_Suggest_The_Stabilization_Of_The_World'S_Second_Largest_Economy." + , + snakePascalCase: + "China'S_Factory_Activity_Expanded_In_March_After_Five_Consecutive_Months_Of_Contraction,An_Official_Survey_Revealed_On_Sunday,Adding_To_A_Run_Of_Indicators_That_Suggest_The_Stabilization_Of_The_World'S_Second_Largest_Economy." + , + snakeUpperCase: + "CHINA'S_FACTORY_ACTIVITY_EXPANDED_IN_MARCH_AFTER_FIVE_CONSECUTIVE_MONTHS_OF_CONTRACTION,AN_OFFICIAL_SURVEY_REVEALED_ON_SUNDAY,ADDING_TO_A_RUN_OF_INDICATORS_THAT_SUGGEST_THE_STABILIZATION_OF_THE_WORLD'S_SECOND_LARGEST_ECONOMY." , kebabCase: "china's-factory-activity-expanded-in-march-after-five-consecutive-months-of-contraction,an-official-survey-revealed-on-sunday,adding-to-a-run-of-indicators-that-suggest-the-stabilization-of-the-world's-second-largest-economy." , - camelkebabCase: + kebabCamelCase: + "china'S-Factory-Activity-Expanded-In-March-After-Five-Consecutive-Months-Of-Contraction,An-Official-Survey-Revealed-On-Sunday,Adding-To-A-Run-Of-Indicators-That-Suggest-The-Stabilization-Of-The-World'S-Second-Largest-Economy." + , + kebabPascalCase: "China'S-Factory-Activity-Expanded-In-March-After-Five-Consecutive-Months-Of-Contraction,An-Official-Survey-Revealed-On-Sunday,Adding-To-A-Run-Of-Indicators-That-Suggest-The-Stabilization-Of-The-World'S-Second-Largest-Economy." , kebabUpperCase: "CHINA'S-FACTORY-ACTIVITY-EXPANDED-IN-MARCH-AFTER-FIVE-CONSECUTIVE-MONTHS-OF-CONTRACTION,AN-OFFICIAL-SURVEY-REVEALED-ON-SUNDAY,ADDING-TO-A-RUN-OF-INDICATORS-THAT-SUGGEST-THE-STABILIZATION-OF-THE-WORLD'S-SECOND-LARGEST-ECONOMY." , + lowerCase: + "china's factory activity expanded in march after five consecutive months of contraction, an official survey revealed on sunday, adding to a run of indicators that suggest the stabilization of the world's second-largest economy." + , + upperCase: + "CHINA'S FACTORY ACTIVITY EXPANDED IN MARCH AFTER FIVE CONSECUTIVE MONTHS OF CONTRACTION, AN OFFICIAL SURVEY REVEALED ON SUNDAY, ADDING TO A RUN OF INDICATORS THAT SUGGEST THE STABILIZATION OF THE WORLD'S SECOND-LARGEST ECONOMY." + , }, }, // 输入包含数字 @@ -160,11 +205,16 @@ const testGroups: Array = [ output: { camelCase: 'entity2Map', pascalCase: 'Entity2Map', - upperCase: 'ENTITY2MAP', - lowerCase: 'entity2map', + snakeCase: 'entity2map', + snakeCamelCase: 'entity2Map', + snakePascalCase: 'Entity2Map', + snakeUpperCase: 'ENTITY2MAP', kebabCase: 'entity2map', - camelkebabCase: 'Entity2Map', + kebabCamelCase: 'entity2Map', + kebabPascalCase: 'Entity2Map', kebabUpperCase: 'ENTITY2MAP', + lowerCase: 'entity2map', + upperCase: 'ENTITY2MAP', }, }, // 输入包含换行 @@ -181,11 +231,16 @@ const testGroups: Array = [ output: { camelCase: 'howDoYou\bDo?\n howDoYou\tDo!', pascalCase: 'HowDoYou\bDo?\n HowDoYou\tDo!', - upperCase: 'HOW DO YOU\bDO?\n HOW DO YOU\tDO!', - lowerCase: 'how do you\bdo?\n how do you\tdo!', + snakeCase: 'how_do_you\bdo?\n how_do_you\tdo!', + snakeCamelCase: 'how_Do_You\bDo?\n how_Do_You\tDo!', + snakePascalCase: 'How_Do_You\bDo?\n How_Do_You\tDo!', + snakeUpperCase: 'HOW_DO_YOU\bDO?\n HOW_DO_YOU\tDO!', kebabCase: 'how-do-you\bdo?\n how-do-you\tdo!', - camelkebabCase: 'How-Do-You\bDo?\n How-Do-You\tDo!', + kebabCamelCase: 'how-Do-You\bDo?\n how-Do-You\tDo!', + kebabPascalCase: 'How-Do-You\bDo?\n How-Do-You\tDo!', kebabUpperCase: 'HOW-DO-YOU\bDO?\n HOW-DO-YOU\tDO!', + lowerCase: 'how do you\bdo?\n how do you\tdo!', + upperCase: 'HOW DO YOU\bDO?\n HOW DO YOU\tDO!', }, }, // 非英文字符,特殊字符 @@ -201,11 +256,16 @@ const testGroups: Array = [ output: { camelCase: '今天是星期日', pascalCase: '今天是星期日', - upperCase: '今天是星期日', - lowerCase: '今天是星期日', + snakeCase: '今天是星期日', + snakeCamelCase: '今天是星期日', + snakePascalCase: '今天是星期日', + snakeUpperCase: '今天是星期日', kebabCase: '今天是星期日', - camelkebabCase: '今天是星期日', + kebabCamelCase: '今天是星期日', + kebabPascalCase: '今天是星期日', kebabUpperCase: '今天是星期日', + lowerCase: '今天是星期日', + upperCase: '今天是星期日', }, }, { @@ -220,11 +280,16 @@ const testGroups: Array = [ output: { camelCase: '担心你dAf=Coffee—爸妈不在家— ', pascalCase: '担心你DAf=Coffee—爸妈不在家— ', - upperCase: '--担心你DAF_=COFFEE—爸妈不在家_— ', - lowerCase: '--担心你daf_=coffee—爸妈不在家_— ', + snakeCase: '担心你d_af=coffee—爸妈不在家— ', + snakeCamelCase: '担心你d_Af=Coffee—爸妈不在家— ', + snakePascalCase: '担心你D_Af=Coffee—爸妈不在家— ', + snakeUpperCase: '担心你D_AF=COFFEE—爸妈不在家— ', kebabCase: '担心你d-af=coffee—爸妈不在家— ', - camelkebabCase: '担心你D-Af=Coffee—爸妈不在家— ', + kebabCamelCase: '担心你d-Af=Coffee—爸妈不在家— ', + kebabPascalCase: '担心你D-Af=Coffee—爸妈不在家— ', kebabUpperCase: '担心你D-AF=COFFEE—爸妈不在家— ', + lowerCase: '--担心你daf_=coffee—爸妈不在家_— ', + upperCase: '--担心你DAF_=COFFEE—爸妈不在家_— ', }, }, { @@ -239,11 +304,16 @@ const testGroups: Array = [ output: { camelCase: '🥰aCup/OfCoffee🍻,Please!. ', pascalCase: '🥰ACup/OfCoffee🍻,Please!. ', - upperCase: '🥰 A-CUP/_OF COFFEE🍻,-_PLEASE!. ', - lowerCase: '🥰 a-cup/_of coffee🍻,-_please!. ', + snakeCase: '🥰a_cup/of_coffee🍻,please!. ', + snakeCamelCase: '🥰a_Cup/Of_Coffee🍻,Please!. ', + snakePascalCase: '🥰A_Cup/Of_Coffee🍻,Please!. ', + snakeUpperCase: '🥰A_CUP/OF_COFFEE🍻,PLEASE!. ', kebabCase: '🥰a-cup/of-coffee🍻,please!. ', - camelkebabCase: '🥰A-Cup/Of-Coffee🍻,Please!. ', + kebabCamelCase: '🥰a-Cup/Of-Coffee🍻,Please!. ', + kebabPascalCase: '🥰A-Cup/Of-Coffee🍻,Please!. ', kebabUpperCase: '🥰A-CUP/OF-COFFEE🍻,PLEASE!. ', + lowerCase: '🥰 a-cup/_of coffee🍻,-_please!. ', + upperCase: '🥰 A-CUP/_OF COFFEE🍻,-_PLEASE!. ', }, }, { @@ -258,11 +328,16 @@ const testGroups: Array = [ output: { camelCase: 'takeARest😊HaPPy,😢TriSte,Enfadado,驚きました,❤️,笑,😎COol,😳Embarrassed', pascalCase: 'TakeARest😊HaPPy,😢TriSte,Enfadado,驚きました,❤️,笑,😎COol,😳Embarrassed', - upperCase: 'TAKEAREST😊HAPPY,😢TRISTE,ENFADADO, 驚きました,❤️, 笑, 😎COOL, 😳-EMBARRASSED', - lowerCase: 'takearest😊happy,😢triste,enfadado, 驚きました,❤️, 笑, 😎cool, 😳-embarrassed', + snakeCase: 'take_a_rest😊ha_p_py,😢tri_ste,enfadado,驚きました,❤️,笑,😎c_ool,😳embarrassed', + snakeCamelCase: 'take_A_Rest😊Ha_P_Py,😢Tri_Ste,Enfadado,驚きました,❤️,笑,😎C_Ool,😳Embarrassed', + snakePascalCase: 'Take_A_Rest😊Ha_P_Py,😢Tri_Ste,Enfadado,驚きました,❤️,笑,😎C_Ool,😳Embarrassed', + snakeUpperCase: 'TAKE_A_REST😊HA_P_PY,😢TRI_STE,ENFADADO,驚きました,❤️,笑,😎C_OOL,😳EMBARRASSED', kebabCase: 'take-a-rest😊ha-p-py,😢tri-ste,enfadado,驚きました,❤️,笑,😎c-ool,😳embarrassed', - camelkebabCase: 'Take-A-Rest😊Ha-P-Py,😢Tri-Ste,Enfadado,驚きました,❤️,笑,😎C-Ool,😳Embarrassed', + kebabCamelCase: 'take-A-Rest😊Ha-P-Py,😢Tri-Ste,Enfadado,驚きました,❤️,笑,😎C-Ool,😳Embarrassed', + kebabPascalCase: 'Take-A-Rest😊Ha-P-Py,😢Tri-Ste,Enfadado,驚きました,❤️,笑,😎C-Ool,😳Embarrassed', kebabUpperCase: 'TAKE-A-REST😊HA-P-PY,😢TRI-STE,ENFADADO,驚きました,❤️,笑,😎C-OOL,😳EMBARRASSED', + lowerCase: 'takearest😊happy,😢triste,enfadado, 驚きました,❤️, 笑, 😎cool, 😳-embarrassed', + upperCase: 'TAKEAREST😊HAPPY,😢TRISTE,ENFADADO, 驚きました,❤️, 笑, 😎COOL, 😳-EMBARRASSED', }, }, { @@ -277,34 +352,20 @@ const testGroups: Array = [ output: { camelCase: ' nHDAs+90Usz&* ', pascalCase: ' NHDAs+90Usz&* ', - upperCase: ' NHDAS--+90-USZ&* ', - lowerCase: ' nhdas--+90-usz&* ', + snakeCase: ' n_h_d_as+90usz&* ', + snakeCamelCase: ' n_H_D_As+90Usz&* ', + snakePascalCase: ' N_H_D_As+90Usz&* ', + snakeUpperCase: ' N_H_D_AS+90USZ&* ', kebabCase: ' n-h-d-as+90usz&* ', - camelkebabCase: ' N-H-D-As+90Usz&* ', + kebabCamelCase: ' n-H-D-As+90Usz&* ', + kebabPascalCase: ' N-H-D-As+90Usz&* ', kebabUpperCase: ' N-H-D-AS+90USZ&* ', + lowerCase: ' nhdas--+90-usz&* ', + upperCase: ' NHDAS--+90-USZ&* ', }, }, { title: 'Mixed input 1', - input: - '--担心你鸿dAf_=coffee—_— ' - , - eol: [LF, CRLF], - transformText: [ - '担心你鸿|d|af|=|coffee|—|— ', - ], - output: { - camelCase: '担心你鸿dAf=Coffee—— ', - pascalCase: '担心你鸿DAf=Coffee—— ', - upperCase: '--担心你鸿DAF_=COFFEE—_— ', - lowerCase: '--担心你鸿daf_=coffee—_— ', - kebabCase: '担心你鸿d-af=coffee—— ', - camelkebabCase: '担心你鸿D-Af=Coffee—— ', - kebabUpperCase: '担心你鸿D-AF=COFFEE—— ', - }, - }, - { - title: 'Mixed input 2', input: 'fsdi_sdacsaf+desd' , @@ -315,11 +376,16 @@ const testGroups: Array = [ output: { camelCase: 'fsdiSdacsaf+Desd', pascalCase: 'FsdiSdacsaf+Desd', - upperCase: 'FSDI_SDACSAF+DESD', - lowerCase: 'fsdi_sdacsaf+desd', + snakeCase: 'fsdi_sdacsaf+desd', + snakeCamelCase: 'fsdi_Sdacsaf+Desd', + snakePascalCase: 'Fsdi_Sdacsaf+Desd', + snakeUpperCase: 'FSDI_SDACSAF+DESD', kebabCase: 'fsdi-sdacsaf+desd', - camelkebabCase: 'Fsdi-Sdacsaf+Desd', + kebabCamelCase: 'fsdi-Sdacsaf+Desd', + kebabPascalCase: 'Fsdi-Sdacsaf+Desd', kebabUpperCase: 'FSDI-SDACSAF+DESD', + lowerCase: 'fsdi_sdacsaf+desd', + upperCase: 'FSDI_SDACSAF+DESD', }, }, // add more cases... @@ -344,11 +410,16 @@ const testGroups: Array = [ output: { camelCase: 'fooBar', pascalCase: 'FooBar', - // upperCase: 'FOO--BAR', - // lowerCase: 'foo--bar', + snakeCase: 'foo_bar', + snakeCamelCase: 'foo_Bar', + snakePascalCase: 'Foo_Bar', + snakeUpperCase: 'FOO_BAR', kebabCase: 'foo-bar', - camelkebabCase: 'Foo-Bar', + kebabCamelCase: 'foo-Bar', + kebabPascalCase: 'Foo-Bar', kebabUpperCase: 'FOO-BAR', + // lowerCase: 'foo--bar', + // upperCase: 'FOO--BAR', }, }, { @@ -368,11 +439,16 @@ const testGroups: Array = [ output: { camelCase: 'testCase', pascalCase: 'TestCase', - // upperCase: 'TEST CASE', - // lowerCase: 'test case', + snakeCase: 'test_case', + snakeCamelCase: 'test_Case', + snakePascalCase: 'Test_Case', + snakeUpperCase: 'TEST_CASE', kebabCase: 'test-case', - camelkebabCase: 'Test-Case', + kebabCamelCase: 'test-Case', + kebabPascalCase: 'Test-Case', kebabUpperCase: 'TEST-CASE', + // lowerCase: 'test case', + // upperCase: 'TEST CASE', }, }, { @@ -389,11 +465,16 @@ const testGroups: Array = [ output: { camelCase: 'apple', pascalCase: 'Apple', - upperCase: 'APPLE', - lowerCase: 'apple', + snakeCase: 'apple', + snakeCamelCase: 'apple', + snakePascalCase: 'Apple', + snakeUpperCase: 'APPLE', kebabCase: 'apple', - camelkebabCase: 'Apple', + kebabCamelCase: 'apple', + kebabPascalCase: 'Apple', kebabUpperCase: 'APPLE', + lowerCase: 'apple', + upperCase: 'APPLE', }, }, { @@ -408,11 +489,16 @@ const testGroups: Array = [ output: { camelCase: 'pineApple', pascalCase: 'PineApple', - upperCase: 'PINEAPPLE', - lowerCase: 'pineapple', + snakeCase: 'pine_apple', + snakeCamelCase: 'pine_Apple', + snakePascalCase: 'Pine_Apple', + snakeUpperCase: 'PINE_APPLE', kebabCase: 'pine-apple', - camelkebabCase: 'Pine-Apple', + kebabCamelCase: 'pine-Apple', + kebabPascalCase: 'Pine-Apple', kebabUpperCase: 'PINE-APPLE', + lowerCase: 'pineapple', + upperCase: 'PINEAPPLE', }, }, { @@ -436,11 +522,16 @@ const testGroups: Array = [ output: { camelCase: 'haveANiceDay!', pascalCase: 'HaveANiceDay!', - // upperCase: 'HAVE A NICE DAY!', - // lowerCase: 'have a nice day!', + snakeCase: 'have_a_nice_day!', + snakeCamelCase: 'have_A_Nice_Day!', + snakePascalCase: 'Have_A_Nice_Day!', + snakeUpperCase: 'HAVE_A_NICE_DAY!', kebabCase: 'have-a-nice-day!', - camelkebabCase: 'Have-A-Nice-Day!', + kebabCamelCase: 'have-A-Nice-Day!', + kebabPascalCase: 'Have-A-Nice-Day!', kebabUpperCase: 'HAVE-A-NICE-DAY!', + // lowerCase: 'have a nice day!', + // upperCase: 'HAVE A NICE DAY!', }, }, { @@ -462,11 +553,16 @@ const testGroups: Array = [ output: { camelCase: ' aNiceDay! ', pascalCase: ' ANiceDay! ', - // upperCase: ' A NICE DAY! ', - // lowerCase: ' a nice day! ', + snakeCase: ' a_nice_day! ', + snakeCamelCase: ' a_Nice_Day! ', + snakePascalCase: ' A_Nice_Day! ', + snakeUpperCase: ' A_NICE_DAY! ', kebabCase: ' a-nice-day! ', - camelkebabCase: ' A-Nice-Day! ', + kebabCamelCase: ' a-Nice-Day! ', + kebabPascalCase: ' A-Nice-Day! ', kebabUpperCase: ' A-NICE-DAY! ', + // lowerCase: ' a nice day! ', + // upperCase: ' A NICE DAY! ', }, }, { @@ -482,11 +578,16 @@ const testGroups: Array = [ output: { camelCase: ' aNiceDay', pascalCase: ' ANiceDay', - // upperCase: ' A NICE-DAY-', - // lowerCase: ' a nice-day-', + snakeCase: ' a_nice_day', + snakeCamelCase: ' a_Nice_Day', + snakePascalCase: ' A_Nice_Day', + snakeUpperCase: ' A_NICE_DAY', kebabCase: ' a-nice-day', - camelkebabCase: ' A-Nice-Day', + kebabCamelCase: ' a-Nice-Day', + kebabPascalCase: ' A-Nice-Day', kebabUpperCase: ' A-NICE-DAY', + // lowerCase: ' a nice-day-', + // upperCase: ' A NICE-DAY-', }, }, { @@ -501,18 +602,23 @@ const testGroups: Array = [ output: { camelCase: 'tomLikesEatIceCream.', pascalCase: 'TomLikesEatIceCream.', - upperCase: 'TOMLIKES EAT ICECREAM.', - lowerCase: 'tomlikes eat icecream.', + snakeCase: 'tom_likes_eat_ice_cream.', + snakeCamelCase: 'tom_Likes_Eat_Ice_Cream.', + snakePascalCase: 'Tom_Likes_Eat_Ice_Cream.', + snakeUpperCase: 'TOM_LIKES_EAT_ICE_CREAM.', kebabCase: 'tom-likes-eat-ice-cream.', - camelkebabCase: 'Tom-Likes-Eat-Ice-Cream.', + kebabCamelCase: 'tom-Likes-Eat-Ice-Cream.', + kebabPascalCase: 'Tom-Likes-Eat-Ice-Cream.', kebabUpperCase: 'TOM-LIKES-EAT-ICE-CREAM.', + lowerCase: 'tomlikes eat icecream.', + upperCase: 'TOMLIKES EAT ICECREAM.', }, }, { title: 'Normal input (apple-2-Tree)', input: [ ' apple2-Tree ', - // ' apple2Tree ', + ' apple2Tree ', ], eol: [LF, CRLF], transformText: [ @@ -521,11 +627,16 @@ const testGroups: Array = [ output: { camelCase: ' apple2Tree ', pascalCase: ' Apple2Tree ', - upperCase: ' APPLE2-TREE ', - lowerCase: ' apple2-tree ', + snakeCase: ' apple2tree ', + snakeCamelCase: ' apple2Tree ', + snakePascalCase: ' Apple2Tree ', + snakeUpperCase: ' APPLE2TREE ', kebabCase: ' apple2tree ', - camelkebabCase: ' Apple2Tree ', + kebabCamelCase: ' apple2Tree ', + kebabPascalCase: ' Apple2Tree ', kebabUpperCase: ' APPLE2TREE ', + // lowerCase: ' apple2-tree ', + // upperCase: ' APPLE2-TREE ', }, }, { @@ -540,11 +651,16 @@ const testGroups: Array = [ output: { camelCase: ' juliusCaesar,WilliamShakespeare,AlbertEinstein,MarieCurie,WolfgangAmadeusMozart,VincentVanGogh. ', pascalCase: ' JuliusCaesar,WilliamShakespeare,AlbertEinstein,MarieCurie,WolfgangAmadeusMozart,VincentVanGogh. ', - upperCase: ' JULIUS_CAESAR, WILLIAM_SHAKESPEARE, ALBERT_EINSTEIN, MARIE_CURIE, WOLFGANGAMADEUSMOZART, VINCENT-VAN-GOGH. ', - lowerCase: ' julius_caesar, william_shakespeare, albert_einstein, marie_curie, wolfgangamadeusmozart, vincent-van-gogh. ', + snakeCase: ' julius_caesar,william_shakespeare,albert_einstein,marie_curie,wolfgang_amadeus_mozart,vincent_van_gogh. ', + snakeCamelCase: ' julius_Caesar,William_Shakespeare,Albert_Einstein,Marie_Curie,Wolfgang_Amadeus_Mozart,Vincent_Van_Gogh. ', + snakePascalCase: ' Julius_Caesar,William_Shakespeare,Albert_Einstein,Marie_Curie,Wolfgang_Amadeus_Mozart,Vincent_Van_Gogh. ', + snakeUpperCase: ' JULIUS_CAESAR,WILLIAM_SHAKESPEARE,ALBERT_EINSTEIN,MARIE_CURIE,WOLFGANG_AMADEUS_MOZART,VINCENT_VAN_GOGH. ', kebabCase: ' julius-caesar,william-shakespeare,albert-einstein,marie-curie,wolfgang-amadeus-mozart,vincent-van-gogh. ', - camelkebabCase: ' Julius-Caesar,William-Shakespeare,Albert-Einstein,Marie-Curie,Wolfgang-Amadeus-Mozart,Vincent-Van-Gogh. ', + kebabCamelCase: ' julius-Caesar,William-Shakespeare,Albert-Einstein,Marie-Curie,Wolfgang-Amadeus-Mozart,Vincent-Van-Gogh. ', + kebabPascalCase: ' Julius-Caesar,William-Shakespeare,Albert-Einstein,Marie-Curie,Wolfgang-Amadeus-Mozart,Vincent-Van-Gogh. ', kebabUpperCase: ' JULIUS-CAESAR,WILLIAM-SHAKESPEARE,ALBERT-EINSTEIN,MARIE-CURIE,WOLFGANG-AMADEUS-MOZART,VINCENT-VAN-GOGH. ', + lowerCase: ' julius_caesar, william_shakespeare, albert_einstein, marie_curie, wolfgangamadeusmozart, vincent-van-gogh. ', + upperCase: ' JULIUS_CAESAR, WILLIAM_SHAKESPEARE, ALBERT_EINSTEIN, MARIE_CURIE, WOLFGANGAMADEUSMOZART, VINCENT-VAN-GOGH. ', }, }, { @@ -567,19 +683,31 @@ const testGroups: Array = [ ' &Quot;You&Quot;(Or&Quot;Your&Quot;)ShallMeanAnIndividualOrLegalEntity\n' + ' ExercisingPermissionsGrantedByThisLicense.' , - upperCase: - ' "YOU" (OR "YOUR") SHALL MEAN AN INDIVIDUAL OR LEGAL ENTITY\n' + - ' EXERCISING PERMISSIONS GRANTED BY THIS LICENSE.' + snakeCase: + ' "you"(or"your")shall_mean_an_individual_or_legal_entity\n' + + ' exercising_permissions_granted_by_this_license.' , - lowerCase: - ' "you" (or "your") shall mean an individual or legal entity\n' + - ' exercising permissions granted by this license.' + snakeCamelCase: + ' "You&Quot;(Or&Quot;Your&Quot;)Shall_Mean_An_Individual_Or_Legal_Entity\n' + + ' exercising_Permissions_Granted_By_This_License.' + , + snakePascalCase: + ' &Quot;You&Quot;(Or&Quot;Your&Quot;)Shall_Mean_An_Individual_Or_Legal_Entity\n' + + ' Exercising_Permissions_Granted_By_This_License.' + , + snakeUpperCase: + ' "YOU"(OR"YOUR")SHALL_MEAN_AN_INDIVIDUAL_OR_LEGAL_ENTITY\n' + + ' EXERCISING_PERMISSIONS_GRANTED_BY_THIS_LICENSE.' , kebabCase: ' "you"(or"your")shall-mean-an-individual-or-legal-entity\n' + ' exercising-permissions-granted-by-this-license.' , - camelkebabCase: + kebabCamelCase: + ' "You&Quot;(Or&Quot;Your&Quot;)Shall-Mean-An-Individual-Or-Legal-Entity\n' + + ' exercising-Permissions-Granted-By-This-License.' + , + kebabPascalCase: ' &Quot;You&Quot;(Or&Quot;Your&Quot;)Shall-Mean-An-Individual-Or-Legal-Entity\n' + ' Exercising-Permissions-Granted-By-This-License.' , @@ -587,6 +715,14 @@ const testGroups: Array = [ ' "YOU"(OR"YOUR")SHALL-MEAN-AN-INDIVIDUAL-OR-LEGAL-ENTITY\n' + ' EXERCISING-PERMISSIONS-GRANTED-BY-THIS-LICENSE.' , + lowerCase: + ' "you" (or "your") shall mean an individual or legal entity\n' + + ' exercising permissions granted by this license.' + , + upperCase: + ' "YOU" (OR "YOUR") SHALL MEAN AN INDIVIDUAL OR LEGAL ENTITY\n' + + ' EXERCISING PERMISSIONS GRANTED BY THIS LICENSE.' + , }, }, // add more cases... @@ -616,11 +752,16 @@ const testGroups: Array = [ // output: { // camelCase: '', // pascalCase: '', - // upperCase: '', - // lowerCase: '', + // snakeCase: '', + // snakeCamelCase: '', + // snakePascalCase: '', + // snakeUpperCase: '', // kebabCase: '', - // camelkebabCase: '', + // kebabCamelCase: '', + // kebabPascalCase: '', // kebabUpperCase: '', + // lowerCase: '', + // upperCase: '', // }, // }, // add more cases... diff --git a/src/type-definition/EOLType.ts b/src/type-definition/EOLType.ts new file mode 100644 index 0000000..8dcaf7a --- /dev/null +++ b/src/type-definition/EOLType.ts @@ -0,0 +1 @@ +export type EOL = '\n' | '\r\n'; diff --git a/src/type-definition/extended-quick-pick-item-type.ts b/src/type-definition/QuickPickItemExType.ts similarity index 100% rename from src/type-definition/extended-quick-pick-item-type.ts rename to src/type-definition/QuickPickItemExType.ts diff --git a/src/type-definition/support-case-type.ts b/src/type-definition/SupportCaseType.ts similarity index 59% rename from src/type-definition/support-case-type.ts rename to src/type-definition/SupportCaseType.ts index 5fa0b5f..fd4d8d3 100644 --- a/src/type-definition/support-case-type.ts +++ b/src/type-definition/SupportCaseType.ts @@ -4,6 +4,7 @@ * Code: * - Add `commands`, `menus` parts in package.json (and package-comment.jsonc) * - Add main conversion logic in src/main-code/variable-conversion.ts + * - Add disposable in src/extension.ts * * Test: * - Add test case type definition in src/type-definition/test-case-type.ts @@ -16,8 +17,9 @@ * - Add changes in CHANGELOG.md */ export enum SupportCase { + /** - * 小驼峰 / 驼峰命名 + * 小驼峰(驼峰)命名 * Camel Case * e.g. fooBar * @@ -27,7 +29,7 @@ export enum SupportCase { CAMEL_CASE, /** - * 大驼峰 / 帕斯卡命名 + * 大驼峰(帕斯卡)命名 * Pascal Case * e.g. FooBar * @@ -37,7 +39,47 @@ export enum SupportCase { PASCAL_CASE, /** - * 连字符 / 脊柱式命名 + * 下划线(蛇形)命名 + * Snake Case + * e.g. foo_bar + * + * @alias: snakeCase / SnakeCase / snake case / snake_case / SNAKE_CASE + * @since 2024-04-02 + */ + SNAKE_CASE, + + /** + * 下划线(蛇形) + 小驼峰(驼峰)命名 + * Snake Camel Case + * e.g. foo_Bar + * + * @alias: snakeCamelCase / SnakeCamelCase / snake camel case / snake_camel_case / SNAKE_CAMEL_CASE + * @since 2024-04-02 + */ + SNAKE_CAMEL_CASE, + + /** + * 下划线(蛇形) + 大驼峰(帕斯卡)命名 + * Snake Pascal Case + * e.g. Foo_Bar + * + * @alias: snakePascalCase / SnakePascalCase / snake pascal case / snake_pascal_case / SNAKE_PASCAL_CASE + * @since 2024-04-02 + */ + SNAKE_PASCAL_CASE, + + /** + * 下划线(蛇形) + 全大写命名 + * Snake Upper Case + * e.g. FOO_BAR + * + * @alias: snakeUpperCase / SnakeUpperCase / snake upper case / snake_upper_case / SNAKE_UPPER_CASE + * @since 2024-04-02 + */ + SNAKE_UPPER_CASE, + + /** + * 连字符(脊柱式)命名 * Kebab Case / Spinal Case * e.g. foo-bar * @@ -48,17 +90,27 @@ export enum SupportCase { KEBAB_CASE, /** - * 驼峰脊柱式命名 - * Camel Kebab Case - * e.g. Foo-Bar + * 连字符(脊柱式) + 小驼峰(驼峰)命名 + * Kebab Camel Case + * e.g. foo-Bar * - * @alias: camelkebabCase / CamelKebabCase / camel kebab case / camel_kebab_case / CAMEL_UPPER_CASE + * @alias: kebabCamelCase / KebabCamelCase / kebab camel case / kebab_camel_case / KEBAB_CAMEL_CASE * @since 2024-04-03 */ - CAMEL_UPPER_CASE, + KEBAB_CAMEL_CASE, /** - * 连字符大写命名 + * 连字符(脊柱式) + 大驼峰(帕斯卡)命名 + * Kebab Pascal Case + * e.g. Foo-Bar + * + * @alias: kebabPascalCase / KebabPascalCase / kebab pascal case / kebab_pascal_case / KEBAB_PASCAL_CASE + * @since 2024-04-03 + */ + KEBAB_PASCAL_CASE, + + /** + * 连字符(脊柱式) + 全大写命名 * Kebab Upper Case * e.g. FOO-BAR * @@ -67,46 +119,6 @@ export enum SupportCase { */ KEBAB_UPPER_CASE, - /** - * 下划线 / 蛇形命名 - * Snake Case - * e.g. foo_bar - * - * @alias: snakeCase / SnakeCase / snake case / snake_case / SNAKE_CASE - * @since 2024-04-02 - */ - SNAKE_CASE, - - /** - * 驼峰蛇形命名 - * Camel Snake Case - * e.g. Foo_Bar - * - * @alias: camelSnakeCase / CamelSnakeCase / camel snake case / camel_snake_case / CAMEL_SNAKE_CASE - * @since 2024-04-02 - */ - CAMEL_SNAKE_CASE, - - /** - * 下划线大写命名 - * Snake Upper Case - * e.g. FOO_BAR - * - * @alias: snakeUpperCase / SnakeUpperCase / snake upper case / snake_upper_case / SNAKE_UPPER_CASE - * @since 2024-04-02 - */ - SNAKE_UPPER_CASE, - - /** - * 全大写 - * Upper Case - * e.g. FOO_BAR / FOOBAR - * - * @alias: upperCase / UpperCase / upper case / upper_case / UPPER_CASE - * @since 2024-04-02 - */ - UPPER_CASE, - /** * 全小写 * Lower Case @@ -116,4 +128,29 @@ export enum SupportCase { * @since 2024-04-02 */ LOWER_CASE, + + /** + * 全大写 + * Upper Case + * e.g. FOO_BAR / FOOBAR + * + * @alias: upperCase / UpperCase / upper case / upper_case / UPPER_CASE + * @since 2024-04-02 + */ + UPPER_CASE, } + +export const AllSupportCase = [ + SupportCase.CAMEL_CASE, + SupportCase.PASCAL_CASE, + SupportCase.SNAKE_CASE, + SupportCase.SNAKE_CAMEL_CASE, + SupportCase.SNAKE_PASCAL_CASE, + SupportCase.SNAKE_UPPER_CASE, + SupportCase.KEBAB_CASE, + SupportCase.KEBAB_CAMEL_CASE, + SupportCase.KEBAB_PASCAL_CASE, + SupportCase.KEBAB_UPPER_CASE, + SupportCase.LOWER_CASE, + SupportCase.UPPER_CASE, +]; diff --git a/src/type-definition/test-case-type.ts b/src/type-definition/TestCaseType.ts similarity index 66% rename from src/type-definition/test-case-type.ts rename to src/type-definition/TestCaseType.ts index a627bca..00ddb62 100644 --- a/src/type-definition/test-case-type.ts +++ b/src/type-definition/TestCaseType.ts @@ -1,4 +1,4 @@ -import { EOL } from "./convert-function-type"; +import { EOL } from "./EOLType"; export type TestCaseGroup = { group: string @@ -14,10 +14,15 @@ export type TestCase = { output: { camelCase: string pascalCase: string - upperCase?: string - lowerCase?: string + snakeCase: string + snakeCamelCase: string + snakePascalCase: string + snakeUpperCase: string kebabCase: string - camelkebabCase: string + kebabCamelCase: string + kebabPascalCase: string kebabUpperCase: string + lowerCase?: string + upperCase?: string } }; diff --git a/src/type-definition/variable-transform-type.ts b/src/type-definition/TransformTextResultType.ts similarity index 100% rename from src/type-definition/variable-transform-type.ts rename to src/type-definition/TransformTextResultType.ts diff --git a/src/type-definition/convert-function-type.ts b/src/type-definition/convert-function-type.ts deleted file mode 100644 index 831471d..0000000 --- a/src/type-definition/convert-function-type.ts +++ /dev/null @@ -1,3 +0,0 @@ -export type EOL = '\n' | '\r\n'; - -export type ConvertFunction = (selectionText: string, eol: EOL) => string; \ No newline at end of file