This package has been deprecated

Author message:

Use the WHATWG URL API

get-base-url
TypeScript icon, indicating that this package has built-in type declarations

3.0.2 • Public • Published

Get base url

Deprecated: This library was created due to me being unaware of the existence of the WHATWG URL API. As it is widely supported in both browsers and node and probably more accurate, I see no need for this anymore.

Get the host of an arbitrary url - relative or absulute or static. Suited for comparison with location.host to inquire whether an url is in local domain scope.

Usage

import getBaseUrl from "get-base-url"
const { getBaseUrl } = require("get-base-url")

getBaseUrl("https://maps.google.com/sub/")   // maps.google.com
getBaseUrl("maps.google.com/sub")            // maps.google.com
getBaseUrl("google.com")                     // google.com
getBaseUrl()                                 // The current host if in a browser or localhost of in node
getBaseUrl("relative")                       // The current host if in a browser or localhost of in node
getBaseUrl("./relative")                     // The current host if in a browser or localhost of in node
getBaseUrl("/absolute")                      // The current host if in a browser or localhost of in node

Compare whether arbitrary url is in local domain scope

import getBaseUrl from "get-base-url"
const { getBaseUrl } = require("get-base-url")

function isLocalUrl(url) {
  return getBaseUrl(url) === getBaseUrl()
}

isLocalUrl("sub")           // true
isLocalUrl("google.com")    // false
isLocalUrl("/google.com")   // true

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Published

Version History

  • Version
    Downloads (Last 7 Days)
    • Published

Package Sidebar

Install

npm i get-base-url

Weekly Downloads

139

Version

3.0.2

License

ISC

Unpacked Size

9.62 kB

Total Files

16

Last publish

Collaborators

  • zzrv