New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

switch-path

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

switch-path - npm Package Compare versions

Comparing version 1.1.1 to 1.1.2

5

dist/switch-path.js

@@ -39,3 +39,6 @@ (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){

});
return params;
var matched = patternParts.every(function (part, i) {
return part.match(/:\w+/) !== null || part === sourceParts[i];
});
return matched ? params : [];
}

@@ -42,0 +45,0 @@

2

dist/switch-path.min.js

@@ -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 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])&&params.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)});
(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});var matched=patternParts.every(function(part,i){return part.match(/:\w+/)!==null||part===sourceParts[i]});return matched?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])&&params.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)});

@@ -12,3 +12,3 @@ /* global describe, it */

describe('switchPath', function () {
describe('switchPath basic usage', function () {
it('should match a basic path', function () {

@@ -199,3 +199,5 @@ var _switchPath = (0, _libIndex2['default'])('/home/foo', {

});
});
describe('switchPath corner cases', function () {
it('should match more specific path in case many match', function () {

@@ -215,2 +217,17 @@ var _switchPath13 = (0, _libIndex2['default'])('/home/1736', {

});
it('should match exact path in case many match', function () {
var _switchPath14 = (0, _libIndex2['default'])('/', {
'/home/:id': function homeId(id) {
return 'id is ' + id;
},
'/': 'root'
});
var path = _switchPath14.path;
var value = _switchPath14.value;
(0, _chai.expect)(path).to.be.equal('/');
(0, _chai.expect)(value).to.be.equal('root');
});
});

@@ -38,3 +38,6 @@ "use strict";

});
return params;
var matched = patternParts.every(function (part, i) {
return part.match(/:\w+/) !== null || part === sourceParts[i];
});
return matched ? params : [];
}

@@ -41,0 +44,0 @@

{
"name": "switch-path",
"version": "1.1.1",
"version": "1.1.2",
"description": "switch case for URLs, a small tool for routing in JavaScript",

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

@@ -31,3 +31,6 @@ function isPattern(candidate) {

}).filter(x => x !== null)
return params
const matched = patternParts.every((part, i) =>
part.match(/:\w+/) !== null || part === sourceParts[i]
)
return matched ? params : []
}

@@ -34,0 +37,0 @@

@@ -148,2 +148,21 @@ /* global describe, it */

});
it('should match exact path in case many match', () => {
const {path, value} = switchPath('/', {
'/home/:id': id => `id is ${id}`,
'/': 'root',
});
expect(path).to.be.equal('/');
expect(value).to.be.equal('root');
});
it('should not match unrelated paths that have with params', () => {
const {path, value} = switchPath('/home/123', {
'/': 'root',
'/home/:id': id => `home is ${id}`,
'/external/:id': id => `external is ${id}`,
});
expect(path).to.be.equal('/home/123');
expect(value).to.be.equal('home is 123');
});
});
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