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.3 to 1.1.4

15

dist/switch-path.js

@@ -90,2 +90,15 @@ (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 matchPartial(sourcePath, pattern) {
var patternParts = splitPath(pattern);
var sourceParts = splitPath(sourcePath);
var matchedParts = [];
for (var i = 0; i < patternParts.length; ++i) {
matchedParts.push(sourceParts[i]);
}
return matchedParts.join("/");
}
function validatePath(sourcePath, matchedPath) {

@@ -151,3 +164,3 @@ if (matchedPath === null) {

if (params.length > 0 && betterMatch(sourcePath, matchedPath)) {
matchedPath = sourcePath;
matchedPath = matchPartial(sourcePath, pattern);
value = getParamsFnValue(routes[pattern], params);

@@ -154,0 +167,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});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.apply(null,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.apply(null,params)}function splitPath(path){var pathParts=path.split("/");if(pathParts[pathParts.length-1]===""){pathParts.pop()}return pathParts}function matchPartial(sourcePath,pattern){var patternParts=splitPath(pattern);var sourceParts=splitPath(sourcePath);var matchedParts=[];for(var i=0;i<patternParts.length;++i){matchedParts.push(sourceParts[i])}return matchedParts.join("/")}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=matchPartial(sourcePath,pattern);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)});

@@ -89,2 +89,15 @@ "use strict";

function matchPartial(sourcePath, pattern) {
var patternParts = splitPath(pattern);
var sourceParts = splitPath(sourcePath);
var matchedParts = [];
for (var i = 0; i < patternParts.length; ++i) {
matchedParts.push(sourceParts[i]);
}
return matchedParts.join("/");
}
function validatePath(sourcePath, matchedPath) {

@@ -150,3 +163,3 @@ if (matchedPath === null) {

if (params.length > 0 && betterMatch(sourcePath, matchedPath)) {
matchedPath = sourcePath;
matchedPath = matchPartial(sourcePath, pattern);
value = getParamsFnValue(routes[pattern], params);

@@ -153,0 +166,0 @@ }

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

@@ -10,3 +10,3 @@ "main": "lib/index.js",

"type": "git",
"url": "https://github.com/cyclejs/switch-path"
"url": "https://github.com/staltz/switch-path"
},

@@ -13,0 +13,0 @@ "devDependencies": {

@@ -5,2 +5,4 @@ # `switchPath`

[![npm version](https://badge.fury.io/js/switch-path.svg)](https://badge.fury.io/js/switch-path)
```

@@ -7,0 +9,0 @@ npm install switch-path

@@ -86,2 +86,15 @@ function isPattern(candidate) {

function matchPartial(sourcePath, pattern) {
const patternParts = splitPath(pattern)
const sourceParts = splitPath(sourcePath)
const matchedParts = []
for (let i = 0; i < patternParts.length; ++i) {
matchedParts.push(sourceParts[i])
}
return matchedParts.join(`/`)
}
function validatePath(sourcePath, matchedPath) {

@@ -140,3 +153,3 @@ if (matchedPath === null) {

if (params.length > 0 && betterMatch(sourcePath, matchedPath)) {
matchedPath = sourcePath
matchedPath = matchPartial(sourcePath, pattern)
value = getParamsFnValue(routes[pattern], params)

@@ -143,0 +156,0 @@ }

@@ -196,2 +196,22 @@ /* global describe, it */

});
it('should partially match :key type params', () => {
const {path, value} = switchPath('/home/123/456', {
'/': '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');
});
it('should partially match multiple :key type params', () => {
const {path, value} = switchPath('/home/123/456/something', {
'/': 'root',
'/home/:id/:other': (id, other) => `${id}:${other}`,
'/external/:id/:other': (id, other) => `external`,
});
expect(path).to.be.equal('/home/123/456')
expect(value).to.be.equal('123:456')
})
});
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