46 lines
934 B
JavaScript
46 lines
934 B
JavaScript
var extend = require('util')._extend;
|
|
|
|
|
|
/**
|
|
* @class URL
|
|
* Utility class for formatting URLs.
|
|
*/
|
|
var URL = function(url, params) {
|
|
this.url = url;
|
|
this.params = params;
|
|
};
|
|
|
|
|
|
/**
|
|
* Format the URL as a string.
|
|
*
|
|
* @return {string} The formatted URL including the querystring.
|
|
*/
|
|
URL.prototype.toString = function() {
|
|
var url = this.url;
|
|
|
|
// If there are no params, simply return the base URL.
|
|
if (typeof params !== 'object') return url;
|
|
|
|
// Replace all the :params with the actual value.
|
|
for (var key in params) {
|
|
if (url.indexOf(':' + key) > -1) {
|
|
url = url.replace(':' + key, params[key]);
|
|
delete params[key];
|
|
}
|
|
}
|
|
|
|
// Remove all the remainign :params which weren't replaced by any value.
|
|
url = url.replace(/\/:[^\/]+/g, '');
|
|
|
|
// Add the querystring,
|
|
var querystring = Object.keys(params).map(function (key) {
|
|
return key + '=' +
|
|
})
|
|
|
|
|
|
return url;
|
|
};
|
|
|
|
|
|
module.exports = URL;
|