Socket
Socket
Sign inDemoInstall

agnostic-router

Package Overview
Dependencies
Maintainers
1
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

agnostic-router - npm Package Compare versions

Comparing version 0.2.1 to 0.2.2

test/path.js

2

bower.json
{
"name": "agnostic-router",
"version": "0.2.1",
"version": "0.2.2",
"description": "Route URLs without requiring Express, or even a webserver",

@@ -5,0 +5,0 @@ "authors": [

{
"name": "agnostic-router",
"version": "0.2.1",
"version": "0.2.2",
"description": "Route URLs without requiring Express and friends",

@@ -5,0 +5,0 @@ "license": "MIT",

@@ -48,2 +48,5 @@ # Agnostic Router #

});
// Build a path
var generated = router.path('/books/:category/:title', {category: 'mycategory', title: 'mytitle'});

@@ -50,0 +53,0 @@ ## Testing ##

(function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Router = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var slice=[].slice;!function(e,r){"function"==typeof define&&null!=define.amd?define([],r):"undefined"!=typeof exports&&null!==exports?module.exports=r():e.UrlPattern=r()}(this,function(){var e,r,t,n,a,i,s,u,o,l,c,g,f,h,m;return o=function(e){return e.replace(/[-\/\\^$*+?.()|[\]{}]/g,"\\$&")},s=function(e,r){var t,n,a;for(a=[],t=-1,n=e.length;++t<n;)a=a.concat(r(e[t]));return a},h=function(e,r){var t,n,a;for(a="",t=-1,n=e.length;++t<n;)a+=r(e[t]);return a},f=function(e){return new RegExp(e.toString()+"|").exec("").length-1},c=function(e,r){var t,n,a,i,s;for(i={},t=-1,a=e.length;++t<a;)n=e[t],null!=(s=r[t])&&(null!=i[n]?(Array.isArray(i[n])||(i[n]=[i[n]]),i[n].push(s)):i[n]=s);return i},e={},e.Result=function(e,r){this.value=e,this.rest=r},e.Tagged=function(e,r){this.tag=e,this.value=r},e.tag=function(r,t){return function(n){var a,i;if(null!=(a=t(n)))return i=new e.Tagged(r,a.value),new e.Result(i,a.rest)}},e.regex=function(r){return function(t){var n,a;if(null!=(n=r.exec(t)))return a=n[0],new e.Result(a,t.slice(a.length))}},e.sequence=function(){var r;return r=1<=arguments.length?slice.call(arguments,0):[],function(t){var n,a,i,s,u,o;for(n=-1,a=r.length,o=[],s=t;++n<a;){if(i=r[n],null==(u=i(s)))return;o.push(u.value),s=u.rest}return new e.Result(o,s)}},e.pick=function(){var r,t;return r=arguments[0],t=2<=arguments.length?slice.call(arguments,1):[],function(n){var a,i;if(null!=(i=e.sequence.apply(e,t)(n)))return a=i.value,i.value=a[r],i}},e.string=function(r){var t;return t=r.length,function(n){if(n.slice(0,t)===r)return new e.Result(r,n.slice(t))}},e.lazy=function(e){var r;return r=null,function(t){return null==r&&(r=e()),r(t)}},e.baseMany=function(r,t,n,a,i){var s,u,o;for(u=i,o=n?"":[];;){if(null!=t&&null!=t(u))break;if(null==(s=r(u)))break;n?o+=s.value:o.push(s.value),u=s.rest}if(!a||0!==o.length)return new e.Result(o,u)},e.many1=function(r){return function(t){return e.baseMany(r,null,!1,!0,t)}},e.concatMany1Till=function(r,t){return function(n){return e.baseMany(r,t,!0,!0,n)}},e.firstChoice=function(){var e;return e=1<=arguments.length?slice.call(arguments,0):[],function(r){var t,n,a,i;for(t=-1,n=e.length;++t<n;)if(a=e[t],null!=(i=a(r)))return i}},g=function(r){var t;return t={},t.wildcard=e.tag("wildcard",e.string(r.wildcardChar)),t.optional=e.tag("optional",e.pick(1,e.string(r.optionalSegmentStartChar),e.lazy(function(){return t.pattern}),e.string(r.optionalSegmentEndChar))),t.name=e.regex(new RegExp("^["+r.segmentNameCharset+"]+")),t.named=e.tag("named",e.pick(1,e.string(r.segmentNameStartChar),e.lazy(function(){return t.name}))),t.escapedChar=e.pick(1,e.string(r.escapeChar),e.regex(/^./)),t.static=e.tag("static",e.concatMany1Till(e.firstChoice(e.lazy(function(){return t.escapedChar}),e.regex(/^./)),e.firstChoice(e.string(r.segmentNameStartChar),e.string(r.optionalSegmentStartChar),e.string(r.optionalSegmentEndChar),t.wildcard))),t.token=e.lazy(function(){return e.firstChoice(t.wildcard,t.optional,t.named,t.static)}),t.pattern=e.many1(e.lazy(function(){return t.token})),t},u={escapeChar:"\\",segmentNameStartChar:":",segmentValueCharset:"a-zA-Z0-9-_~ %",segmentNameCharset:"a-zA-Z0-9",optionalSegmentStartChar:"(",optionalSegmentEndChar:")",wildcardChar:"*"},i=function(e,r){if(Array.isArray(e))return h(e,function(e){return i(e,r)});switch(e.tag){case"wildcard":return"(.*?)";case"named":return"(["+r+"]+)";case"static":return o(e.value);case"optional":return"(?:"+i(e.value,r)+")?"}},a=function(e,r){return null==r&&(r=u.segmentValueCharset),"^"+i(e,r)+"$"},n=function(e){if(Array.isArray(e))return s(e,n);switch(e.tag){case"wildcard":return["_"];case"named":return[e.value];case"static":return[];case"optional":return n(e.value)}},l=function(e,r,t,n){var a,i,s,u;if(null==n&&(n=!1),null!=(u=e[r])){if(a=t[r]||0,i=Array.isArray(u)?u.length-1:0,!(a>i))return s=Array.isArray(u)?u[a]:u,n&&(t[r]=a+1),s;if(n)throw new Error("too few values provided for key `"+r+"`")}else if(n)throw new Error("no values provided for key `"+r+"`")},t=function(e,r,n){var a,i;if(Array.isArray(e)){for(a=-1,i=e.length;++a<i;)if(t(e[a],r,n))return!0;return!1}switch(e.tag){case"wildcard":return null!=l(r,"_",n,!1);case"named":return null!=l(r,e.value,n,!1);case"static":return!1;case"optional":return t(e.value,r,n)}},m=function(e,r,n){if(Array.isArray(e))return h(e,function(e){return m(e,r,n)});switch(e.tag){case"wildcard":return l(r,"_",n,!0);case"named":return l(r,e.value,n,!0);case"static":return e.value;case"optional":return t(e.value,r,n)?m(e.value,r,n):""}},r=function(e,t){var i,s,o,l;if(e instanceof r)return this.isRegex=e.isRegex,this.regex=e.regex,this.ast=e.ast,void(this.names=e.names);if(this.isRegex=e instanceof RegExp,"string"!=typeof e&&!this.isRegex)throw new TypeError("argument must be a regex or a string");if(this.isRegex){if(this.regex=e,null!=t){if(!Array.isArray(t))throw new Error("if first argument is a regex the second argument may be an array of group names but you provided something else");if(i=f(this.regex),t.length!==i)throw new Error("regex contains "+i+" groups but array of group names contains "+t.length);this.names=t}}else{if(""===e)throw new Error("argument must not be the empty string");if(e.replace(/\s+/g,"")!==e)throw new Error("argument must not contain whitespace");if(s={escapeChar:(null!=t?t.escapeChar:void 0)||u.escapeChar,segmentNameStartChar:(null!=t?t.segmentNameStartChar:void 0)||u.segmentNameStartChar,segmentNameCharset:(null!=t?t.segmentNameCharset:void 0)||u.segmentNameCharset,segmentValueCharset:(null!=t?t.segmentValueCharset:void 0)||u.segmentValueCharset,optionalSegmentStartChar:(null!=t?t.optionalSegmentStartChar:void 0)||u.optionalSegmentStartChar,optionalSegmentEndChar:(null!=t?t.optionalSegmentEndChar:void 0)||u.optionalSegmentEndChar,wildcardChar:(null!=t?t.wildcardChar:void 0)||u.wildcardChar},l=g(s),null==(o=l.pattern(e)))throw new Error("couldn't parse pattern");if(""!==o.rest)throw new Error("could only partially parse pattern");this.ast=o.value,this.regex=new RegExp(a(this.ast,s.segmentValueCharset)),this.names=n(this.ast)}},r.prototype.match=function(e){var r,t;return null==(t=this.regex.exec(e))?null:(r=t.slice(1),this.names?c(this.names,r):r)},r.prototype.stringify=function(e){if(null==e&&(e={}),this.isRegex)throw new Error("can't stringify patterns generated from a regex");if(e!==Object(e))throw new Error("argument must be an object or undefined");return m(this.ast,e,{})},r.escapeForRegex=o,r.concatMap=s,r.stringConcatMap=h,r.regexGroupCount=f,r.keysAndValuesToObject=c,r.P=e,r.newParser=g,r.defaultOptions=u,r.astNodeToRegexString=a,r.astNodeToNames=n,r.getParam=l,r.astNodeContainsSegmentsForProvidedParams=t,r.stringify=m,r});
},{}],2:[function(require,module,exports){
function Router(t){this.options="object"==typeof t?t:{},this.useRoutes=[],this.methodRoutes={},this.index=0,this.UrlPattern=UrlPattern}var UrlPattern=require("url-pattern");Router.prototype.route=function(){function t(){var r=a.shift();void 0!==r?(e.params=r.params,"function"==typeof r.handler?r.handler(e,o,t):Router._isRouter(r.handler)?r.handler.route(e.method,"/"+e.params._,e,o,t):t()):"function"==typeof n&&n()}if(3==arguments.length)var e={},o=arguments[2];else if(arguments.length>=4){var e=arguments[2];"object"==typeof e&&e||(e={});var o=arguments[3]}e.method=arguments[0];var r=arguments[1];void 0===e.path&&(e.path=r);var n=arguments[4],a=this._methodPathMatches(e.method,r);t()},Router.prototype.use=function(){var t="string"==typeof arguments[0]?arguments[0]:"",e=Router._validHandler(arguments[0])?arguments[0]:arguments[1];return this._addRouteToArray(this.useRoutes,Router._normalizePath(t),e)},Router.prototype.useFirst=function(){var t="string"==typeof arguments[0]?arguments[0]:"",e=Router._validHandler(arguments[0])?arguments[0]:arguments[1];return this._addRouteToArray(this.useRoutes,Router._normalizePath(t),e,!0)},Router.prototype.method=function(t,e,o){return this.methodRoutes[t]instanceof Array||(this.methodRoutes[t]=[]),this._addRouteToArray(this.methodRoutes[t],Router._normalizePath(e),o)},Router._validHandler=function(t){return"function"==typeof t||Router._isRouter(t)},Router._isRouter=function(t){return"object"==typeof t&&"function"==typeof t.route},Router._normalizePath=function(t){return t.replace(/\/$/,"")},Router.prototype._methodPathMatches=function(t,e){var o=Router._normalizePath(e),r=[],n=this;return n.useRoutes.forEach(function(t){var e=new UrlPattern(t.path+"(/*)",n.options).match(o);null!==e&&r.push({path:t.path,handler:t.handler,params:e})}),n.methodRoutes[t]instanceof Array&&n.methodRoutes[t].forEach(function(t){var e=new UrlPattern(t.path,n.options).match(o);null!==e&&r.push({path:t.path,handler:t.handler,params:e})}),r},Router.prototype._addRouteToArray=function(t,e,o,r){var n={path:e,handler:o};"boolean"==typeof r?t.unshift(n):t.push(n)},module.exports=function(t){return new Router(t)};
function Router(t){this.options="object"==typeof t?t:{},this.useRoutes=[],this.methodRoutes={},this.index=0,this.UrlPattern=UrlPattern}var UrlPattern=require("url-pattern");Router.prototype.route=function(){function t(){var r=a.shift();void 0!==r?(e.params=r.params,"function"==typeof r.handler?r.handler(e,o,t):Router._isRouter(r.handler)?r.handler.route(e.method,"/"+e.params._,e,o,t):t()):"function"==typeof n&&n()}if(3==arguments.length)var e={},o=arguments[2];else if(arguments.length>=4){var e=arguments[2];"object"==typeof e&&e||(e={});var o=arguments[3]}e.method=arguments[0];var r=arguments[1];void 0===e.path&&(e.path=r);var n=arguments[4],a=this._methodPathMatches(e.method,r);t()},Router.prototype.path=function(t,e){return"object"==typeof e&&null!=e||(e={}),new UrlPattern(t+"(/*)",this.options).stringify(e)},Router.prototype.use=function(){var t="string"==typeof arguments[0]?arguments[0]:"",e=Router._validHandler(arguments[0])?arguments[0]:arguments[1];return this._addRouteToArray(this.useRoutes,Router._normalizePath(t),e)},Router.prototype.useFirst=function(){var t="string"==typeof arguments[0]?arguments[0]:"",e=Router._validHandler(arguments[0])?arguments[0]:arguments[1];return this._addRouteToArray(this.useRoutes,Router._normalizePath(t),e,!0)},Router.prototype.method=function(t,e,o){return this.methodRoutes[t]instanceof Array||(this.methodRoutes[t]=[]),this._addRouteToArray(this.methodRoutes[t],Router._normalizePath(e),o)},Router._validHandler=function(t){return"function"==typeof t||Router._isRouter(t)},Router._isRouter=function(t){return"object"==typeof t&&"function"==typeof t.route},Router._normalizePath=function(t){return t.replace(/\/$/,"")},Router.prototype._methodPathMatches=function(t,e){var o=Router._normalizePath(e),r=[],n=this;return n.useRoutes.forEach(function(t){var e=new UrlPattern(t.path+"(/*)",n.options).match(o);null!==e&&r.push({path:t.path,handler:t.handler,params:e})}),n.methodRoutes[t]instanceof Array&&n.methodRoutes[t].forEach(function(t){var e=new UrlPattern(t.path,n.options).match(o);null!==e&&r.push({path:t.path,handler:t.handler,params:e})}),r},Router.prototype._addRouteToArray=function(t,e,o,r){var n={path:e,handler:o};"boolean"==typeof r?t.unshift(n):t.push(n)},module.exports=function(t){return new Router(t)};
},{"url-pattern":1}]},{},[2])(2)

@@ -6,0 +6,0 @@ });

@@ -51,2 +51,10 @@ var UrlPattern = require('url-pattern');

Router.prototype.path = function(path, params) {
// path, [params]
if(typeof params != 'object' || params == null)
params = {};
return (new UrlPattern(path + '(/*)', this.options)).stringify(params)
};
Router.prototype.use = function() {

@@ -53,0 +61,0 @@ // [path], handler

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

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