The components of URLs are compared in the following order:
PROTOCOL
USERNAME
PASSWORD
AUTH
TLD
DOMAIN
SUBDOMAIN
HOSTNAME
PORT
HOST
SEGMENTS
FILENAME
PATHNAME
SEARCH
PATH
HASH
As you may have noticed, there are a few breaks in linearity:
TLD is prioritized beforeDOMAIN because matching a domain on a different top-level domain is very uncommon (but still possible via the ignoreComponents option).
SUBDOMAIN is prioritized afterDOMAIN.
Other considerations:
URLs with invalid domain names, reserved domains, unlisted TLDs or IP addresses that have been determined to have related HOSTNAME components will also have related TLD, DOMAIN and SUBDOMAIN components due to the above mentioned comparison order only; not because they actually have those components.
Install
npm install url-relation
Consumer Usage
URLRelation.match(url1, url2[, options])
For a single match between two URLs.
import URLRelation, { URLComponent, type URLRelationOptionsWithTarget } from 'url-relation';const url1 = new URL('http://domain.com/');const url2 = new URL('http://domain.com/#hash');const options: URLRelationOptionsWithTarget = { components: [URLComponent.HASH], // Ignored components //ignoreComponents: true, //targetComponent: URLComponent.HASH,};if (URLRelation.match(url1, url2, options)) { // Considered a match}
URLRelation::upTo(component[, ignoredComponents])
For multiple matches between the same two URLs.
import URLRelation, { URLComponent /*, type URLRelationOptions*/ } from 'url-relation';const url1 = new URL('http://domain.com/');const url2 = new URL('http://domain.com/#hash');const relation = new URLRelation(url1, url2 /*, options*/);if (relation.upTo(URLComponent.HASH, [URLComponent.HASH])) { // Considered a match}if (relation.upTo(URLComponent.PATH)) { // Considered a match}
Option Profiles
CAREFUL_PROFILE is useful for a URL to an unknown or third-party server that could be incorrectly configured according to specifications and common best practices.
COMMON_PROFILE, the default profile, is useful for a URL to a known server that you trust and expect to be correctly configured according to specifications and common best practices.