switch-path
Advanced tools
Comparing version 1.1.0 to 1.1.1
@@ -122,2 +122,12 @@ (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.switchPath = 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){ | ||
function betterMatch(candidate, reference) { | ||
if (candidate === null) { | ||
return false; | ||
} | ||
if (reference === null) { | ||
return true; | ||
} | ||
return candidate.length >= reference.length; | ||
} | ||
function switchPath(sourcePath, routes) { | ||
@@ -132,3 +142,3 @@ validateSwitchPathPreconditions(sourcePath, routes); | ||
validatePatternPreconditions(pattern); | ||
if (sourcePath.search(pattern) === 0) { | ||
if (sourcePath.search(pattern) === 0 && betterMatch(pattern, matchedPath)) { | ||
matchedPath = pattern; | ||
@@ -138,3 +148,3 @@ value = routes[pattern]; | ||
var params = matchesWithParams(sourcePath, pattern); | ||
if (params.length > 0) { | ||
if (params.length > 0 && betterMatch(sourcePath, matchedPath)) { | ||
matchedPath = sourcePath; | ||
@@ -145,4 +155,5 @@ value = getParamsFnValue(routes[pattern], params); | ||
var child = switchPath(unprefixed(sourcePath, pattern), routes[pattern]); | ||
if (child.path !== null) { | ||
matchedPath = pattern + child.path; | ||
var nestedPath = pattern + child.path; | ||
if (child.path !== null && betterMatch(nestedPath, matchedPath)) { | ||
matchedPath = nestedPath; | ||
value = child.value; | ||
@@ -149,0 +160,0 @@ } |
@@ -1,1 +0,1 @@ | ||
(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.switchPath=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){"use strict";function isPattern(candidate){return typeof candidate==="string"&&(candidate.charAt(0)==="/"||candidate==="*")}function isRouteConfigurationObject(routes){if(typeof routes!=="object"){return false}for(var path in routes){if(routes.hasOwnProperty(path)){return isPattern(path)}}}function unprefixed(fullStr,prefix){return fullStr.split(prefix)[1]}function matchesWithParams(sourcePath,pattern){var sourceParts=sourcePath.split("/").filter(function(s){return s.length>0});var patternParts=pattern.split("/").filter(function(s){return s.length>0});var params=patternParts.map(function(patternPart,index){if(patternPart.match(/:\w+/)!==null){return sourceParts[index]}else{return null}}).filter(function(x){return x!==null});return params}function validateSwitchPathPreconditions(sourcePath,routes){if(typeof sourcePath!=="string"){throw new Error("Invalid source path. We expected to see a string given "+"as the sourcePath (first argument) to switchPath.")}if(!isRouteConfigurationObject(routes)){throw new Error("Invalid routes object. We expected to see a routes "+"configuration object where keys are strings that look like '/foo'. "+"These keys must start with a slash '/'.")}}function validatePatternPreconditions(pattern){if(!isPattern(pattern)){throw new Error("Paths in route configuration must be strings that start "+"with a slash '/'.")}}function isNormalPattern(routes,pattern){if(pattern==="*"||!routes.hasOwnProperty(pattern)){return false}return true}function handleTrailingSlash(paramsFn){if(isRouteConfigurationObject(paramsFn)){return paramsFn["/"]}return paramsFn}function getParamsFnValue(paramFn,params){var _paramFn=handleTrailingSlash(paramFn);if(typeof _paramFn!=="function"){return _paramFn}return _paramFn(params)}function splitPath(path){var pathParts=path.split("/");if(pathParts[pathParts.length-1]===""){pathParts.pop()}return pathParts}function validatePath(sourcePath,matchedPath){if(matchedPath===null){return""}var sourceParts=splitPath(sourcePath);var matchedParts=splitPath(matchedPath);var validPath=sourceParts.map(function(part,index){if(part!==matchedParts[index]){return null}return part}).filter(function(x){return x!==null}).join("/");return validPath}function validate(_ref){var sourcePath=_ref.sourcePath;var matchedPath=_ref.matchedPath;var value=_ref.value;var routes=_ref.routes;var validPath=validatePath(sourcePath,matchedPath);if(!validPath){validPath=!routes["*"]?null:sourcePath;var validValue=!validPath?null:routes["*"];return{validPath:validPath,validValue:validValue}}return{validPath:validPath,validValue:value}}function switchPath(sourcePath,routes){validateSwitchPathPreconditions(sourcePath,routes);var matchedPath=null;var value=null;for(var pattern in routes){if(!isNormalPattern(routes,pattern)){continue}validatePatternPreconditions(pattern);if(sourcePath.search(pattern)===0){matchedPath=pattern;value=routes[pattern]}var params=matchesWithParams(sourcePath,pattern);if(params.length>0){matchedPath=sourcePath;value=getParamsFnValue(routes[pattern],params)}if(isRouteConfigurationObject(routes[pattern])&¶ms.length===0){var child=switchPath(unprefixed(sourcePath,pattern),routes[pattern]);if(child.path!==null){matchedPath=pattern+child.path;value=child.value}}if(pattern===sourcePath){return{path:pattern,value:handleTrailingSlash(routes[pattern])}}}var _validate=validate({sourcePath:sourcePath,matchedPath:matchedPath,value:value,routes:routes});var validPath=_validate.validPath;var validValue=_validate.validValue;return{path:validPath,value:validValue}}module.exports=switchPath},{}]},{},[1])(1)}); | ||
(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.switchPath=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){"use strict";function isPattern(candidate){return typeof candidate==="string"&&(candidate.charAt(0)==="/"||candidate==="*")}function isRouteConfigurationObject(routes){if(typeof routes!=="object"){return false}for(var path in routes){if(routes.hasOwnProperty(path)){return isPattern(path)}}}function unprefixed(fullStr,prefix){return fullStr.split(prefix)[1]}function matchesWithParams(sourcePath,pattern){var sourceParts=sourcePath.split("/").filter(function(s){return s.length>0});var patternParts=pattern.split("/").filter(function(s){return s.length>0});var params=patternParts.map(function(patternPart,index){if(patternPart.match(/:\w+/)!==null){return sourceParts[index]}else{return null}}).filter(function(x){return x!==null});return params}function validateSwitchPathPreconditions(sourcePath,routes){if(typeof sourcePath!=="string"){throw new Error("Invalid source path. We expected to see a string given "+"as the sourcePath (first argument) to switchPath.")}if(!isRouteConfigurationObject(routes)){throw new Error("Invalid routes object. We expected to see a routes "+"configuration object where keys are strings that look like '/foo'. "+"These keys must start with a slash '/'.")}}function validatePatternPreconditions(pattern){if(!isPattern(pattern)){throw new Error("Paths in route configuration must be strings that start "+"with a slash '/'.")}}function isNormalPattern(routes,pattern){if(pattern==="*"||!routes.hasOwnProperty(pattern)){return false}return true}function handleTrailingSlash(paramsFn){if(isRouteConfigurationObject(paramsFn)){return paramsFn["/"]}return paramsFn}function getParamsFnValue(paramFn,params){var _paramFn=handleTrailingSlash(paramFn);if(typeof _paramFn!=="function"){return _paramFn}return _paramFn(params)}function splitPath(path){var pathParts=path.split("/");if(pathParts[pathParts.length-1]===""){pathParts.pop()}return pathParts}function validatePath(sourcePath,matchedPath){if(matchedPath===null){return""}var sourceParts=splitPath(sourcePath);var matchedParts=splitPath(matchedPath);var validPath=sourceParts.map(function(part,index){if(part!==matchedParts[index]){return null}return part}).filter(function(x){return x!==null}).join("/");return validPath}function validate(_ref){var sourcePath=_ref.sourcePath;var matchedPath=_ref.matchedPath;var value=_ref.value;var routes=_ref.routes;var validPath=validatePath(sourcePath,matchedPath);if(!validPath){validPath=!routes["*"]?null:sourcePath;var validValue=!validPath?null:routes["*"];return{validPath:validPath,validValue:validValue}}return{validPath:validPath,validValue:value}}function betterMatch(candidate,reference){if(candidate===null){return false}if(reference===null){return true}return candidate.length>=reference.length}function switchPath(sourcePath,routes){validateSwitchPathPreconditions(sourcePath,routes);var matchedPath=null;var value=null;for(var pattern in routes){if(!isNormalPattern(routes,pattern)){continue}validatePatternPreconditions(pattern);if(sourcePath.search(pattern)===0&&betterMatch(pattern,matchedPath)){matchedPath=pattern;value=routes[pattern]}var params=matchesWithParams(sourcePath,pattern);if(params.length>0&&betterMatch(sourcePath,matchedPath)){matchedPath=sourcePath;value=getParamsFnValue(routes[pattern],params)}if(isRouteConfigurationObject(routes[pattern])&¶ms.length===0){var child=switchPath(unprefixed(sourcePath,pattern),routes[pattern]);var nestedPath=pattern+child.path;if(child.path!==null&&betterMatch(nestedPath,matchedPath)){matchedPath=nestedPath;value=child.value}}if(pattern===sourcePath){return{path:pattern,value:handleTrailingSlash(routes[pattern])}}}var _validate=validate({sourcePath:sourcePath,matchedPath:matchedPath,value:value,routes:routes});var validPath=_validate.validPath;var validValue=_validate.validValue;return{path:validPath,value:validValue}}module.exports=switchPath},{}]},{},[1])(1)}); |
@@ -121,2 +121,12 @@ "use strict"; | ||
function betterMatch(candidate, reference) { | ||
if (candidate === null) { | ||
return false; | ||
} | ||
if (reference === null) { | ||
return true; | ||
} | ||
return candidate.length >= reference.length; | ||
} | ||
function switchPath(sourcePath, routes) { | ||
@@ -131,3 +141,3 @@ validateSwitchPathPreconditions(sourcePath, routes); | ||
validatePatternPreconditions(pattern); | ||
if (sourcePath.search(pattern) === 0) { | ||
if (sourcePath.search(pattern) === 0 && betterMatch(pattern, matchedPath)) { | ||
matchedPath = pattern; | ||
@@ -137,3 +147,3 @@ value = routes[pattern]; | ||
var params = matchesWithParams(sourcePath, pattern); | ||
if (params.length > 0) { | ||
if (params.length > 0 && betterMatch(sourcePath, matchedPath)) { | ||
matchedPath = sourcePath; | ||
@@ -144,4 +154,5 @@ value = getParamsFnValue(routes[pattern], params); | ||
var child = switchPath(unprefixed(sourcePath, pattern), routes[pattern]); | ||
if (child.path !== null) { | ||
matchedPath = pattern + child.path; | ||
var nestedPath = pattern + child.path; | ||
if (child.path !== null && betterMatch(nestedPath, matchedPath)) { | ||
matchedPath = nestedPath; | ||
value = child.value; | ||
@@ -148,0 +159,0 @@ } |
{ | ||
"name": "switch-path", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "switch case for URLs, a small tool for routing in JavaScript", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -111,2 +111,12 @@ function isPattern(candidate) { | ||
function betterMatch(candidate, reference) { | ||
if (candidate === null) { | ||
return false | ||
} | ||
if (reference === null) { | ||
return true | ||
} | ||
return candidate.length >= reference.length | ||
} | ||
function switchPath(sourcePath, routes) { | ||
@@ -121,3 +131,3 @@ validateSwitchPathPreconditions(sourcePath, routes) | ||
validatePatternPreconditions(pattern) | ||
if (sourcePath.search(pattern) === 0) { | ||
if (sourcePath.search(pattern) === 0 && betterMatch(pattern, matchedPath)) { | ||
matchedPath = pattern | ||
@@ -127,3 +137,3 @@ value = routes[pattern] | ||
const params = matchesWithParams(sourcePath, pattern) | ||
if (params.length > 0) { | ||
if (params.length > 0 && betterMatch(sourcePath, matchedPath)) { | ||
matchedPath = sourcePath | ||
@@ -134,4 +144,5 @@ value = getParamsFnValue(routes[pattern], params) | ||
const child = switchPath(unprefixed(sourcePath, pattern), routes[pattern]) | ||
if (child.path !== null) { | ||
matchedPath = pattern + child.path | ||
const nestedPath = pattern + child.path | ||
if (child.path !== null && betterMatch(nestedPath, matchedPath)) { | ||
matchedPath = nestedPath | ||
value = child.value | ||
@@ -138,0 +149,0 @@ } |
@@ -5,3 +5,3 @@ /* global describe, it */ | ||
describe('switchPath', () => { | ||
describe('switchPath basic usage', () => { | ||
it('should match a basic path', () => { | ||
@@ -139,1 +139,12 @@ const {path, value} = switchPath('/home/foo', { | ||
}); | ||
describe('switchPath corner cases', () => { | ||
it('should match more specific path in case many match', () => { | ||
const {path, value} = switchPath('/home/1736', { | ||
'/home/:id': id => `id is ${id}`, | ||
'/': 'root', | ||
}); | ||
expect(path).to.be.equal('/home/1736'); | ||
expect(value).to.be.equal('id is 1736'); | ||
}); | ||
}); |
36879
17
787