Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

2way-router

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

2way-router - npm Package Compare versions

Comparing version 1.2.0 to 1.2.1

26

index.js

@@ -59,2 +59,3 @@ var Promise = require('promise');

this.params = {};
this.urlResolvers = {};

@@ -205,2 +206,12 @@ this.registerType(require('./lib/param/NumberParam'), ['int', 'number']);

/**
* @param {string} name
* @param {function} resolver
* @returns {Router}
*/
Router.prototype.urlResolver = function (name, resolver) {
this.urlResolvers[name] = resolver;
return this;
};
/**
* @param {string} typeName

@@ -242,8 +253,17 @@ * @param {string} name

Router.prototype.url = function (routeName, params) {
if (this.namedRoutes.hasOwnProperty(routeName)) {
return this.namedRoutes[routeName].url(params);
var _this = this;
function getUrl(routeName, params) {
if (_this.namedRoutes.hasOwnProperty(routeName)) {
return _this.namedRoutes[routeName].url(params);
}
return Promise.reject(Error('no route with name "' + routeName + '"'));
}
return Promise.reject(Error('no route with name "' + routeName + '"'));
if (this.urlResolvers.hasOwnProperty(routeName)) {
return this.urlResolvers[routeName](params || {}, getUrl);
}
return getUrl(routeName, params);
};
module.exports = Router;

2

package.json
{
"name": "2way-router",
"version": "1.2.0",
"version": "1.2.1",
"description": "2-way router",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -252,2 +252,31 @@ /*global describe, xdescribe, it, expect*/

});
describe("supports url resolvers:", function () {
var router = new Router();
router.route("/news/")
.name("news");
router.route("/news/archive/{year:int}-{month:int}-{day:int}/")
.name("news.archive");
router.urlResolver("news", function (params, url) {
if (params.date) {
return url("news.archive", {
year: params.date.getFullYear(),
month: params.date.getMonth() + 1,
day: params.date.getDate()
});
}
return url("news");
});
var date = new Date();
router.url("news").then(function (url) {
expect(url)
.toBe("/news/");
});
router.url("news", { date: date }).then(function (url) {
expect(url)
.toBe("/news/archive/" + date.getFullYear() + "-" + (date.getMonth() + 1) + "-" + date.getDate() + "/");
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc