Skip to content
This repository was archived by the owner on Dec 28, 2019. It is now read-only.

Commit e128a66

Browse files
committed
Implemented /react <Router group={}>
1 parent 19dd0ed commit e128a66

File tree

3 files changed

+255
-20
lines changed

3 files changed

+255
-20
lines changed

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"type": "node",
1515
"request": "launch",
1616
"name": "test",
17-
"program": "${workspaceRoot}/test/routes.js"
17+
"program": "${workspaceRoot}/test/react.js"
1818
}
1919
]
2020
}

src/react/index.js

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
const locationProperty = 'location'
2-
const separatorChar = '-'
2+
const groupProperty = 'group'
3+
const separatorChar = '_'
34

45
export function Router(props) {
56
let children = props.children
67
let childrens = Array.isArray(children) ? children : [children]
78
let location = props[locationProperty]
9+
let group = props[groupProperty]
810
for (let index = 0, total = childrens.length; index < total; ++index) {
911
children = childrens[index]
10-
if (Check(children.props, location))
12+
if (Check(children.props, location, group))
1113
return getChildrenOfChildren(children)
1214
}
1315
return null
1416
}
1517

1618
export function Route(props) {
17-
let { children } = props
19+
const { children } = props
1820
// let location = props[locationProperty]
1921
return Check(props) ? children : null
2022
}
@@ -35,24 +37,40 @@ function getChildrenOfChildren(children) {
3537
return Array.isArray(child) ? child[0] : child //[0] // We can remove [0] when preact supports array of childrens. react16 already does
3638
}
3739

