Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width">
- <title>JS Bin</title>
- </head>
- <body>
- <script id="jsbin-javascript">
- 'use strict';
- var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ('value' in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
- function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
- var Router = (function () {
- function Router() {
- var base = arguments.length <= 0 || arguments[0] === undefined ? '/' : arguments[0];
- var hash = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
- _classCallCheck(this, Router);
- this.base = base;
- this.hash = hash;
- }
- _createClass(Router, [{
- key: '_cleanPath',
- value: function _cleanPath(path) {
- return path;
- }
- }, {
- key: 'get',
- value: function get(path, target) {
- this.hash['GET ' + this._cleanPath(this.base + path)] = target;
- }
- }, {
- key: 'post',
- value: function post(path, target) {
- this.hash['POST ' + this._cleanPath(this.base + path)] = target;
- }
- }, {
- key: 'delete',
- value: function _delete(path, target) {
- this.hash['DELETE ' + this._cleanPath(this.base + path)] = target;
- }
- }, {
- key: 'put',
- value: function put(path, target) {
- this.hash['PUT ' + this._cleanPath(this.base + path)] = target;
- }
- }]);
- return Router;
- })();
- function cleanPath(path) {
- return path;
- }
- function buildRouter() {
- var rootpath = arguments.length <= 0 || arguments[0] === undefined ? '/' : arguments[0];
- var collection = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
- function router(path, cb) {
- var hash = collection;
- var root = undefined;
- var routerWrapper = undefined;
- if (typeof path === 'string') {
- root = path;
- routerWrapper = cb;
- }
- if (typeof path === 'function') {
- root = '/';
- routerWrapper = path;
- }
- if (routerWrapper) {
- routerWrapper(buildRouter(rootpath + path, collection));
- }
- return hash;
- }
- router.get = function (path, target) {
- collection['GET ' + cleanPath(rootpath + path)] = target;
- };
- return router;
- }
- var router = buildRouter();
- var routes = router('/', function (router) {
- router.get('path', 'target');
- router('/myathoer', function (router) {
- router.get('/nestedpath', 'othertarget');
- });
- });
- console.log(routes);
- /*let routes = {};
- let route = new Router('/hello', routes);
- route('/', router => {
- router.get()
- })
- route.get('/bonjour/:id', 'Controller.target')*/
- </script>
- <script id="jsbin-source-javascript" type="text/javascript">class Router {
- constructor(base = '/', hash = {}){
- this.base = base;
- this.hash = hash;
- }
- _cleanPath(path){
- return path;
- }
- get(path,target){
- this.hash['GET ' + this._cleanPath(this.base + path)] = target;
- }
- post(path,target){
- this.hash['POST ' + this._cleanPath(this.base + path)] = target;
- }
- delete(path,target){
- this.hash['DELETE ' + this._cleanPath(this.base + path)] = target;
- }
- put(path,target){
- this.hash['PUT ' + this._cleanPath(this.base + path)] = target;
- }
- }
- function cleanPath(path){
- return path;
- }
- function buildRouter(rootpath = '/', collection = {}){
- function router(path, cb){
- let hash = collection;
- let root;
- let routerWrapper;
- if(typeof path === 'string'){
- root = path;
- routerWrapper = cb;
- }
- if(typeof path === 'function'){
- root = '/';
- routerWrapper = path;
- }
- if(routerWrapper){
- routerWrapper(buildRouter(rootpath + path, collection))
- }
- return hash
- }
- router.get = (path, target) => {
- collection['GET ' + cleanPath(rootpath + path)] = target;
- }
- return router;
- }
- let router = buildRouter();
- let routes = router('/', router => {
- router.get('path', 'target')
- router('/myathoer', router => {
- router.get('/nestedpath', 'othertarget')
- })
- })
- console.log(routes);
- /*let routes = {};
- let route = new Router('/hello', routes);
- route('/', router => {
- router.get()
- })
- route.get('/bonjour/:id', 'Controller.target')*/
- </script></body>
- </html>
Add Comment
Please, Sign In to add comment