Skip to content

Commit 0fb8d72

Browse files
authored
fix(utils): make slugify replace all whitespace (#96777)
When attempting to use our `slugify` util, I realized that it only ever replaced the first whitespace character with `-`. This PR adjusts `slugify` so it more closely matches the built-in Django `slugify` function. Thankfully this is really only used in forms for creating new orgs/projects/teams/monitors, so the impact is minimal. Pinging @malwilley as the most recent person to modify this function (albeit years ago)
1 parent ee0db41 commit 0fb8d72

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

static/app/utils/slugify.spec.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ describe('slugify', function () {
55
expect(slugify('STOPYELLING')).toBe('stopyelling');
66
});
77

8-
it('replaces spaces with a hyphen', function () {
8+
it('replaces space with a hyphen', function () {
99
expect(slugify('STOP YELLING')).toBe('stop-yelling');
1010
});
1111

12+
it('replaces all spaces with a hyphen', function () {
13+
expect(slugify('STOP YELLING AT ME')).toBe('stop-yelling-at-me');
14+
});
15+
1216
it('replaces accented characters', function () {
1317
expect(slugify('Áá')).toBe('aa');
1418
});

static/app/utils/slugify.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,6 @@ export default function slugify(str: string): string {
99
return str
1010
.normalize('NFKD') // Converts accents/ligatures/etc to latin alphabet
1111
.toLowerCase()
12-
.replace(' ', '-')
13-
.replace(/[^a-z0-9-_]/g, ''); // Remove all invalid characters
12+
.replace(/[^a-z0-9_\s-]/g, '') // Remove all invalid characters
13+
.replace(/[-\s]+/g, '-'); // Replace multiple spaces or hyphens with a single hyphen
1414
}

0 commit comments

Comments
 (0)