38-
function Check(props, location) {
40+
function Check(props, location, group) {
41+
// console.log(group.getRoute(location.href))
3942
if (props.hasOwnProperty('if')) if (!props.if) return false
43+
if (props.hasOwnProperty('is') && isObject(group)) {
44+
const route = isObject(location)
45+
? group.getRoute(location.href)
46+
: group.getRoute()
47+
if (route !== props.is) return false
48+
}
4049

41-
let prop
42-
for (prop in props) {
43-
if (
44-
location !== undefined &&
45-
prop !== 'children' &&
46-
prop !== locationProperty &&
47-
prop !== 'if'
48-
) {
49-
let value = location.hasOwnProperty(prop)
50-
? location[prop]
51-
: get(location, prop.split(separatorChar))
50+
if (location !== undefined) {
51+
for (let prop in props) {
52+
let has_property = location.hasOwnProperty(prop)
53+
let value = location[prop]
54+
if (!has_property) {
55+
const path = prop.split(separatorChar)
56+
const lastprop = path.pop()
57+
const obj = get(location, path)
58+
if (isObject(obj) && obj.hasOwnProperty(lastprop)) {
59+
has_property = true
60+
value = obj[lastprop]
61+
}
62+
}
5263

53-
if (props[prop] instanceof RegExp) {
54-
if (!props[prop].test(value)) return false
55-
} else if (props[prop] !== value) return false
64+
if (
65+
has_property &&
66+
// prop !== locationProperty &&
67+
prop !== 'children' &&
68+
prop !== 'if'
69+
) {
70+
if (props[prop] instanceof RegExp) {
71+
if (!props[prop].test(value)) return false
72+
} else if (props[prop] !== value) return false
73+
}
5674
}
5775
}
5876

@@ -81,3 +99,7 @@ function get(object, path) {
8199

82100
return object[path[index]]
83101
}
102+
103+
function isObject(object) {
104+
return object && typeof object == 'object'
105+
}

test/react.js

Lines changed: 214 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ const test = require('tape')
22
const React = require('react')
33
const ReactDOMServer = require('react-dom/server')
44
const { Router, Route } = require('../react')
5+
const { createRoute, createGroup } = require('../routes')
6+
57
const e = React.createElement
68
const element = e('div', null, 'Hello World')
79
const html = '<div><div>Hello World</div></div>'
@@ -137,11 +139,222 @@ test('<Router location={}> multiple and if 2 </Router>', function(t) {
137139
t.end()
138140
})
139141

140-
test('<Router location={}> multiple and if 2 </Router>', function(t) {
142+
test('<Router location={}> multiple and if 3 </Router>', function(t) {
141143
const component = e(Router, { location: { a: true, b: false } }, [
142144
e(Route, { a: true, b: false, if: false }, 'element'),
143145
e(Route, { if: false }, element)
144146
])
145147
t.equal(render(component), htmlEmpty)
146148
t.end()
147149
})
150+
151+
test('<Router location={}> multiple and if 4 </Router>', function(t) {
152+
const component = e(Router, { location: { a: true, b: false } }, [
153+
e(Route, { a: true, b: false, if: false }, 'element'),
154+
e(Route, { c: false }, element)
155+
])
156+
t.equal(render(component), html)
157+
t.end()
158+
})
159+
160+
test('<Router location={}> regexp </Router>', function(t) {
161+
const component = e(Router, { location: { a: 'hello' } }, [
162+
e(Route, { a: new RegExp('hello') }, element)
163+
])
164+
t.equal(render(component), html)
165+
t.end()
166+
})
167+
168+
test('<Router location={}> regexp fail </Router>', function(t) {
169+
const component = e(Router, { location: { a: 'hello' } }, [
170+
e(Route, { a: new RegExp('lla') }, element)
171+
])
172+
t.equal(render(component), htmlEmpty)
173+
t.end()
174+
})
175+
176+
test('<Router location={}> sub </Router>', function(t) {
177+
const component = e(Router, { location: { a: { b: true } } }, [
178+
e(Route, { a_b: false }, element)
179+
])
180+
t.equal(render(component), htmlEmpty)
181+
t.end()
182+
})
183+
184+
test('<Router location={}> sub </Router>', function(t) {
185+
const component = e(Router, { location: { a: { b: true } } }, [
186+
e(Route, { a_c: true }, element)
187+
])
188+
t.equal(render(component), html)
189+
t.end()
190+
})
191+
192+
test('<Router location={}> sub </Router>', function(t) {
193+
const component = e(Router, { location: { a: { b: true } } }, [
194+
e(Route, { a_: true }, element)
195+
])
196+
t.equal(render(component), html)
197+
t.end()
198+
})
199+
200+
test('<Router location={}> sub double </Router>', function(t) {
201+
const component = e(Router, { location: { a: { b: true, c: true } } }, [
202+
e(Route, { a_b: true, a_c: true }, element)
203+
])
204+
t.equal(render(component), html)
205+
t.end()
206+
})
207+
208+
test('<Router location={}> sub double 2 </Router>', function(t) {
209+
const component = e(Router, { location: { a: { b: true, c: true } } }, [
210+
e(Route, { a_b: true, a_c: false }, element)
211+
])
212+
t.equal(render(component), htmlEmpty)
213+
t.end()
214+
})
215+
216+
test('<Router group={}>', function(t) {
217+
const group = createGroup()
218+
const route1 = group.add(createRoute('/path/:page'))
219+
const route2 = group.add(createRoute('/path/profile/:name'))
220+
const component = e(Router, { group: group }, [
221+
e(Route, { is: route1 }, 'element'),
222+
e(Route, { is: route2 }, element)
223+
])
224+
t.equal(render(component), htmlEmpty)
225+
t.end()
226+
})
227+
228+
test('<Router group={createGroup(location)}>', function(t) {
229+
const location = { href: '/path/profile/enzo' }
230+
const group = createGroup(location)
231+
const route1 = group.add(createRoute('/path/:page'))
232+
const route2 = group.add(createRoute('/path/profile/:name'))
233+
const component = e(Router, { group: group }, [
234+
e(Route, { is: route1 }, 'element'),
235+
e(Route, { is: route2 }, element)
236+
])
237+
t.equal(render(component), html)
238+
t.end()
239+
})
240+
241+
test('<Router group={} location={}></Router>', function(t) {
242+
const location = { href: '/path/profile/enzo' }
243+
const group = createGroup()
244+
const route1 = group.add(createRoute('/path/:page'))
245+
const route2 = group.add(createRoute('/path/profile/:name'))
246+
const component = e(Router, { group: group, location: location }, [
247+
e(Route, { is: route1 }, 'element'),
248+
e(Route, { is: route2 }, element)
249+
])
250+
t.equal(render(component), html)
251+
t.end()
252+
})
253+
254+
test('<Router group={createGroup(location)} location={}></Router>', function(t) {
255+
const location = { href: '/path/123' }
256+
const location2 = { href: '/path/profile/enzo' }
257+
const group = createGroup(location)
258+
const route1 = group.add(createRoute('/path/:page'))
259+
const route2 = group.add(createRoute('/path/profile/:name'))
260+
const component = e(Router, { group: group, location: location2 }, [
261+
e(Route, { is: route1 }, 'element'),
262+
e(Route, { is: route2 }, element)
263+
])
264+
t.equal(render(component), html)
265+
t.end()
266+
})
267+
268+
test('<Router group={createGroup(location)}>2</Router>', function(t) {
269+
const location = { href: '/path/123' }
270+
const group = createGroup(location)
271+
const route1 = group.add(createRoute('/path/:page'))
272+
const route2 = group.add(createRoute('/path/profile/:name'))
273+
const component = e(Router, { group: group }, [
274+
e(Route, { is: route1 }, element),
275+
e(Route, { is: route2 }, 'element')
276+
])
277+
t.equal(render(component), html)
278+
t.end()
279+
})
280+
281+
test('<Router group={createGroup(location)}>fail</Router>', function(t) {
282+
const location = { href: '/path/yeah/123' }
283+
const group = createGroup(location)
284+
const route1 = group.add(createRoute('/path/:page'))
285+
const route2 = group.add(createRoute('/path/profile/:name'))
286+
const component = e(Router, { group: group }, [
287+
e(Route, { is: route1 }, element),
288+
e(Route, { is: route2 }, 'element')
289+
])
290+
t.equal(render(component), htmlEmpty)
291+
t.end()
292+
})
293+
294+
test('<Router group={}>if</Router>', function(t) {
295+
const location = { href: '/path/123' }
296+
const group = createGroup(location)
297+
const route1 = group.add(createRoute('/path/:page'))
298+
const route2 = group.add(createRoute('/path/profile/:name'))
299+
const component = e(Router, { group: group }, [
300+
e(Route, { is: route1, if: true }, element),
301+
e(Route, { is: route2 }, 'element')
302+
])
303+
t.equal(render(component), html)
304+
t.end()
305+
})
306+
307+
test('<Router group={}>if:false</Router>', function(t) {
308+
const location = { href: '/path/123' }
309+
const group = createGroup(location)
310+
const route1 = group.add(createRoute('/path/:page'))
311+
const route2 = group.add(createRoute('/path/profile/:name'))
312+
const component = e(Router, { group: group }, [
313+
e(Route, { is: route1, if: false }, element),
314+
e(Route, { is: route2 }, 'element')
315+
])
316+
t.equal(render(component), htmlEmpty)
317+
t.end()
318+
})
319+
320+
test('<Router group={} location={}>if path</Router>', function(t) {
321+
const href = '/path/123'
322+
const location = { href: href }
323+
const group = createGroup()
324+
const route1 = group.add(createRoute('/path/:page'))
325+
const route2 = group.add(createRoute('/path/profile/:name'))
326+
const component = e(Router, { group: group, location: location }, [
327+
e(Route, { is: route1, if: true, href: href }, element),
328+
e(Route, { is: route2 }, 'element')
329+
])
330+
t.equal(render(component), html)
331+
t.end()
332+
})
333+
334+
test('<Router group={} location={}>if path:false</Router>', function(t) {
335+
const href = '/path/123'
336+
const location = { href: href }
337+
const group = createGroup()
338+
const route1 = group.add(createRoute('/path/:page'))
339+
const route2 = group.add(createRoute('/path/profile/:name'))
340+
const component = e(Router, { group: group, location: location }, [
341+
e(Route, { is: route1, if: true, href: 'href' }, element),
342+
e(Route, { is: route2 }, 'element')
343+
])
344+
t.equal(render(component), htmlEmpty)
345+
t.end()
346+
})
347+
348+
test('<Router group={} location={}>without is</Router>', function(t) {
349+
const href = '/path/123'
350+
const location = { href: href }
351+
const group = createGroup()
352+
const route1 = group.add(createRoute('/path/:page'))
353+
const route2 = group.add(createRoute('/path/profile/:name'))
354+
const component = e(Router, { group: group, location: location }, [
355+
e(Route, { if: true, href: href }, element),
356+
e(Route, { is: route2 }, 'element')
357+
])
358+
t.equal(render(component), html)
359+
t.end()
360+
})

0 commit comments

Comments
 (0)