All Projects → cerebral → Url Mapper

cerebral / Url Mapper

Licence: mit
Take a URL and map to functions, parsing params

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Url Mapper

Ffrouter
Powerful and easy-to-use URL routing library in iOS that supports URL Rewrite(强大、易用、支持 URL Rewrite的 iOS 路由库)
Stars: ✭ 263 (+574.36%)
Mutual labels:  router, routing
Cortex
Routing system for WordPress
Stars: ✭ 300 (+669.23%)
Mutual labels:  router, routing
Simple Php Router
Simple, fast and yet powerful PHP router that is easy to get integrated and in any project. Heavily inspired by the way Laravel handles routing, with both simplicity and expand-ability in mind.
Stars: ✭ 279 (+615.38%)
Mutual labels:  router, routing
react-mobx-router5
React components for routing solution using router5 and mobx
Stars: ✭ 58 (+48.72%)
Mutual labels:  router, routing
Micro Router
🚉 A tiny and functional router for Zeit's Micro
Stars: ✭ 621 (+1492.31%)
Mutual labels:  router, routing
yew-router
Router extension to yew
Stars: ✭ 27 (-30.77%)
Mutual labels:  router, routing
Fluro
Fluro is a Flutter routing library that adds flexible routing options like wildcards, named parameters and clear route definitions.
Stars: ✭ 3,372 (+8546.15%)
Mutual labels:  router, routing
OpenBSDFirewall
Simple OpenBSD Home Firewall Config for ALIX Board
Stars: ✭ 41 (+5.13%)
Mutual labels:  router, routing
Router
🛣 Simple Navigation for iOS
Stars: ✭ 438 (+1023.08%)
Mutual labels:  router, routing
Graphpath
Graphpath generates an ASCII network diagram from the route table of a Unix/Linux
Stars: ✭ 321 (+723.08%)
Mutual labels:  router, routing
Routing
The Routing component maps an HTTP request to a set of configuration variables.
Stars: ✭ 7,080 (+18053.85%)
Mutual labels:  router, routing
Bidi
Bidirectional URI routing
Stars: ✭ 941 (+2312.82%)
Mutual labels:  router, routing
r5r
ipeagit.github.io/r5r/
Stars: ✭ 90 (+130.77%)
Mutual labels:  router, routing
gatsby-plugin-dynamic-routes
Creating dynamic routes based on your environment and/or renaming existing routes
Stars: ✭ 14 (-64.1%)
Mutual labels:  router, routing
go router
The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
Stars: ✭ 380 (+874.36%)
Mutual labels:  router, routing
Abstract State Router
Like ui-router, but without all the Angular. The best way to structure a single-page webapp.
Stars: ✭ 288 (+638.46%)
Mutual labels:  router, routing
router
Bidirectional Ring router. REST oriented. Rails inspired.
Stars: ✭ 78 (+100%)
Mutual labels:  router, routing
CRRouter
A simple and powerful router
Stars: ✭ 54 (+38.46%)
Mutual labels:  router, routing
Freerouting
Advanced PCB autorouter (finally, no Java installation required)
Stars: ✭ 307 (+687.18%)
Mutual labels:  router, routing
Storeon Async Router
Asynchronous router for Storeon. It provides possibility for prefetch the data, lazy load, navigation cancellation, and routes modification on the fly.
Stars: ✭ 22 (-43.59%)
Mutual labels:  router, routing

url-mapper

Two way URL <==> route(params) converter with mapper.

NPM version Build status Test coverage bitHound Score Commitizen friendly Semantic Release js-standard-style

Installation

npm install url-mapper --save

Usage

Overview

The main purpose of url-mapper is to match given URL to one of the routes. It will return the matched route (key and associated value) and parsed parameters. You can associate anything you want with route: function, React component or just plain object.

url-mapper is helpful when creating router packages for frameworks or can be used as router itself. It allows you to outsource working with a url (mapping, parsing, stringifying) and concentrate on wiring up things related to your favorite framework.

Example

import React from 'react'
import ReactDOM from 'react-dom'
import Mapper from 'url-mapper'
import { CoreApp, ComponentA, ComponentB, Component404 } from './components'

const urlMapper = Mapper()

var matchedRoute = urlMapper.map('/bar/baz/:42', { // routable part of url
  '/foo/:id': ComponentA,
  '/bar/:list/:itemId': ComponentB,
  '*': Component404
})

if (matchedRoute) {
  const Component = matchedRoute.match // ComponentB
  const props = matchedRoute.values // { list: 'baz', itemId: 42 }

  ReactDOM.render(
    <CoreApp>
      <Component {...props} />
    </CoreApp>
  )
}

See @cerebral/router as an example of building your own router solution on top of url-mapper. Also see example at Runkit Sandbox to try it right in your browser.

API

Main module

At top level the url-mapper module exports a factory which returns default implementation of an URL <==> route(params) converter.

Factory

Usage
var urlMapper = require('url-mapper')
var mapper = urlMapper(options)
Arguments
Param Type Details
options Object Options passed to converter.
Property Type Details
query Boolean Enables converting values not defined in route as query in URL Object Notation
querySeparator String String used to separate query from routable part. Default '?'.
Returns

Object - Object with parse, stringify and map methods.

Returned methods deals with Express-style route definitions and cleaned routable part of url (without origin, base path, leading hash symbol).

Params defined in route are mapped to the same named properties in the values Object with help of path-to-regexp module. It is safe to pass Numbers and Booleans as well as Strings as path parameteres. The original type would be preserved while parsing back stringified one.

By default, the query part is ignored. Query part params are mapped to the same named properties in values Object if { query: true } option was passed to factory. Conversion of the query part is made with help of URLON module. Therefore, it can accept any JSON serializable value.

Hash part is ignored at all if any present. You still can manage your routes in location.hash but don't provide # symbol before routable part.

parse method

Usage

mapper.parse(route, url)

Arguments
Param Type Details
route String Express style route definition
url String Routable part of url
Returns

Object - values parsed from url with given route.

Path parsed using path-to-regexp module, tweaked to support Boolean and Number. Query part parsed with URLON module if { query: true } option was passed to factory.

stringify method

Usage

mapper.stringify(route, values)

Arguments
Param Type Details
route String Express style route definition
values Object Object used to populate parameters in route definition
Returns

String - values stringified to url with given route.

Properties defined in route are stringified to path part using path-to-regexp module, tweaked to support Boolean and Number. Properties not defined in route are stringified to query part using URLON module if { query: true } option was passed to factory.

map method

Usage

mapper.map(url, routes)

Arguments
Param Type Details
url String Routable part of url
routes Object Routes to map url with
Returns

Object - Object representing matched route with properties:

Property Type Details
route String Matched route defined as key in routes
match Any Value from routes associated with matched route
values Object Values parsed from given url with matched route

Matcher

Custom converting algoritms could be implemented by providing a custom compile function. If you don't like default route definition format or converting algorithms, feel free to make your own.

Factory

Usage
var urlMapper = require('url-mapper/mapper')
var mapper = urlMapper(compileFn, options)
Arguments
Param Type Details
compileFn Function Function used by mapper to "compile" a route.
options Any Optional. Passed to compileFn as second argument.

For each route mapper would call compileFn(route, options) and cache result internally. compileFn should return parse(url) and stringify(values) methods for any given route. See default implementation for reference.

Returns

Object - Object with parse(route, url), stringify(route, values) and map(url, routes) methods.

These methods will use cached methods returned by compileFn for given routes.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].