URL handling utlity functions

Index

Functions

deleteQuery

  • deleteQuery(url: URL, matchQuery: string): URL
  • 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

    Returns URL

    the modified URL

getAbsoluteURL

  • getAbsoluteURL(url: string, baseURI?: string): string
  • Parameters

    • url: string

      May be either an absolute or a relative URL.

    • Default value baseURI: string = document.baseURI

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

    Returns string

getDownloadFilenameForUrl

  • getDownloadFilenameForUrl(url: URL): 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

    Returns string

    the filename according to the above rules

getExtensionForMimetype

  • getExtensionForMimetype(mime: string): string
  • Very incomplete lookup of extension for common mime types that might be encountered when saving elements on a page. There are NPM libs for this, but this should cover 99% of basic cases

    Parameters

    • mime: string

      mime type to get extension for (eg 'image/png')

    Returns string

    an extension for that mimetype, or undefined if that type is not supported

getUrlParent

  • getUrlParent(url: any, option: any, count?: number): any
  • 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.

    • Default value 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

getUrlQueries

  • getUrlQueries(url: URL): string[]
  • Get an Array of the queries in a URL.

    These could be like "query" or "query=val"

    Parameters

    • url: URL

    Returns string[]

getUrlRoot

  • getUrlRoot(url: any): URL
  • 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.

graftUrlPath

  • graftUrlPath(url: URL, newTail: string, level: number): URL
  • 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

incrementUrl

  • incrementUrl(url: any, count: any): string
  • 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

interpolateSearchItem

  • interpolateSearchItem(urlPattern: URL, query: string): URL
  • 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.

    If the interpolation point is in the query string of the URL, it is percent encoded, otherwise it is is inserted verbatim.

    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 (if needed) and inserted at the relevant point

replaceQueryValue

  • replaceQueryValue(url: URL, matchQuery: string, newVal: string): URL
  • 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

setQueryValue

  • setQueryValue(url: URL, matchQuery: string, value: string): URL
  • 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

setUrlQueries

  • setUrlQueries(url: URL, qys: string[]): void
  • Update a URL with a new array of queries

    Parameters

    • url: URL
    • qys: string[]

    Returns void