Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

preact-router

Package Overview
Dependencies
Maintainers
2
Versions
46
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

preact-router - npm Package Compare versions

Comparing version 2.5.7 to 2.6.0

70

dist/preact-router.es.js

@@ -14,4 +14,2 @@ import { Component, cloneElement, h } from 'preact';

function exec(url, route, opts) {
if ( opts === void 0 ) opts=EMPTY$1;
var reg = /(?:\?([^#]*))?(#.*)?$/,

@@ -58,22 +56,32 @@ c = url.match(reg),

function pathRankSort(a, b) {
var aAttr = a.attributes || EMPTY$1,
bAttr = b.attributes || EMPTY$1;
if (aAttr.default) { return 1; }
if (bAttr.default) { return -1; }
var diff = rank(aAttr.path) - rank(bAttr.path);
return diff || (aAttr.path.length - bAttr.path.length);
return (
(a.rank < b.rank) ? 1 :
(a.rank > b.rank) ? -1 :
(a.index - b.index)
);
}
// filter out VNodes without attributes (which are unrankeable), and add `index`/`rank` properties to be used in sorting.
function prepareVNodeForRanking(vnode, index) {
vnode.index = index;
vnode.rank = rankChild(vnode);
return vnode.attributes;
}
function segmentize(url) {
return strip(url).split('/');
return url.replace(/(^\/+|\/+$)/g, '').split('/');
}
function rank(url) {
return (strip(url).match(/\/+/g) || '').length;
function rankSegment(segment) {
return segment.charAt(0)==':' ? (1 + '*+?'.indexOf(segment.charAt(segment.length-1))) || 4 : 5;
}
function strip(url) {
return url.replace(/(^\/+|\/+$)/g, '');
function rank(path) {
return segmentize(path).map(rankSegment).join('');
}
function rankChild(vnode) {
return vnode.attributes.default ? 0 : rank(vnode.attributes.path);
}
var customHistory = null;

@@ -213,9 +221,9 @@

function initEventListeners() {
if (eventListenersInitialized){
return;
}
if (eventListenersInitialized) { return; }
if (typeof addEventListener==='function') {
if (!customHistory) {
addEventListener('popstate', function () { return routeTo(getCurrentUrl()); });
addEventListener('popstate', function () {
routeTo(getCurrentUrl());
});
}

@@ -298,16 +306,18 @@ addEventListener('click', delegateLinkHandler);

Router.prototype.getMatchingChildren = function getMatchingChildren (children, url, invoke) {
return children.slice().sort(pathRankSort).map( function (vnode) {
var attrs = vnode.attributes || {},
path = attrs.path,
matches = exec(url, path, attrs);
if (matches) {
if (invoke!==false) {
var newProps = { url: url, matches: matches };
assign(newProps, matches);
return cloneElement(vnode, newProps);
return children
.filter(prepareVNodeForRanking)
.sort(pathRankSort)
.map( function (vnode) {
var matches = exec(url, vnode.attributes.path, vnode.attributes);
if (matches) {
if (invoke !== false) {
var newProps = { url: url, matches: matches };
assign(newProps, matches);
delete newProps.ref;
delete newProps.key;
return cloneElement(vnode, newProps);
}
return vnode;
}
return vnode;
}
return false;
}).filter(Boolean);
}).filter(Boolean);
};

@@ -314,0 +324,0 @@

@@ -1,2 +0,2 @@

!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("preact")):"function"==typeof define&&define.amd?define(["preact"],e):t.preactRouter=e(t.preact)}(this,function(t){function e(t,e){for(var n in e)t[n]=e[n];return t}function n(t,e,n){void 0===n&&(n=v);var r,i=/(?:\?([^#]*))?(#.*)?$/,u=t.match(i),a={};if(u&&u[1])for(var p=u[1].split("&"),c=0;c<p.length;c++){var f=p[c].split("=");a[decodeURIComponent(f[0])]=decodeURIComponent(f.slice(1).join("="))}t=o(t.replace(i,"")),e=o(e||"");for(var l=Math.max(t.length,e.length),s=0;s<l;s++)if(e[s]&&":"===e[s].charAt(0)){var h=e[s].replace(/(^\:|[+*?]+$)/g,""),d=(e[s].match(/[+*?]+$/)||v)[0]||"",g=~d.indexOf("+"),m=~d.indexOf("*"),y=t[s]||"";if(!y&&!m&&(d.indexOf("?")<0||g)){r=!1;break}if(a[h]=decodeURIComponent(y),g||m){a[h]=t.slice(s).map(decodeURIComponent).join("/");break}}else if(e[s]!==t[s]){r=!1;break}return(n.default===!0||r!==!1)&&a}function r(t,e){var n=t.attributes||v,r=e.attributes||v;if(n.default)return 1;if(r.default)return-1;var o=i(n.path)-i(r.path);return o||n.path.length-r.path.length}function o(t){return u(t).split("/")}function i(t){return(u(t).match(/\/+/g)||"").length}function u(t){return t.replace(/(^\/+|\/+$)/g,"")}function a(t){return null!=t.__preactattr_||"undefined"!=typeof Symbol&&null!=t[Symbol.for("preactattr")]}function p(t,e){void 0===e&&(e="push"),b&&b[e]?b[e](t):"undefined"!=typeof history&&history[e+"State"]&&history[e+"State"](null,null,t)}function c(){var t;return t=b&&b.location?b.location:b&&b.getCurrentLocation?b.getCurrentLocation():"undefined"!=typeof location?location:U,""+(t.pathname||"")+(t.search||"")}function f(t,e){return void 0===e&&(e=!1),"string"!=typeof t&&t.url&&(e=t.replace,t=t.url),l(t)&&p(t,e?"replace":"push"),s(t)}function l(t){for(var e=C.length;e--;)if(C[e].canRoute(t))return!0;return!1}function s(t){for(var e=!1,n=0;n<C.length;n++)C[n].routeTo(t)===!0&&(e=!0);for(var r=R.length;r--;)R[r](t);return e}function h(t){if(t&&t.getAttribute){var e=t.getAttribute("href"),n=t.getAttribute("target");if(e&&e.match(/^\//g)&&(!n||n.match(/^_?self$/i)))return f(e)}}function d(t){if(0==t.button)return h(t.currentTarget||t.target||this),g(t)}function g(t){return t&&(t.stopImmediatePropagation&&t.stopImmediatePropagation(),t.stopPropagation&&t.stopPropagation(),t.preventDefault()),!1}function m(t){if(!(t.ctrlKey||t.metaKey||t.altKey||t.shiftKey||0!==t.button)){var e=t.target;do if("A"===(e.nodeName+"").toUpperCase()&&e.getAttribute("href")&&a(e)){if(e.hasAttribute("native"))return;if(h(e))return g(t)}while(e=e.parentNode)}}function y(){_||("function"==typeof addEventListener&&(b||addEventListener("popstate",function(){return s(c())}),addEventListener("click",m)),_=!0)}var v={},b=null,C=[],R=[],U={},_=!1,x=function(o){function i(t){o.call(this,t),t.history&&(b=t.history),this.state={url:t.url||c()},y()}return o&&(i.__proto__=o),i.prototype=Object.create(o&&o.prototype),i.prototype.constructor=i,i.prototype.shouldComponentUpdate=function(t){return t.static!==!0||(t.url!==this.props.url||t.onChange!==this.props.onChange)},i.prototype.canRoute=function(t){return this.getMatchingChildren(this.props.children,t,!1).length>0},i.prototype.routeTo=function(t){return this._didRoute=!1,this.setState({url:t}),this.updating?this.canRoute(t):(this.forceUpdate(),this._didRoute)},i.prototype.componentWillMount=function(){C.push(this),this.updating=!0},i.prototype.componentDidMount=function(){var t=this;b&&(this.unlisten=b.listen(function(e){t.routeTo(""+(e.pathname||"")+(e.search||""))})),this.updating=!1},i.prototype.componentWillUnmount=function(){"function"==typeof this.unlisten&&this.unlisten(),C.splice(C.indexOf(this),1)},i.prototype.componentWillUpdate=function(){this.updating=!0},i.prototype.componentDidUpdate=function(){this.updating=!1},i.prototype.getMatchingChildren=function(o,i,u){return o.slice().sort(r).map(function(r){var o=r.attributes||{},a=o.path,p=n(i,a,o);if(p){if(u!==!1){var c={url:i,matches:p};return e(c,p),t.cloneElement(r,c)}return r}return!1}).filter(Boolean)},i.prototype.render=function(t,e){var n=t.children,r=t.onChange,o=e.url,i=this.getMatchingChildren(n,o,!0),u=i[0]||null;this._didRoute=!!u;var a=this.previousUrl;return o!==a&&(this.previousUrl=o,"function"==typeof r&&r({router:this,url:o,previous:a,active:i,current:u})),u},i}(t.Component),A=function(n){return t.h("a",e({onClick:d},n))},k=function(e){return t.h(e.component,e)};return x.subscribers=R,x.getCurrentUrl=c,x.route=f,x.Router=x,x.Route=k,x.Link=A,x});
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e(require("preact")):"function"==typeof define&&define.amd?define(["preact"],e):t.preactRouter=e(t.preact)}(this,function(t){function e(t,e){for(var n in e)t[n]=e[n];return t}function n(t,e,n){var r,o=/(?:\?([^#]*))?(#.*)?$/,u=t.match(o),a={};if(u&&u[1])for(var p=u[1].split("&"),c=0;c<p.length;c++){var f=p[c].split("=");a[decodeURIComponent(f[0])]=decodeURIComponent(f.slice(1).join("="))}t=i(t.replace(o,"")),e=i(e||"");for(var l=Math.max(t.length,e.length),s=0;s<l;s++)if(e[s]&&":"===e[s].charAt(0)){var h=e[s].replace(/(^\:|[+*?]+$)/g,""),d=(e[s].match(/[+*?]+$/)||C)[0]||"",g=~d.indexOf("+"),m=~d.indexOf("*"),y=t[s]||"";if(!y&&!m&&(d.indexOf("?")<0||g)){r=!1;break}if(a[h]=decodeURIComponent(y),g||m){a[h]=t.slice(s).map(decodeURIComponent).join("/");break}}else if(e[s]!==t[s]){r=!1;break}return(n.default===!0||r!==!1)&&a}function r(t,e){return t.rank<e.rank?1:t.rank>e.rank?-1:t.index-e.index}function o(t,e){return t.index=e,t.rank=p(t),t.attributes}function i(t){return t.replace(/(^\/+|\/+$)/g,"").split("/")}function u(t){return":"==t.charAt(0)?1+"*+?".indexOf(t.charAt(t.length-1))||4:5}function a(t){return i(t).map(u).join("")}function p(t){return t.attributes.default?0:a(t.attributes.path)}function c(t){return null!=t.__preactattr_||"undefined"!=typeof Symbol&&null!=t[Symbol.for("preactattr")]}function f(t,e){void 0===e&&(e="push"),R&&R[e]?R[e](t):"undefined"!=typeof history&&history[e+"State"]&&history[e+"State"](null,null,t)}function l(){var t;return t=R&&R.location?R.location:R&&R.getCurrentLocation?R.getCurrentLocation():"undefined"!=typeof location?location:x,""+(t.pathname||"")+(t.search||"")}function s(t,e){return void 0===e&&(e=!1),"string"!=typeof t&&t.url&&(e=t.replace,t=t.url),h(t)&&f(t,e?"replace":"push"),d(t)}function h(t){for(var e=U.length;e--;)if(U[e].canRoute(t))return!0;return!1}function d(t){for(var e=!1,n=0;n<U.length;n++)U[n].routeTo(t)===!0&&(e=!0);for(var r=k.length;r--;)k[r](t);return e}function g(t){if(t&&t.getAttribute){var e=t.getAttribute("href"),n=t.getAttribute("target");if(e&&e.match(/^\//g)&&(!n||n.match(/^_?self$/i)))return s(e)}}function m(t){if(0==t.button)return g(t.currentTarget||t.target||this),y(t)}function y(t){return t&&(t.stopImmediatePropagation&&t.stopImmediatePropagation(),t.stopPropagation&&t.stopPropagation(),t.preventDefault()),!1}function v(t){if(!(t.ctrlKey||t.metaKey||t.altKey||t.shiftKey||0!==t.button)){var e=t.target;do if("A"===(e.nodeName+"").toUpperCase()&&e.getAttribute("href")&&c(e)){if(e.hasAttribute("native"))return;if(g(e))return y(t)}while(e=e.parentNode)}}function b(){_||("function"==typeof addEventListener&&(R||addEventListener("popstate",function(){d(l())}),addEventListener("click",v)),_=!0)}var C={},R=null,U=[],k=[],x={},_=!1,A=function(i){function u(t){i.call(this,t),t.history&&(R=t.history),this.state={url:t.url||l()},b()}return i&&(u.__proto__=i),u.prototype=Object.create(i&&i.prototype),u.prototype.constructor=u,u.prototype.shouldComponentUpdate=function(t){return t.static!==!0||(t.url!==this.props.url||t.onChange!==this.props.onChange)},u.prototype.canRoute=function(t){return this.getMatchingChildren(this.props.children,t,!1).length>0},u.prototype.routeTo=function(t){return this._didRoute=!1,this.setState({url:t}),this.updating?this.canRoute(t):(this.forceUpdate(),this._didRoute)},u.prototype.componentWillMount=function(){U.push(this),this.updating=!0},u.prototype.componentDidMount=function(){var t=this;R&&(this.unlisten=R.listen(function(e){t.routeTo(""+(e.pathname||"")+(e.search||""))})),this.updating=!1},u.prototype.componentWillUnmount=function(){"function"==typeof this.unlisten&&this.unlisten(),U.splice(U.indexOf(this),1)},u.prototype.componentWillUpdate=function(){this.updating=!0},u.prototype.componentDidUpdate=function(){this.updating=!1},u.prototype.getMatchingChildren=function(i,u,a){return i.filter(o).sort(r).map(function(r){var o=n(u,r.attributes.path,r.attributes);if(o){if(a!==!1){var i={url:u,matches:o};return e(i,o),delete i.ref,delete i.key,t.cloneElement(r,i)}return r}}).filter(Boolean)},u.prototype.render=function(t,e){var n=t.children,r=t.onChange,o=e.url,i=this.getMatchingChildren(n,o,!0),u=i[0]||null;this._didRoute=!!u;var a=this.previousUrl;return o!==a&&(this.previousUrl=o,"function"==typeof r&&r({router:this,url:o,previous:a,active:i,current:u})),u},u}(t.Component),I=function(n){return t.h("a",e({onClick:m},n))},L=function(e){return t.h(e.component,e)};return A.subscribers=k,A.getCurrentUrl=l,A.route=s,A.Router=A,A.Route=L,A.Link=I,A});
//# sourceMappingURL=preact-router.js.map

@@ -51,2 +51,4 @@ import * as preact from 'preact';

export const subscribers: Array<(url: string) => void>
type AnyComponent<Props> =

@@ -66,2 +68,7 @@ | preact.FunctionalComponent<Props>

declare module 'preact' {
export interface ComponentProps<C extends preact.Component<any, any> | preact.FunctionalComponent<any>> extends RoutableProps {}
export interface PreactHTMLAttributes extends RoutableProps {}
}
export default Router;
{
"name": "preact-router",
"amdName": "preactRouter",
"version": "2.5.7",
"version": "2.6.0",
"description": "Connect your components up to that address bar.",

@@ -16,6 +16,7 @@ "main": "dist/preact-router.js",

"size": "size=$(gzip-size $npm_package_main) && echo \"gzip size: $size / $(pretty-bytes $size)\"",
"test": "npm-run-all lint build test:karma",
"test": "npm-run-all lint build test:karma test:ts",
"lint": "eslint {src,test,test_helpers}",
"test:karma": "karma start --single-run",
"test:watch": "karma start",
"test:ts": "tsc -p ./test",
"prepublish": "npm-run-all build test",

@@ -89,4 +90,5 @@ "release": "npm run build && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish"

"sinon-chai": "^2.8.0",
"typescript": "^2.5.3",
"webpack": "^1.13.1"
}
}

@@ -69,7 +69,7 @@ # preact-router

path="/friends"
component={ () => import('./friends').then(module => module.default) }
getComponent={ () => import('./friends').then(module => module.default) }
/>
<AsyncRoute
path="/friends/:id"
component={ () => import('./friend').then(module => module.default) }
getComponent={ () => import('./friend').then(module => module.default) }
loading={ () => <div>loading...</div> }

@@ -141,3 +141,3 @@ />

Can easily be impleted with a custom `Redirect` component;
Can easily be implemented with a custom `Redirect` component;

@@ -150,3 +150,3 @@ ```js

componentWillMount() {
route(this.props.to);
route(this.props.to, true);
}

@@ -153,0 +153,0 @@

@@ -51,2 +51,4 @@ import * as preact from 'preact';

export const subscribers: Array<(url: string) => void>
type AnyComponent<Props> =

@@ -66,2 +68,7 @@ | preact.FunctionalComponent<Props>

declare module 'preact' {
export interface ComponentProps<C extends preact.Component<any, any> | preact.FunctionalComponent<any>> extends RoutableProps {}
export interface PreactHTMLAttributes extends RoutableProps {}
}
export default Router;
import { cloneElement, h, Component } from 'preact';
import { exec, pathRankSort, assign } from './util';
import { exec, prepareVNodeForRanking, assign, pathRankSort } from './util';

@@ -134,9 +134,9 @@ let customHistory = null;

function initEventListeners() {
if (eventListenersInitialized){
return;
}
if (eventListenersInitialized) return;
if (typeof addEventListener==='function') {
if (!customHistory) {
addEventListener('popstate', () => routeTo(getCurrentUrl()));
addEventListener('popstate', () => {
routeTo(getCurrentUrl());
});
}

@@ -213,16 +213,18 @@ addEventListener('click', delegateLinkHandler);

getMatchingChildren(children, url, invoke) {
return children.slice().sort(pathRankSort).map( vnode => {
let attrs = vnode.attributes || {},
path = attrs.path,
matches = exec(url, path, attrs);
if (matches) {
if (invoke!==false) {
let newProps = { url, matches };
assign(newProps, matches);
return cloneElement(vnode, newProps);
return children
.filter(prepareVNodeForRanking)
.sort(pathRankSort)
.map( vnode => {
let matches = exec(url, vnode.attributes.path, vnode.attributes);
if (matches) {
if (invoke !== false) {
let newProps = { url, matches };
assign(newProps, matches);
delete newProps.ref;
delete newProps.key;
return cloneElement(vnode, newProps);
}
return vnode;
}
return vnode;
}
return false;
}).filter(Boolean);
}).filter(Boolean);
}

@@ -229,0 +231,0 @@

@@ -12,3 +12,3 @@

export function exec(url, route, opts=EMPTY) {
export function exec(url, route, opts) {
let reg = /(?:\?([^#]*))?(#.*)?$/,

@@ -55,20 +55,30 @@ c = url.match(reg),

export function pathRankSort(a, b) {
let aAttr = a.attributes || EMPTY,
bAttr = b.attributes || EMPTY;
if (aAttr.default) return 1;
if (bAttr.default) return -1;
let diff = rank(aAttr.path) - rank(bAttr.path);
return diff || (aAttr.path.length - bAttr.path.length);
return (
(a.rank < b.rank) ? 1 :
(a.rank > b.rank) ? -1 :
(a.index - b.index)
);
}
// filter out VNodes without attributes (which are unrankeable), and add `index`/`rank` properties to be used in sorting.
export function prepareVNodeForRanking(vnode, index) {
vnode.index = index;
vnode.rank = rankChild(vnode);
return vnode.attributes;
}
export function segmentize(url) {
return strip(url).split('/');
return url.replace(/(^\/+|\/+$)/g, '').split('/');
}
export function rank(url) {
return (strip(url).match(/\/+/g) || '').length;
export function rankSegment(segment) {
return segment.charAt(0)==':' ? (1 + '*+?'.indexOf(segment.charAt(segment.length-1))) || 4 : 5;
}
export function strip(url) {
return url.replace(/(^\/+|\/+$)/g, '');
export function rank(path) {
return segmentize(path).map(rankSegment).join('');
}
function rankChild(vnode) {
return vnode.attributes.default ? 0 : rank(vnode.attributes.path);
}
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc