Skip to content

Commit 6bf0402

Browse files
committed
fix(utils): refactor tests
1 parent da37aeb commit 6bf0402

File tree

3 files changed

+80
-21
lines changed

3 files changed

+80
-21
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { assertType, describe, expectTypeOf, it } from 'vitest';
2+
import type { CamelCaseToKebabCase, KebabCaseToCamelCase } from './types.js';
3+
4+
/* eslint-disable vitest/expect-expect */
5+
describe('CamelCaseToKebabCase', () => {
6+
// ✅ CamelCase → kebab-case Type Tests
7+
8+
it('CamelCaseToKebabCase works correctly', () => {
9+
expectTypeOf<
10+
CamelCaseToKebabCase<'myTestString'>
11+
>().toEqualTypeOf<'my-test-string'>();
12+
expectTypeOf<
13+
CamelCaseToKebabCase<'APIResponse'>
14+
>().toEqualTypeOf<'a-p-i-response'>();
15+
expectTypeOf<
16+
CamelCaseToKebabCase<'myXMLParser'>
17+
>().toEqualTypeOf<'my-x-m-l-parser'>();
18+
expectTypeOf<
19+
CamelCaseToKebabCase<'singleWord'>
20+
>().toEqualTypeOf<'single-word'>();
21+
22+
// @ts-expect-error Ensures that non-camelCase strings do not pass
23+
assertType<CamelCaseToKebabCase<'hello_world'>>();
24+
25+
// @ts-expect-error Numbers should not be transformed
26+
assertType<CamelCaseToKebabCase<'version2Release'>>();
27+
});
28+
29+
// ✅ kebab-case → CamelCase Type Tests
30+
it('KebabCaseToCamelCase works correctly', () => {
31+
expectTypeOf<
32+
KebabCaseToCamelCase<'my-test-string'>
33+
>().toEqualTypeOf<'myTestString'>();
34+
expectTypeOf<
35+
KebabCaseToCamelCase<'a-p-i-response'>
36+
>().toEqualTypeOf<'aPIResponse'>();
37+
expectTypeOf<
38+
KebabCaseToCamelCase<'my-x-m-l-parser'>
39+
>().toEqualTypeOf<'myXMLParser'>();
40+
expectTypeOf<
41+
KebabCaseToCamelCase<'single-word'>
42+
>().toEqualTypeOf<'singleWord'>();
43+
44+
// @ts-expect-error Ensures that non-kebab-case inputs are not accepted
45+
assertType<KebabCaseToCamelCase<'my Test String'>>();
46+
47+
// @ts-expect-error Numbers should not be transformed
48+
assertType<KebabCaseToCamelCase<'version-2-release'>>();
49+
});
50+
51+
// ✅ Edge Cases
52+
it('Edge cases for case conversions', () => {
53+
expectTypeOf<CamelCaseToKebabCase<''>>().toEqualTypeOf<''>();
54+
expectTypeOf<KebabCaseToCamelCase<''>>().toEqualTypeOf<''>();
55+
56+
// @ts-expect-error Ensures no spaces allowed in input
57+
assertType<CamelCaseToKebabCase<'this is not camelCase'>>();
58+
59+
// @ts-expect-error Ensures no mixed case with dashes
60+
assertType<KebabCaseToCamelCase<'this-Is-Wrong'>>();
61+
});
62+
});
63+
/* eslint-enable vitest/expect-expect */

packages/utils/src/lib/case-conversions.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,15 @@ export function kebabCaseToCamelCase<T extends string>(
1616

1717
/**
1818
* Converts a camelCase string to kebab-case.
19-
* @param string - The camelCase string to convert.
19+
* @param input - The camelCase string to convert.
2020
* @returns The kebab-case string.
2121
*/
2222
export function camelCaseToKebabCase<T extends string>(
23-
string: T,
23+
input: T,
2424
): CamelCaseToKebabCase<T> {
25-
return string
26-
.replace(/([A-Z])([A-Z][a-z])/g, '$1-$2') // Split between uppercase followed by uppercase+lowercase
27-
.replace(/([a-z])([A-Z])/g, '$1-$2') // Split between lowercase followed by uppercase
28-
.replace(/([A-Z]+)([A-Z][a-z])/g, '$1-$2') // Additional split for consecutive uppercase
29-
.replace(/[\s_]+/g, '-') // Replace spaces and underscores with hyphens
25+
return input
26+
.replace(/([a-z])([A-Z])/g, '$1-$2') // Insert dash before uppercase letters
27+
.replace(/([A-Z])([A-Z][a-z])/g, '$1-$2') // Handle consecutive uppercase letters
3028
.toLowerCase() as CamelCaseToKebabCase<T>;
3129
}
3230

packages/utils/src/lib/case-conversions.unit.test.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { beforeEach, describe, expect, it } from 'vitest';
1+
import { describe, expect, it } from 'vitest';
22
import {
33
camelCaseToKebabCase,
44
capitalize,
@@ -50,30 +50,28 @@ describe('kebabCaseToCamelCase', () => {
5050
});
5151

5252
describe('camelCaseToKebabCase', () => {
53-
it('should convert simple camelCase to kebab-case', () => {
54-
expect(camelCaseToKebabCase('helloWorld')).toBe('hello-world');
53+
it('should convert camelCase to kebab-case', () => {
54+
expect(camelCaseToKebabCase('myTestString')).toBe('my-test-string');
5555
});
5656

57-
it('should handle multiple capital letters', () => {
58-
expect(camelCaseToKebabCase('thisIsALongString')).toBe(
59-
'this-is-a-long-string',
60-
);
57+
it('should handle acronyms properly', () => {
58+
expect(camelCaseToKebabCase('APIResponse')).toBe('api-response');
6159
});
6260

63-
it('should handle consecutive capital letters', () => {
61+
it('should handle consecutive uppercase letters correctly', () => {
6462
expect(camelCaseToKebabCase('myXMLParser')).toBe('my-xml-parser');
6563
});
6664

67-
it('should handle spaces and underscores', () => {
68-
expect(camelCaseToKebabCase('hello_world test')).toBe('hello-world-test');
65+
it('should handle single-word camelCase', () => {
66+
expect(camelCaseToKebabCase('singleWord')).toBe('single-word');
6967
});
7068

71-
it('should handle single word', () => {
72-
expect(camelCaseToKebabCase('hello')).toBe('hello');
69+
it('should not modify already kebab-case strings', () => {
70+
expect(camelCaseToKebabCase('already-kebab')).toBe('already-kebab');
7371
});
7472

75-
it('should handle empty string', () => {
76-
expect(camelCaseToKebabCase('')).toBe('');
73+
it('should not modify non-camelCase inputs', () => {
74+
expect(camelCaseToKebabCase('not_camelCase')).toBe('not_camel-case');
7775
});
7876
});
7977

0 commit comments

Comments
 (0)