File tree Expand file tree Collapse file tree 2 files changed +12
-11
lines changed Expand file tree Collapse file tree 2 files changed +12
-11
lines changed Original file line number Diff line number Diff line change 1
1
{
2
2
"name" : " next-router-segments" ,
3
- "version" : " 1.0.2 " ,
3
+ "version" : " 1.0.3 " ,
4
4
"description" : " Parsing Next.js pathname to structured segments, which you can use in various ways." ,
5
5
"files" : [
6
6
" lib/**/*"
Original file line number Diff line number Diff line change 1
1
export const getRouteSegments = ( pathname : string ) => {
2
- const segments = pathname . split ( '/' ) . filter ( ( segment ) => segment || segment === '' ) ; // Keep empty segments, including the root segment
3
-
2
+ const segments = pathname . split ( '/' ) . filter ( ( segment ) => segment || segment === '' ) ;
3
+
4
4
return segments . map ( ( segment , index ) => {
5
- const slug = segment || 'home' ; // Use '/' as the slug for the root segment
6
- const url = `/${ segments . slice ( 0 , index + 1 ) . join ( '/' ) } ` ; // Construct the URL for each segment
5
+ const slug = segment || 'home' ;
6
+ const urlSegments = segments . slice ( 0 , index + 1 ) ;
7
+ const url = urlSegments . length > 1 ? `${ urlSegments . join ( '/' ) } ` : '/' ;
7
8
const name = segment
8
9
? segment
9
- . split ( '-' )
10
- . map ( ( word ) => word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) ) // Capitalize each word
11
- . join ( ' ' )
12
- : 'Home' ; // Use 'Home' as the name for the root segment
13
-
14
- return { slug, url, name } ;
10
+ . split ( '-' )
11
+ . map ( ( word ) => word . charAt ( 0 ) . toUpperCase ( ) + word . slice ( 1 ) )
12
+ . join ( ' ' )
13
+ : 'Home' ;
14
+
15
+ return { slug, url, name} ;
15
16
} ) ;
16
17
} ;
You can’t perform that action at this time.
0 commit comments