UNPKG

skynet-mysky-utils

Version:
80 lines 2.26 kB
"use strict"; exports.__esModule = true; exports.sanitizePath = exports.getParentPath = exports.getPathDomain = void 0; var utils_1 = require("./utils"); /** * Gets the root path domain for the given path. * * @param path - The given path. * @returns - The path domain. */ function getPathDomain(path) { // Sanitize the path. var sanitizedPath = sanitizePath(path); if (sanitizedPath === null) { return null; } // Split the string and extract the domain. If there are no slashes, the first // element will contain the entire string. var domain = sanitizedPath.split("/")[0]; if (domain === "") { return null; } return domain; } exports.getPathDomain = getPathDomain; /** * Gets the parent path for the given path. * * @param path - The given path. * @returns - The parent path, or null if no parent. */ function getParentPath(path) { // Sanitize the path. var sanitizedPath = sanitizePath(path); if (sanitizedPath === null) { return null; } // Split the path. var pathArray = sanitizedPath.split("/"); if (pathArray.length <= 1) { return null; } // Get the parent path. pathArray.pop(); var parentPath = pathArray.join("/"); if (parentPath === "") { return null; } return parentPath; } exports.getParentPath = getParentPath; /** * Sanitizes the path by removing trailing slashes and removing repeating adjacent slashes. * * @param path - The given path * @returns - The sanitized path. */ function sanitizePath(path) { // Trim the path. path = path.trim(); // Paths starting with a slash are invalid. if (path.startsWith("/")) { return null; } // Remove trailing slashes. path = (0, utils_1.trimSuffix)(path, "/"); // Remove duplicate adjacent slashes. path = (0, utils_1.removeAdjacentChars)(path, "/"); // Convert the domain to lowercase. var pathArray = path.split("/"); pathArray[0] = pathArray[0].toLowerCase(); // Get the sanitized path. var sanitizedPath = pathArray.join("/"); if (sanitizedPath === "") { return null; } return sanitizedPath; } exports.sanitizePath = sanitizePath; //# sourceMappingURL=paths.js.map