Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix formatting when add spacing at the beginning and/end #1672

Merged
merged 2 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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, '-');
Comment on lines +12 to +15
Copy link
Member

@micalevisk micalevisk Jan 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure that the str parameter can be nil? 🤔 if yes, please add yet another test to cover this scenario

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are we sure that the str parameter can be nil? 🤔 if yes, please add yet another test to cover this scenario

So I just checked into the source and I saw that all implements with default value ...but I just added more unit tests

}
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);
});
});