space-router 0.8.5 → 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/dist/esm/match.js CHANGED
@@ -1,90 +1,76 @@
1
1
  export function match(routes, url, qs) {
2
- if (!url) {
3
- return;
4
- }
5
-
6
- for (let i = 0; i < routes.length; i++) {
7
- const m = matchOne(routes[i].pattern, url, qs);
8
-
9
- if (m) {
10
- return m;
2
+ if (!url) {
3
+ return;
4
+ }
5
+ for(var i = 0; i < routes.length; i++){
6
+ var m = matchOne(routes[i].pattern, url, qs);
7
+ if (m) {
8
+ return m;
9
+ }
11
10
  }
12
- }
13
11
  }
14
12
  export function matchOne(pattern, url, qs) {
15
- if (!pattern) {
16
- return false;
17
- }
18
-
19
- const re = /(?:\?([^#]*))?(#.*)?$/;
20
- const originalUrl = url;
21
- const originalPattern = pattern;
22
- const c = url.match(re);
23
- const params = {};
24
- let query = {};
25
- let search = '';
26
- let hash = '';
27
- let ret;
28
-
29
- if (c && c[1]) {
30
- search = '?' + c[1];
31
- query = qs.parse(c[1]);
32
- }
33
-
34
- if (c && c[2]) {
35
- hash = c[2] || '';
36
- }
37
-
38
- if (pattern !== '*') {
39
- url = segmentize(url.replace(re, ''));
40
- pattern = segmentize(pattern || '');
41
- const max = Math.max(url.length, pattern.length);
42
-
43
- for (let i = 0; i < max; i++) {
44
- if (pattern[i] && pattern[i].charAt(0) === ':') {
45
- const param = pattern[i].replace(/(^:|[+*?]+$)/g, '');
46
- const flags = (pattern[i].match(/[+*?]+$/) || {})[0] || '';
47
- const plus = flags.indexOf('+') > -1;
48
- const star = flags.indexOf('*') > -1;
49
- const val = url[i] || '';
50
-
51
- if (!val && !star && (flags.indexOf('?') < 0 || plus)) {
52
- ret = false;
53
- break;
13
+ if (!pattern) {
14
+ return false;
15
+ }
16
+ var re = /(?:\?([^#]*))?(#.*)?$/;
17
+ var originalUrl = url;
18
+ var originalPattern = pattern;
19
+ var c = url.match(re);
20
+ var params = {};
21
+ var query = {};
22
+ var search = "";
23
+ var hash = "";
24
+ var ret;
25
+ if (c && c[1]) {
26
+ search = "?" + c[1];
27
+ query = qs.parse(c[1]);
28
+ }
29
+ if (c && c[2]) {
30
+ hash = c[2] || "";
31
+ }
32
+ if (pattern !== "*") {
33
+ url = segmentize(url.replace(re, ""));
34
+ pattern = segmentize(pattern || "");
35
+ var max = Math.max(url.length, pattern.length);
36
+ for(var i = 0; i < max; i++){
37
+ if (pattern[i] && pattern[i].charAt(0) === ":") {
38
+ var param = pattern[i].replace(/(^:|[+*?]+$)/g, "");
39
+ var flags = (pattern[i].match(/[+*?]+$/) || {})[0] || "";
40
+ var plus = flags.indexOf("+") > -1;
41
+ var star = flags.indexOf("*") > -1;
42
+ var val = url[i] || "";
43
+ if (!val && !star && (flags.indexOf("?") < 0 || plus)) {
44
+ ret = false;
45
+ break;
46
+ }
47
+ params[param] = decodeURIComponent(val);
48
+ if (plus || star) {
49
+ params[param] = url.slice(i).map(decodeURIComponent).join("/");
50
+ break;
51
+ }
52
+ } else if (pattern[i] !== url[i]) {
53
+ ret = false;
54
+ break;
55
+ }
54
56
  }
55
-
56
- params[param] = decodeURIComponent(val);
57
-
58
- if (plus || star) {
59
- params[param] = url.slice(i).map(decodeURIComponent).join('/');
60
- break;
57
+ if (ret === false) {
58
+ return false;
61
59
  }
62
- } else if (pattern[i] !== url[i]) {
63
- ret = false;
64
- break;
65
- }
66
- }
67
-
68
- if (ret === false) {
69
- return false;
70
60
  }
71
- }
72
-
73
- return {
74
- pattern: originalPattern,
75
- url: originalUrl,
76
- pathname: originalUrl.replace(re, ''),
77
- params,
78
- query,
79
- search,
80
- hash
81
- };
61
+ return {
62
+ pattern: originalPattern,
63
+ url: originalUrl,
64
+ pathname: originalUrl.replace(re, ""),
65
+ params: params,
66
+ query: query,
67
+ search: search,
68
+ hash: hash
69
+ };
82
70
  }
83
-
84
71
  function segmentize(url) {
85
- return strip(url).split('/');
72
+ return strip(url).split("/");
86
73
  }
87
-
88
74
  function strip(url) {
89
- return url.replace(/(^\/+|\/+$)/g, '');
90
- }
75
+ return url.replace(/(^\/+|\/+$)/g, "");
76
+ }
package/dist/esm/qs.js CHANGED
@@ -1,20 +1,17 @@
1
- export const qs = {
2
- parse(queryString) {
3
- return queryString.split('&').reduce((acc, pair) => {
4
- const parts = pair.split('=');
5
- acc[parts[0]] = decodeURIComponent(parts[1]);
6
- return acc;
7
- }, {});
8
- },
9
-
10
- stringify(query) {
11
- return Object.keys(query).reduce((acc, key) => {
12
- if (query[key] !== undefined) {
13
- acc.push(key + '=' + encodeURIComponent(query[key]));
14
- }
15
-
16
- return acc;
17
- }, []).join('&');
18
- }
19
-
20
- };
1
+ export var qs = {
2
+ parse: function parse(queryString) {
3
+ return queryString.split("&").reduce(function(acc, pair) {
4
+ var parts = pair.split("=");
5
+ acc[parts[0]] = decodeURIComponent(parts[1]);
6
+ return acc;
7
+ }, {});
8
+ },
9
+ stringify: function stringify(query) {
10
+ return Object.keys(query).reduce(function(acc, key) {
11
+ if (query[key] !== undefined) {
12
+ acc.push(key + "=" + encodeURIComponent(query[key]));
13
+ }
14
+ return acc;
15
+ }, []).join("&");
16
+ }
17
+ };
@@ -1,175 +1,249 @@
1
- import { match as findMatch } from './match';
2
- import { createHistory } from './history';
3
- import { qs as defaultQs } from './qs';
4
- export function createRouter(options = {}) {
5
- let history = null;
6
- let routes = [];
7
- const mode = options.mode || 'history';
8
- const qs = options.qs || defaultQs;
9
- const sync = options.sync || false;
10
- const router = {
11
- listen(routeMap, cb) {
12
- if (history) {
13
- throw new Error('Already listening');
14
- }
15
-
16
- routes = flatten(routeMap);
17
- history = createHistory({
18
- mode,
19
- sync
20
- });
21
- const dispose = history.listen(url => transition(router, url, cb));
22
- return () => {
23
- dispose();
24
- history = null;
25
- routes = [];
26
- };
27
- },
28
-
29
- navigate(to, curr) {
30
- if (typeof to === 'string') {
31
- to = {
32
- url: to
33
- };
34
- }
35
-
36
- const url = router.href(to, curr);
37
-
38
- if (to.replace) {
39
- history.replace(url);
40
- } else {
41
- history.push(url);
42
- }
43
- },
44
-
45
- href(to, curr) {
46
- // already a url
47
- if (typeof to === 'string') {
48
- return to;
49
- } // align with navigate API
50
-
51
-
52
- if (to.url) {
53
- return to.url;
54
- }
55
-
56
- if (to.merge) {
57
- curr = curr || router.match(router.getUrl());
58
- to = merge(curr, to);
59
- }
60
-
61
- let url = to.pathname || '/';
62
-
63
- if (to.params) {
64
- Object.keys(to.params).forEach(param => {
65
- url = url.replace(':' + param, to.params[param]);
1
+ function _define_property(obj, key, value) {
2
+ if (key in obj) {
3
+ Object.defineProperty(obj, key, {
4
+ value: value,
5
+ enumerable: true,
6
+ configurable: true,
7
+ writable: true
66
8
  });
67
- }
68
-
69
- if (to.query && Object.keys(to.query).length) {
70
- const query = qs.stringify(to.query);
71
-
72
- if (query) {
73
- url = url + '?' + query;
9
+ } else {
10
+ obj[key] = value;
11
+ }
12
+ return obj;
13
+ }
14
+ function _object_spread(target) {
15
+ for(var i = 1; i < arguments.length; i++){
16
+ var source = arguments[i] != null ? arguments[i] : {};
17
+ var ownKeys = Object.keys(source);
18
+ if (typeof Object.getOwnPropertySymbols === "function") {
19
+ ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
20
+ return Object.getOwnPropertyDescriptor(source, sym).enumerable;
21
+ }));
22
+ }
23
+ ownKeys.forEach(function(key) {
24
+ _define_property(target, key, source[key]);
25
+ });
26
+ }
27
+ return target;
28
+ }
29
+ function ownKeys(object, enumerableOnly) {
30
+ var keys = Object.keys(object);
31
+ if (Object.getOwnPropertySymbols) {
32
+ var symbols = Object.getOwnPropertySymbols(object);
33
+ if (enumerableOnly) {
34
+ symbols = symbols.filter(function(sym) {
35
+ return Object.getOwnPropertyDescriptor(object, sym).enumerable;
36
+ });
37
+ }
38
+ keys.push.apply(keys, symbols);
39
+ }
40
+ return keys;
41
+ }
42
+ function _object_spread_props(target, source) {
43
+ source = source != null ? source : {};
44
+ if (Object.getOwnPropertyDescriptors) {
45
+ Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
46
+ } else {
47
+ ownKeys(Object(source)).forEach(function(key) {
48
+ Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
49
+ });
50
+ }
51
+ return target;
52
+ }
53
+ function _object_without_properties(source, excluded) {
54
+ if (source == null) return {};
55
+ var target = _object_without_properties_loose(source, excluded);
56
+ var key, i;
57
+ if (Object.getOwnPropertySymbols) {
58
+ var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
59
+ for(i = 0; i < sourceSymbolKeys.length; i++){
60
+ key = sourceSymbolKeys[i];
61
+ if (excluded.indexOf(key) >= 0) continue;
62
+ if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
63
+ target[key] = source[key];
74
64
  }
75
- }
76
-
77
- if (to.hash) {
78
- const prefix = to.hash.startsWith('#') ? '' : '#';
79
- url = url + prefix + to.hash;
80
- }
81
-
82
- return url;
83
- },
84
-
85
- match(url) {
86
- const route = findMatch(routes, url, qs);
87
-
88
- if (route) {
89
- return { ...route,
90
- data: data(routes, route)
91
- };
92
- }
93
- },
94
-
95
- getUrl() {
96
- return history.getUrl();
97
65
  }
98
-
99
- };
100
- return router;
66
+ return target;
67
+ }
68
+ function _object_without_properties_loose(source, excluded) {
69
+ if (source == null) return {};
70
+ var target = {};
71
+ var sourceKeys = Object.keys(source);
72
+ var key, i;
73
+ for(i = 0; i < sourceKeys.length; i++){
74
+ key = sourceKeys[i];
75
+ if (excluded.indexOf(key) >= 0) continue;
76
+ target[key] = source[key];
77
+ }
78
+ return target;
79
+ }
80
+ import { match as findMatch } from "./match.js";
81
+ import { createHistory } from "./history.js";
82
+ import { qs as defaultQs } from "./qs.js";
83
+ export function createRouter() {
84
+ var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
85
+ var history = null;
86
+ var routes = [];
87
+ var mode = options.mode || "history";
88
+ var qs = options.qs || defaultQs;
89
+ var sync = options.sync || false;
90
+ var router = {
91
+ listen: function listen(routeMap, cb) {
92
+ if (history) {
93
+ throw new Error("Already listening");
94
+ }
95
+ routes = flatten(routeMap);
96
+ history = createHistory({
97
+ mode: mode,
98
+ sync: sync
99
+ });
100
+ var dispose = history.listen(function(url) {
101
+ return transition(router, url, cb);
102
+ });
103
+ return function() {
104
+ dispose();
105
+ history = null;
106
+ routes = [];
107
+ };
108
+ },
109
+ navigate: function navigate(to, curr) {
110
+ if (typeof to === "string") {
111
+ to = {
112
+ url: to
113
+ };
114
+ }
115
+ var url = router.href(to, curr);
116
+ if (to.replace) {
117
+ history.replace(url);
118
+ } else {
119
+ history.push(url);
120
+ }
121
+ },
122
+ href: function href(to, curr) {
123
+ // already a url
124
+ if (typeof to === "string") {
125
+ return to;
126
+ }
127
+ // align with navigate API
128
+ if (to.url) {
129
+ return to.url;
130
+ }
131
+ if (to.merge) {
132
+ curr = curr || router.match(router.getUrl());
133
+ to = merge(curr, to);
134
+ }
135
+ var url = to.pathname || "/";
136
+ if (to.params) {
137
+ Object.keys(to.params).forEach(function(param) {
138
+ url = url.replace(":" + param, to.params[param]);
139
+ });
140
+ }
141
+ if (to.query && Object.keys(to.query).length) {
142
+ var query = qs.stringify(to.query);
143
+ if (query) {
144
+ url = url + "?" + query;
145
+ }
146
+ }
147
+ if (to.hash) {
148
+ var prefix = to.hash.startsWith("#") ? "" : "#";
149
+ url = url + prefix + to.hash;
150
+ }
151
+ return url;
152
+ },
153
+ match: function match(url) {
154
+ var route = findMatch(routes, url, qs);
155
+ if (route) {
156
+ return _object_spread_props(_object_spread({}, route), {
157
+ data: data(routes, route)
158
+ });
159
+ }
160
+ },
161
+ getUrl: function getUrl() {
162
+ return history.getUrl();
163
+ }
164
+ };
165
+ return router;
101
166
  }
102
167
  export function flatten(routeMap) {
103
- const routes = [];
104
- const parentData = [];
105
-
106
- function addLevel(level) {
107
- level.forEach(route => {
108
- const {
109
- path = '',
110
- routes: children,
111
- ...routeData
112
- } = route;
113
- routes.push({
114
- pattern: path,
115
- data: parentData.concat([routeData])
116
- });
117
-
118
- if (children) {
119
- parentData.push(routeData);
120
- addLevel(children);
121
- parentData.pop();
122
- }
123
- });
124
- }
125
-
126
- addLevel(routeMap);
127
- return routes;
168
+ var routes = [];
169
+ var parentData = [];
170
+ function addLevel(level) {
171
+ level.forEach(function(route) {
172
+ var _route_path = route.path, path = _route_path === void 0 ? "" : _route_path, children = route.routes, routeData = _object_without_properties(route, [
173
+ "path",
174
+ "routes"
175
+ ]);
176
+ routes.push({
177
+ pattern: path,
178
+ data: parentData.concat([
179
+ routeData
180
+ ])
181
+ });
182
+ if (children) {
183
+ parentData.push(routeData);
184
+ addLevel(children);
185
+ parentData.pop();
186
+ }
187
+ });
188
+ }
189
+ addLevel(routeMap);
190
+ return routes;
128
191
  }
129
-
130
192
  function transition(router, url, onNavigated) {
131
- const route = router.match(url);
132
-
133
- if (route) {
134
- for (const r of route.data) {
135
- if (r.redirect) {
136
- const url = redirectUrl(router, r.redirect, route);
137
- return router.navigate({
138
- url,
139
- replace: true
140
- });
141
- }
193
+ var route = router.match(url);
194
+ if (route) {
195
+ var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
196
+ try {
197
+ for(var _iterator = route.data[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
198
+ var r = _step.value;
199
+ if (r.redirect) {
200
+ var _$url = redirectUrl(router, r.redirect, route);
201
+ return router.navigate({
202
+ url: _$url,
203
+ replace: true
204
+ });
205
+ }
206
+ }
207
+ } catch (err) {
208
+ _didIteratorError = true;
209
+ _iteratorError = err;
210
+ } finally{
211
+ try {
212
+ if (!_iteratorNormalCompletion && _iterator.return != null) {
213
+ _iterator.return();
214
+ }
215
+ } finally{
216
+ if (_didIteratorError) {
217
+ throw _iteratorError;
218
+ }
219
+ }
220
+ }
221
+ onNavigated && onNavigated(route);
142
222
  }
143
-
144
- onNavigated && onNavigated(route);
145
- }
146
223
  }
147
-
148
224
  function redirectUrl(router, redirect, matchingRoute) {
149
- if (typeof redirect === 'function') {
150
- redirect = redirect(matchingRoute);
151
- }
152
-
153
- return router.href(redirect);
225
+ if (typeof redirect === "function") {
226
+ redirect = redirect(matchingRoute);
227
+ }
228
+ return router.href(redirect);
154
229
  }
155
-
156
230
  function data(routes, matchingRoute) {
157
- for (let i = 0; i < routes.length; i++) {
158
- if (routes[i].pattern === matchingRoute.pattern) {
159
- return routes[i].data;
231
+ for(var i = 0; i < routes.length; i++){
232
+ if (routes[i].pattern === matchingRoute.pattern) {
233
+ return routes[i].data;
234
+ }
160
235
  }
161
- }
162
236
  }
163
-
164
- export function merge(curr = {}, to) {
165
- const pathname = to.pathname || curr.pattern || curr.pathname;
166
- const params = Object.assign({}, curr.params, to.params);
167
- const query = to.query === null ? null : Object.assign({}, curr.query, to.query);
168
- const hash = to.hash === null ? null : to.hash || curr.hash || '';
169
- return {
170
- pathname,
171
- params,
172
- query,
173
- hash
174
- };
175
- }
237
+ export function merge() {
238
+ var curr = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, to = arguments.length > 1 ? arguments[1] : void 0;
239
+ var pathname = to.pathname || curr.pattern || curr.pathname;
240
+ var params = Object.assign({}, curr.params, to.params);
241
+ var query = to.query === null ? null : Object.assign({}, curr.query, to.query);
242
+ var hash = to.hash === null ? null : to.hash || curr.hash || "";
243
+ return {
244
+ pathname: pathname,
245
+ params: params,
246
+ query: query,
247
+ hash: hash
248
+ };
249
+ }
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.8.5",
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}' && nyc ava",
25
+ "test": "npm run build && healthier && prettier --check '**/*.{js,css,yml}' && c8 ava",
25
26
  "format": "prettier --write '**/*.{js,css,yml}'",
26
- "coverage": "nyc --reporter=html ava",
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
- "@babel/cli": "^7.17.6",
36
- "@babel/preset-env": "^7.16.11",
37
- "ava": "^3.15.0",
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": "^5.1.1",
40
- "gh-pages": "^3.2.3",
41
- "healthier": "^5.0.1",
42
- "np": "^7.6.1",
43
- "nyc": "^15.1.0",
44
- "prettier": "^2.6.2"
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/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