Skip to content

Instantly share code, notes, and snippets.

@thejhh
Last active December 10, 2015 00:39
Show Gist options
  • Select an option

  • Save thejhh/4353238 to your computer and use it in GitHub Desktop.

Select an option

Save thejhh/4353238 to your computer and use it in GitHub Desktop.
URI implementation
var uri_builder = require('./uri.js');
var service_uri = uri_builder('http://api.example.com');
var collection_uri = service_uri('/users');
console.log(require('url').format(collection_uri));
/** Returns an URI parser as a function.
* @param value string|object|function URI to parse
* @returns New function([value]) which returns the URI parsed into an
* object. You can change the returned object by providing changes in
* an optional value argument. Value can be string, object or
* URI parser as a function.
*/
function uri_builder(value) {
var uri;
function do_parse(v) {
if(v && (typeof v === 'function')) {
return v();
} else {
return (v && (typeof v === 'object')) ? v : require('url').parse(v);
}
}
uri = function do_uri(value2) {
if(value2 === undefined) return do_parse(value);
var opts = do_parse(value), opts2 = do_parse(value2);
Object.keys(opts2).map(function(key) {
opts[key] = opts2[key];
});
return opts;
};
return uri;
}
module.exports = uri_builder;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment