Skip to content

Commit e2c30a3

Browse files
authored
Merge pull request #5 from z4nr34l/hotfix-double-slash-in-url
Fixed #4
2 parents da2eb4b + 134b8f1 commit e2c30a3

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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/**/*"

src/index.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
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+
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
};

0 commit comments

Comments
 (0)