space-router 0.8.4 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.swc-cjs +11 -0
- package/CHANGELOG.md +10 -0
- package/LICENSE +1 -1
- package/dist/cjs/history.js +91 -100
- package/dist/cjs/index.js +26 -16
- package/dist/cjs/match.js +82 -85
- package/dist/cjs/qs.js +24 -20
- package/dist/cjs/router.js +252 -190
- package/dist/esm/history.js +83 -97
- package/dist/esm/index.js +3 -3
- package/dist/esm/match.js +65 -79
- package/dist/esm/qs.js +17 -20
- package/dist/esm/router.js +235 -163
- package/docs/content/_index.md +30 -6
- package/package.json +12 -13
- package/src/history.js +13 -13
- package/src/index.js +3 -3
- package/src/router.js +11 -11
- package/tasks/build.js +11 -5
- package/test/flatten.test.js +1 -1
- package/test/match.test.js +2 -2
- package/test/router.test.js +4 -2
- package/.babelrc-cjs +0 -17
- /package/{.babelrc-esm → .swc-esm} +0 -0
package/package.json
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "space-router",
|
|
3
3
|
"description": "All the routing essentials.",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.9.0",
|
|
5
5
|
"main": "dist/cjs",
|
|
6
6
|
"module": "dist/esm",
|
|
7
|
+
"type": "module",
|
|
7
8
|
"license": "ICS",
|
|
8
9
|
"repository": {
|
|
9
10
|
"type": "git",
|
|
@@ -21,27 +22,25 @@
|
|
|
21
22
|
],
|
|
22
23
|
"author": "Karolis Narkevicius",
|
|
23
24
|
"scripts": {
|
|
24
|
-
"test": "healthier && prettier --check '**/*.{js,css,yml}' &&
|
|
25
|
+
"test": "npm run build && healthier && prettier --check '**/*.{js,css,yml}' && c8 ava",
|
|
25
26
|
"format": "prettier --write '**/*.{js,css,yml}'",
|
|
26
|
-
"coverage": "
|
|
27
|
+
"coverage": "c8 --reporter=html ava",
|
|
27
28
|
"build": "node ./tasks/build.js",
|
|
28
29
|
"watch": "node ./tasks/build.js -w",
|
|
29
|
-
"version": "npm run build",
|
|
30
30
|
"release": "np --no-release-draft",
|
|
31
31
|
"release:beta": "np --tag=beta --no-release-draft",
|
|
32
32
|
"release:docs": "hugo -s docs && gh-pages -d docs/public"
|
|
33
33
|
},
|
|
34
34
|
"devDependencies": {
|
|
35
|
-
"@
|
|
36
|
-
"@
|
|
37
|
-
"ava": "^
|
|
35
|
+
"@swc/cli": "^0.1.62",
|
|
36
|
+
"@swc/core": "^1.3.49",
|
|
37
|
+
"ava": "^5.2.0",
|
|
38
|
+
"c8": "^7.13.0",
|
|
38
39
|
"esm": "^3.2.25",
|
|
39
|
-
"execa": "^
|
|
40
|
-
"gh-pages": "^
|
|
41
|
-
"healthier": "^
|
|
42
|
-
"
|
|
43
|
-
"nyc": "^15.1.0",
|
|
44
|
-
"prettier": "^2.4.1"
|
|
40
|
+
"execa": "^7.1.1",
|
|
41
|
+
"gh-pages": "^5.0.0",
|
|
42
|
+
"healthier": "^6.3.0",
|
|
43
|
+
"prettier": "^2.8.7"
|
|
45
44
|
},
|
|
46
45
|
"healthier": {
|
|
47
46
|
"global": [
|
package/src/history.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
export function createHistory(
|
|
1
|
+
export function createHistory(options) {
|
|
2
2
|
const sync = options.sync
|
|
3
3
|
let mode = options.mode
|
|
4
4
|
let raf
|
|
5
|
+
let onPop
|
|
5
6
|
|
|
6
7
|
const memory = []
|
|
7
8
|
let off
|
|
@@ -17,19 +18,22 @@ export function createHistory(onChange, options) {
|
|
|
17
18
|
}
|
|
18
19
|
}
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
}
|
|
21
|
+
function listen(onChange) {
|
|
22
|
+
onPop = () => onChange(getUrl())
|
|
23
23
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
if (mode !== 'memory') {
|
|
25
|
+
off = on(window, mode === 'history' ? 'popstate' : 'hashchange', onPop)
|
|
26
|
+
raf(onPop)
|
|
27
|
+
}
|
|
27
28
|
|
|
28
|
-
|
|
29
|
-
|
|
29
|
+
return () => {
|
|
30
|
+
destroyed = true
|
|
31
|
+
off && off()
|
|
32
|
+
}
|
|
30
33
|
}
|
|
31
34
|
|
|
32
35
|
return {
|
|
36
|
+
listen,
|
|
33
37
|
getUrl,
|
|
34
38
|
push(url) {
|
|
35
39
|
go(url)
|
|
@@ -37,10 +41,6 @@ export function createHistory(onChange, options) {
|
|
|
37
41
|
replace(url) {
|
|
38
42
|
go(url, true)
|
|
39
43
|
},
|
|
40
|
-
destroy() {
|
|
41
|
-
destroyed = true
|
|
42
|
-
off && off()
|
|
43
|
-
},
|
|
44
44
|
}
|
|
45
45
|
|
|
46
46
|
function go(url, replace) {
|
package/src/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export { createRouter, merge } from './router'
|
|
2
|
-
export { createHistory } from './history'
|
|
3
|
-
export { qs } from './qs'
|
|
1
|
+
export { createRouter, merge } from './router.js'
|
|
2
|
+
export { createHistory } from './history.js'
|
|
3
|
+
export { qs } from './qs.js'
|
package/src/router.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { match as findMatch } from './match'
|
|
2
|
-
import { createHistory } from './history'
|
|
3
|
-
import { qs as defaultQs } from './qs'
|
|
1
|
+
import { match as findMatch } from './match.js'
|
|
2
|
+
import { createHistory } from './history.js'
|
|
3
|
+
import { qs as defaultQs } from './qs.js'
|
|
4
4
|
|
|
5
5
|
export function createRouter(options = {}) {
|
|
6
6
|
let history = null
|
|
@@ -16,21 +16,21 @@ export function createRouter(options = {}) {
|
|
|
16
16
|
}
|
|
17
17
|
|
|
18
18
|
routes = flatten(routeMap)
|
|
19
|
-
|
|
20
|
-
|
|
19
|
+
history = createHistory({ mode, sync })
|
|
20
|
+
const dispose = history.listen((url) => transition(router, url, cb))
|
|
21
21
|
|
|
22
22
|
return () => {
|
|
23
|
-
|
|
23
|
+
dispose()
|
|
24
24
|
history = null
|
|
25
25
|
routes = []
|
|
26
26
|
}
|
|
27
27
|
},
|
|
28
28
|
|
|
29
|
-
navigate(to) {
|
|
29
|
+
navigate(to, curr) {
|
|
30
30
|
if (typeof to === 'string') {
|
|
31
31
|
to = { url: to }
|
|
32
32
|
}
|
|
33
|
-
const url = router.href(to)
|
|
33
|
+
const url = router.href(to, curr)
|
|
34
34
|
if (to.replace) {
|
|
35
35
|
history.replace(url)
|
|
36
36
|
} else {
|
|
@@ -38,7 +38,7 @@ export function createRouter(options = {}) {
|
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
40
|
|
|
41
|
-
href(to) {
|
|
41
|
+
href(to, curr) {
|
|
42
42
|
// already a url
|
|
43
43
|
if (typeof to === 'string') {
|
|
44
44
|
return to
|
|
@@ -50,7 +50,7 @@ export function createRouter(options = {}) {
|
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
if (to.merge) {
|
|
53
|
-
|
|
53
|
+
curr = curr || router.match(router.getUrl())
|
|
54
54
|
to = merge(curr, to)
|
|
55
55
|
}
|
|
56
56
|
|
|
@@ -139,7 +139,7 @@ function data(routes, matchingRoute) {
|
|
|
139
139
|
}
|
|
140
140
|
|
|
141
141
|
export function merge(curr = {}, to) {
|
|
142
|
-
const pathname = to.pathname || curr.pattern
|
|
142
|
+
const pathname = to.pathname || curr.pattern || curr.pathname
|
|
143
143
|
const params = Object.assign({}, curr.params, to.params)
|
|
144
144
|
const query = to.query === null ? null : Object.assign({}, curr.query, to.query)
|
|
145
145
|
const hash = to.hash === null ? null : to.hash || curr.hash || ''
|
package/tasks/build.js
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
import fs from 'fs'
|
|
2
|
+
import path from 'path'
|
|
3
|
+
import { fileURLToPath } from 'url'
|
|
4
|
+
import { execa } from 'execa'
|
|
5
|
+
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url)
|
|
7
|
+
const __dirname = path.dirname(__filename)
|
|
2
8
|
|
|
3
9
|
const sh = (...args) => execa(...args, { stdio: 'inherit', shell: true })
|
|
4
10
|
|
|
@@ -6,9 +12,9 @@ const sh = (...args) => execa(...args, { stdio: 'inherit', shell: true })
|
|
|
6
12
|
await sh('rm -rf dist')
|
|
7
13
|
await sh('mkdir -p dist')
|
|
8
14
|
|
|
9
|
-
const pkg =
|
|
15
|
+
const pkg = JSON.parse(fs.readFileSync(path.join(__dirname, '../package.json')))
|
|
10
16
|
|
|
11
|
-
const
|
|
12
|
-
await sh(`${
|
|
13
|
-
await sh(`${
|
|
17
|
+
const swc = './node_modules/.bin/swc'
|
|
18
|
+
await sh(`${swc} --no-swcrc src -d ${pkg.main} --config-file=./.swc-cjs`)
|
|
19
|
+
await sh(`${swc} --no-swcrc src -d ${pkg.module} --config-file=./.swc-esm`)
|
|
14
20
|
})()
|
package/test/flatten.test.js
CHANGED
package/test/match.test.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import test from 'ava'
|
|
2
|
-
import { qs } from '../src'
|
|
3
|
-
import { matchOne as match } from '../src/match'
|
|
2
|
+
import { qs } from '../src/index.js'
|
|
3
|
+
import { matchOne as match } from '../src/match.js'
|
|
4
4
|
|
|
5
5
|
test('match explicit equality', (t) => {
|
|
6
6
|
t.deepEqual(match('/', '/').params, {})
|
package/test/router.test.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import test from 'ava'
|
|
2
|
-
import { qs, createRouter } from '../src'
|
|
2
|
+
import { qs, createRouter } from '../src/index.js'
|
|
3
3
|
|
|
4
4
|
test('createRouter, listen, navigate and dispose', (t) => {
|
|
5
5
|
const calls = []
|
|
@@ -20,6 +20,7 @@ test('createRouter, listen, navigate and dispose', (t) => {
|
|
|
20
20
|
router.navigate({ query: { a: 11, b: 22 }, merge: true })
|
|
21
21
|
router.navigate({ query: { b: undefined, c: 'bla' }, hash: 'test', merge: true })
|
|
22
22
|
router.navigate({ params: { id: 3 }, merge: true })
|
|
23
|
+
router.navigate({ query: { curr: 'test' }, merge: true }, { pathname: '/user/curr' })
|
|
23
24
|
|
|
24
25
|
t.deepEqual(
|
|
25
26
|
[
|
|
@@ -35,6 +36,7 @@ test('createRouter, listen, navigate and dispose', (t) => {
|
|
|
35
36
|
'user=2?a=11&b=22',
|
|
36
37
|
'user=2?a=11&c=bla#test',
|
|
37
38
|
'user=3?a=11&c=bla#test',
|
|
39
|
+
'user=curr?curr=test',
|
|
38
40
|
],
|
|
39
41
|
calls
|
|
40
42
|
)
|
|
@@ -42,7 +44,7 @@ test('createRouter, listen, navigate and dispose', (t) => {
|
|
|
42
44
|
dispose()
|
|
43
45
|
})
|
|
44
46
|
|
|
45
|
-
test('.href(
|
|
47
|
+
test('.href(to)', (t) => {
|
|
46
48
|
const { router, dispose } = createTestRouter()
|
|
47
49
|
|
|
48
50
|
t.deepEqual('/user/7/friends?a=1&b=2', router.href({ pathname: '/user/7/friends', query: { a: 1, b: 2 } }))
|
package/.babelrc-cjs
DELETED
|
File without changes
|