File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -13,7 +13,9 @@ function createSlug(title) {
1313
1414 return slug
1515 . replace ( / [ ^ \w \- ] + / g, '' ) // Remove any non word characters
16- . replace ( / \- \- + / g, '-' ) ; // Replace multiple hyphens
16+ . replace ( / \- \- + / g, '-' ) // Replace multiple hyphens with single
17+ . replace ( / ^ - / , '' ) // Remove any leading hyphen
18+ . replace ( / - $ / , '' ) ; // Remove any trailing hyphen
1719}
1820
1921module . exports = createSlug ;
Original file line number Diff line number Diff line change 1+ import createSlug from '../../src/util/createSlug' ;
2+
3+ describe ( 'Tests for createSlug' , ( ) => {
4+ it ( 'generates a slug' , ( ) => {
5+ const slugs = [
6+ 'How to install Node.js' ,
7+ 'The V8 JavaScript Engine' ,
8+ 'The package.json guide' ,
9+ 'Node.js & something.' ,
10+ '/usr/local/nodejs/bin' ,
11+ 'a_b_c' ,
12+ 'a, b and c' ,
13+ 'title: subtitle' ,
14+ 'a; b' ,
15+ 'C:\\Program Files\\nodejs' ,
16+ 'a---b'
17+ ] . map ( createSlug ) ;
18+ expect ( slugs ) . toEqual ( [
19+ 'how-to-install-nodejs' ,
20+ 'the-v8-javascript-engine' ,
21+ 'the-package-json-guide' ,
22+ 'nodejs-and-something' ,
23+ 'usr-local-nodejs-bin' ,
24+ 'a-b-c' ,
25+ 'a-b-and-c' ,
26+ 'title-subtitle' ,
27+ 'a-b' ,
28+ 'c-program-files-nodejs' ,
29+ 'a-b'
30+ ] ) ;
31+ } ) ;
32+ } ) ;
You can’t perform that action at this time.
0 commit comments