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.
URL components used in comparison operations.
component.NOTHING
component.PROTOCOL
orcomponents.SCHEME
component.TLD
component.DOMAIN
component.SUB_DOMAIN
component.HOSTNAME
component.PORT
component.HOST
component.AUTH
component.DIRECTORY
component.FILENAME
component.PATHNAME
component.QUERY
orcomponents.SEARCH
component.PATH
component.HASH
orcomponents.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
|
DOMAIN
Note: there are a few breaks in the linearity of these values:
AUTH
is prioritized afterHOST
because matching authentication on a different domain is pointlessTLD
is prioritized beforeDOMAIN
because matching a domain on a different top-level domain is pointlessSUB_DOMAIN
is prioritized afterDOMAIN
URL types used for discerning input.
type.UNKNOWN
type.ABSOLUTE
type.PROTOCOL_RELATIVE
type.ROOT_RELATIVE
type.DIRECTORY_RELATIVE
type.FILENAME_RELATIVE
type.QUERY_RELATIVE
type.EMPTY
type.HASH_RELATIVE
The following methods will accept URLs as Strings and/or Objects.
Converts a URL Object
to a formatted URL String
. Is merely an alias to core url.format()
.
Normalizes and minifies a URL with the following options:
clone
; when set totrue
, the function will return a copy ofurl
instead 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.
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.
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.
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!");
}
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
.
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.
Compares two query Objects to see if their data is the same. Order does not matter.
Joins all directories of a directory Array into a String. leadingSlash
denotes that the path is absolute and not relative.
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
.
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
Parses a path String into an Object containing a directory Array and a filename String.
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.
- Use whatwg-url package: more info