@sunamo/sunodejs
Version:
Node.js utilities for file system operations, process management, and Electron apps. Includes TypeScript support with functions for file operations, directory management, and cross-platform compatibility.
50 lines (49 loc) • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeQueryString = removeQueryString;
exports.parseURL = parseURL;
exports.buildBaseUrl = buildBaseUrl;
const jsdom_1 = require("jsdom");
function removeQueryString(windowLocationHref) {
const dom = new jsdom_1.JSDOM(`<!DOCTYPE html>`, {
url: windowLocationHref,
});
const { window } = dom;
const url = new URL(window.location.href);
url.search = "";
return url.toString();
}
function parseURL(templateURL, actualURL) {
const template = templateURL;
const regexTemplate = template.replace(/\{([^}]+)\}/g, "([^/]+)");
const regex = new RegExp("^" + regexTemplate + "$");
const baseUrl = buildBaseUrl(actualURL);
const url = new URL(actualURL, baseUrl);
const pathname = url.pathname;
const matches = pathname.match(regex);
if (matches) {
const parameters = {};
const wildcards = template.match(/\{([^}]+)\}/g);
if (wildcards) {
wildcards.forEach((wildcard, index) => {
const parameterName = wildcard.replace(/\{|\}/g, "");
parameters[parameterName] = matches[index + 1];
});
}
return parameters;
}
else {
return null;
}
}
function buildBaseUrl(windowLocationHref) {
const dom = new jsdom_1.JSDOM(`<!DOCTYPE html>`, {
url: windowLocationHref,
});
const { window } = dom;
const protokol = window.location.protocol;
const hostname = window.location.hostname;
const port = window.location.port;
const portCast = port ? `:${port}` : "";
return `${protokol}//${hostname}${portCast}`;
}