url2obj
v0.1.6
Published
It will take a URL and the object keys as arguments and convert it to an object. It support query params by using the names as obj keys.
Downloads
6
Maintainers
Readme
URL2OBJ
It will take a URL and the object keys as arguments and convert it to an object. You can pass an array of keys or just a string with the url format. It support query params by using the names as obj keys.
url2obj( url, object keys, omit certain keys );
url2obj( url, format url string);
Install
$ npm install --save url2obj
Usage
first argument is the url that you want to convert, then you can pass an array of keys and then an array of the keys that you want to omit on your obj
var url2obj = require('url2obj');
var obj1 = url2obj( '/4/api/users/2', ['version', 'api', 'collection', 'id'], ['api'] );
// This will return an object like this:
{
version: "4",
collection: "users",
id: "2"
}
var obj2 = url2obj( '/4/api/users/2?sort=desc&limit=10', ['version', 'api', 'collection', 'id'], ['api'] );
// If you have query params on your url it will use the names as keys:
{
version: "4",
collection: "users",
id: "2",
sort: "desc",
limit: "10"
}
you have the option to pass only two arguments being the first one the url and the second one the format of your url, it will take the dynamic params as keys and omit the non dynamic ones.
var url2obj = require('url2obj');
var obj1 = url2obj( '/6/api/listings/3', "/:version/api/:collection/:id" );
// This will return an object like this:
{
version: "4",
collection: "users",
id: "2"
}
var obj2 = url2obj( '/6/api/listings/3?sort=desc&limit=10', "/:version/api/:collection/:id" );
// If you have query params on your url it will use the names as keys:
{
version: "4",
collection: "users",
id: "2",
sort: "desc",
limit: "10"
}
# creates a browser.js
$ npm run browser
License
MIT © Sebastian Baroni