Check if a URL is absolute
npx @tessl/cli install tessl/npm-is-absolute-url@4.0.0Check if a URL is absolute. A lightweight utility function that determines whether a given URL string contains a protocol scheme, following RFC 3986 standards with special handling for Windows file path edge cases.
npm install is-absolute-urlimport isAbsoluteUrl from 'is-absolute-url';This package is ES module only and does not support CommonJS require().
import isAbsoluteUrl from 'is-absolute-url';
// Absolute URLs (contain protocol scheme)
isAbsoluteUrl('https://sindresorhus.com/foo/bar');
//=> true
isAbsoluteUrl('http://example.com');
//=> true
isAbsoluteUrl('file://path/to/file');
//=> true
isAbsoluteUrl('mailto:someone@example.com');
//=> true
isAbsoluteUrl('data:text/plain;base64,SGVsbG8sIFdvcmxkIQ%3D%3D');
//=> true
// Case insensitive scheme matching
isAbsoluteUrl('httpS://example.com');
//=> true
// Relative URLs (no protocol scheme)
isAbsoluteUrl('//sindresorhus.com');
//=> false
isAbsoluteUrl('/foo/bar');
//=> false
isAbsoluteUrl('foo/bar');
//=> false
// Windows paths are handled as non-absolute
isAbsoluteUrl('c:\\Windows\\System32');
//=> false
// Invalid scheme characters
isAbsoluteUrl('ht,tp://example.com');
//=> falseDetermines if a URL string is absolute by checking for a valid protocol scheme.
/**
* Check if a URL is absolute
* @param {string} url - The URL string to check
* @returns {boolean} true if the URL is absolute, false otherwise
* @throws {TypeError} if url parameter is not a string
*/
export default function isAbsoluteUrl(url) {}Parameters:
url (string): The URL string to validateReturns:
boolean: true if the URL contains a valid protocol scheme, false otherwiseThrows:
TypeError: If the url parameter is not a stringImplementation Details:
+, -, or .c:\) to avoid false positivesSupported URL Schemes: All RFC 3986 compliant schemes are supported, including but not limited to:
http:// and https://file://mailto:data:ftp://ldap://Edge Cases:
//example.com) return falsec:\, C:\Dev\) return falseht,tp://) return falsefalseThe function validates input types and throws a descriptive error for non-string inputs:
try {
isAbsoluteUrl(123);
} catch (error) {
console.log(error.message);
// "Expected a `string`, got `number`"
}