Last active
December 10, 2015 00:39
-
-
Save thejhh/4353238 to your computer and use it in GitHub Desktop.
URI implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** 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