1
0
mirror of https://github.com/bitwarden/browser.git synced 2024-11-13 10:24:20 +01:00
bitwarden-browser/src/misc/serviceUtils.ts
Oscar Hinton e516692559
Upgrade TypeScript (#148)
* Update typescript to 3.6.5 along with tslint to latest.

* Upgrade @types/node to 12.12.54 to get rid of compile errors.

* Update tslint.

* Use @types/node 10.17.28 instead
2020-08-12 15:42:42 -04:00

55 lines
1.9 KiB
TypeScript

import {
ITreeNodeObject,
TreeNode,
} from '../models/domain/treeNode';
export class ServiceUtils {
static nestedTraverse(nodeTree: TreeNode<ITreeNodeObject>[], partIndex: number, parts: string[],
obj: ITreeNodeObject, parent: ITreeNodeObject, delimiter: string) {
if (parts.length <= partIndex) {
return;
}
const end = partIndex === parts.length - 1;
const partName = parts[partIndex];
for (let i = 0; i < nodeTree.length; i++) {
if (nodeTree[i].node.name !== parts[partIndex]) {
continue;
}
if (end && nodeTree[i].node.id !== obj.id) {
// Another node with the same name.
nodeTree.push(new TreeNode(obj, partName, parent));
return;
}
ServiceUtils.nestedTraverse(nodeTree[i].children, partIndex + 1, parts,
obj, nodeTree[i].node, delimiter);
return;
}
if (nodeTree.filter((n) => n.node.name === partName).length === 0) {
if (end) {
nodeTree.push(new TreeNode(obj, partName, parent));
return;
}
const newPartName = parts[partIndex] + delimiter + parts[partIndex + 1];
ServiceUtils.nestedTraverse(nodeTree, 0, [newPartName, ...parts.slice(partIndex + 2)],
obj, parent, delimiter);
}
}
static getTreeNodeObject(nodeTree: TreeNode<ITreeNodeObject>[], id: string): TreeNode<ITreeNodeObject> {
for (let i = 0; i < nodeTree.length; i++) {
if (nodeTree[i].node.id === id) {
return nodeTree[i];
} else if (nodeTree[i].children != null) {
const node = ServiceUtils.getTreeNodeObject(nodeTree[i].children, id);
if (node !== null) {
return node;
}
}
}
return null;
}
}