2023-03-15 16:00:52 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) HashiCorp, Inc.
|
|
|
|
* SPDX-License-Identifier: MPL-2.0
|
|
|
|
*/
|
|
|
|
|
2018-04-03 14:16:57 +00:00
|
|
|
function keyIsFolder(key) {
|
|
|
|
return key ? !!key.match(/\/$/) : false;
|
|
|
|
}
|
|
|
|
|
|
|
|
function keyPartsForKey(key) {
|
|
|
|
if (!key) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
var isFolder = keyIsFolder(key);
|
|
|
|
var parts = key.split('/');
|
|
|
|
if (isFolder) {
|
|
|
|
parts.pop();
|
|
|
|
}
|
|
|
|
return parts.length > 1 ? parts : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function parentKeyForKey(key) {
|
|
|
|
var parts = keyPartsForKey(key);
|
|
|
|
if (!parts) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return parts.slice(0, -1).join('/') + '/';
|
|
|
|
}
|
|
|
|
|
|
|
|
function keyWithoutParentKey(key) {
|
|
|
|
return key ? key.replace(parentKeyForKey(key), '') : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function ancestorKeysForKey(key) {
|
|
|
|
var ancestors = [],
|
|
|
|
parentKey = parentKeyForKey(key);
|
|
|
|
|
|
|
|
while (parentKey) {
|
|
|
|
ancestors.unshift(parentKey);
|
|
|
|
parentKey = parentKeyForKey(parentKey);
|
|
|
|
}
|
|
|
|
|
2019-08-01 20:49:37 +00:00
|
|
|
return ancestors;
|
2018-04-03 14:16:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export default {
|
|
|
|
keyIsFolder,
|
|
|
|
keyPartsForKey,
|
|
|
|
parentKeyForKey,
|
|
|
|
keyWithoutParentKey,
|
|
|
|
ancestorKeysForKey,
|
|
|
|
};
|