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

switch case for URLs, a small tool for routing in JavaScript

  • 1.1.6
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
213
decreased by-17.76%
Maintainers
1
Weekly downloads
 
Created
Source

switchPath

Advanced switch case for URLs, a small tool for routing in JavaScript

npm version

npm install switch-path

Usage

Basic usage:

const {path, value} = switchPath('/home/foo', {
  '/bar': 123,
  '/home/foo': 456,
});
// path is `/home/foo`
// value is 456

Supports trailing slashes

const {path, value} = switchPath('/home/foo/', {
  '/bar': 123,
  '/home/foo': 456,
});
// path is `/home/foo`
// value is 456

Supports nested route configuration:

const {path, value} = switchPath('/home/foo', {
  '/bar': 123,
  '/home': {
    '/foo': 456,
  },
});
// path is `/home/foo`
// value is 456

Supports base paths in nested route configurations

const {path, value} = switchPath('/home', {
  '/bar': 123,
  '/home': {
    '/': 456,
    '/foo': 789
  }
});
// path is `/home`
// value is 456

Incomplete patterns will be optimistically matched:

const {path, value} = switchPath('/home/foo', {
  '/bar': 123,
  '/home': 456,
});
// path is `/home`
// value is 456

Optional not found pattern:

const {path, value} = switchPath('/home/33', {
  '/': 123,
  'books': {
    '/': 456,
    '/:id': 789
  },
  '*': 'Route not found'
});
// path = '/home/33'
// value = 'Route not found'

Match a route with :param parameters and get the parameter value in a function:

const {path, value} = switchPath('/home/1736', {
  '/bar': 123,
  '/home/:id': id => `id is ${id}`,
});
// path is `/home/1736`
// value is 'id is 1736'

Match a route with :param parameters also inside nested configurations:

const {path, value} = switchPath('/home/1736', {
  '/bar': 123,
  '/home': {
    '/:id': id => `id is ${id}`,
  },
});
// path is `/home/1736`
// value is 'id is 1736'

Match a route with :param parameters base inside nested configurations:

const {path, value} = switchPath('/1736', {
  '/bar': 123,
  '/:id': {
    '/': id => `id is ${id}`,
    '/home': 789
  }
});
// path is `/1736`
// value is 'id is 1736'

FAQs

Package last updated on 18 Apr 2016

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

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