Coder Social home page Coder Social logo

urlresolve's Introduction

urlresolve travis_status

Django-style URL resolver for browsers and Node.js - resolves a path against a list of patterns, gives you back the handler you configured and any captured parameters, then backs away slowly.

Browsers:

Node.js:

npm install urlresolve

Basic Usage

Declare some URL patterns:

var url = urlresolve.url

var Views = {
  index: function() { }
, list: function() { }
, detail: function(id) { }
}

var urlpatterns = urlresolve.patterns(Views
, url('',             'index',  'index')
, url('things/',      'list',   'thing_list')
, url('things/:id/'), 'detail', 'thing_detail')
)

Create a resolver to make use them:

var resolver = urlresolve.getResolver(urlpatterns)

Resolve some paths:

var match = resolver.resolve('/')
// match.func is Views.index, bound to Views
// match.urlName is 'index'

match = resolver.resolve('/things/123/')
// match.func is Views.detail, bound to Views
// match.args is ['123']
// match.urlName is 'thing_detail'

try {
  resolver.resolve('/notthere/')
}
catch (e) {
  // e is a urlresolve.Resolver404 and contains details of the patterns
  // it tried to match against
}

Decouple your URLs by naming them and constructing URLs names and arguments:

resolver.reverse('index')               // '/'
resolver.reverse('thing_list')          // '/things/'
resolver.reverse('thing_detail', [123]) // '/things/123/'

try {
  resolver.reverse('blah')
}
catch (e) {
  // e is a urlresolve.NoReverseMatch
}

Creating URL Patterns

urlresolve.patterns(context, pattern1[, pattern2, ...])

Creates a list of URL patterns, which can be specified using the url function or a list of [pattern, handler, urlName].

Handler properties can be specified as strings to be looked up from a context object, which should be passed as the first argument in that case, otherwise it should be null or falsy.

urlresolve.url(pattern, view, urlName)

Creates a URL pattern or roots a list of patterns to the given pattern if view is a list of URL patterns.

The URL name is used in reverse URL lookups and should be unique.

Patterns:

  • Should not start with a leading slash, but should end with a trailing slash if being used to root other patterns, otherwise to your own taste.

  • Can identify named parameters to be extracted from resolved URLS using a leading ":", e.g.:

    widgets/:id/edit/
    

Using URL Patterns

urlresolve.getResolver(patterns)

Creates a urlresolve.URLResolver with the given URL patterns and provides an interface for using it repeatedly.

Returns an Object with the following API:

patterns
The URL patterns which were passed into getResolver.
resolver
The root URLResolver created with the given URL patterns.
resolve(path)
Resolves the given URL path, returning an object with func, args and urlName properties if successful, otherwise throwing a Resolver404 error.
reverse(urlName[, args])
Reverse-resolves the given named URL, with the given Array of args if provided, returning a URL string if successful, otherwise throwing a NoReverseMatch error.

MIT License

Copyright (c) 2011, Jonathan Buchanan

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

urlresolve's People

Contributors

insin avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.