Skip to content

Commit

Permalink
cleanForSlug: Replace multiple hyphens with a single one (#44873)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored Oct 11, 2022
1 parent bac2c08 commit a3360a8
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/url/src/clean-for-slug.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ export function cleanForSlug( string ) {
.replace( /[^\p{L}\p{N}_-]+/gu, '' )
// Convert to lowercase
.toLowerCase()
// Replace multiple hyphens with a single one.
.replace( /-+/g, '-' )
// Remove any remaining leading or trailing hyphens.
.replace( /(^-+)|(-+$)/g, '' )
);
Expand Down
5 changes: 5 additions & 0 deletions packages/url/src/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1004,6 +1004,11 @@ describe( 'cleanForSlug', () => {
'is-tht-deja_vu'
);
} );

it( 'Should replace multiple hyphens with a single one', () => {
expect( cleanForSlug( 'the long - cat' ) ).toBe( 'the-long-cat' );
expect( cleanForSlug( 'the----long---cat' ) ).toBe( 'the-long-cat' );
} );
} );

describe( 'normalizePath', () => {
Expand Down

0 comments on commit a3360a8

Please sign in to comment.