Dentist.js

Dentist JS helps extract out those darn parameters of an URL! Useful with Google Docs URL and stuff!

Build Status

Dentist JS

TL;DR

Working with URLs or jQuery's serialize() method? Call on Dentist JS to extract those parameters! Dentist JS extracts anything as long as they are strings!

EXAMPLE:

// document.URL == "http://helloworld.com/quotes?id=1337&author=kelvin&message=hello"
var currentURL = document.URL;
var params = currentURL.extract();
console.log(params.id); // 1337
console.log(params.author) // "kelvin"
console.log(params.message) // "hello"

Dentist JS is smart enough to know when to return a string or integer instead too!

WHY

Often, we have systems that require the user to enter url addresses. These scenarios include (inexhaustive):

Usually, these URLs include parameters that can be valuable or essential for the developer. Sadly, Javascript does not come with a method that allows us to pull out these parameters.

Also, instead of using jQuery's seralizeArray() (when dealing with AJAX and forms), we can now use Dentist JS to get to those parameters more efficiently ( O(1), rather than O(n))

HOW

Example, a form (id: form) contains two inputs : {name: 'username', value; "nyancat"} and {name: 'password', value: "nyannyan"}

$('#form').submit(function(e){
    e.preventDefault();
    var s = $(this).serialize(); // "username=nyancat&password=nyannyan"
    $(this).serializeArray(); // [{name: 'username', value; "nyancat"}, {name: 'password', value: "nyannyan"}]
    var formParams = s.extract() // {username: 'nyancat', password: 'nyannyan'}
    formParams.username; // 'nyancat'
    formParams.password; // 'nyannyan'
});

API Usage

Dentist JS allows the following options when extracting.

name description default
delimiter char or string which separates each parameter '&'
keyValueSeparator char or string which connects the key and value in each parameter '='
startAfter tells Dentist JS to only consider extract after the first occurance of this char or string '?'
limit an integer N, where Dentist JS only returns the first N parameters it can find Infinity

Examples:

var str = "helloworld>a-1|b-2|c-3";
var params = str.extract({startAfter: ">", delimiter: '|', keyValueSeparator: '-', limit: 2});
params.a; // 1
params.b; // 2
params.c; // undefined since limit at 2

More usage examples can be found under tests/test.coffee

Dentist JS uses: