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 11{
22 "name" : " next-router-segments" ,
3- "version" : " 1.0.2 " ,
3+ "version" : " 1.0.3 " ,
44 "description" : " Parsing Next.js pathname to structured segments, which you can use in various ways." ,
55 "files" : [
66 " lib/**/*"
Original file line number Diff line number Diff line change 11export 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+
44 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 ( '/' ) } ` : '/' ;
78 const name = segment
89 ? 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} ;
1516 } ) ;
1617} ;
You can’t perform that action at this time.
0 commit comments