mirror of
https://github.com/hashicorp/vault.git
synced 2026-04-21 06:09:01 -04:00
* license: update headers to IBM Corp. * `make proto` * update offset because source file changed Signed-off-by: Ryan Cragun <me@ryan.ec> Co-authored-by: Ryan Cragun <me@ryan.ec>
45 lines
966 B
TypeScript
45 lines
966 B
TypeScript
/**
|
|
* Copyright IBM Corp. 2016, 2025
|
|
* SPDX-License-Identifier: BUSL-1.1
|
|
*/
|
|
|
|
export function keyIsFolder(key: string) {
|
|
return key ? !!key.match(/\/$/) : false;
|
|
}
|
|
|
|
export function keyPartsForKey(key: string) {
|
|
if (!key) {
|
|
return null;
|
|
}
|
|
const isFolder = keyIsFolder(key);
|
|
const parts = key.split('/');
|
|
if (isFolder) {
|
|
// remove last item which is empty
|
|
parts.pop();
|
|
}
|
|
return parts.length > 1 ? parts : null;
|
|
}
|
|
|
|
export function parentKeyForKey(key: string) {
|
|
const parts = keyPartsForKey(key);
|
|
if (!parts) {
|
|
return '';
|
|
}
|
|
return parts.slice(0, -1).join('/') + '/';
|
|
}
|
|
|
|
export function keyWithoutParentKey(key: string) {
|
|
return key ? key.replace(parentKeyForKey(key), '') : null;
|
|
}
|
|
|
|
export function ancestorKeysForKey(key: string) {
|
|
const ancestors = [];
|
|
let parentKey = parentKeyForKey(key);
|
|
|
|
while (parentKey) {
|
|
ancestors.unshift(parentKey);
|
|
parentKey = parentKeyForKey(parentKey);
|
|
}
|
|
|
|
return ancestors;
|
|
}
|