@ajces/router
Advanced tools
Comparing version 1.0.5 to 1.1.2
@@ -1,2 +0,2 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@ajces/idiom")):"function"==typeof define&&define.amd?define(["exports","@ajces/idiom"],t):t(e.Router={},e._ajces_idiom)}(this,function(e,t){"use strict";function n(e){function t(e){if(null==document.getElementsByTagName)window.meta=e;else{document.title=e.title;var t=[].filter.call(document.getElementsByTagName("meta"),function(e){return""!==e.name&&"origin"!==e.name&&"referrer"!==e.name&&"viewport"!==e.name}),n=Object.keys(e).filter(function(e){}),r=[];n.forEach(function(n){var o=t.filter(function(e){e.name})[0];if(void 0===o){var i=document.createElement("meta");i.setAttribute("name",n),i.setAttribute("content",e[n]),document.head.appendChild(i)}else o.setAttribute("content",e[n]);r.push(n)}),n.forEach(function(e){if(-1===r.indexOf(e)){var n=t.filter(function(e){e.name})[0];document.head.removeChild(n)}})}}function n(t,n){for(var r,o,i,a={},u=0;u<n.length&&!r;u++){var c=n[u].path,f=[];t.replace(RegExp("*"===c?".*":"^"+c.replace(/\//g,"\\/").replace(/:([\w]+)/g,function(e,t){return f.push(t),"([-\\.%\\w\\(\\)]+)"})+"/?$","g"),function(){for(var t=1;t<arguments.length-2;){var l=arguments[t++];try{l=decodeURI(l)}catch(e){}a[f.shift()]=l}r=c,i=u,o=Object.assign({},e,n[u].meta)})}return{meta:o,match:r,index:i,params:a}}return null==e&&(e={}),function(e){return{state:{router:{}},actions:{router:{set:function(e,n,r){return null!=r.meta&&t(r.meta),{router:r}},go:function(e,t,n){location.pathname+location.search!==n&&(history.pushState({},"",n),t.router.set({path:n}))}}},events:{load:function(e,t){addEventListener("popstate",function(){t.router.set({})})},render:function(t,r,o){return o[(t.router.index>=0?t:r.router.set(e("route",n(location.pathname,o)))).router.index].view}}}}}function r(e,n){return e.href=e.to,e.to=null,e.onclick=function(t){0!==t.button||t.metaKey||t.altKey||t.ctrlKey||t.shiftKey||"_blank"===e.target||t.currentTarget.origin!==window.location.origin||(t.preventDefault(),e.go(e.href))},t.h("a",e,n)}e.router=n,e.Link=r}); | ||
//# sourceMappingURL=router.js.map | ||
(function(a,b){'object'==typeof exports&&'undefined'!=typeof module?b(exports):'function'==typeof define&&define.amd?define(['exports'],b):b(a.router={})})(this,function(a){'use strict';a.updateMeta=function(a){document.title=a.title;const b=[].filter.call(document.getElementsByTagName('meta'),(a)=>''===a.name||'origin'===a.name||'referrer'===a.name||'viewport'===a.name?!1:!0),c=Object.keys(a).filter((a)=>{}),d=[];c.forEach(function(c){const e=b.filter((a)=>{a.name===c})[0];if(e===void 0){const b=document.createElement('meta');b.setAttribute('name',c),b.setAttribute('content',a[c]),document.head.appendChild(b)}else e.setAttribute('content',a[c]);d.push(c)}),c.forEach(function(a){if(-1===d.indexOf(a)){const c=b.filter((b)=>{b.name===a})[0];document.head.removeChild(c)}})},a.Router=function(a,b){return a.forEach((a)=>{a.meta=Object.assign({},b,a.meta)}),{match:function(b){for(var c,d,e,f={},g=0;g<a.length&&!c;g++){var h=a[g].path,i=[];b.replace(RegExp('*'===h?'.*':'^'+h.replace(/\//g,'\\/').replace(/:([\w]+)/g,function(a,b){return i.push(b),'([-\\.%\\w\\(\\)]+)'})+'/?$','g'),function(){for(var b,k=1;k<arguments.length-2;){b=arguments[k++];try{b=decodeURIComponent(b)}catch(a){}f[i.shift()]=b}c=h,e=a[g].component,d=a[g].meta})}return{pathname:b,match:c,meta:d,component:e,params:f}}}},Object.defineProperty(a,'__esModule',{value:!0})}); | ||
//# sourceMappingURL=router.js.map |
Copyright © 2017-present [Jorge Bucaran](https://github.com/jbucaran) | ||
Modified by [Andy Johnson] (https://github.com/andyrj) | ||
@@ -3,0 +4,0 @@ Permission is hereby granted, free of charge, to any person obtaining a copy |
{ | ||
"name": "@ajces/router", | ||
"description": "Utilities for routing client-side pages with Hyperapp/Idiom.", | ||
"version": "1.0.5", | ||
"description": "Utilities for routing client-side pages.", | ||
"version": "1.1.2", | ||
"main": "dist/router.js", | ||
@@ -10,2 +10,6 @@ "jsnext:main": "src/index.js", | ||
"repository": "https://github.com/ajces/router.git", | ||
"directories": { | ||
"src": "src", | ||
"test": "test" | ||
}, | ||
"files": [ | ||
@@ -18,37 +22,46 @@ "src", | ||
"idiom", | ||
"hyperapp", | ||
"react", | ||
"elm", | ||
"jsx", | ||
"virtual dom", | ||
"vdom", | ||
"hyperx", | ||
"hyperscript", | ||
"router", | ||
"meta", | ||
"async" | ||
"meta" | ||
], | ||
"scripts": { | ||
"test": "jest --coverage --no-cache --testURL https://hyperapp.js.org", | ||
"build": "npm run bundle && npm run minify && cp router.js.flow dist/", | ||
"bundle": "rollup -i src/index.js -o dist/router.js -f umd -mn Router", | ||
"minify": "uglifyjs dist/router.js -o dist/router.js --mangle --compress warnings=false --pure-funcs=Object.defineProperty -p relative --source-map dist/router.js.map", | ||
"test": "nyc ava", | ||
"test:cov": "nyc report --reporter=html", | ||
"build": "rollup --config rollup.config.prod.js", | ||
"build:dev": "rollup --config rollup.config.js", | ||
"prepublish": "npm run build", | ||
"lint": "eslint --ext .js src/", | ||
"lint:fix": "eslint --fix --ext .js src/", | ||
"format": "prettier --write 'src/**/*.js'", | ||
"release": "npm run build && npm test && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish" | ||
"release": "npm run build && git commit -am $npm_package_version && git tag $npm_package_version && git push && git push --tags && npm publish --access=public" | ||
}, | ||
"babel": { | ||
"presets": "es2015" | ||
}, | ||
"devDependencies": { | ||
"@ajces/idiom": "^1.0.1", | ||
"ava": "^0.22.0", | ||
"babel-eslint": "^8.0.0", | ||
"babel-plugin-transform-runtime": "^6.23.0", | ||
"babel-preset-env": "^1.6.0", | ||
"babel-preset-es2015": "^6.24.1", | ||
"jest": "^20.0.4", | ||
"prettier": "~1.5.3", | ||
"rollup": "^0.47.6", | ||
"uglify-js": "^2.7.5" | ||
"babel-preset-stage-1": "^6.24.1", | ||
"babel-register": "^6.26.0", | ||
"eslint": "^4.7.1", | ||
"eslint-config-airbnb": "^15.1.0", | ||
"eslint-plugin-import": "^2.7.0", | ||
"eslint-plugin-jsx-a11y": "^5.1.1", | ||
"eslint-plugin-prettier": "^2.3.1", | ||
"eslint-plugin-promise": "^3.5.0", | ||
"eslint-plugin-react": "^7.3.0", | ||
"eslint-plugin-smells": "^1.0.1", | ||
"eslint-plugin-standard": "^3.0.1", | ||
"nyc": "^11.2.1", | ||
"prettier": "^1.5.3", | ||
"rollup-plugin-babel-minify": "^3.1.2" | ||
}, | ||
"peerDependencies": { | ||
"@ajces/idiom": "^1.0.1" | ||
"ava": { | ||
"require": [ | ||
"babel-register" | ||
], | ||
"babel": "inherit" | ||
}, | ||
"dependencies": { | ||
"rollup": "^0.50.0" | ||
} | ||
} |
@@ -1,44 +0,9 @@ | ||
# AJCES router fork of @hyperapp/router | ||
The main difference being the addition of meta tag diffing on route changes, using array of route objects [{path, view, meta}] instead of array of arrays which I feel is clearer in the code and I will also be adding async routing soon via an async view helper function which will provide functionality similar to react-loadable... | ||
# AJCES router | ||
This is forked from hyperapp/router and extracts the match logic into a framework agnostic router. Addition handling of meta tag diffing on route changes, has also been added, using array of route objects [{path, view, meta}] instead of array of arrays which I feel is clearer in the code. | ||
The defaultMeta provided to the Router mixin will allow defaults for routes that choose not to specify a meta value, anything supplied to a route meta object will be merged with defaultMeta via Object.assign({}, defaultMeta, view.meta) | ||
The defaultMeta provided to the Router will allow defaults for routes that choose not to specify a meta value, anything supplied to a route meta object will be merged with defaultMeta via Object.assign({}, defaultMeta, view.meta) | ||
There are meta tags that cause bugs in certain browsers if modified after page load, these are skipped in our meta dom diff to avoid issues: origin, referrer, viewport. ( any other key value pair should work in the meta object ) | ||
There are meta tags that cause bugs in certain browsers if modified after page load, these are skipped in our meta diff to avoid issues: origin, referrer, viewport. ( any other key value pair should work in the meta object ) | ||
```jsx | ||
import { router, Link } from "@ajces/router" | ||
app({ | ||
view: [ | ||
{ | ||
path: "/", | ||
view: (state, actions) => | ||
<Link to="/test" go={actions.router.go}> | ||
Test | ||
</Link>, | ||
meta: { | ||
title: "Idiom - Router", | ||
description: "SEO description for Idiom Router" | ||
} | ||
}, | ||
{ | ||
path: "/test", | ||
view: (state, actions) => | ||
<Link to="/" go={actions.router.go}> | ||
Back | ||
</Link>, | ||
meta: { | ||
title: "Test", | ||
description: "SEO test", | ||
keywords: "comma,delimited,list" | ||
} | ||
} | ||
], | ||
mixins: [router({ | ||
title: "AJCES - Router", | ||
description: "AJCES customized fork of @hyperapp/router", | ||
author: "Andy Johnson", | ||
keywords: "idiom,router,hyperapp,meta,async" | ||
})] | ||
}) | ||
```js | ||
``` | ||
@@ -49,2 +14,1 @@ | ||
@ajces/router is MIT licensed. See [LICENSE](LICENSE.md). | ||
@@ -1,2 +0,1 @@ | ||
export { router } from "./router" | ||
export { Link } from "./Link" | ||
export * from "./router"; |
@@ -1,139 +0,102 @@ | ||
export function router(defaultMeta) { | ||
if (defaultMeta == null) { | ||
defaultMeta = {}; | ||
} | ||
return function (emit) { | ||
return { | ||
state: { | ||
router: {} | ||
}, | ||
actions: { | ||
router: { | ||
set: function(state, actions, data) { | ||
if (data.meta != null) { | ||
updateMeta(data.meta); | ||
} | ||
return { | ||
router: data | ||
} | ||
}, | ||
go: function(state, actions, path) { | ||
if (location.pathname + location.search !== path) { | ||
history.pushState({}, "", path); | ||
actions.router.set({ | ||
path: path | ||
}); | ||
} | ||
} | ||
} | ||
}, | ||
events: { | ||
load: function(state, actions) { | ||
addEventListener("popstate", function() { | ||
actions.router.set({}); | ||
}) | ||
}, | ||
render: function(state, actions, view) { | ||
return view[ | ||
(state.router.index >= 0 | ||
? state | ||
: actions.router.set(emit("route", match(location.pathname, view)))) | ||
.router.index | ||
].view; | ||
} | ||
export function updateMeta(meta) { | ||
document.title = meta.title; | ||
const dynamicMeta = [].filter.call( | ||
document.getElementsByTagName("meta"), | ||
el => { | ||
if ( | ||
el.name === "" || | ||
el.name === "origin" || | ||
el.name === "referrer" || | ||
el.name === "viewport" | ||
) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
} | ||
function updateMeta(meta) { | ||
if (document.getElementsByTagName == null) { | ||
window.meta = meta; // hack work around for SSR... | ||
); | ||
const keys = Object.keys(meta).filter(k => { | ||
k !== "title"; | ||
}); | ||
const handled = []; | ||
keys.forEach(function(k) { | ||
const metaEl = dynamicMeta.filter(el => { | ||
el.name === k; | ||
})[0]; | ||
if (metaEl === undefined) { | ||
// add missing meta element to head | ||
const newMeta = document.createElement("meta"); | ||
newMeta.setAttribute("name", k); | ||
newMeta.setAttribute("content", meta[k]); | ||
document.head.appendChild(newMeta); | ||
} else { | ||
document.title = meta.title; | ||
var dynamicMeta = [].filter.call( | ||
document.getElementsByTagName("meta"), | ||
function(el) { | ||
if ( | ||
el.name === "" || | ||
el.name === "origin" || | ||
el.name === "referrer" || | ||
el.name === "viewport" | ||
) { | ||
return false; | ||
} else { | ||
return true; | ||
} | ||
} | ||
); | ||
var keys = Object.keys(meta).filter(function(k) { k !== "title" }); | ||
var handled = []; | ||
keys.forEach(function(k) { | ||
var metaEl = dynamicMeta.filter(function (el) { el.name === k })[0]; | ||
if (metaEl === undefined) { | ||
// add missing meta element to head | ||
var newMeta = document.createElement("meta"); | ||
newMeta.setAttribute("name", k); | ||
newMeta.setAttribute("content", meta[k]); | ||
document.head.appendChild(newMeta); | ||
} else { | ||
// update existing meta element | ||
metaEl.setAttribute("content", meta[k]); | ||
} | ||
handled.push(k); | ||
}); | ||
keys.forEach(function(k) { | ||
if (handled.indexOf(k) === -1) { | ||
// remove meta from document | ||
var metaEl = dynamicMeta.filter(function (el) { el.name === k })[0]; | ||
document.head.removeChild(metaEl); | ||
} | ||
}); | ||
// update existing meta element | ||
metaEl.setAttribute("content", meta[k]); | ||
} | ||
} | ||
handled.push(k); | ||
}); | ||
keys.forEach(function(k) { | ||
if (handled.indexOf(k) === -1) { | ||
// remove meta from document | ||
const metaEl = dynamicMeta.filter(el => { | ||
el.name === k; | ||
})[0]; | ||
document.head.removeChild(metaEl); | ||
} | ||
}); | ||
} | ||
function match(pathname, routes) { | ||
var match; | ||
var meta; | ||
var index; | ||
var params = {}; | ||
export function Router(config, defaultMeta) { | ||
config.forEach(route => { | ||
route.meta = Object.assign({}, defaultMeta, route.meta); | ||
}); | ||
return { | ||
match: function(pathname) { | ||
var match; | ||
var meta; | ||
var component; | ||
var params = {}; | ||
for (var i = 0; i < routes.length && !match; i++) { | ||
var route = routes[i].path; | ||
var keys = []; | ||
pathname.replace( | ||
RegExp( | ||
route === "*" | ||
? ".*" | ||
: "^" + | ||
route.replace(/\//g, "\\/").replace(/:([\w]+)/g, function(_, key) { | ||
keys.push(key) | ||
return "([-\\.%\\w\\(\\)]+)" | ||
}) + | ||
"/?$", | ||
"g" | ||
), | ||
function() { | ||
for (var j = 1; j < arguments.length - 2; ) { | ||
var value = arguments[j++]; | ||
try { | ||
value = decodeURI(value); | ||
} catch (_) {} | ||
params[keys.shift()] = value; | ||
for (var i = 0; i < config.length && !match; i++) { | ||
var route = config[i].path; | ||
var keys = []; | ||
pathname.replace( | ||
RegExp( | ||
route === "*" | ||
? ".*" | ||
: "^" + | ||
route | ||
.replace(/\//g, "\\/") | ||
.replace(/:([\w]+)/g, function(_, key) { | ||
keys.push(key); | ||
return "([-\\.%\\w\\(\\)]+)"; | ||
}) + | ||
"/?$", | ||
"g" | ||
), | ||
function() { | ||
for (var j = 1; j < arguments.length - 2; ) { | ||
var value = arguments[j++]; | ||
try { | ||
value = decodeURIComponent(value); | ||
} catch (_) {} // eslint-disable-line | ||
params[keys.shift()] = value; | ||
} | ||
match = route; | ||
component = config[i].component; | ||
meta = config[i].meta; | ||
} | ||
); | ||
} | ||
match = route; | ||
index = i; | ||
meta = Object.assign({}, defaultMeta, routes[i].meta); | ||
} | ||
); | ||
return { | ||
pathname, | ||
match, | ||
meta, | ||
component, | ||
params | ||
}; | ||
} | ||
return { | ||
meta: meta, | ||
match: match, | ||
index: index, | ||
params: params | ||
}; | ||
} | ||
} | ||
}; | ||
} |
Sorry, the diff of this file is not supported yet
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
1
12562
19
7
106
14
+ Addedrollup@^0.50.0
+ Addedrollup@0.50.1(transitive)
- Removed@ajces/idiom@1.0.5(transitive)