Skip to content

Commit

Permalink
Merge pull request #1672 from KsAkira10/fix/formatting
Browse files Browse the repository at this point in the history
fix: fix formatting when add spacing at the beginning and/end
  • Loading branch information
kamilmysliwiec authored Feb 7, 2024
2 parents 3bad933 + f4c43d2 commit 8b908d6
Show file tree
Hide file tree
Showing 3 changed files with 50 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';
45 changes: 45 additions & 0 deletions test/utils/formatting.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});

0 comments on commit 8b908d6

Please sign in to comment.