Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

martinetto

jliuhtonen22MITdeprecated1.1.3

This library is deprecated and no longer supported, see https://github.com/jliuhtonen/refractive for a new version

A modular, functional JS route parser micro library

route, parser, micro-library

readme

martinetto.js

Build Status

Martinetto.js is a micro-library for parsing and matching relative url paths or routes. It supports named path parameters and multiple wildcards. It also parses the fragment and query parts for matched routes.

Usage

> const Martinetto = require('./dist/martinetto')
> const routeMatcher = Martinetto.routing([
  { 
    route: '/artists/:name/album/:albumName', 
    fn: (routeData) => {
      console.log(routeData)
      return 'Album page'
    } 
  },
  { 
    route: '/artists/:name/*', 
    fn: (routeData) =>  {
      console.log(routeData)
      return 'Artist page'
    } 
  }
])
> routeMatcher('/artists/Aphex%20Twin/album/Syro')
{ path: '/artists/Aphex%20Twin/album/Syro',
  fragment: '',
  queryParams: {},
  pathParams: { name: 'Aphex Twin', albumName: 'Syro' },
  wildcards: [] }
'Album page'
> routeMatcher('/artists/Aphex%20Twin/gigs')
{ path: '/artists/Aphex%20Twin/gigs',
  fragment: '',
  queryParams: {},
  pathParams: { name: 'Aphex Twin' },
  wildcards: [ 'gigs' ] }
'Artist page'