UNPKG

@xapp/arachne-utils

Version:

52 lines 1.65 kB
"use strict"; /*! Copyright (c) 2024, XAPP AI */ Object.defineProperty(exports, "__esModule", { value: true }); exports.hasExtension = hasExtension; /** * Determines if the provided URL has a certain file extension. This is useful to filter out undesirable URLs to follow such as .pdf, .jpg, .png or other file types that don't have hyperlinks themselves. * * It assumes that a URL without an extension is HTML. * * @param url * @param extension */ function hasExtension(url, extension) { if (!url) { return false; } let testUrl; try { testUrl = new URL(url); } catch (e) { try { // URL here doesn't really matter, we just need to make sure it parses testUrl = new URL(`https://example.com${url}`); } catch (e) { console.error(`⚠️ Error parsing URL: ${url}`); return false; } } const path = testUrl.pathname; const extensionIndex = path.lastIndexOf('.'); const foundExtension = extensionIndex > 0 ? path.substring(extensionIndex) : '.html'; const testExtensions = []; if (typeof extension === 'string') { testExtensions.push(extension); } else { testExtensions.push(...extension); } let hasExtension = false; testExtensions.forEach((ext) => { // lowercase and ensure we start with a period ext = ext.toLowerCase(); ext = ext.startsWith('.') ? ext : `.${ext}`; if (ext === foundExtension.toLowerCase()) { hasExtension = true; } }); return hasExtension; } //# sourceMappingURL=hasExtension.js.map