fix(shared): handle consecutive capitals and single-letter prefixes in toKebabCase (#5237)

* fix(shared): handle consecutive capitals and single-letter prefixes in toKebabCase

* refactor(shared): simplify toKebabCase regex per review feedback

* Update toKebabCase.ts

* Update toKebabCase.spec.ts

* Update toKebabCase.ts

* fix(packages/shared): fix type error in toKebabCase

* fix(packages/shared): fix unit tests for toKebabCase

---------

Co-authored-by: Karsa <karsa@karsa.org>
Co-authored-by: Karsa <contact@karsa.org>
This commit is contained in:
k l n murthi
2026-11-18 14:13:07 +01:00
committed by GitHub
co-authored by Karsa Karsa
parent 149eba0aea
commit 30a04ff11c
2 changed files with 17 additions and 4 deletions
+3 -3
View File
@@ -1,8 +1,8 @@
/**
* Converts string to kebab case
*
* @param {string} string
* @param {string} value
* @returns {string} A kebabized string
*/
export const toKebabCase = (string: string) =>
string?.replace(/([a-z0-9])([A-Z])/g, '$1-$2').toLowerCase();
export const toKebabCase = (value: string): string =>
(value.match(/\d+[xX]\d+|[A-Z]?[a-z]+|[A-Z]|\d+/g) ?? []).join('-').toLowerCase();
+14 -1
View File
@@ -11,7 +11,7 @@ describe('toKebabCase', () => {
});
it('should handle strings with numbers', () => {
expect(toKebabCase('test123String')).toBe('test123-string');
expect(toKebabCase('test123String')).toBe('test-123-string');
});
it('should handle already kebab-case strings', () => {
@@ -22,6 +22,19 @@ describe('toKebabCase', () => {
expect(toKebabCase('word')).toBe('word');
});
it('should handle consecutive capital letters such as single-letter icon prefixes and mixed numbers', () => {
expect(toKebabCase('AArrowDown')).toBe('a-arrow-down');
expect(toKebabCase('AArrowUp')).toBe('a-arrow-up');
expect(toKebabCase('ALargeSmall')).toBe('a-large-small');
expect(toKebabCase('XLineTop')).toBe('x-line-top');
expect(toKebabCase('Grid3X3')).toBe('grid-3x3');
expect(toKebabCase('Grid3x3')).toBe('grid-3x3');
expect(toKebabCase('Grid2X2Plus')).toBe('grid-2x2-plus');
expect(toKebabCase('ArrowDownAZ')).toBe('arrow-down-a-z');
expect(toKebabCase('ArrowDown01')).toBe('arrow-down-01');
expect(toKebabCase('Clock12')).toBe('clock-12');
});
it('should handle empty string', () => {
expect(toKebabCase('')).toBe('');
});