GithubHelp home page GithubHelp logo

pathbuilderjs's Introduction

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.

Set value to false to remove key.

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.

Note: If you set a prefix the path must contain it, otherwise an error will be thrown.

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

// This will throw an error
pb.prefix('/search/')
pb.path('/first/the-first/second/the-second/');

.toObject()

Get the current path as an object.

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

pathbuilderjs's People

Contributors

benconstable avatar

Stargazers

 avatar

Watchers

 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.