mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-10-30 18:17:55 +00:00
* 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
34 lines
943 B
JavaScript
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));
|
|
}
|