From 34324c9771f82ea1cb9d9c379d688fb54f1a9455 Mon Sep 17 00:00:00 2001 From: Jonathan Lehman Date: Fri, 24 Jul 2015 23:57:24 -0400 Subject: [PATCH] Make use of ES7 features --- .babelrc | 3 ++ dist/switcheroo.min.js | 2 +- modules/components/Switcher.js | 67 ++++++++++++++------------------ test/components/Switcher_test.js | 10 ++--- 4 files changed, 39 insertions(+), 43 deletions(-) create mode 100644 .babelrc diff --git a/.babelrc b/.babelrc new file mode 100644 index 0000000..b0b9a96 --- /dev/null +++ b/.babelrc @@ -0,0 +1,3 @@ +{ + "stage": 0 +} diff --git a/dist/switcheroo.min.js b/dist/switcheroo.min.js index 3279968..b4583f2 100644 --- a/dist/switcheroo.min.js +++ b/dist/switcheroo.min.js @@ -1 +1 @@ -!function(t,e){"object"==typeof exports&&"object"==typeof module?module.exports=e(require("window"),require("react")):"function"==typeof define&&define.amd?define(["window","react"],e):"object"==typeof exports?exports.switcheroo=e(require("window"),require("react")):t.switcheroo=e(t.window,t.React)}(this,function(t,e){return function(t){function e(r){if(n[r])return n[r].exports;var i=n[r]={exports:{},id:r,loaded:!1};return t[r].call(i.exports,i,i.exports,e),i.loaded=!0,i.exports}var n={};return e.m=t,e.c=n,e.p="",e(0)}([function(t,e,n){"use strict";e.Switcher=n(1)},function(t,e,n){"use strict";function r(t){return t&&t.__esModule?t:{"default":t}}function i(t,e){if(!(t instanceof e))throw new TypeError("Cannot call a class as a function")}function a(t,e){if("function"!=typeof e&&null!==e)throw new TypeError("Super expression must either be null or a function, not "+typeof e);t.prototype=Object.create(e&&e.prototype,{constructor:{value:t,enumerable:!1,writable:!0,configurable:!0}}),e&&(t.__proto__=e)}Object.defineProperty(e,"__esModule",{value:!0});var o=function(){function t(t,e){for(var n=0;ni;i++)r+=t[i].path.length;e=e.substr(r);var o={path:e,handler:n};t.push(o)}function h(t,e,n,r){var i=e.routes;for(var a in i)if(i.hasOwnProperty(a)){var o=t.slice();s(o,a,i[a]),e.children[a]?h(o,e.children[a],n,r):n.call(r,o)}}function u(t){return"[object Array]"===Object.prototype.toString.call(t)}function c(t){this.string=t}function p(t){this.name=t}function l(t){this.name=t}function f(){}function d(t,e,n){"/"===t.charAt(0)&&(t=t.substr(1));for(var r=t.split("/"),i=[],a=0,o=r.length;o>a;a++){var s,h=r[a];(s=h.match(/^:([^\/]+)$/))?(i.push(new p(s[1])),e.push(s[1]),n.dynamics++):(s=h.match(/^\*([^\/]+)$/))?(i.push(new l(s[1])),e.push(s[1]),n.stars++):""===h?i.push(new f):(i.push(new c(h)),n.statics++)}return i}function g(t){this.charSpec=t,this.nextStates=[]}function v(t){return t.sort(function(t,e){if(t.types.stars!==e.types.stars)return t.types.stars-e.types.stars;if(t.types.stars){if(t.types.statics!==e.types.statics)return e.types.statics-t.types.statics;if(t.types.dynamics!==e.types.dynamics)return e.types.dynamics-t.types.dynamics}return t.types.dynamics!==e.types.dynamics?t.types.dynamics-e.types.dynamics:t.types.statics!==e.types.statics?e.types.statics-t.types.statics:0})}function y(t,e){for(var n=[],r=0,i=t.length;i>r;r++){var a=t[r];n=n.concat(a.match(e))}return n}function m(t){this.queryParams=t||{}}function w(t,e,n){for(var r=t.handlers,i=t.regex,a=e.match(i),o=1,s=new m(n),h=0,u=r.length;u>h;h++){for(var c=r[h],p=c.names,l={},f=0,d=p.length;d>f;f++)l[p[f]]=a[o++];s.push({handler:c.handler,params:l,isDynamic:!!p.length})}return s}function b(t,e){return e.eachChar(function(e){t=t.put(e)}),t}function S(t){return t=t.replace(/\+/gm,"%20"),decodeURIComponent(t)}i.prototype={to:function(t,e){var n=this.delegate;if(n&&n.willAddRoute&&(t=n.willAddRoute(this.matcher.target,t)),this.matcher.add(this.path,t),e){if(0===e.length)throw new Error("You must have an argument in the function passed to `to`");this.matcher.addChild(this.path,t,e,this.delegate)}return this}},a.prototype={add:function(t,e){this.routes[t]=e},addChild:function(t,e,n,r){var i=new a(e);this.children[t]=i;var s=o(t,i,r);r&&r.contextEntered&&r.contextEntered(e,s),n(s)}};var C=function(t,e){var n=new a;t(o("",n,this.delegate)),h([],n,function(t){e?e(this,t):this.add(t)},this)},x=["/",".","*","+","?","|","(",")","[","]","{","}","\\"],P=new RegExp("(\\"+x.join("|\\")+")","g");c.prototype={eachChar:function(t){for(var e,n=this.string,r=0,i=n.length;i>r;r++)e=n.charAt(r),t({validChars:e})},regex:function(){return this.string.replace(P,"\\$1")},generate:function(){return this.string}},p.prototype={eachChar:function(t){t({invalidChars:"/",repeat:!0})},regex:function(){return"([^/]+)"},generate:function(t){return t[this.name]}},l.prototype={eachChar:function(t){t({invalidChars:"",repeat:!0})},regex:function(){return"(.+)"},generate:function(t){return t[this.name]}},f.prototype={eachChar:function(){},regex:function(){return""},generate:function(){return""}},g.prototype={get:function(t){for(var e=this.nextStates,n=0,r=e.length;r>n;n++){var i=e[n],a=i.charSpec.validChars===t.validChars;if(a=a&&i.charSpec.invalidChars===t.invalidChars)return i}},put:function(t){var e;return(e=this.get(t))?e:(e=new g(t),this.nextStates.push(e),t.repeat&&e.nextStates.push(e),e)},match:function(t){for(var e,n,r,i=this.nextStates,a=[],o=0,s=i.length;s>o;o++)e=i[o],n=e.charSpec,"undefined"!=typeof(r=n.validChars)?-1!==r.indexOf(t)&&a.push(e):"undefined"!=typeof(r=n.invalidChars)&&-1===r.indexOf(t)&&a.push(e);return a}};var R=Object.create||function(t){function e(){}return e.prototype=t,new e};m.prototype=R({splice:Array.prototype.splice,slice:Array.prototype.slice,push:Array.prototype.push,length:0,queryParams:null});var E=function(){this.rootState=new g,this.names={}};E.prototype={add:function(t,e){for(var n,r=this.rootState,i="^",a={statics:0,dynamics:0,stars:0},o=[],s=[],h=!0,u=0,c=t.length;c>u;u++){var p=t[u],l=[],g=d(p.path,l,a);s=s.concat(g);for(var v=0,y=g.length;y>v;v++){var m=g[v];m instanceof f||(h=!1,r=r.put({validChars:"/"}),i+="/",r=b(r,m),i+=m.regex())}var w={handler:p.handler,names:l};o.push(w)}h&&(r=r.put({validChars:"/"}),i+="/"),r.handlers=o,r.regex=new RegExp(i+"$"),r.types=a,(n=e&&e.as)&&(this.names[n]={segments:s,handlers:o})},handlersFor:function(t){var e=this.names[t],n=[];if(!e)throw new Error("There is no route named "+t);for(var r=0,i=e.handlers.length;i>r;r++)n.push(e.handlers[r]);return n},hasRoute:function(t){return!!this.names[t]},generate:function(t,e){var n=this.names[t],r="";if(!n)throw new Error("There is no route named "+t);for(var i=n.segments,a=0,o=i.length;o>a;a++){var s=i[a];s instanceof f||(r+="/",r+=s.generate(e))}return"/"!==r.charAt(0)&&(r="/"+r),e&&e.queryParams&&(r+=this.generateQueryString(e.queryParams,n.handlers)),r},generateQueryString:function(t,e){var n=[],r=[];for(var i in t)t.hasOwnProperty(i)&&r.push(i);r.sort();for(var a=0,o=r.length;o>a;a++){i=r[a];var s=t[i];if(null!=s){var h=encodeURIComponent(i);if(u(s))for(var c=0,p=s.length;p>c;c++){var l=i+"[]="+encodeURIComponent(s[c]);n.push(l)}else h+="="+encodeURIComponent(s),n.push(h)}}return 0===n.length?"":"?"+n.join("&")},parseQueryString:function(t){for(var e=t.split("&"),n={},r=0;r2&&"[]"===o.slice(s-2)&&(h=!0,o=o.slice(0,s-2),n[o]||(n[o]=[])),i=a[1]?S(a[1]):""),h?n[o].push(i):n[o]=i}return n},recognize:function(t){var e,n,r,i,a=[this.rootState],o={},s=!1;if(i=t.indexOf("?"),-1!==i){var h=t.substr(i+1,t.length);t=t.substr(0,i),o=this.parseQueryString(h)}for(t=decodeURI(t),"/"!==t.charAt(0)&&(t="/"+t),e=t.length,e>1&&"/"===t.charAt(e-1)&&(t=t.substr(0,e-1),s=!0),n=0,r=t.length;r>n&&(a=y(a,t.charAt(n)),a.length);n++);var u=[];for(n=0,r=a.length;r>n;n++)a[n].handlers&&u.push(a[n]);a=v(u);var c=u[0];return c&&c.handlers?(s&&"(.+)$"===c.regex.source.slice(-5)&&(t+="/"),w(c,t,o)):void 0}},E.prototype.map=C,E.VERSION="0.1.5";var O=E;n(3).amd?(r=function(){return O}.call(e,n,e,t),!(void 0!==r&&(t.exports=r))):"undefined"!=typeof t&&t.exports?t.exports=O:"undefined"!=typeof this&&(this.RouteRecognizer=O)}).call(this)}).call(e,n(4)(t))},function(t,e,n){t.exports=function(){throw new Error("define cannot be used indirect")}},function(t,e,n){t.exports=function(t){return t.webpackPolyfill||(t.deprecate=function(){},t.paths=[],t.children=[],t.webpackPolyfill=1),t}},function(e,n,r){e.exports=t},function(t,n,r){t.exports=e}])}); \ No newline at end of file +!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("window"),require("react")):"function"==typeof define&&define.amd?define(["window","react"],t):"object"==typeof exports?exports.switcheroo=t(require("window"),require("react")):e.switcheroo=t(e.window,e.React)}(this,function(e,t){return function(e){function t(r){if(n[r])return n[r].exports;var a=n[r]={exports:{},id:r,loaded:!1};return e[r].call(a.exports,a,a.exports,t),a.loaded=!0,a.exports}var n={};return t.m=e,t.c=n,t.p="",t(0)}([function(e,t,n){"use strict";t.Switcher=n(1)},function(e,t,n){"use strict";function r(e){return e&&e.__esModule?e:{"default":e}}function a(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function i(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var o=function(){function e(e,t){for(var n=0;na;a++)r+=e[a].path.length;t=t.substr(r);var o={path:t,handler:n};e.push(o)}function u(e,t,n,r){var a=t.routes;for(var i in a)if(a.hasOwnProperty(i)){var o=e.slice();s(o,i,a[i]),t.children[i]?u(o,t.children[i],n,r):n.call(r,o)}}function h(e){return"[object Array]"===Object.prototype.toString.call(e)}function c(e){this.string=e}function p(e){this.name=e}function l(e){this.name=e}function f(){}function d(e,t,n){"/"===e.charAt(0)&&(e=e.substr(1));for(var r=e.split("/"),a=[],i=0,o=r.length;o>i;i++){var s,u=r[i];(s=u.match(/^:([^\/]+)$/))?(a.push(new p(s[1])),t.push(s[1]),n.dynamics++):(s=u.match(/^\*([^\/]+)$/))?(a.push(new l(s[1])),t.push(s[1]),n.stars++):""===u?a.push(new f):(a.push(new c(u)),n.statics++)}return a}function v(e){this.charSpec=e,this.nextStates=[]}function y(e){return e.sort(function(e,t){if(e.types.stars!==t.types.stars)return e.types.stars-t.types.stars;if(e.types.stars){if(e.types.statics!==t.types.statics)return t.types.statics-e.types.statics;if(e.types.dynamics!==t.types.dynamics)return t.types.dynamics-e.types.dynamics}return e.types.dynamics!==t.types.dynamics?e.types.dynamics-t.types.dynamics:e.types.statics!==t.types.statics?t.types.statics-e.types.statics:0})}function g(e,t){for(var n=[],r=0,a=e.length;a>r;r++){var i=e[r];n=n.concat(i.match(t))}return n}function m(e){this.queryParams=e||{}}function w(e,t,n){for(var r=e.handlers,a=e.regex,i=t.match(a),o=1,s=new m(n),u=0,h=r.length;h>u;u++){for(var c=r[u],p=c.names,l={},f=0,d=p.length;d>f;f++)l[p[f]]=i[o++];s.push({handler:c.handler,params:l,isDynamic:!!p.length})}return s}function b(e,t){return t.eachChar(function(t){e=e.put(t)}),e}function S(e){return e=e.replace(/\+/gm,"%20"),decodeURIComponent(e)}a.prototype={to:function(e,t){var n=this.delegate;if(n&&n.willAddRoute&&(e=n.willAddRoute(this.matcher.target,e)),this.matcher.add(this.path,e),t){if(0===t.length)throw new Error("You must have an argument in the function passed to `to`");this.matcher.addChild(this.path,e,t,this.delegate)}return this}},i.prototype={add:function(e,t){this.routes[e]=t},addChild:function(e,t,n,r){var a=new i(t);this.children[e]=a;var s=o(e,a,r);r&&r.contextEntered&&r.contextEntered(t,s),n(s)}};var x=function(e,t){var n=new i;e(o("",n,this.delegate)),u([],n,function(e){t?t(this,e):this.add(e)},this)},C=["/",".","*","+","?","|","(",")","[","]","{","}","\\"],P=new RegExp("(\\"+C.join("|\\")+")","g");c.prototype={eachChar:function(e){for(var t,n=this.string,r=0,a=n.length;a>r;r++)t=n.charAt(r),e({validChars:t})},regex:function(){return this.string.replace(P,"\\$1")},generate:function(){return this.string}},p.prototype={eachChar:function(e){e({invalidChars:"/",repeat:!0})},regex:function(){return"([^/]+)"},generate:function(e){return e[this.name]}},l.prototype={eachChar:function(e){e({invalidChars:"",repeat:!0})},regex:function(){return"(.+)"},generate:function(e){return e[this.name]}},f.prototype={eachChar:function(){},regex:function(){return""},generate:function(){return""}},v.prototype={get:function(e){for(var t=this.nextStates,n=0,r=t.length;r>n;n++){var a=t[n],i=a.charSpec.validChars===e.validChars;if(i=i&&a.charSpec.invalidChars===e.invalidChars)return a}},put:function(e){var t;return(t=this.get(e))?t:(t=new v(e),this.nextStates.push(t),e.repeat&&t.nextStates.push(t),t)},match:function(e){for(var t,n,r,a=this.nextStates,i=[],o=0,s=a.length;s>o;o++)t=a[o],n=t.charSpec,"undefined"!=typeof(r=n.validChars)?-1!==r.indexOf(e)&&i.push(t):"undefined"!=typeof(r=n.invalidChars)&&-1===r.indexOf(e)&&i.push(t);return i}};var R=Object.create||function(e){function t(){}return t.prototype=e,new t};m.prototype=R({splice:Array.prototype.splice,slice:Array.prototype.slice,push:Array.prototype.push,length:0,queryParams:null});var E=function(){this.rootState=new v,this.names={}};E.prototype={add:function(e,t){for(var n,r=this.rootState,a="^",i={statics:0,dynamics:0,stars:0},o=[],s=[],u=!0,h=0,c=e.length;c>h;h++){var p=e[h],l=[],v=d(p.path,l,i);s=s.concat(v);for(var y=0,g=v.length;g>y;y++){var m=v[y];m instanceof f||(u=!1,r=r.put({validChars:"/"}),a+="/",r=b(r,m),a+=m.regex())}var w={handler:p.handler,names:l};o.push(w)}u&&(r=r.put({validChars:"/"}),a+="/"),r.handlers=o,r.regex=new RegExp(a+"$"),r.types=i,(n=t&&t.as)&&(this.names[n]={segments:s,handlers:o})},handlersFor:function(e){var t=this.names[e],n=[];if(!t)throw new Error("There is no route named "+e);for(var r=0,a=t.handlers.length;a>r;r++)n.push(t.handlers[r]);return n},hasRoute:function(e){return!!this.names[e]},generate:function(e,t){var n=this.names[e],r="";if(!n)throw new Error("There is no route named "+e);for(var a=n.segments,i=0,o=a.length;o>i;i++){var s=a[i];s instanceof f||(r+="/",r+=s.generate(t))}return"/"!==r.charAt(0)&&(r="/"+r),t&&t.queryParams&&(r+=this.generateQueryString(t.queryParams,n.handlers)),r},generateQueryString:function(e,t){var n=[],r=[];for(var a in e)e.hasOwnProperty(a)&&r.push(a);r.sort();for(var i=0,o=r.length;o>i;i++){a=r[i];var s=e[a];if(null!=s){var u=encodeURIComponent(a);if(h(s))for(var c=0,p=s.length;p>c;c++){var l=a+"[]="+encodeURIComponent(s[c]);n.push(l)}else u+="="+encodeURIComponent(s),n.push(u)}}return 0===n.length?"":"?"+n.join("&")},parseQueryString:function(e){for(var t=e.split("&"),n={},r=0;r2&&"[]"===o.slice(s-2)&&(u=!0,o=o.slice(0,s-2),n[o]||(n[o]=[])),a=i[1]?S(i[1]):""),u?n[o].push(a):n[o]=a}return n},recognize:function(e){var t,n,r,a,i=[this.rootState],o={},s=!1;if(a=e.indexOf("?"),-1!==a){var u=e.substr(a+1,e.length);e=e.substr(0,a),o=this.parseQueryString(u)}for(e=decodeURI(e),"/"!==e.charAt(0)&&(e="/"+e),t=e.length,t>1&&"/"===e.charAt(t-1)&&(e=e.substr(0,t-1),s=!0),n=0,r=e.length;r>n&&(i=g(i,e.charAt(n)),i.length);n++);var h=[];for(n=0,r=i.length;r>n;n++)i[n].handlers&&h.push(i[n]);i=y(h);var c=h[0];return c&&c.handlers?(s&&"(.+)$"===c.regex.source.slice(-5)&&(e+="/"),w(c,e,o)):void 0}},E.prototype.map=x,E.VERSION="0.1.5";var O=E;n(3).amd?(r=function(){return O}.call(t,n,t,e),!(void 0!==r&&(e.exports=r))):"undefined"!=typeof e&&e.exports?e.exports=O:"undefined"!=typeof this&&(this.RouteRecognizer=O)}).call(this)}).call(t,n(4)(e))},function(e,t,n){e.exports=function(){throw new Error("define cannot be used indirect")}},function(e,t,n){e.exports=function(e){return e.webpackPolyfill||(e.deprecate=function(){},e.paths=[],e.children=[],e.webpackPolyfill=1),e}},function(t,n,r){t.exports=e},function(e,n,r){e.exports=t}])}); \ No newline at end of file diff --git a/modules/components/Switcher.js b/modules/components/Switcher.js index 4913285..cfc3bc5 100644 --- a/modules/components/Switcher.js +++ b/modules/components/Switcher.js @@ -3,18 +3,37 @@ import Recognizer from 'route-recognizer'; import window from 'window'; export default class Switcher extends Component { + static displayName = 'Switcher'; + + static propTypes = { + children: React.PropTypes.oneOfType([ + React.PropTypes.arrayOf(React.PropTypes.element), + React.PropTypes.element + ]).isRequired, + pushState: React.PropTypes.bool, + hashChange: React.PropTypes.bool, + load: React.PropTypes.bool, + defaultHandler: React.PropTypes.func, + defaultHandlerProps: React.PropTypes.object, + onChange: React.PropTypes.func, + wrapper: React.PropTypes.any, + location: React.PropTypes.string, + baseURL: React.PropTypes.string + }; + + static defaultProps = { + pushState: false, + hashChange: true, + load: true, + location: 'hash', + basePath: '' + }; + constructor(props) { super(props); - // bind methods - this.getLocation = this.getLocation.bind(this); - this.handleRouteChange = this.handleRouteChange.bind(this); - this.getSwitch = this.getSwitch.bind(this); - this.initializeRecognizer = this.initializeRecognizer.bind(this); - this.defaultSwitch = props.defaultHandler ? React.createElement(props.defaultHandler, props.defaultHandlerProps) : null; this.initializeRecognizer(props); - // set initial state var currentPath = this.getLocation(); var switchElement = this.getSwitch(currentPath); this.state = { @@ -57,7 +76,7 @@ export default class Switcher extends Component { }); } - initializeRecognizer(props) { + initializeRecognizer = (props) => { this.recognizer = new Recognizer(); var children = [].concat(props.children); children.forEach((child) => { @@ -68,7 +87,7 @@ export default class Switcher extends Component { }); } - getLocation() { + getLocation = () => { var location = decodeURI(window.location[this.props.location].slice(1).split('?')[0]); if(location.charAt(0) !== '/') { return `/${location}`; @@ -78,12 +97,12 @@ export default class Switcher extends Component { } } - getSwitch(path) { + getSwitch = (path) => { var handlers = this.recognizer.recognize(path); return (handlers && handlers[0] && handlers[0].handler) || null; } - handleRouteChange(e) { + handleRouteChange = (e) => { var currentPath = this.getLocation(); var switchElement = this.getSwitch(currentPath); @@ -109,29 +128,3 @@ export default class Switcher extends Component { } } } - -Switcher.displayName = 'Switcher'; - -Switcher.propTypes = { - children: React.PropTypes.oneOfType([ - React.PropTypes.arrayOf(React.PropTypes.element), - React.PropTypes.element - ]).isRequired, - pushState: React.PropTypes.bool, - hashChange: React.PropTypes.bool, - load: React.PropTypes.bool, - defaultHandler: React.PropTypes.func, - defaultHandlerProps: React.PropTypes.object, - onChange: React.PropTypes.func, - wrapper: React.PropTypes.any, - location: React.PropTypes.string, - baseURL: React.PropTypes.string -}; - -Switcher.defaultProps = { - pushState: false, - hashChange: true, - load: true, - location: 'hash', - basePath: '' -}; diff --git a/test/components/Switcher_test.js b/test/components/Switcher_test.js index 319ce2a..128158b 100644 --- a/test/components/Switcher_test.js +++ b/test/components/Switcher_test.js @@ -5,16 +5,16 @@ import window from 'window'; import Switcher from 'components/Switcher'; class Handler extends Component { + static displayName = 'Handler'; + static propTypes = { + text: PropTypes.string + }; + render() { return
{this.props.text}
; } } -Handler.displayName = 'Handler'; -Handler.propTypes = { - text: PropTypes.string -}; - describe('Switcher', function() { describe('#getLocation', function() { describe('using location.hash', function() {