oxssy-router
Advanced tools
Comparing version 0.1.8 to 0.1.9
@@ -22,10 +22,4 @@ 'use strict'; | ||
var _queryString = require('query-string'); | ||
var _querystring = require('querystring'); | ||
var _queryString2 = _interopRequireDefault(_queryString); | ||
var _randomKey = require('random-key'); | ||
var _randomKey2 = _interopRequireDefault(_randomKey); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -42,6 +36,6 @@ | ||
var history = (0, _createBrowserHistory2.default)(); | ||
var routes = {}; | ||
var routes = new Set(); | ||
var resolveRoute = function resolveRoute() { | ||
Object.values(routes).forEach(function (route) { | ||
routes.forEach(function (route) { | ||
return route.forceUpdate(); | ||
@@ -61,5 +55,7 @@ }); | ||
var route = props.route, | ||
option = props.option; | ||
_this.params = []; | ||
_this.route = (0, _pathToRegexp2.default)(props.route, _this.params, props.options); | ||
_this.key = _randomKey2.default.generate(); | ||
_this.route = (0, _pathToRegexp2.default)(route, _this.params, option); | ||
return _this; | ||
@@ -71,3 +67,3 @@ } | ||
value: function componentWillMount() { | ||
routes[this.key] = this; | ||
routes.add(this); | ||
} | ||
@@ -77,4 +73,4 @@ }, { | ||
value: function componentWillUnmount() { | ||
if (routes[this.key]) { | ||
delete routes[this.key]; | ||
if (routes.has(this)) { | ||
routes.delete(this); | ||
} | ||
@@ -87,2 +83,5 @@ } | ||
var _props = this.props, | ||
children = _props.children, | ||
unmatch = _props.unmatch; | ||
var _window$location = window.location, | ||
@@ -93,3 +92,3 @@ pathname = _window$location.pathname, | ||
var match = this.route.exec(pathname); | ||
var isMatch = !!match ^ !!this.props.unmatch; | ||
var isMatch = !!match ^ !!unmatch; | ||
if (!isMatch) { | ||
@@ -104,4 +103,4 @@ return null; | ||
}); | ||
var query = _queryString2.default.parse(search); | ||
return (0, _react.cloneElement)(_react.Children.only(this.props.children), { params: params, query: query }); | ||
var query = (0, _querystring.parse)(search); | ||
return (0, _react.cloneElement)(_react.Children.only(children), { params: params, query: query }); | ||
} | ||
@@ -118,3 +117,3 @@ }]); | ||
pathname: pathname, | ||
search: typeof search === 'string' ? search : _queryString2.default.stringify(search) | ||
search: typeof search === 'string' ? search : (0, _querystring.stringify)(search) | ||
}); | ||
@@ -121,0 +120,0 @@ }; |
27
index.js
import { cloneElement, Children, Component, createElement } from 'react'; | ||
import createHistory from 'history/createBrowserHistory'; | ||
import toRegex from 'path-to-regexp'; | ||
import queryString from 'query-string'; | ||
import randomKey from 'random-key'; | ||
import { parse, stringify } from 'querystring'; | ||
const history = createHistory(); | ||
const routes = {}; | ||
const routes = new Set(); | ||
const resolveRoute = () => { | ||
Object.values(routes).forEach(route => route.forceUpdate()); | ||
routes.forEach(route => route.forceUpdate()); | ||
}; | ||
@@ -20,14 +20,14 @@ | ||
super(props); | ||
const { route, option } = props; | ||
this.params = []; | ||
this.route = toRegex(props.route, this.params, props.options); | ||
this.key = randomKey.generate(); | ||
this.route = toRegex(route, this.params, option); | ||
} | ||
componentWillMount() { | ||
routes[this.key] = this; | ||
routes.add(this); | ||
} | ||
componentWillUnmount() { | ||
if (routes[this.key]) { | ||
delete routes[this.key]; | ||
if (routes.has(this)) { | ||
routes.delete(this); | ||
} | ||
@@ -37,5 +37,6 @@ } | ||
render() { | ||
const { children, unmatch } = this.props; | ||
const { pathname, search } = window.location; | ||
const match = this.route.exec(pathname); | ||
const isMatch = !!match ^ !!this.props.unmatch; | ||
const isMatch = !!match ^ !!unmatch; | ||
if (!isMatch) { | ||
@@ -50,4 +51,4 @@ return null; | ||
}); | ||
const query = queryString.parse(search); | ||
return cloneElement(Children.only(this.props.children), { params, query }); | ||
const query = parse(search); | ||
return cloneElement(Children.only(children), { params, query }); | ||
} | ||
@@ -59,3 +60,3 @@ } | ||
pathname, | ||
search: typeof search === 'string' ? search : queryString.stringify(search), | ||
search: typeof search === 'string' ? search : stringify(search), | ||
}); | ||
@@ -62,0 +63,0 @@ }; |
{ | ||
"name": "oxssy-router", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"author": "Oxssy <oxssy.ai@gmail.com>", | ||
@@ -20,4 +20,2 @@ "description": "A simple router for react applications", | ||
"path-to-regexp": ">=2.1.0", | ||
"query-string": ">=6.1.0", | ||
"random-key": ">=0.3.2", | ||
"react": ">=16.2.0", | ||
@@ -24,0 +22,0 @@ "react-dom": ">=16.2.0" |
4
172
8743
- Removedquery-string@>=6.1.0
- Removedrandom-key@>=0.3.2
- Removeddecode-uri-component@0.4.1(transitive)
- Removedfilter-obj@5.1.0(transitive)
- Removedquery-string@9.0.0(transitive)
- Removedrandom-key@0.3.2(transitive)
- Removedsplit-on-first@3.0.0(transitive)