URL handling utlity functions

decodeUrlForDisplay
deleteQuery
getAbsoluteURL
getDownloadFilenameForUrl
getUrlParent
getUrlRoot
graftUrlPath
incrementUrl
interpolateSearchItem
replaceQueryValue
searchUrlToArgs
setQueryValue
  • Parameters

    • url: string

    Returns string

  • Delete a query (and its value) in a URL

    If a query appears multiple times (which is a bit odd), all instances are removed

    Parameters

    • url: URL

      the URL to act on

    • matchQuery: string

      the query to delete

    Returns URL

    the modified URL

  • Parameters

    • url: string

      May be either an absolute or a relative URL.

    • baseURI: string = document.baseURI

      The URL the absolute URL should be relative to. This is usually the URL of the current page.

    Returns string

  • Get a suitable default filename for a given URL

    If the URL:

    • is a data URL, construct from the data and mimetype
    • has a path, use the last part of that (eg image.png, index.html)
    • otherwise, use the hostname of the URL
    • if that fails, "download"

    Parameters

    • url: URL

      the URL to make a filename for

    Returns string

    the filename according to the above rules

  • Get the parent of the current URL. Parent is determined as:

    • if there is a hash fragment and option.ignoreFragment is falsy, strip that, or
    • If there is a query string and option.ignoreSearch is falsy, strip that, or
    • If option.ignorePathRegExp match the path, strip that and
    • Remove one level from the path if there is one, or
    • Remove one subdomain from the front if there is one

    Parameters

    • url: any

      the URL to get the parent of

    • option: any

      removal option. Boolean properties: trailingSlash, ignoreFragment and ignoreSearch. Regular Expression properties: ignorePathRegExp. All properties are optional.

    • count: number = 1

      how many "generations" you wish to go back (1 = parent, 2 = grandparent, etc.)

    Returns any

    the parent of the URL, or null if there is no parent

  • Get the root of a URL

    Parameters

    • url: any

      the url to find the root of

    Returns URL

    the root of the URL, or the original URL when the URL isn't suitable for finding the root of.

  • Graft a new path onto some parent of the current URL

    E.g. grafting "by-name/foobar" onto the 2nd parent path: example.com/items/by-id/42 -> example.com/items/by-name/foobar

    Parameters

    • url: URL

      the URL to modify

    • newTail: string

      the new "grafted" URL path tail

    • level: number

      the graft point in terms of path levels >= 0: start at / and count right <0: start at the current path and count left

    Returns URL

  • Increment the last number in a URL.

    (perhaps this could be made so you can select the "nth" number in a URL rather than just the last one?)

    Parameters

    • url: any

      the URL to increment

    • count: any

      increment step to advance by (can be negative)

    Returns string

    the incremented URL, or null if cannot be incremented

  • Interpolates a query or other search item into a URL

    If the URL pattern contains "%s", the query is interpolated there. If not, it is appended to the end of the pattern.

    The search item is percent encoded before it is inserted.

    Parameters

    • urlPattern: URL

      a URL to interpolate/append a query to

    • query: string

      a query to interpolate/append into the URL

    Returns URL

    the URL with the query encoded and inserted at the relevant point

  • Replace the value of a query in a URL with a new one

    Parameters

    • url: URL

      the URL to act on

    • matchQuery: string

      the query key to replace the value for

    • newVal: string

      the new value to use

    Returns URL

  • Convert a URL matching a configured search URL back to command arguments.

    Parameters

    • url: string
    • searchurls: Record<string, string>

    Returns string

  • Sets the value of a query in a URL with a specific one

    Parameters

    • url: URL

      the URL to act on

    • matchQuery: string

      the query key to set the value for

    • value: string

      the value to use

    Returns URL