diff --git a/src/utils/formatting.ts b/src/utils/formatting.ts index 3f9b39bdf..c80202c9f 100644 --- a/src/utils/formatting.ts +++ b/src/utils/formatting.ts @@ -9,7 +9,8 @@ export function normalizeToKebabOrSnakeCase(str: string) { const STRING_DASHERIZE_REGEXP = /\s/g; const STRING_DECAMELIZE_REGEXP = /([a-z\d])([A-Z])/g; return str - .replace(STRING_DECAMELIZE_REGEXP, '$1-$2') - .toLowerCase() - .replace(STRING_DASHERIZE_REGEXP, '-'); + ?.trim() + ?.replace(STRING_DECAMELIZE_REGEXP, '$1-$2') + ?.toLowerCase() + ?.replace(STRING_DASHERIZE_REGEXP, '-'); } diff --git a/src/utils/index.ts b/src/utils/index.ts index 8c3bacc51..ffb5e02ac 100644 --- a/src/utils/index.ts +++ b/src/utils/index.ts @@ -6,3 +6,4 @@ export * from './module.finder'; export * from './name.parser'; export * from './path.solver'; export * from './source-root.helpers'; +export * from './formatting'; diff --git a/test/utils/formatting.test.ts b/test/utils/formatting.test.ts new file mode 100644 index 000000000..ec16308c0 --- /dev/null +++ b/test/utils/formatting.test.ts @@ -0,0 +1,45 @@ +import { normalizeToKebabOrSnakeCase } from '../../src/utils'; + +describe('normalizeToKebabOrSnakeCase', () => { + it('should convert camelCase to kebab-case', () => { + const input = 'camelCaseString'; + const output = normalizeToKebabOrSnakeCase(input); + expect(output).toBe('camel-case-string'); + }); + + it('should replace spaces with dashes', () => { + const input = 'string with spaces'; + const output = normalizeToKebabOrSnakeCase(input); + expect(output).toBe('string-with-spaces'); + }); + + it('should keep underscores', () => { + const input = 'string_with_underscores'; + const output = normalizeToKebabOrSnakeCase(input); + expect(output).toBe('string_with_underscores'); + }); + + it('should handle empty string', () => { + const input = ''; + const output = normalizeToKebabOrSnakeCase(input); + expect(output).toBe(''); + }); + + it('should handle strings with leading/trailing spaces', () => { + const input = ' leading and trailing spaces '; + const output = normalizeToKebabOrSnakeCase(input); + expect(output).toBe('leading-and-trailing-spaces'); + }); + + it('should handle nil value', () => { + const input = null; + const output = normalizeToKebabOrSnakeCase(input); + expect(output).toBe(undefined); + }); + + it('should handle undefined value', () => { + const input = undefined; + const output = normalizeToKebabOrSnakeCase(input); + expect(output).toBe(undefined); + }); +});