Urlgrey is a library for url manipulation. It's got a chainable/fluent interface
that makes a number of methods available for querying different aspects of a url,
and even modifying it to create new urls.
Most methods are named after different parts of the url and allow you to read that part from the
current url if you don't pass any parameters, or they allow you to generate a new url with a
change to that part in the current url if you do pass a parameter.
For the examples below, we'll use the following url:
https://user:pass@subdomain.asdf.com/path/kid?asdf=1234#frag
To create a new urlgrey object, just pass a url to urlgrey like so:
var url = urlgrey("https://user:pass@subdomain.asdf.com/path/kid?asdf=1234#frag")
##API specifics:
###url.child([lastPart])
Setter/getter for the last part of a path:
url.child();
url.child("grandkid");
###url.decode(encodedString);
Returns the decoded version of the input string using node's standard querystring.unescape().
url.decode('this%20is%20a%20test');
###url.encode(unencodedString);
Returns the encoded version of the input string using node's standard querystring.escape().
url.encode('this is a test');
###url.hash([newHash])
Setter/getter for the url fragment/anchor/hash of a path.
url.hash();
url.hash("blah");
###url.hostname([newHostname])
Setter/getter for the url hostname.
url.hostname();
url.hostname("geocities.com");
###url.parent();
Get the parent URI of the current URI. (This property is read-only).
url.parent();
###url.password([newPassword]);
Setter/getter for the password portion of the url.
url.password();
url.password("newpass");
###url.extendedPath([string]);
Setter/getter for the path, querystring and fragment portion of the url
all at once.
url.extendedPath();
url.extendedPath("/newpath?new=query#newfrag");
###url.path([mixed]);
Setter/getter for the path portion of the url.
url.path();
url.path("newpath");
url.path(['qwer', '/asdf'], 'qwer/1234/', '/1234/');
Note: changing the path will remove the querystring and hash, since they rarely make sense on a new path.
###url.port([newPort]);
Setter/getter for the port portion of the url.
url.port();
url.port(8080);
###url.protocol([newProtocol]);
Setter/getter for the protocol portion of the url.
url.protocol();
url.protocol("http");
###url.query([mixed]);
Setter/getter for the querystring using javascript objects.
url.query();
url.query(false);
url.query({spaced : 'space test'})
NOTE: escaping and unescaping of applicable characters happens automatically. (eg " " to "%20", and vice versa)
NOTE: an input object will overwrite an existing querystring where they have the same names.
NOTE: an input object will remove an existing name-value pair where they have the same names and the value in the input name-value pair is null.
###url.queryString([newQueryString]);
Setter/getter for the querystring using a plain string representation. This is lower-level than .query(), but allows complete control of the querystring.
url.queryString();
url.queryString("blah");
NOTE: no escaping/unescaping of applicable characters will occur. This must be done manually.
###url.rawChild();
This method is the same as url.child() but does not automatically url-encode
any part of the input.
###url.rawPath();
This method is the same as url.path() but does not automatically url-encode
any part of the path.
###url.rawQuery();
This method is the same as url.query() but does not automatically url-encode
any of the keys or values in an input object.
###url.toJson();
Returns the json representation of the uri object, which is simply the uri as a string. The output is exactly the same as .toString(). This method is read-only.
url.toJson();
###url.toString();
Returns the string representation of the uri object, which is simply the uri as a string. This method is read-only.
url.toString();
###url.username([newUsername])
Setter/getter for the username portion of the url.
url.username();
url.username("newuser");
##Installation:
node.js:
npm install urlgrey --save
Also! If you're using urlgrey in an http application, see urlgrey-connect. It gives you an urlgrey object already instantiated with the request url as req.uri in all your request handlers.
in the browser:
Lots of options:
- grab urlgrey.js from the root of this repo for browserify-built, unminified version.
- grab urlgrey.min.js from the root of this repo for a browserify-built, minified version.
- use browserify and include this like any other node package.
##Contributing:
###Testing:
####Run the node tests:
####Run the browser file:// tests:
make browser-build
- ...then open test.html in a browser
####Run the browser tests on a real server:
###Building before committing
###Running node tests with a coverage report