Guest User

Untitled

a guest
Mar 21st, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.63 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta name="viewport" content="width=device-width">
  6. <title>JS Bin</title>
  7. </head>
  8. <body>
  9.  
  10. <script id="jsbin-javascript">
  11. 'use strict';
  12.  
  13. 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; }; })();
  14.  
  15. function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError('Cannot call a class as a function'); } }
  16.  
  17. var Router = (function () {
  18. function Router() {
  19. var base = arguments.length <= 0 || arguments[0] === undefined ? '/' : arguments[0];
  20. var hash = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  21.  
  22. _classCallCheck(this, Router);
  23.  
  24. this.base = base;
  25. this.hash = hash;
  26. }
  27.  
  28. _createClass(Router, [{
  29. key: '_cleanPath',
  30. value: function _cleanPath(path) {
  31. return path;
  32. }
  33. }, {
  34. key: 'get',
  35. value: function get(path, target) {
  36. this.hash['GET ' + this._cleanPath(this.base + path)] = target;
  37. }
  38. }, {
  39. key: 'post',
  40. value: function post(path, target) {
  41. this.hash['POST ' + this._cleanPath(this.base + path)] = target;
  42. }
  43. }, {
  44. key: 'delete',
  45. value: function _delete(path, target) {
  46. this.hash['DELETE ' + this._cleanPath(this.base + path)] = target;
  47. }
  48. }, {
  49. key: 'put',
  50. value: function put(path, target) {
  51. this.hash['PUT ' + this._cleanPath(this.base + path)] = target;
  52. }
  53. }]);
  54.  
  55. return Router;
  56. })();
  57.  
  58. function cleanPath(path) {
  59. return path;
  60. }
  61.  
  62. function buildRouter() {
  63. var rootpath = arguments.length <= 0 || arguments[0] === undefined ? '/' : arguments[0];
  64. var collection = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
  65.  
  66. function router(path, cb) {
  67.  
  68. var hash = collection;
  69. var root = undefined;
  70. var routerWrapper = undefined;
  71.  
  72. if (typeof path === 'string') {
  73. root = path;
  74. routerWrapper = cb;
  75. }
  76.  
  77. if (typeof path === 'function') {
  78. root = '/';
  79. routerWrapper = path;
  80. }
  81.  
  82. if (routerWrapper) {
  83. routerWrapper(buildRouter(rootpath + path, collection));
  84. }
  85.  
  86. return hash;
  87. }
  88.  
  89. router.get = function (path, target) {
  90. collection['GET ' + cleanPath(rootpath + path)] = target;
  91. };
  92.  
  93. return router;
  94. }
  95.  
  96. var router = buildRouter();
  97.  
  98. var routes = router('/', function (router) {
  99. router.get('path', 'target');
  100.  
  101. router('/myathoer', function (router) {
  102. router.get('/nestedpath', 'othertarget');
  103. });
  104. });
  105.  
  106. console.log(routes);
  107.  
  108. /*let routes = {};
  109. let route = new Router('/hello', routes);
  110. route('/', router => {
  111. router.get()
  112. })
  113. route.get('/bonjour/:id', 'Controller.target')*/
  114. </script>
  115.  
  116.  
  117.  
  118. <script id="jsbin-source-javascript" type="text/javascript">class Router {
  119. constructor(base = '/', hash = {}){
  120. this.base = base;
  121. this.hash = hash;
  122. }
  123. _cleanPath(path){
  124. return path;
  125. }
  126.  
  127. get(path,target){
  128. this.hash['GET ' + this._cleanPath(this.base + path)] = target;
  129. }
  130. post(path,target){
  131. this.hash['POST ' + this._cleanPath(this.base + path)] = target;
  132. }
  133. delete(path,target){
  134. this.hash['DELETE ' + this._cleanPath(this.base + path)] = target;
  135. }
  136. put(path,target){
  137. this.hash['PUT ' + this._cleanPath(this.base + path)] = target;
  138. }
  139.  
  140. }
  141.  
  142.  
  143.  
  144. function cleanPath(path){
  145. return path;
  146. }
  147.  
  148. function buildRouter(rootpath = '/', collection = {}){
  149.  
  150. function router(path, cb){
  151.  
  152. let hash = collection;
  153. let root;
  154. let routerWrapper;
  155.  
  156. if(typeof path === 'string'){
  157. root = path;
  158. routerWrapper = cb;
  159. }
  160.  
  161. if(typeof path === 'function'){
  162. root = '/';
  163. routerWrapper = path;
  164. }
  165.  
  166. if(routerWrapper){
  167. routerWrapper(buildRouter(rootpath + path, collection))
  168. }
  169.  
  170. return hash
  171. }
  172.  
  173. router.get = (path, target) => {
  174. collection['GET ' + cleanPath(rootpath + path)] = target;
  175. }
  176.  
  177. return router;
  178. }
  179.  
  180. let router = buildRouter();
  181.  
  182. let routes = router('/', router => {
  183. router.get('path', 'target')
  184.  
  185. router('/myathoer', router => {
  186. router.get('/nestedpath', 'othertarget')
  187. })
  188. })
  189.  
  190.  
  191.  
  192. console.log(routes);
  193.  
  194. /*let routes = {};
  195. let route = new Router('/hello', routes);
  196. route('/', router => {
  197. router.get()
  198. })
  199. route.get('/bonjour/:id', 'Controller.target')*/
  200.  
  201.  
  202. </script></body>
  203. </html>
Add Comment
Please, Sign In to add comment