diff --git a/packages/shared/src/utils/toKebabCase.ts b/packages/shared/src/utils/toKebabCase.ts index 02e09970e..f178b964b 100644 --- a/packages/shared/src/utils/toKebabCase.ts +++ b/packages/shared/src/utils/toKebabCase.ts @@ -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(); diff --git a/packages/shared/tests/toKebabCase.spec.ts b/packages/shared/tests/toKebabCase.spec.ts index 6aa18b95d..38adfc22c 100644 --- a/packages/shared/tests/toKebabCase.spec.ts +++ b/packages/shared/tests/toKebabCase.spec.ts @@ -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(''); });