Comparing version 0.0.3 to 0.0.4
@@ -14,1 +14,2 @@ | ||
* Added [Karma](http://karma-runner.github.io) configuration to assist with test running. | ||
* Initial implementation of `ESToolbox/Router` component using TDD methodology. |
{ | ||
"name": "estoolbox", | ||
"description": "A collection of libraries intended to augment the development of ES2015 targeted applications.", | ||
"version": "0.0.3", | ||
"version": "0.0.4", | ||
"main": "./src/main.js", | ||
@@ -6,0 +6,0 @@ "scripts": { |
# [ESToolbox](https://dbtedman.github.io/estoolbox/) `v0.0.3` [![Build Status](https://travis-ci.org/dbtedman/estoolbox.svg?branch=master)](https://travis-ci.org/dbtedman/estoolbox) [![NPM Version](https://img.shields.io/npm/v/estoolbox.svg)](https://www.npmjs.com/package/estoolbox) | ||
# [ESToolbox](https://dbtedman.github.io/estoolbox/) `v0.0.4` [![Build Status](https://travis-ci.org/dbtedman/estoolbox.svg?branch=master)](https://travis-ci.org/dbtedman/estoolbox) [![NPM Version](https://img.shields.io/npm/v/estoolbox.svg)](https://www.npmjs.com/package/estoolbox) | ||
@@ -18,6 +18,10 @@ A collection of libraries intended to augment the development of ES2015 targeted applications. | ||
2\. Import the desired component. | ||
2\. Import and use the desired component. | ||
```javascript | ||
import router from "estoolbox/router"; | ||
import Router from "estoolbox/router"; | ||
Router.when("/about", () => { | ||
// Show the about route content. | ||
}); | ||
``` | ||
@@ -24,0 +28,0 @@ |
@@ -1,2 +0,2 @@ | ||
import router from "../src/router"; | ||
import Router from "../src/router"; | ||
@@ -7,31 +7,34 @@ describe("ESToolbox", () => { | ||
expect(router).not.toBe(undefined); | ||
expect(Router).not.toBeUndefined(); | ||
}); | ||
it("is instantiated", () => { | ||
it("has when() method", () => { | ||
expect(router.constructor.name).toBe("Router"); | ||
expect(Router.when).not.toBeUndefined(); | ||
}); | ||
it("accepts a simple /about route and callback", () => { | ||
let foundError = false; | ||
it("has when() method that accepts a route string and function callback that gets called when a" + | ||
" matching hash is found after a future hash change", (done) => { | ||
try { | ||
router.when("/about", () => { | ||
}); | ||
Router.when("/about", () => { | ||
done(); | ||
}); | ||
} catch (error) { | ||
foundError = true; | ||
} | ||
expect(foundError).toBe(false); | ||
// Hash updated after Router.when is called. | ||
window.location.hash = "/about"; | ||
}); | ||
it("has a start method", () => { | ||
expect(router.when).not.toBe(undefined); | ||
}); | ||
it("has when() method that accepts a route string and function callback that gets called when a" + | ||
" matching hash is found in the current URL hash", (done) => { | ||
it("window is defined", () => { | ||
expect(window).not.toBe(undefined); | ||
// Hash updated before Router.when is called. | ||
window.location.hash = "/support"; | ||
// Have a short timeout to ensure that the hash is changed before the when method is called. | ||
window.setTimeout(() => { | ||
Router.when("/support", () => { | ||
done(); | ||
}); | ||
}, 100); | ||
}); | ||
}); |
export default class ESToolbox { | ||
constructor() { | ||
this.something = "nothing"; | ||
} | ||
} |
@@ -1,50 +0,76 @@ | ||
/** | ||
* Handle performing actions based on matched hash routes. | ||
*/ | ||
class Router { | ||
export default class Router { | ||
/** | ||
* @constructor | ||
*/ | ||
constructor() { | ||
/** | ||
* @type {Array<Pattern>} | ||
*/ | ||
this.patterns = []; | ||
this.routes = []; | ||
} | ||
/** | ||
* Map a route pattern to a callback. | ||
* Add a pattern route and associated callback function to the Router instance attached to the | ||
* window object. | ||
* | ||
* @param {string} pattern - Match request to this pattern. | ||
* @param {function} callback - Executed when pattern is matched. | ||
* @returns {Router} - Reference to class. | ||
* @param route {String} | ||
* @param callback {Function} | ||
*/ | ||
when(pattern, callback) { | ||
static when(route, callback) { | ||
if (window.ESToolboxRouter === undefined) { | ||
window.ESToolboxRouter = new Router(); | ||
window.ESToolboxRouter.watch(); | ||
} | ||
this.patterns.push(new Pattern(pattern, callback)); | ||
window.ESToolboxRouter.when(route, callback); | ||
window.ESToolboxRouter.check(); | ||
} | ||
return Router; | ||
/** | ||
* Watch for any changes that occur in the URL hash and run the route checker. | ||
*/ | ||
watch() { | ||
window.addEventListener("hashchange", () => { | ||
this.check(); | ||
}, false); | ||
} | ||
/** | ||
* @returns {Router} - Reference to class. | ||
* Check if we have a pattern that matches the current URL hash. | ||
*/ | ||
start() { | ||
check() { | ||
const hash = window.location.hash; | ||
let found = false; | ||
// TODO: Add hash watch and initial route test. | ||
this.routes.forEach((route) => { | ||
if (!found) { | ||
if (this.compare(hash.replace(/^[^/]*/, ""), route.route)) { | ||
found = true; | ||
route.callback(); | ||
} | ||
} | ||
}); | ||
} | ||
return Router; | ||
/** | ||
* Compare a pattern to a route. | ||
* | ||
* @param route {String} A URL hash route. | ||
* @param pattern {String} A URL route matching pattern. | ||
* @return {Boolean} True if pattern matches route, else false. | ||
*/ | ||
compare(route, pattern) { | ||
return pattern == route; | ||
} | ||
} | ||
class Pattern { | ||
/** | ||
* @param {string} pattern - | ||
* @param {function} callback - | ||
* @return {Pattern} - | ||
* Add a pattern route and associated callback function. | ||
* | ||
* @param route {String} | ||
* @param callback {Function} | ||
*/ | ||
constructor(pattern, callback) { | ||
this.pattern = pattern; | ||
this.callback = callback; | ||
when(route, callback) { | ||
this.routes.push({ | ||
route: route, | ||
callback: callback | ||
}); | ||
} | ||
} | ||
export default new Router(); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
130295
147
37