Files
vault/ui/lib/core/addon/utils/sanitize-path.js
Chelsea Shaw b833b30315 UI: always send capabilities-self request in user's root namespace (#24168)
* Add getRelativePath helper and use to calculate relativeNamespace

* Always request capabilities-self on users root ns and prefix body with relative path

* Update capabilities adapter with test

* add changelog

* Simplify getRelativePath logic

* test update
2023-11-20 13:21:00 -06:00

34 lines
943 B
JavaScript

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: BUSL-1.1
*/
export function sanitizePath(path) {
if (!path) return '';
//remove whitespace + remove trailing and leading slashes
return path.trim().replace(/^\/+|\/+$/g, '');
}
export function ensureTrailingSlash(path) {
return path.replace(/(\w+[^/]$)/g, '$1/');
}
/**
* getRelativePath is for removing matching segments of a subpath from the front of a full path.
* This method assumes that the full path starts with all of the root path.
* @param {string} fullPath eg apps/prod/app_1/test
* @param {string} rootPath eg apps/prod
* @returns the leftover segment, eg app_1/test
*/
export function getRelativePath(fullPath = '', rootPath = '') {
const root = sanitizePath(rootPath);
const full = sanitizePath(fullPath);
if (!root) {
return full;
} else if (root === full) {
return '';
}
return sanitizePath(full.substring(root.length));
}