Skip to content

Commit 7e2503e

Browse files
author
Lucas Akira Uehara
committed
fix: fix formatting when add spacing at the beginning and/end
1 parent 2013047 commit 7e2503e

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/utils/formatting.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ export function normalizeToKebabOrSnakeCase(str: string) {
99
const STRING_DASHERIZE_REGEXP = /\s/g;
1010
const STRING_DECAMELIZE_REGEXP = /([a-z\d])([A-Z])/g;
1111
return str
12-
.replace(STRING_DECAMELIZE_REGEXP, '$1-$2')
13-
.toLowerCase()
14-
.replace(STRING_DASHERIZE_REGEXP, '-');
12+
?.trim()
13+
?.replace(STRING_DECAMELIZE_REGEXP, '$1-$2')
14+
?.toLowerCase()
15+
?.replace(STRING_DASHERIZE_REGEXP, '-');
1516
}

src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ export * from './module.finder';
66
export * from './name.parser';
77
export * from './path.solver';
88
export * from './source-root.helpers';
9+
export * from './formatting';

test/utils/formatting.test.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { normalizeToKebabOrSnakeCase } from '../../src/utils';
2+
3+
describe('normalizeToKebabOrSnakeCase', () => {
4+
it('should convert camelCase to kebab-case', () => {
5+
const input = 'camelCaseString';
6+
const output = normalizeToKebabOrSnakeCase(input);
7+
expect(output).toBe('camel-case-string');
8+
});
9+
10+
it('should replace spaces with dashes', () => {
11+
const input = 'string with spaces';
12+
const output = normalizeToKebabOrSnakeCase(input);
13+
expect(output).toBe('string-with-spaces');
14+
});
15+
16+
it('should keep underscores', () => {
17+
const input = 'string_with_underscores';
18+
const output = normalizeToKebabOrSnakeCase(input);
19+
expect(output).toBe('string_with_underscores');
20+
});
21+
22+
it('should handle empty string', () => {
23+
const input = '';
24+
const output = normalizeToKebabOrSnakeCase(input);
25+
expect(output).toBe('');
26+
});
27+
28+
it('should handle strings with leading/trailing spaces', () => {
29+
const input = ' leading and trailing spaces ';
30+
const output = normalizeToKebabOrSnakeCase(input);
31+
expect(output).toBe('leading-and-trailing-spaces');
32+
});
33+
});

0 commit comments

Comments
 (0)