Analytics
/

URL Utils

Utility library for working with URLs


A tiny utility library for working with URLs in 2.53kb.

This library will work with analytics or as a standalone package.

See live demo.

Why this package?

This package makes it a little easy to work with URLs

How to install

Install @analytics/url-utils from npm.

npm install @analytics/url-utils

API

Below is the api for @analytics/url-utils.

parseUrl

Parse a url parts

import { parseUrl } from '@analytics/url-utils'

parseUrl('https://www.cool.com/my-path/here?hello=true#my-hash=cool')
/*
{
  protocol: 'https',
  hostname: 'www.cool.com',
  port: '',
  path: '/my-path/here',
  query: 'hello=true',
  hash: 'my-hash=cool'
}
*/

getHost

Get host domain of url

import { getHost } from '@analytics/url-utils'

getHost('https://subdomain.my-site.com/')
// subdomain.my-site.com

getDomain

Get domain of url

import { getDomain } from '@analytics/url-utils'

getDomain('https://subdomain.my-site.com/')
// my-site.com

getSubDomain

Get sub-domain of url

import { getSubDomain } from '@analytics/url-utils'

getSubDomain('https://subdomain.my-site.com/')
// subdomain

trimTld

Trim TLD from domain name

import { trimTld } from '@analytics/url-utils'

trimTld('my-site.com')
// my-site

Alternative libs

  • If in node.js context, you can also use the native url module