space-router 0.8.5 → 0.9.1

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 ADDED
@@ -0,0 +1,11 @@
1
+ {
2
+ "module": {
3
+ "type": "commonjs"
4
+ },
5
+ "env": {
6
+ "targets": [
7
+ "last 2 versions",
8
+ "IE >= 9"
9
+ ]
10
+ }
11
+ }
package/CHANGELOG.md CHANGED
@@ -1,3 +1,12 @@
1
+ ## 0.9.1
2
+
3
+ - Revert to commonjs (remove `type: "module"`) to keep support for cjs in node and esm in browsers.
4
+
5
+ ## 0.9.0
6
+
7
+ - Upgrade all dependencies.
8
+ - Switch from babel to swc for dist compilation.
9
+
1
10
  ## 0.8.5
2
11
 
3
12
  - fix redirects - the history was not ready by the time initial transition was kicked off by redirecting
package/LICENSE CHANGED
@@ -1,6 +1,6 @@
1
1
  ISC License
2
2
 
3
- Copyright 2021 Karolis Narkevicius
3
+ Copyright 2023 Karolis Narkevicius
4
4
 
5
5
  Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
6
6
 
@@ -1,107 +1,96 @@
1
1
  "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.createHistory = createHistory;
5
-
6
- function createHistory(options) {
7
- var sync = options.sync;
8
- var mode = options.mode;
9
- var raf;
10
- var onPop;
11
- var memory = [];
12
- var off;
13
- var destroyed = false;
14
-
15
- if (typeof window === 'undefined') {
16
- mode = 'memory';
17
- raf = sync ? function (cb) {
18
- return cb();
19
- } : global.setImmediate;
20
- } else {
21
- raf = sync ? function (cb) {
22
- return cb();
23
- } : requestAnimationFrame;
24
-
25
- if (mode === 'history' && !history.pushState) {
26
- mode = 'hash';
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "createHistory", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return createHistory;
27
9
  }
28
- }
29
-
30
- function listen(onChange) {
31
- onPop = function onPop() {
32
- return onChange(getUrl());
10
+ });
11
+ function createHistory(options) {
12
+ var listen = function listen(onChange) {
13
+ onPop = function() {
14
+ return onChange(getUrl());
15
+ };
16
+ if (mode !== "memory") {
17
+ off = on(window, mode === "history" ? "popstate" : "hashchange", onPop);
18
+ raf(onPop);
19
+ }
20
+ return function() {
21
+ destroyed = true;
22
+ off && off();
23
+ };
33
24
  };
34
-
35
- if (mode !== 'memory') {
36
- off = on(window, mode === 'history' ? 'popstate' : 'hashchange', onPop);
37
- raf(onPop);
38
- }
39
-
40
- return function () {
41
- destroyed = true;
42
- off && off();
25
+ var go = function go(url, replace) {
26
+ if (destroyed) return;
27
+ url = url.replace(/^\/?#?\/?/, "/").replace(/\/$/, "") || "/";
28
+ if (mode === "history") {
29
+ history[replace ? "replaceState" : "pushState"]({}, "", url);
30
+ raf(onPop);
31
+ } else if (mode === "hash") {
32
+ location[replace ? "replace" : "assign"]("#" + url);
33
+ } else if (mode === "memory") {
34
+ replace ? memory[memory.length - 1] = url : memory.push(url);
35
+ raf(onPop);
36
+ }
43
37
  };
44
- }
45
-
46
- return {
47
- listen: listen,
48
- getUrl: getUrl,
49
- push: function push(url) {
50
- go(url);
51
- },
52
- replace: function replace(url) {
53
- go(url, true);
54
- }
55
- };
56
-
57
- function go(url, replace) {
58
- if (destroyed) return;
59
- url = url.replace(/^\/?#?\/?/, '/').replace(/\/$/, '') || '/';
60
-
61
- if (mode === 'history') {
62
- history[replace ? 'replaceState' : 'pushState']({}, '', url);
63
- raf(onPop);
64
- } else if (mode === 'hash') {
65
- location[replace ? 'replace' : 'assign']('#' + url);
66
- } else if (mode === 'memory') {
67
- replace ? memory[memory.length - 1] = url : memory.push(url);
68
- raf(onPop);
69
- }
70
- }
71
-
72
- function getUrl() {
73
- if (mode === 'memory') {
74
- return memory[memory.length - 1];
75
- }
76
-
77
- var hash = getHash();
78
-
79
- if (mode === 'hash') {
80
- return hash === '' ? '/' : hash;
81
- }
82
-
83
- if (mode === 'history') {
84
- var url = location.pathname + location.search;
85
-
86
- if (hash !== '') {
87
- url += '#' + hash;
88
- }
89
-
90
- return url;
38
+ var getUrl = function getUrl() {
39
+ if (mode === "memory") {
40
+ return memory[memory.length - 1];
41
+ }
42
+ var hash = getHash();
43
+ if (mode === "hash") {
44
+ return hash === "" ? "/" : hash;
45
+ }
46
+ if (mode === "history") {
47
+ var url = location.pathname + location.search;
48
+ if (hash !== "") {
49
+ url += "#" + hash;
50
+ }
51
+ return url;
52
+ }
53
+ };
54
+ var getHash = // Gets the true hash value. Cannot use location.hash directly due to bug
55
+ // in Firefox where location.hash will always be decoded.
56
+ function getHash() {
57
+ var match = location.href.match(/#(.*)$/);
58
+ return match ? match[1].replace("#", "") : "";
59
+ };
60
+ var sync = options.sync;
61
+ var mode = options.mode;
62
+ var raf;
63
+ var onPop;
64
+ var memory = [];
65
+ var off;
66
+ var destroyed = false;
67
+ if (typeof window === "undefined") {
68
+ mode = "memory";
69
+ raf = sync ? function(cb) {
70
+ return cb();
71
+ } : global.setImmediate;
72
+ } else {
73
+ raf = sync ? function(cb) {
74
+ return cb();
75
+ } : requestAnimationFrame;
76
+ if (mode === "history" && !history.pushState) {
77
+ mode = "hash";
78
+ }
91
79
  }
92
- } // Gets the true hash value. Cannot use location.hash directly due to bug
93
- // in Firefox where location.hash will always be decoded.
94
-
95
-
96
- function getHash() {
97
- var match = location.href.match(/#(.*)$/);
98
- return match ? match[1].replace('#', '') : '';
99
- }
80
+ return {
81
+ listen: listen,
82
+ getUrl: getUrl,
83
+ push: function(url) {
84
+ go(url);
85
+ },
86
+ replace: function(url) {
87
+ go(url, true);
88
+ }
89
+ };
100
90
  }
101
-
102
91
  function on(el, type, fn) {
103
- el.addEventListener(type, fn, false);
104
- return function off() {
105
- el.removeEventListener(type, fn, false);
106
- };
107
- }
92
+ el.addEventListener(type, fn, false);
93
+ return function off() {
94
+ el.removeEventListener(type, fn, false);
95
+ };
96
+ }
package/dist/cjs/index.js CHANGED
@@ -1,17 +1,27 @@
1
1
  "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.qs = exports.merge = exports.createRouter = exports.createHistory = void 0;
5
-
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ createRouter: function() {
13
+ return _router.createRouter;
14
+ },
15
+ merge: function() {
16
+ return _router.merge;
17
+ },
18
+ createHistory: function() {
19
+ return _history.createHistory;
20
+ },
21
+ qs: function() {
22
+ return _qs.qs;
23
+ }
24
+ });
6
25
  var _router = require("./router");
7
-
8
- exports.createRouter = _router.createRouter;
9
- exports.merge = _router.merge;
10
-
11
26
  var _history = require("./history");
12
-
13
- exports.createHistory = _history.createHistory;
14
-
15
27
  var _qs = require("./qs");
16
-
17
- exports.qs = _qs.qs;
package/dist/cjs/match.js CHANGED
@@ -1,97 +1,94 @@
1
1
  "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.match = match;
5
- exports.matchOne = matchOne;
6
-
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ match: function() {
13
+ return match;
14
+ },
15
+ matchOne: function() {
16
+ return matchOne;
17
+ }
18
+ });
7
19
  function match(routes, url, qs) {
8
- if (!url) {
9
- return;
10
- }
11
-
12
- for (var i = 0; i < routes.length; i++) {
13
- var m = matchOne(routes[i].pattern, url, qs);
14
-
15
- if (m) {
16
- return m;
20
+ if (!url) {
21
+ return;
22
+ }
23
+ for(var i = 0; i < routes.length; i++){
24
+ var m = matchOne(routes[i].pattern, url, qs);
25
+ if (m) {
26
+ return m;
27
+ }
17
28
  }
18
- }
19
29
  }
20
-
21
30
  function matchOne(pattern, url, qs) {
22
- if (!pattern) {
23
- return false;
24
- }
25
-
26
- var re = /(?:\?([^#]*))?(#.*)?$/;
27
- var originalUrl = url;
28
- var originalPattern = pattern;
29
- var c = url.match(re);
30
- var params = {};
31
- var query = {};
32
- var search = '';
33
- var hash = '';
34
- var ret;
35
-
36
- if (c && c[1]) {
37
- search = '?' + c[1];
38
- query = qs.parse(c[1]);
39
- }
40
-
41
- if (c && c[2]) {
42
- hash = c[2] || '';
43
- }
44
-
45
- if (pattern !== '*') {
46
- url = segmentize(url.replace(re, ''));
47
- pattern = segmentize(pattern || '');
48
- var max = Math.max(url.length, pattern.length);
49
-
50
- for (var i = 0; i < max; i++) {
51
- if (pattern[i] && pattern[i].charAt(0) === ':') {
52
- var param = pattern[i].replace(/(^:|[+*?]+$)/g, '');
53
- var flags = (pattern[i].match(/[+*?]+$/) || {})[0] || '';
54
- var plus = flags.indexOf('+') > -1;
55
- var star = flags.indexOf('*') > -1;
56
- var val = url[i] || '';
57
-
58
- if (!val && !star && (flags.indexOf('?') < 0 || plus)) {
59
- ret = false;
60
- break;
31
+ if (!pattern) {
32
+ return false;
33
+ }
34
+ var re = /(?:\?([^#]*))?(#.*)?$/;
35
+ var originalUrl = url;
36
+ var originalPattern = pattern;
37
+ var c = url.match(re);
38
+ var params = {};
39
+ var query = {};
40
+ var search = "";
41
+ var hash = "";
42
+ var ret;
43
+ if (c && c[1]) {
44
+ search = "?" + c[1];
45
+ query = qs.parse(c[1]);
46
+ }
47
+ if (c && c[2]) {
48
+ hash = c[2] || "";
49
+ }
50
+ if (pattern !== "*") {
51
+ url = segmentize(url.replace(re, ""));
52
+ pattern = segmentize(pattern || "");
53
+ var max = Math.max(url.length, pattern.length);
54
+ for(var i = 0; i < max; i++){
55
+ if (pattern[i] && pattern[i].charAt(0) === ":") {
56
+ var param = pattern[i].replace(/(^:|[+*?]+$)/g, "");
57
+ var flags = (pattern[i].match(/[+*?]+$/) || {})[0] || "";
58
+ var plus = flags.indexOf("+") > -1;
59
+ var star = flags.indexOf("*") > -1;
60
+ var val = url[i] || "";
61
+ if (!val && !star && (flags.indexOf("?") < 0 || plus)) {
62
+ ret = false;
63
+ break;
64
+ }
65
+ params[param] = decodeURIComponent(val);
66
+ if (plus || star) {
67
+ params[param] = url.slice(i).map(decodeURIComponent).join("/");
68
+ break;
69
+ }
70
+ } else if (pattern[i] !== url[i]) {
71
+ ret = false;
72
+ break;
73
+ }
61
74
  }
62
-
63
- params[param] = decodeURIComponent(val);
64
-
65
- if (plus || star) {
66
- params[param] = url.slice(i).map(decodeURIComponent).join('/');
67
- break;
75
+ if (ret === false) {
76
+ return false;
68
77
  }
69
- } else if (pattern[i] !== url[i]) {
70
- ret = false;
71
- break;
72
- }
73
78
  }
74
-
75
- if (ret === false) {
76
- return false;
77
- }
78
- }
79
-
80
- return {
81
- pattern: originalPattern,
82
- url: originalUrl,
83
- pathname: originalUrl.replace(re, ''),
84
- params: params,
85
- query: query,
86
- search: search,
87
- hash: hash
88
- };
79
+ return {
80
+ pattern: originalPattern,
81
+ url: originalUrl,
82
+ pathname: originalUrl.replace(re, ""),
83
+ params: params,
84
+ query: query,
85
+ search: search,
86
+ hash: hash
87
+ };
89
88
  }
90
-
91
89
  function segmentize(url) {
92
- return strip(url).split('/');
90
+ return strip(url).split("/");
93
91
  }
94
-
95
92
  function strip(url) {
96
- return url.replace(/(^\/+|\/+$)/g, '');
97
- }
93
+ return url.replace(/(^\/+|\/+$)/g, "");
94
+ }
package/dist/cjs/qs.js CHANGED
@@ -1,23 +1,27 @@
1
1
  "use strict";
2
-
3
- exports.__esModule = true;
4
- exports.qs = void 0;
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "qs", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return qs;
9
+ }
10
+ });
5
11
  var qs = {
6
- parse: function parse(queryString) {
7
- return queryString.split('&').reduce(function (acc, pair) {
8
- var parts = pair.split('=');
9
- acc[parts[0]] = decodeURIComponent(parts[1]);
10
- return acc;
11
- }, {});
12
- },
13
- stringify: function stringify(query) {
14
- return Object.keys(query).reduce(function (acc, key) {
15
- if (query[key] !== undefined) {
16
- acc.push(key + '=' + encodeURIComponent(query[key]));
17
- }
18
-
19
- return acc;
20
- }, []).join('&');
21
- }
12
+ parse: function(queryString) {
13
+ return queryString.split("&").reduce(function(acc, pair) {
14
+ var parts = pair.split("=");
15
+ acc[parts[0]] = decodeURIComponent(parts[1]);
16
+ return acc;
17
+ }, {});
18
+ },
19
+ stringify: function(query) {
20
+ return Object.keys(query).reduce(function(acc, key) {
21
+ if (query[key] !== undefined) {
22
+ acc.push(key + "=" + encodeURIComponent(query[key]));
23
+ }
24
+ return acc;
25
+ }, []).join("&");
26
+ }
22
27
  };
23
- exports.qs = qs;