Skip to content

Commit

Permalink
Added the functionality of removing special characters
Browse files Browse the repository at this point in the history
  • Loading branch information
RipeCherries committed Jan 15, 2025
1 parent 63eb87e commit 3a58136
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 5 deletions.
40 changes: 39 additions & 1 deletion src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,42 @@ describe('slugPress', () => {

expect(actual).toBe(expected);
});
});

test('should remove special characters by default', () => {
const input = 'hello@world!';

const actual = slugPress(input);
const expected = 'hello-world';

expect(actual).toBe(expected);
});

test('should not remove special characters if removeSpecialChars is false', () => {
const input = 'hello@world!';
const options: SlugPressOptions = { removeSpecialChars: false };

const actual = slugPress(input, options);
const expected = 'hello@world!';

expect(actual).toBe(expected);
});

test('should handle input with only special characters', () => {
const input = '@#$%^&*()';

const actual = slugPress(input);
const expected = '';

expect(actual).toBe(expected);
});

test('should not remove special characters but replace spaces with custom separator', () => {
const input = 'hello @ world !';
const options: SlugPressOptions = { separator: '_', removeSpecialChars: false };

const actual = slugPress(input, options);
const expected = 'hello_@_world_!';

expect(actual).toBe(expected);
});
});
13 changes: 9 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
export interface SlugPressOptions {
separator?: string;
removeSpecialChars?: boolean;
}

export const slugPress = (input: string, options: SlugPressOptions = {}): string => {
const {
separator = '-'
} = options;
const { separator = '-', removeSpecialChars = true } = options;

// Removing spaces at the edges of a line
let result = input.trim();

// Deleting all characters except letters, numbers, and spaces
if (removeSpecialChars) {
result = result.replace(/[^\w\s]/g, ' ');
result = result.trim();
}

// Replacing spaces with a separator
result = result.replace(/\s+/g, separator);

return result;
};
};

0 comments on commit 3a58136

Please sign in to comment.