Comparing version 1.5.0 to 1.6.0
{ | ||
"name": "anumargak", | ||
"version": "1.5.0", | ||
"description": "Amazing fast multipurpose web/ HTTP router", | ||
"version": "1.6.0", | ||
"description": "Amazing fast multipurpose simple to use web/ HTTP router", | ||
"main": "./src/letsRoute.js", | ||
@@ -9,3 +9,4 @@ "scripts": { | ||
"postinstall": "node tasks/postinstall.js", | ||
"coverage": "nyc jasmine tests/*test.js; nyc report --reporter=lcov " | ||
"coverage": "nyc jasmine tests/*test.js; nyc report --reporter=lcov ", | ||
"updateContributors": "node static/contributors.js" | ||
}, | ||
@@ -29,3 +30,4 @@ "author": "Amit Gupta (https://github.com/amitguptagwl)", | ||
"async", | ||
"await" | ||
"await", | ||
"named expressions" | ||
], | ||
@@ -32,0 +34,0 @@ "devDependencies": { |
@@ -1,2 +0,2 @@ | ||
# anumargak (अनुमार्गक) | ||
# अनुमार्गक (anumargak) | ||
Fastest HTTP Router | ||
@@ -124,2 +124,26 @@ | ||
## Named Expressions | ||
Anumargak lets you add named expressions. You can use them at the time of registering the route. | ||
```js | ||
router.addNamedExpression("num","\\d+"); | ||
router.addNamedExpression({ | ||
"name1" : "regx1", | ||
"name2" : "regx2", | ||
}); | ||
``` | ||
Example routes | ||
```js | ||
/send/to/:phone(:phone:) | ||
/authenticate/:token(:alphanum:) | ||
``` | ||
Adding them make this router simple to use. | ||
## Benchmark | ||
@@ -152,1 +176,7 @@ |method | url type | anumargak (अनुमार्गक) | find-my-way| | ||
- [Muneem (मुनीम)](https://github.com/muneem4node/muneem): A framework to write fast web services in easy way. Designed specially for developers, QAs, Maintainers, and BAs. | ||
## Contributors | ||
- <img src="https://avatars2.githubusercontent.com/u/7692328?v=4" width="20" height="20"/> [amitguptagwl](https://github.com/amitguptagwl) | ||
- <img src="https://avatars2.githubusercontent.com/u/10572008?v=4" width="20" height="20"/> [rajeshdh](https://github.com/rajeshdh) | ||
- <img src="https://avatars3.githubusercontent.com/u/4491530?v=4" width="20" height="20"/> [shuklajay117](https://github.com/shuklajay117) |
var { getFirstMatche, getAllMatches, doesMatch, urlSlice } = require("./util"); | ||
var { getFirstMatche, getAllMatches, doesMatch, urlSlice, getNamedExpressionMatches } = require("./util"); | ||
@@ -7,2 +7,20 @@ var httpMethods = ["GET", "HEAD", "PUT", "POST", "DELETE", "OPTIONS", "PATCH", "TRACE", "CONNECT", "COPY", "LINK", "UNLINK", "PURGE", "LOCK", "UNLOCK", "PROPFIND", "VIEW"]; | ||
/** | ||
* Adds named expression to a namedExpression object which can be used directly in params | ||
* @param {string | object} handledByArgumentsObject | ||
*/ | ||
Anumargak.prototype.addNamedExpression = function () { | ||
var firstParam = arguments[0]; | ||
if (typeof firstParam === "string" && typeof arguments[1] === "string") { | ||
this.namedExpression[firstParam] = arguments[1]; | ||
} else if (typeof firstParam === "object" && Object.prototype.toString.call(firstParam) === "[object Object]") { | ||
for (var key in firstParam) { | ||
this.namedExpression[key] = firstParam[key]; | ||
} | ||
} else { | ||
throw Error("Invalid method argument. Two parameters of type String or only object is expected."); | ||
} | ||
} | ||
/** | ||
* Adds routes against the given method and URL | ||
@@ -30,2 +48,20 @@ * @param {string} method | ||
function replaceNamedExpression(url) { | ||
var namedExpressionRegexStr = "\\(:(.*?):\\)"; | ||
var namedExpressionMatches = getNamedExpressionMatches(url, namedExpressionRegexStr); | ||
if (namedExpressionMatches && namedExpressionMatches.length > 0) { | ||
for (var i = 0; i < namedExpressionMatches.length; i++) { | ||
var matchedNamedKey = namedExpressionMatches[i][1]; | ||
if (this.namedExpression[matchedNamedKey]) { | ||
url = url.replace(":" + matchedNamedKey + ":", this.namedExpression[matchedNamedKey]); | ||
} else { | ||
throw Error("Usage of named expression in url as " + matchedNamedKey + ". Define it using addNamedExpression method before using in URLs."); | ||
} | ||
} | ||
} | ||
return url; | ||
} | ||
Anumargak.prototype._on = function (method, url, fn) { | ||
@@ -41,2 +77,4 @@ if (httpMethods.indexOf(method) === -1) throw Error("Invalid method type " + method); | ||
url = replaceNamedExpression.call(this, url); | ||
var matches = getFirstMatche(url, wildcardRegexStr); | ||
@@ -260,2 +298,3 @@ if (matches) { | ||
this.count = 0; | ||
this.namedExpression = {}; | ||
this.dynamicRoutes = { | ||
@@ -262,0 +301,0 @@ GET: {}, |
const RandExp = require('randexp'); | ||
RandExp.prototype.randInt = (a,b) => { | ||
RandExp.prototype.randInt = (a, b) => { | ||
return a; | ||
} | ||
exports.doesMatch = function(a, b) { | ||
exports.doesMatch = function (a, b) { | ||
var a_IsRegex = a.indexOf("(") > 0; | ||
var b_IsRegex = b.indexOf("(") > 0; | ||
if(a_IsRegex && b_IsRegex){ | ||
if (a_IsRegex && b_IsRegex) { | ||
//genetate random string for both regex and pass to the other regex | ||
@@ -20,13 +20,13 @@ | ||
if(aRand === bRand){ | ||
if (aRand === bRand) { | ||
return true; | ||
}else{ | ||
return exports.doesMatch(a,bRand) || exports.doesMatch(aRand,b); | ||
} else { | ||
return exports.doesMatch(a, bRand) || exports.doesMatch(aRand, b); | ||
} | ||
}else if(!a_IsRegex && b_IsRegex){ | ||
} else if (!a_IsRegex && b_IsRegex) { | ||
return new RegExp(b).test(a); | ||
}else if(a_IsRegex && !b_IsRegex){ | ||
} else if (a_IsRegex && !b_IsRegex) { | ||
return new RegExp(a).test(b); | ||
}else /* if(a_IsRegex && b_IsRegex) */{ | ||
} else /* if(a_IsRegex && b_IsRegex) */ { | ||
return a === b; | ||
@@ -36,3 +36,3 @@ } | ||
exports.getFirstMatche = function(string, regex_str) { | ||
exports.getFirstMatche = function (string, regex_str) { | ||
var regex = new RegExp(regex_str); | ||
@@ -42,8 +42,13 @@ return regex.exec(string); | ||
exports.getAllMatches = function(string, regex_str) { | ||
var regex = new RegExp(regex_str,"g"); | ||
return exports.getAllRegexMatches(string,regex); | ||
exports.getAllMatches = function (string, regex_str) { | ||
var regex = new RegExp(regex_str, "g"); | ||
return exports.getAllRegexMatches(string, regex); | ||
} | ||
exports.getAllRegexMatches = function(string, regex) { | ||
exports.getNamedExpressionMatches = function (string, regex_str) { | ||
var regex = new RegExp(regex_str, "g"); | ||
return exports.getAllRegexMatches(string, regex); | ||
} | ||
exports.getAllRegexMatches = function (string, regex) { | ||
var matches = []; | ||
@@ -53,7 +58,7 @@ var match; | ||
var allmatches = []; | ||
for(var i in match){ | ||
for (var i in match) { | ||
var submatch = match[i]; | ||
allmatches.push(submatch); | ||
} | ||
matches.push(allmatches); | ||
matches.push(allmatches); | ||
} | ||
@@ -63,8 +68,8 @@ return matches; | ||
exports.urlSlice = function(url){ | ||
exports.urlSlice = function (url) { | ||
var index = url.indexOf("?"); | ||
if(index > 0) return url.substr(0,index); | ||
if (index > 0) return url.substr(0, index); | ||
var index = url.indexOf("#"); | ||
if(index > 0) return url.substr(0,index); | ||
if (index > 0) return url.substr(0, index); | ||
return url; | ||
} |
Sorry, the diff of this file is not supported yet
Network access
Supply chain riskThis module accesses the network.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
41795
10
437
181
1
1