0.2.1 • Published 11 years ago

pathbuilder v0.2.1

Weekly downloads
16
License
-
Repository
github
Last release
11 years ago

pathbuilderjs

Easily modify parameters in paths.

This simple library allows you to work easily with parameters in routing frameworks like Backbone.js and pathjs, although it can be used for setting parameters in any delimiter-separated string containing parameter-value pairs.

Installation

In the browser:

<script src="dist/pathbuilder.min.js"></script> 

Note: This library requires Array.indexOf, so you'll need a polyfill for browsers without ES5 support.

On the server:

npm install pathbuilder

Usage

var pb = new PathBuilder();

var path = pb
    .prefix('/search/') // Parameters will be parsed after this
    .path('/search/query/hello+there/categories/hairy/')
    .param('query', 'hello+there+big+and')
    .param('filter', 'old')
    .path();

console.log(path);

// "/search/query/hello+there+big+and/categories/hairy/filter/old/"

Or on the server:

var pb = require('pathbuilder');

// ...

API

.delimiter(delimiter)

Set or get the path delimiter. This is / by default.

Note: Setting the delimiter will reset the path.

pb.delimiter("'");
pb.delimiter(); // "'"

.ordering(ordering)

Set or get the current custom ordering.

This should be an array of parameter names in the order you'd like them to appear in the resulting path.

pb.ordering(['categories', 'query']);
pb.ordering(); // ['categories', 'query']

pb.prefix('/search/');
pb.path('/search/query/hello+there/categories/hairy/');
pb.path(); // "/search/categories/hairy/query/hello+there/"

.param(key, value)

Set or get a parameter in the path.

pb.param('first', 'the-first');
pb.param('second', 'the-second');

pb.param('first'); // "the-first"
pb.param('second'); // "the-second"

pb.path(); // "/first/the-first/second/the-second"

.path(path)

Set or get the current path.

pb.path('/query/hello+there/categories/hairy/');
pb.path(); // "/query/hello+there/categories/hairy/"

.prefix(prefix)

Set or get the path prefix. Anything after the .prefix() will be considered to be the parameters.

pb.prefix('/search/');
pb.prefix(); // "/search/"

.toObject()

Get the current path as an object.

pb.path('/query/hello+there/categories/hairy/');
pb.toObject(); // { "query": "hello+there", "categories": "hairy" }
0.2.1

11 years ago

0.2.0

11 years ago

0.1.0

11 years ago