space-router 0.9.5 → 1.1.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/README.md +2 -1
- package/dist/history.d.ts +12 -0
- package/dist/history.js +98 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +3 -0
- package/dist/match.d.ts +11 -0
- package/dist/match.js +59 -0
- package/dist/qs.d.ts +5 -0
- package/dist/qs.js +27 -0
- package/dist/router.d.ts +53 -0
- package/dist/router.js +141 -0
- package/package.json +28 -21
- package/src/history.ts +116 -0
- package/src/index.ts +18 -0
- package/src/match.ts +76 -0
- package/src/qs.ts +33 -0
- package/src/router.ts +223 -0
- package/.prettierignore +0 -5
- package/.prettierrc +0 -6
- package/.swc-cjs +0 -11
- package/.swc-esm +0 -1
- package/CHANGELOG.md +0 -68
- package/dist/cjs/history.js +0 -100
- package/dist/cjs/index.js +0 -27
- package/dist/cjs/match.js +0 -94
- package/dist/cjs/qs.js +0 -27
- package/dist/cjs/router.js +0 -279
- package/dist/esm/history.js +0 -90
- package/dist/esm/index.js +0 -3
- package/dist/esm/match.js +0 -76
- package/dist/esm/qs.js +0 -17
- package/dist/esm/router.js +0 -258
- package/docs/archetypes/default.md +0 -7
- package/docs/assets/js/bg.js +0 -742
- package/docs/assets/styles/base.scss +0 -2816
- package/docs/assets/styles/main.scss +0 -1368
- package/docs/assets/styles/syntax-m.scss +0 -264
- package/docs/assets/styles/syntax.scss +0 -240
- package/docs/config.toml +0 -9
- package/docs/content/_index.md +0 -189
- package/docs/layouts/404.html +0 -0
- package/docs/layouts/_default/baseof.html +0 -68
- package/docs/layouts/index.html +0 -31
- package/docs/layouts/partials/favicon.html +0 -5
- package/docs/layouts/partials/header.html +0 -0
- package/docs/layouts/partials/meta/name-author.html +0 -6
- package/docs/layouts/partials/meta/ogimage.html +0 -8
- package/docs/layouts/partials/site-verification.html +0 -12
- package/docs/layouts/shortcodes/callout.html +0 -1
- package/docs/static/js/highlightjs-9.15.10.min.js +0 -2
- package/docs/static/js/master.js +0 -0
- package/docs/static/js/tocbot.min.js +0 -18
- package/docs/static/space.png +0 -0
- package/oxlintrc.json +0 -6
- package/src/history.js +0 -97
- package/src/index.js +0 -3
- package/src/match.js +0 -87
- package/src/qs.js +0 -20
- package/src/router.js +0 -147
- package/tasks/build.js +0 -16
- package/test/flatten.test.js +0 -58
- package/test/match.test.js +0 -68
- package/test/router.test.js +0 -157
package/dist/esm/router.js
DELETED
|
@@ -1,258 +0,0 @@
|
|
|
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
|
|
8
|
-
});
|
|
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 = {}, sourceKeys, key, i;
|
|
56
|
-
if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
|
|
57
|
-
sourceKeys = Reflect.ownKeys(Object(source));
|
|
58
|
-
for(i = 0; i < sourceKeys.length; i++){
|
|
59
|
-
key = sourceKeys[i];
|
|
60
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
61
|
-
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
62
|
-
target[key] = source[key];
|
|
63
|
-
}
|
|
64
|
-
return target;
|
|
65
|
-
}
|
|
66
|
-
target = _object_without_properties_loose(source, excluded);
|
|
67
|
-
if (Object.getOwnPropertySymbols) {
|
|
68
|
-
sourceKeys = Object.getOwnPropertySymbols(source);
|
|
69
|
-
for(i = 0; i < sourceKeys.length; i++){
|
|
70
|
-
key = sourceKeys[i];
|
|
71
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
72
|
-
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
73
|
-
target[key] = source[key];
|
|
74
|
-
}
|
|
75
|
-
}
|
|
76
|
-
return target;
|
|
77
|
-
}
|
|
78
|
-
function _object_without_properties_loose(source, excluded) {
|
|
79
|
-
if (source == null) return {};
|
|
80
|
-
var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
|
|
81
|
-
for(i = 0; i < sourceKeys.length; i++){
|
|
82
|
-
key = sourceKeys[i];
|
|
83
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
84
|
-
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
85
|
-
target[key] = source[key];
|
|
86
|
-
}
|
|
87
|
-
return target;
|
|
88
|
-
}
|
|
89
|
-
import { match as findMatch } from './match';
|
|
90
|
-
import { createHistory } from './history';
|
|
91
|
-
import { qs as defaultQs } from './qs';
|
|
92
|
-
export function createRouter() {
|
|
93
|
-
var options = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {};
|
|
94
|
-
var history = null;
|
|
95
|
-
var routes = [];
|
|
96
|
-
var mode = options.mode || 'history';
|
|
97
|
-
var qs = options.qs || defaultQs;
|
|
98
|
-
var sync = options.sync || false;
|
|
99
|
-
var router = {
|
|
100
|
-
listen: function listen(routeMap, cb) {
|
|
101
|
-
if (history) {
|
|
102
|
-
throw new Error('Already listening');
|
|
103
|
-
}
|
|
104
|
-
routes = flatten(routeMap);
|
|
105
|
-
history = createHistory({
|
|
106
|
-
mode: mode,
|
|
107
|
-
sync: sync
|
|
108
|
-
});
|
|
109
|
-
var dispose = history.listen(function(url) {
|
|
110
|
-
return transition(router, url, cb);
|
|
111
|
-
});
|
|
112
|
-
return function() {
|
|
113
|
-
dispose();
|
|
114
|
-
history = null;
|
|
115
|
-
routes = [];
|
|
116
|
-
};
|
|
117
|
-
},
|
|
118
|
-
navigate: function navigate(to, curr) {
|
|
119
|
-
if (typeof to === 'string') {
|
|
120
|
-
to = {
|
|
121
|
-
url: to
|
|
122
|
-
};
|
|
123
|
-
}
|
|
124
|
-
var url = router.href(to, curr);
|
|
125
|
-
if (to.replace) {
|
|
126
|
-
history.replace(url);
|
|
127
|
-
} else {
|
|
128
|
-
history.push(url);
|
|
129
|
-
}
|
|
130
|
-
},
|
|
131
|
-
href: function href(to, curr) {
|
|
132
|
-
// already a url
|
|
133
|
-
if (typeof to === 'string') {
|
|
134
|
-
return to;
|
|
135
|
-
}
|
|
136
|
-
// align with navigate API
|
|
137
|
-
if (to.url) {
|
|
138
|
-
return to.url;
|
|
139
|
-
}
|
|
140
|
-
if (to.merge) {
|
|
141
|
-
curr = curr || router.match(router.getUrl());
|
|
142
|
-
to = merge(curr, to);
|
|
143
|
-
}
|
|
144
|
-
var url = to.pathname || '/';
|
|
145
|
-
if (to.params) {
|
|
146
|
-
Object.keys(to.params).forEach(function(param) {
|
|
147
|
-
url = url.replace(':' + param, to.params[param]);
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
if (to.query && Object.keys(to.query).length) {
|
|
151
|
-
var query = qs.stringify(to.query);
|
|
152
|
-
if (query) {
|
|
153
|
-
url = url + '?' + query;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
|
-
if (to.hash) {
|
|
157
|
-
var prefix = to.hash.startsWith('#') ? '' : '#';
|
|
158
|
-
url = url + prefix + to.hash;
|
|
159
|
-
}
|
|
160
|
-
return url;
|
|
161
|
-
},
|
|
162
|
-
match: function match(url) {
|
|
163
|
-
var route = findMatch(routes, url, qs);
|
|
164
|
-
if (route) {
|
|
165
|
-
return _object_spread_props(_object_spread({}, route), {
|
|
166
|
-
data: data(routes, route)
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
},
|
|
170
|
-
getUrl: function getUrl() {
|
|
171
|
-
return history.getUrl();
|
|
172
|
-
}
|
|
173
|
-
};
|
|
174
|
-
return router;
|
|
175
|
-
}
|
|
176
|
-
export function flatten(routeMap) {
|
|
177
|
-
var routes = [];
|
|
178
|
-
var parentData = [];
|
|
179
|
-
function addLevel(level) {
|
|
180
|
-
level.forEach(function(route) {
|
|
181
|
-
var _route_path = route.path, path = _route_path === void 0 ? '' : _route_path, children = route.routes, routeData = _object_without_properties(route, [
|
|
182
|
-
"path",
|
|
183
|
-
"routes"
|
|
184
|
-
]);
|
|
185
|
-
routes.push({
|
|
186
|
-
pattern: path,
|
|
187
|
-
data: parentData.concat([
|
|
188
|
-
routeData
|
|
189
|
-
])
|
|
190
|
-
});
|
|
191
|
-
if (children) {
|
|
192
|
-
parentData.push(routeData);
|
|
193
|
-
addLevel(children);
|
|
194
|
-
parentData.pop();
|
|
195
|
-
}
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
addLevel(routeMap);
|
|
199
|
-
return routes;
|
|
200
|
-
}
|
|
201
|
-
function transition(router, url, onNavigated) {
|
|
202
|
-
var route = router.match(url);
|
|
203
|
-
if (route) {
|
|
204
|
-
var _iteratorNormalCompletion = true, _didIteratorError = false, _iteratorError = undefined;
|
|
205
|
-
try {
|
|
206
|
-
for(var _iterator = route.data[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true){
|
|
207
|
-
var r = _step.value;
|
|
208
|
-
if (r.redirect) {
|
|
209
|
-
var _$url = redirectUrl(router, r.redirect, route);
|
|
210
|
-
return router.navigate({
|
|
211
|
-
url: _$url,
|
|
212
|
-
replace: true
|
|
213
|
-
});
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
} catch (err) {
|
|
217
|
-
_didIteratorError = true;
|
|
218
|
-
_iteratorError = err;
|
|
219
|
-
} finally{
|
|
220
|
-
try {
|
|
221
|
-
if (!_iteratorNormalCompletion && _iterator.return != null) {
|
|
222
|
-
_iterator.return();
|
|
223
|
-
}
|
|
224
|
-
} finally{
|
|
225
|
-
if (_didIteratorError) {
|
|
226
|
-
throw _iteratorError;
|
|
227
|
-
}
|
|
228
|
-
}
|
|
229
|
-
}
|
|
230
|
-
if (onNavigated) onNavigated(route);
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
function redirectUrl(router, redirect, matchingRoute) {
|
|
234
|
-
if (typeof redirect === 'function') {
|
|
235
|
-
redirect = redirect(matchingRoute);
|
|
236
|
-
}
|
|
237
|
-
return router.href(redirect);
|
|
238
|
-
}
|
|
239
|
-
function data(routes, matchingRoute) {
|
|
240
|
-
for(var i = 0; i < routes.length; i++){
|
|
241
|
-
if (routes[i].pattern === matchingRoute.pattern) {
|
|
242
|
-
return routes[i].data;
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
|
-
export function merge() {
|
|
247
|
-
var curr = arguments.length > 0 && arguments[0] !== void 0 ? arguments[0] : {}, to = arguments.length > 1 ? arguments[1] : void 0;
|
|
248
|
-
var pathname = to.pathname || curr.pattern || curr.pathname;
|
|
249
|
-
var params = Object.assign({}, curr.params, to.params);
|
|
250
|
-
var query = to.query === null ? null : Object.assign({}, curr.query, to.query);
|
|
251
|
-
var hash = to.hash === null ? null : to.hash || curr.hash || '';
|
|
252
|
-
return {
|
|
253
|
-
pathname: pathname,
|
|
254
|
-
params: params,
|
|
255
|
-
query: query,
|
|
256
|
-
hash: hash
|
|
257
|
-
};
|
|
258
|
-
}
|