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.
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.
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.