Skip to content

Commit

Permalink
fix: fix formatting when add spacing at the beginning and/end
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucas Akira Uehara committed Jan 18, 2024
1 parent 2013047 commit 7e2503e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/utils/formatting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, '-');
}
1 change: 1 addition & 0 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ export * from './module.finder';
export * from './name.parser';
export * from './path.solver';
export * from './source-root.helpers';
export * from './formatting';
33 changes: 33 additions & 0 deletions test/utils/formatting.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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');
});
});

0 comments on commit 7e2503e

Please sign in to comment.