urlobj v0.0.11
urlobj

Performant utilities for URL resolution and parsing built on core url.
This module provides many tools for working with Objects parsed with url.parse(). It also performs faster because it avoids the need to constantly reparse URL Strings during multiple operations.
Constants
component
URL components used in comparison operations.
component.NOTHINGcomponent.PROTOCOLorcomponents.SCHEMEcomponent.TLDcomponent.DOMAINcomponent.SUB_DOMAINcomponent.HOSTNAMEcomponent.PORTcomponent.HOSTcomponent.AUTHcomponent.DIRECTORYcomponent.FILENAMEcomponent.PATHNAMEcomponent.QUERYorcomponents.SEARCHcomponent.PATHcomponent.HASHorcomponents.FRAGMENT
HOST PATH
___|___ _______|______
/ \ / \
AUTH HOSTNAME PORT PATHNAME QUERY HASH
_______|_______ ______|______ | __________|_________ ____|____ |
/ \ / \ / \ / \ / \ / \
foo://username:password@www.example.com:123/hello/world/there.html?name=ferret#foo
\_/ \_/ \_____/ \_/ \_________/ \________/
| | | | | |
PROTOCOL SUB_DOMAIN | TLD DIRECTORY FILENAME
|
DOMAINNote: there are a few breaks in the linearity of these values:
AUTHis prioritized afterHOSTbecause matching authentication on a different domain is pointlessTLDis prioritized beforeDOMAINbecause matching a domain on a different top-level domain is pointlessSUB_DOMAINis prioritized afterDOMAIN
type
URL types used for discerning input.
type.UNKNOWNtype.ABSOLUTEtype.PROTOCOL_RELATIVEtype.ROOT_RELATIVEtype.DIRECTORY_RELATIVEtype.FILENAME_RELATIVEtype.QUERY_RELATIVEtype.EMPTYtype.HASH_RELATIVE
Functions
The following methods will accept URLs as Strings and/or Objects.
format(urlObj)
Converts a URL Object to a formatted URL String. Is merely an alias to core url.format().
minify(url, options)
Normalizes and minifies a URL with the following options:
clone; when set totrue, the function will return a copy ofurlinstead of mutating the original.defaultPorts; a map of default ports for various protocols. Default value:{ftp:21, gopher:70, http:80, https:443}.directoryIndexes; a list of filenames that are expected to be treated as directory indexes. Default value:["index.html"].removeAuth; when set totrue, it will remove authentication information. Default value:false.removeDefaultPorts; when set totrue, it will remove ports that match any found indefaultPorts. Default value:true.removeDirectoryIndexes; when set totrue, it will remove filenames that match any found indirectoryIndexes. Default value:true.removeEmptyQueries; when set totrue, it will remove empty query data such as"?","?var="and"&=". Default value:false.removeRootTrailingSlash; when set totrue, it will remove trailing slashes such as"http://domain.com/?var". Default value:true.
If url is an Object, it will be mutated/modified.
normalize(url, options)
Resolves dot segments ("../", "./") in a URL's path, removes port if it is default and removes empty queries ("path/?").
Options:
defaultPorts; a map of default ports for various protocols. Default value:{ftp:21, gopher:70, http:80, https:443}.slashesDenoteHost; when set totrue, it will parse"//domain.com/"as a URL instead of a path. Default value:false.
If url is an Object, it will be mutated/modified.
parse(url, parseQueryString, slashesDenoteHost)
parse(url, options)
Parses (or re-parses) a URL into an Object containing its URL components with the following options:
defaultPorts; a map of default ports for various protocols. Default value:{ftp:21, gopher:70, http:80, https:443}.directoryIndexes; a list of filenames that are expected to be treated as directory indexes. Default value:["index.html"].parseQueryString; when set totrue, it will parse the query string into an object. Default value:false.slashesDenoteHost; when set totrue, it will parse"//domain.com/"as a URL instead of a path. Default value:false.
If url is an Object, it will be mutated/modified.
relation(url1, url2)
Returns a Number defining the relation between two URLs. That number corresponds to the value of a URL component in components.
Because the value returned is a Number, more complex comparisons are possible:
var relation = urlobj.relation(url1, url2);
if (relation >= urlobj.components.HOST) {
console.log("same server!");
}resolve(from, to, options)
Resolves a URL with a base URL like a browser would for an anchor tag. If to is an Object, it will be mutated/modified.
Options:
defaultPorts; a map of default ports for various protocols. Default value:{ftp:21, gopher:70, http:80, https:443}.ignoreWww; when set totrue, it will treat"www.domain.com"and"domain.com"as the same host. Default value:false.
Exposed Internal Methods
areSameDir(dirArray1, leadingSlash1, dirArray2, leadingSlash2)
Compares two directory Arrays to see if their paths are the same. leadingSlash1 and leadingSlash2 denote that the corresponding path is absolute and not relative. Input should first be normalized.
areSameQuery(queryObj1, queryObj2)
Compares two query Objects to see if their data is the same. Order does not matter.
joinDirs(dirArray, leadingSlash)
Joins all directories of a directory Array into a String. leadingSlash denotes that the path is absolute and not relative.
joinQuery(queryObj, skipEmpties)
Joins all keys of an Object into a query String.
When skipEmpties is true, empty query data such as "?var=" and "&=" will be excluded. Its default value is false.
normalizeDirs(dirArray, leadingSlash)
Resolves dot segments ("../", "./") in a directory Array and returns a new Array (within an Object). leadingSlash denotes that the path is absolute and not relative. This method will attempt to resolve to a root. If none is found, the parent-most dot segment will remain.
Examples using Strings instead of Arrays:
- Turns
"/dir1/dir2/../"into"/dir1/" - Turns
"dir/../"into"" - Turns
"/../dir/"into"/dir/" - Leaves
"../dir/"untouched - Leaves
"../../dir/"untouched
parsePath(pathString)
Parses a path String into an Object containing a directory Array and a filename String.
resolveDirs(fromDirArray, fromLeadingSlash, toDirArray, toLeadingSlash)
Resolves a base directory Array to another directory Array and returns a new, normalized Array (within an Object). fromLeadingSlash and toLeadingSlash denote that the corresponding path is absolute and not relative.
Roadmap
- Use whatwg-url package: more info