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

Commit 341ddbe

Browse files
committed
routes/createRoute implemented
1 parent 6b56a5b commit 341ddbe

File tree

11 files changed

+490
-59
lines changed

11 files changed

+490
-59
lines changed

.eslintrc

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"parserOptions": {
3+
"ecmaVersion": 6,
4+
"sourceType": "module",
5+
"ecmaFeatures": {
6+
"jsx": true
7+
}
8+
},
9+
"rules": {
10+
"semi": 0
11+
}
12+
}

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

.vscode/launch.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
8+
{
9+
"type": "node",
10+
"request": "launch",
11+
"name": "Launch Program",
12+
"program": "${workspaceRoot}/${relativeFile}"
13+
}
14+
]
15+
}

.vscode/settings.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"eslint.enable": true,
3+
"editor.formatOnSave": true,
4+
"prettier.eslintIntegration": true,
5+
"prettier.tabWidth": 4,
6+
"prettier.singleQuote": true,
7+
"prettier.semi": false,
8+
"git.ignoreLimitWarning": true
9+
}

location/index.js

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
11
var dop = require('dop')
22
var enc = encodeURIComponent
33

4-
export function create(url, object, prop) {
4+
exports.createLocation = function createLocation(url, object, prop) {
55
var shallWeEmit = false
66
var location
77
var urlparsed = parse(url)
88
prop = prop || 'location'
99

1010
if (object !== null && typeof object == 'object') {
11-
if (dop.isRegistered(object))
12-
dop.set(object, prop, urlparsed)
11+
if (dop.isRegistered(object)) dop.set(object, prop, urlparsed)
1312
else {
1413
object[prop] = urlparsed
1514
object = dop.register(object)
1615
}
1716

1817
location = object[prop]
19-
}
20-
else
21-
location = dop.register(urlparsed)
18+
} else location = dop.register(urlparsed)
2219

2320
location.toString = function() {
2421
return location.href
@@ -31,7 +28,10 @@ export function create(url, object, prop) {
3128
pushState(mutation.value)
3229
setHref(getWindowLocation())
3330
} else if (mutation.prop === 'pathname') {
34-
var href = mutation.value.split('/').map(enc).join('/')
31+
var href = mutation.value
32+
.split('/')
33+
.map(enc)
34+
.join('/')
3535
if (mutation.value[0] !== '/') href = '/' + href
3636
href = href + location.search + location.hash
3737
object.pathname = mutation.oldValue
@@ -100,7 +100,11 @@ export function create(url, object, prop) {
100100
object[mutation.prop] = enc(path[mutation.prop])
101101
var href =
102102
'/' +
103-
path.filter(function(p) { return p !== undefined }).join('/') +
103+
path
104+
.filter(function(p) {
105+
return p !== undefined
106+
})
107+
.join('/') +
104108
location.search +
105109
location.hash
106110
if (href !== location.pathname) {
@@ -155,7 +159,8 @@ export function create(url, object, prop) {
155159
newquery = newlocation.query,
156160
query = location.query
157161
for (prop in newquery) dop.set(query, prop, newquery[prop])
158-
for (prop in query) if (!newquery.hasOwnProperty(prop)) dop.del(query, prop)
162+
for (prop in query)
163+
if (!newquery.hasOwnProperty(prop)) dop.del(query, prop)
159164

160165
// emit
161166
shallWeEmit = false
@@ -193,7 +198,9 @@ function parse(url) {
193198
protocol: match[2],
194199
host: match[3],
195200
pathname: match[4],
196-
path: match[4].split('/').filter(function (item) {return item.length > 0}),
201+
path: match[4].split('/').filter(function(item) {
202+
return item.length > 0
203+
}),
197204
search: match[5],
198205
query: query,
199206
hash: match[6] || ''
@@ -202,16 +209,19 @@ function parse(url) {
202209
location.href = getHref(location)
203210

204211
if (location.search.length > 1) {
205-
location.search.substr(1).split('&').forEach(function(item) {
206-
if (item.length > 0) {
207-
var equal = item.indexOf('=')
208-
equal > -1
209-
? (location.query[item.substr(0, equal)] = item.substr(
210-
equal + 1
211-
))
212-
: (location.query[item] = '')
213-
}
214-
})
212+
location.search
213+
.substr(1)
214+
.split('&')
215+
.forEach(function(item) {
216+
if (item.length > 0) {
217+
var equal = item.indexOf('=')
218+
equal > -1
219+
? (location.query[item.substr(0, equal)] = item.substr(
220+
equal + 1
221+
))
222+
: (location.query[item] = '')
223+
}
224+
})
215225
}
216226

217227
return location

0 commit comments

Comments
 (0)