@analytics/url-utils — URL utils. One of the small, focused utility packages that power the analytics library, usable standalone.
Full documentation, synced from the package README:
A tiny utility library for working with URLs in 2.54kb.
Exposes parseUrl, getHost, getDomain, getSubDomain, getUrl, getSearch, getHash, getParams, removeParams, & more functions.
This library will work with analytics or as a standalone package.
This package makes it a little easy to work with URLs
Install @analytics/url-utils from npm.
npm install @analytics/url-utilsBelow is the api for @analytics/url-utils.
parseUrlZero dependency parser that breaks a url into its parts.
import { parseUrl } from '@analytics/url-utils'
parseUrl('https://www.cool.com/my-path/here?hello=true#my-hash=cool')
/*
{
href: 'https://www.cool.com/my-path/here?hello=true#my-hash=cool',
protocol: 'https',
hostname: 'www.cool.com',
port: '',
pathname: '/my-path/here',
search: 'hello=true',
hash: 'my-hash=cool'
}
*/isUrlLikeCheck if a string looks like a URL.
import { isUrlLike } from '@analytics/url-utils'
isUrlLike('https://my-site.com')
// true
isUrlLike('hello world')
// falseisUrlCheck if a string is a valid URL with protocol and hostname.
import { isUrl } from '@analytics/url-utils'
isUrl('https://my-site.com')
// true
isUrl('my-site.com')
// falseisInternalCheck if a url is internal to the current location.
import { isInternal } from '@analytics/url-utils'
isInternal('https://my-site.com/about', 'https://my-site.com')
// trueisExternalCheck if a url points to a different host than the current location.
import { isExternal } from '@analytics/url-utils'
isExternal('https://other-site.com', 'https://my-site.com')
// trueisLocalHostCheck if a url string is localhost or 127.0.0.1.
import { isLocalHost } from '@analytics/url-utils'
isLocalHost('http://localhost:3000')
// truegetLocationGet location data for a url. Falls back to window.location in the browser when no url is passed.
import { getLocation } from '@analytics/url-utils'
getLocation('https://my-site.com/path?foo=bar')
// { href, protocol, hostname, port, pathname, search, hash }getHostGet host domain of url.
import { getHost } from '@analytics/url-utils'
getHost('https://subdomain.my-site.com/')
// subdomain.my-site.comgetUrlGet clean url with no search or hash.
import { getUrl } from '@analytics/url-utils'
getUrl('https://subdomain.my-site.com/path-to/page/here/?param=true#hash=false')
// https://subdomain.my-site.com/path-to/page/heregetDomainGet base domain of url.
import { getDomain } from '@analytics/url-utils'
getDomain('https://subdomain.my-site.com/')
// my-site.comgetSubDomainGet sub-domain of url.
import { getSubDomain } from '@analytics/url-utils'
getSubDomain('https://subdomain.my-site.com/')
// subdomaintrimTrailingSlashTrim a trailing slash from a string.
import { trimTrailingSlash } from '@analytics/url-utils'
trimTrailingSlash('google.com/')
// google.comtrimTldTrim the TLD from a domain name.
import { trimTld } from '@analytics/url-utils'
trimTld('google.com')
// googleparamsCleanStrip a query parameter (string or regex) from a search string.
import { paramsClean } from '@analytics/url-utils'
paramsClean('?', '?foo=bar&baz=true', 'foo')
// ?baz=truegetSearchGet search params from a url as an object. Falls back to window.location.search when no url is passed.
import { getSearch } from '@analytics/url-utils'
getSearch('https://my-site.com/?foo=bar&baz=true')
// { foo: 'bar', baz: 'true' }getSearchValueGet a single search param value by key.
import { getSearchValue } from '@analytics/url-utils'
getSearchValue('foo', 'https://my-site.com/?foo=bar')
// barremoveSearchRemove a search param by key or regex from a url. In the browser with no url, the address bar is updated via the history API.
import { removeSearch } from '@analytics/url-utils'
removeSearch('foo', 'https://my-site.com/?foo=bar&baz=true')
// https://my-site.com/?baz=truegetHashGet hash params from a url as an object. Falls back to window.location.hash when no url is passed.
import { getHash } from '@analytics/url-utils'
getHash('https://my-site.com/#foo=bar&baz=true')
// { foo: 'bar', baz: 'true' }getHashValueGet a single hash param value by key.
import { getHashValue } from '@analytics/url-utils'
getHashValue('foo', 'https://my-site.com/#foo=bar')
// barremoveHashRemove a hash param by key or regex from a url. In the browser with no url, the address bar is updated via the history API.
import { removeHash } from '@analytics/url-utils'
removeHash('foo', 'https://my-site.com/#foo=bar&baz=true')
// https://my-site.com/#baz=truegetParamsGet both search and hash params from a url.
import { getParams } from '@analytics/url-utils'
getParams('https://my-site.com/?foo=bar#baz=true')
// { search: { foo: 'bar' }, hash: { baz: 'true' } }removeParamsRemove a key from both search and hash params.
import { removeParams } from '@analytics/url-utils'
removeParams('foo')compressParamsCompress a search params object into a query string, serializing nested objects.
import { compressParams } from '@analytics/url-utils'
compressParams({ foo: 'bar', nested: { a: 1 } })
// ?foo=bar&nested=~(a~1)decompressParamsDecompress a query string into an object, parsing any compressed nested values.
import { decompressParams } from '@analytics/url-utils'
decompressParams('?foo=bar&nested=~(a~1)')
// { foo: 'bar', nested: { a: 1 } }complexParseSearchParse search params, including bracketed array and nested object keys, into an object.
import { complexParseSearch } from '@analytics/url-utils'
complexParseSearch('?fields[]=name&fields[]=id&filters[status]=active')
// { fields: ['name', 'id'], filters: { status: 'active' } }complexParseHashParse hash params, including bracketed array and nested object keys, into an object.
import { complexParseHash } from '@analytics/url-utils'
complexParseHash('#fields[]=name&fields[]=id')
// { fields: ['name', 'id'] }complexParseParamsParse both search and hash params with full bracket/nesting support.
import { complexParseParams } from '@analytics/url-utils'
complexParseParams('https://my-site.com/?foo[]=a#bar[]=b')
// { search: { foo: ['a'] }, hash: { bar: ['b'] } }url module