Skip to content

Commit da8657e

Browse files
cosmetics
1 parent 17a3b1a commit da8657e

File tree

4 files changed

+54
-35
lines changed

4 files changed

+54
-35
lines changed

Chapter06/00.2/pages.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export default container => {
1818
const anotherDetail = (params) => {
1919
const { id, anotherId } = params
2020
container
21-
.textContent = `This is Detail Page with Id ${id} and AnotherId ${anotherId}`
21+
.textContent = `
22+
This is Detail Page with Id ${id}
23+
and AnotherId ${anotherId}
24+
`
2225
}
2326

2427
const notFound = () => {

Chapter06/00.2/router.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ const ROUTE_PARAMETER_REGEXP = /:(\w+)/g
22
const URL_FRAGMENT_REGEXP = '([^\\/]+)'
33

44
const extractUrlParams = (route, windowHash) => {
5+
const params = {}
6+
57
if (route.params.length === 0) {
6-
return {}
8+
return params
79
}
810

9-
const params = {}
10-
1111
const matches = windowHash
1212
.match(route.testRegExp)
1313

@@ -28,16 +28,22 @@ export default () => {
2828
const router = {}
2929

3030
const checkRoutes = () => {
31+
const { hash } = window.location
32+
3133
const currentRoute = routes.find(route => {
32-
return route.testRegExp.test(window.location.hash)
34+
const { testRegExp } = route
35+
return testRegExp.test(hash)
3336
})
3437

3538
if (!currentRoute) {
3639
notFound()
3740
return
3841
}
3942

40-
const urlParams = extractUrlParams(currentRoute, window.location.hash)
43+
const urlParams = extractUrlParams(
44+
currentRoute,
45+
window.location.hash
46+
)
4147

4248
currentRoute.component(urlParams)
4349
}
@@ -46,10 +52,15 @@ export default () => {
4652
const params = []
4753

4854
const parsedFragment = fragment
49-
.replace(ROUTE_PARAMETER_REGEXP, (match, paramName) => {
50-
params.push(paramName)
51-
return URL_FRAGMENT_REGEXP
52-
}).replace(/\//g, '\\/')
55+
.replace(
56+
ROUTE_PARAMETER_REGEXP,
57+
(match, paramName) => {
58+
params.push(paramName)
59+
return URL_FRAGMENT_REGEXP
60+
})
61+
.replace(/\//g, '\\/')
62+
63+
console.log(`^${parsedFragment}$`)
5364

5465
routes.push({
5566
testRegExp: new RegExp(`^${parsedFragment}$`),
@@ -71,7 +82,11 @@ export default () => {
7182

7283
router.start = () => {
7384
window
74-
.addEventListener('hashchange', checkRoutes)
85+
.addEventListener(
86+
'hashchange',
87+
checkRoutes
88+
)
89+
7590
if (!window.location.hash) {
7691
window.location.hash = '#/'
7792
}

Chapter06/01.1/router.js

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,7 @@
11
const ROUTE_PARAMETER_REGEXP = /:(\w+)/g
22
const URL_FRAGMENT_REGEXP = '([^\\/]+)'
3-
const NAV_A_SELECTOR = 'a[data-navigation]'
43
const TICKTIME = 250
5-
6-
const attachAnchorsHandler = navigate => {
7-
document
8-
.body
9-
.addEventListener('click', e => {
10-
const { target } = e
11-
if (target.matches(NAV_A_SELECTOR)) {
12-
e.preventDefault()
13-
navigate(target.href)
14-
}
15-
})
16-
}
4+
const NAV_A_SELECTOR = 'a[data-navigation]'
175

186
const extractUrlParams = (route, pathname) => {
197
if (route.params.length === 0) {
@@ -95,7 +83,15 @@ export default () => {
9583
checkRoutes()
9684
window.setInterval(checkRoutes, TICKTIME)
9785

98-
attachAnchorsHandler(router.navigate)
86+
document
87+
.body
88+
.addEventListener('click', e => {
89+
const { target } = e
90+
if (target.matches(NAV_A_SELECTOR)) {
91+
e.preventDefault()
92+
router.navigate(target.href)
93+
}
94+
})
9995

10096
return router
10197
}

Chapter06/01/router.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ const URL_FRAGMENT_REGEXP = '([^\\/]+)'
33
const TICKTIME = 250
44

55
const extractUrlParams = (route, pathname) => {
6+
const params = {}
7+
68
if (route.params.length === 0) {
7-
return {}
9+
return params
810
}
911

10-
const params = {}
11-
1212
const matches = pathname
1313
.match(route.testRegExp)
1414

@@ -38,7 +38,8 @@ export default () => {
3838
lastPathname = pathname
3939

4040
const currentRoute = routes.find(route => {
41-
return route.testRegExp.test(pathname)
41+
const { testRegExp } = route
42+
return testRegExp.test(pathname)
4243
})
4344

4445
if (!currentRoute) {
@@ -55,10 +56,13 @@ export default () => {
5556
const params = []
5657

5758
const parsedPath = path
58-
.replace(ROUTE_PARAMETER_REGEXP, (match, paramName) => {
59-
params.push(paramName)
60-
return URL_FRAGMENT_REGEXP
61-
}).replace(/\//g, '\\/')
59+
.replace(
60+
ROUTE_PARAMETER_REGEXP,
61+
(match, paramName) => {
62+
params.push(paramName)
63+
return URL_FRAGMENT_REGEXP
64+
})
65+
.replace(/\//g, '\\/')
6266

6367
routes.push({
6468
testRegExp: new RegExp(`^${parsedPath}$`),
@@ -75,13 +79,14 @@ export default () => {
7579
}
7680

7781
router.navigate = path => {
78-
window.history.pushState(null, null, path)
82+
window
83+
.history
84+
.pushState(null, null, path)
7985
}
8086

8187
router.start = () => {
8288
checkRoutes()
8389
window.setInterval(checkRoutes, TICKTIME)
84-
return router
8590
}
8691

8792
return router

0 commit comments

Comments
 (0)