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

@@ -30,9 +30,9 @@ export function activate(context: vscode.ExtensionContext) {
/**
* 编辑器右键菜单
*
* @param convertFunction
* @returns
*
* @param convertFunction
* @returns
*/
const handleEditorReplace = (convertFunction: ConvertFunction) => {
// 获取当前编辑器
@@ -76,6 +76,9 @@ export function activate(context: vscode.ExtensionContext) {
const commands: Array<{ command: string; convertFunction: ConvertFunction }> = [
{ command: 'extension.toCamelCase', convertFunction: TextConversion.toCamelCase },
{ command: 'extension.toPascalCase', convertFunction: TextConversion.toPascalCase },
{ command: 'extension.toKebabCase', convertFunction: TextConversion.toKebabCase },
{ command: 'extension.toCamelKebabCase', convertFunction: TextConversion.toCamelKebabCase },
{ command: 'extension.toKebabUpperCase', convertFunction: TextConversion.toKebabUpperCase },
{ command: 'extension.toUpperCase', convertFunction: TextConversion.toUpperCase },
{ command: 'extension.toLowerCase', convertFunction: TextConversion.toLowerCase },
];

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
*

View File

@@ -6,7 +6,7 @@ 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, toLowerCase, toPascalCase, toUpperCase } from '../main-code/variable-conversion';
import { toCamelCase, toCamelKebabCase, toKebabCase, toKebabUpperCase, toLowerCase, toPascalCase, toUpperCase } from '../main-code/variable-conversion';
import { TransformTextResult } from '../type-definition/variable-transform-type';
// import * as myExtension from '../../extension';
@@ -59,6 +59,9 @@ suite('Extension Test: run test case', () => {
if (testCase.output.lowerCase !== undefined) {
assert.strictEqual(testCase.output.lowerCase, toLowerCase(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));
}
}
});

View File

@@ -21,6 +21,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: '',
upperCase: '',
lowerCase: '',
kebabCase: '',
camelkebabCase: '',
kebabUpperCase: '',
},
},
{
@@ -35,6 +38,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: ' ',
upperCase: ' ',
lowerCase: ' ',
kebabCase: ' ',
camelkebabCase: ' ',
kebabUpperCase: ' ',
},
},
{
@@ -50,6 +56,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: ' \r\n ',
upperCase: ' \r\n ',
lowerCase: ' \r\n ',
kebabCase: ' \r\n ',
camelkebabCase: ' \r\n ',
kebabUpperCase: ' \r\n ',
},
},
{
@@ -65,6 +74,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: ' X \r\nY ',
upperCase: ' X \r\nY ',
lowerCase: ' x \r\ny ',
kebabCase: ' x \r\ny ',
camelkebabCase: ' X \r\nY ',
kebabUpperCase: ' X \r\nY ',
},
},
{
@@ -80,6 +92,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: ' \n ',
upperCase: ' \n ',
lowerCase: ' \n ',
kebabCase: ' \n ',
camelkebabCase: ' \n ',
kebabUpperCase: ' \n ',
},
},
{
@@ -95,6 +110,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: ' A\nB ',
upperCase: ' A\nB ',
lowerCase: ' a\nb ',
kebabCase: ' a\nb ',
camelkebabCase: ' A\nB ',
kebabUpperCase: ' A\nB ',
},
},
// 输入长文本
@@ -120,6 +138,15 @@ const testGroups: Array<TestCaseGroup> = [
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."
,
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:
"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."
,
},
},
// 输入包含数字
@@ -135,6 +162,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: 'Entity2Map',
upperCase: 'ENTITY2MAP',
lowerCase: 'entity2map',
kebabCase: 'entity2map',
camelkebabCase: 'Entity2Map',
kebabUpperCase: 'ENTITY2MAP',
},
},
// 输入包含换行
@@ -153,6 +183,9 @@ const testGroups: Array<TestCaseGroup> = [
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!',
kebabCase: 'how-do-you\bdo?\n how-do-you\tdo!',
camelkebabCase: 'How-Do-You\bDo?\n How-Do-You\tDo!',
kebabUpperCase: 'HOW-DO-YOU\bDO?\n HOW-DO-YOU\tDO!',
},
},
// 非英文字符,特殊字符
@@ -170,6 +203,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: '今天是星期日',
upperCase: '今天是星期日',
lowerCase: '今天是星期日',
kebabCase: '今天是星期日',
camelkebabCase: '今天是星期日',
kebabUpperCase: '今天是星期日',
},
},
{
@@ -186,6 +222,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: '担心你DAf=Coffee—爸妈不在家— ',
upperCase: '--担心你DAF_=COFFEE—爸妈不在家_— ',
lowerCase: '--担心你daf_=coffee—爸妈不在家_— ',
kebabCase: '担心你d-af=coffee—爸妈不在家— ',
camelkebabCase: '担心你D-Af=Coffee—爸妈不在家— ',
kebabUpperCase: '担心你D-AF=COFFEE—爸妈不在家— ',
},
},
{
@@ -202,6 +241,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: '🥰ACup/OfCoffee🍻,Please!. ',
upperCase: '🥰 A-CUP/_OF COFFEE🍻,-_PLEASE!. ',
lowerCase: '🥰 a-cup/_of coffee🍻,-_please!. ',
kebabCase: '🥰a-cup/of-coffee🍻,please!. ',
camelkebabCase: '🥰A-Cup/Of-Coffee🍻,Please!. ',
kebabUpperCase: '🥰A-CUP/OF-COFFEE🍻,PLEASE!. ',
},
},
{
@@ -218,6 +260,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: 'TakeARest😊HaPPy,😢TriSte,Enfadado,驚きました,❤️,笑,😎COol,😳Embarrassed',
upperCase: 'TAKEAREST😊HAPPY,😢TRISTE,ENFADADO, 驚きました,❤️, 笑, 😎COOL, 😳-EMBARRASSED',
lowerCase: 'takearest😊happy,😢triste,enfadado, 驚きました,❤️, 笑, 😎cool, 😳-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',
kebabUpperCase: 'TAKE-A-REST😊HA-P-PY,😢TRI-STE,ENFADADO,驚きました,❤️,笑,😎C-OOL,😳EMBARRASSED',
},
},
{
@@ -234,6 +279,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: ' NHDAs+90Usz&* ',
upperCase: ' NHDAS--+90-USZ&* ',
lowerCase: ' nhdas--+90-usz&* ',
kebabCase: ' n-h-d-as+90usz&* ',
camelkebabCase: ' N-H-D-As+90Usz&* ',
kebabUpperCase: ' N-H-D-AS+90USZ&* ',
},
},
{
@@ -250,6 +298,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: '担心你鸿DAf=Coffee—— ',
upperCase: '--担心你鸿DAF_=COFFEE—_— ',
lowerCase: '--担心你鸿daf_=coffee—_— ',
kebabCase: '担心你鸿d-af=coffee—— ',
camelkebabCase: '担心你鸿D-Af=Coffee—— ',
kebabUpperCase: '担心你鸿D-AF=COFFEE—— ',
},
},
{
@@ -266,6 +317,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: 'FsdiSdacsaf+Desd',
upperCase: 'FSDI_SDACSAF+DESD',
lowerCase: 'fsdi_sdacsaf+desd',
kebabCase: 'fsdi-sdacsaf+desd',
camelkebabCase: 'Fsdi-Sdacsaf+Desd',
kebabUpperCase: 'FSDI-SDACSAF+DESD',
},
},
// add more cases...
@@ -292,6 +346,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: 'FooBar',
// upperCase: 'FOO--BAR',
// lowerCase: 'foo--bar',
kebabCase: 'foo-bar',
camelkebabCase: 'Foo-Bar',
kebabUpperCase: 'FOO-BAR',
},
},
{
@@ -313,6 +370,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: 'TestCase',
// upperCase: 'TEST CASE',
// lowerCase: 'test case',
kebabCase: 'test-case',
camelkebabCase: 'Test-Case',
kebabUpperCase: 'TEST-CASE',
},
},
{
@@ -331,6 +391,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: 'Apple',
upperCase: 'APPLE',
lowerCase: 'apple',
kebabCase: 'apple',
camelkebabCase: 'Apple',
kebabUpperCase: 'APPLE',
},
},
{
@@ -347,6 +410,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: 'PineApple',
upperCase: 'PINEAPPLE',
lowerCase: 'pineapple',
kebabCase: 'pine-apple',
camelkebabCase: 'Pine-Apple',
kebabUpperCase: 'PINE-APPLE',
},
},
{
@@ -372,6 +438,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: 'HaveANiceDay!',
// upperCase: 'HAVE A NICE DAY!',
// lowerCase: 'have a nice day!',
kebabCase: 'have-a-nice-day!',
camelkebabCase: 'Have-A-Nice-Day!',
kebabUpperCase: 'HAVE-A-NICE-DAY!',
},
},
{
@@ -395,6 +464,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: ' ANiceDay! ',
// upperCase: ' A NICE DAY! ',
// lowerCase: ' a nice day! ',
kebabCase: ' a-nice-day! ',
camelkebabCase: ' A-Nice-Day! ',
kebabUpperCase: ' A-NICE-DAY! ',
},
},
{
@@ -412,6 +484,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: ' ANiceDay',
// upperCase: ' A NICE-DAY-',
// lowerCase: ' a nice-day-',
kebabCase: ' a-nice-day',
camelkebabCase: ' A-Nice-Day',
kebabUpperCase: ' A-NICE-DAY',
},
},
{
@@ -428,6 +503,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: 'TomLikesEatIceCream.',
upperCase: 'TOMLIKES EAT ICECREAM.',
lowerCase: 'tomlikes eat icecream.',
kebabCase: 'tom-likes-eat-ice-cream.',
camelkebabCase: 'Tom-Likes-Eat-Ice-Cream.',
kebabUpperCase: 'TOM-LIKES-EAT-ICE-CREAM.',
},
},
{
@@ -445,6 +523,9 @@ const testGroups: Array<TestCaseGroup> = [
pascalCase: ' Apple2Tree ',
upperCase: ' APPLE2-TREE ',
lowerCase: ' apple2-tree ',
kebabCase: ' apple2tree ',
camelkebabCase: ' Apple2Tree ',
kebabUpperCase: ' APPLE2TREE ',
},
},
{
@@ -461,6 +542,9 @@ const testGroups: Array<TestCaseGroup> = [
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. ',
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. ',
kebabUpperCase: ' JULIUS-CAESAR,WILLIAM-SHAKESPEARE,ALBERT-EINSTEIN,MARIE-CURIE,WOLFGANG-AMADEUS-MOZART,VINCENT-VAN-GOGH. ',
},
},
{
@@ -491,6 +575,18 @@ const testGroups: Array<TestCaseGroup> = [
' &quot;you&quot; (or &quot;your&quot;) shall mean an individual or legal entity\n' +
' exercising permissions granted by this license.'
,
kebabCase:
' &quot;you&quot;(or&quot;your&quot;)shall-mean-an-individual-or-legal-entity\n' +
' exercising-permissions-granted-by-this-license.'
,
camelkebabCase:
' &Quot;You&Quot;(Or&Quot;Your&Quot;)Shall-Mean-An-Individual-Or-Legal-Entity\n' +
' Exercising-Permissions-Granted-By-This-License.'
,
kebabUpperCase:
' &QUOT;YOU&QUOT;(OR&QUOT;YOUR&QUOT;)SHALL-MEAN-AN-INDIVIDUAL-OR-LEGAL-ENTITY\n' +
' EXERCISING-PERMISSIONS-GRANTED-BY-THIS-LICENSE.'
,
},
},
// add more cases...
@@ -522,6 +618,9 @@ const testGroups: Array<TestCaseGroup> = [
// pascalCase: '',
// upperCase: '',
// lowerCase: '',
// kebabCase: '',
// camelkebabCase: '',
// kebabUpperCase: '',
// },
// },
// add more cases...

View File

@@ -8,6 +8,7 @@
* Test:
* - Add test case type definition in src/type-definition/test-case-type.ts
* - Add test case in src/test/test-case.ts
* - Add test code in src/test/extension.test.ts
*
* Docs:
* - Add type definition in below `SupportCase` enum
@@ -19,7 +20,7 @@ export enum SupportCase {
* 小驼峰 / 驼峰命名
* Camel Case
* e.g. fooBar
*
*
* @alias: camelCase / CamelCase / camel case / camel_case / CAMEL_CASE
* @since 2024-04-02
*/
@@ -29,7 +30,7 @@ export enum SupportCase {
* 大驼峰 / 帕斯卡命名
* Pascal Case
* e.g. FooBar
*
*
* @alias: pascalCase / PascalCase / pascal case / pascal_case / PASCAL_CASE
* @since 2024-04-02
*/
@@ -39,18 +40,38 @@ export enum SupportCase {
* 连字符 / 脊柱式命名
* Kebab Case / Spinal Case
* e.g. foo-bar
*
*
* @alias: kebabCase / KebabCase / kebab case / kebab_case / KEBAB_CASE
* spinalCase / SpinalCase / spinal case / spinal_case / SPINAL_CASE
* @since 2024-04-02
*/
KEBAB_CASE,
/**
* 驼峰脊柱式命名
* Camel Kebab Case
* e.g. Foo-Bar
*
* @alias: camelkebabCase / CamelKebabCase / camel kebab case / camel_kebab_case / CAMEL_UPPER_CASE
* @since 2024-04-03
*/
CAMEL_UPPER_CASE,
/**
* 连字符命名大写
* Kebab Upper Case
* e.g. FOO-BAR
*
* @alias: kebabUpperCase / KebabUpperCase / kebab upper case / kebab_upper_case / KEBAB_UPPER_CASE
* @since 2024-04-03
*/
KEBAB_UPPER_CASE,
/**
* 下划线 / 蛇形命名
* Snake Case
* e.g. foo_bar
*
*
* @alias: snakeCase / SnakeCase / snake case / snake_case / SNAKE_CASE
* @since 2024-04-02
*/
@@ -60,7 +81,7 @@ export enum SupportCase {
* 驼峰蛇形命名
* Camel Snake Case
* e.g. Foo_Bar
*
*
* @alias: camelSnakeCase / CamelSnakeCase / camel snake case / camel_snake_case / CAMEL_SNAKE_CASE
* @since 2024-04-02
*/
@@ -70,7 +91,7 @@ export enum SupportCase {
* 下划线大写
* Snake Upper Case
* e.g. FOO_BAR
*
*
* @alias: snakeUpperCase / SnakeUpperCase / snake upper case / snake_upper_case / SNAKE_UPPER_CASE
* @since 2024-04-02
*/
@@ -80,7 +101,7 @@ export enum SupportCase {
* 全大写
* Upper Case
* e.g. FOO_BAR / FOOBAR
*
*
* @alias: upperCase / UpperCase / upper case / upper_case / UPPER_CASE
* @since 2024-04-02
*/
@@ -90,7 +111,7 @@ export enum SupportCase {
* 全小写
* Lower Case
* e.g. foo_bar / foobar
*
*
* @alias: lowerCase / LowerCase / lower case / lower_case / LOWER_CASE
* @since 2024-04-02
*/

View File

@@ -15,6 +15,9 @@ export type TestCase = {
camelCase: string
pascalCase: string
upperCase?: string
lowerCase?: string
lowerCase?: string
kebabCase: string
camelkebabCase: string
kebabUpperCase: string
}
};