Added tree.

This commit is contained in:
SamTV12345 2024-03-21 08:56:05 +01:00
parent 81a9ef924f
commit f9fd9805c0
2 changed files with 515 additions and 472 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,3 +1,5 @@
import {MapArrayType} from "../types/MapType";
export class SettingsTree { export class SettingsTree {
private children: Map<string, SettingsNode>; private children: Map<string, SettingsNode>;
constructor() { constructor() {
@ -24,46 +26,87 @@ export class SettingsTree {
export class SettingsNode { export class SettingsNode {
private readonly key: string; private readonly key: string;
private value: string|undefined; private value: string | number | boolean | null | undefined;
private children: Map<string, SettingsNode>; private children: MapArrayType<SettingsNode>;
constructor(key: string, value?: string) { constructor(key: string, value?: string | number | boolean | null | undefined) {
this.key = key; this.key = key;
this.value = value; this.value = value;
this.children = new Map(); this.children = {}
} }
public addChild(key: string[], value?: string) { public addChild(path: string[], value: string) {
let depth = 0 let currentNode:SettingsNode = this;
for (let i = 0; i < path.length; i++) {
while (depth < key.length) { const key = path[i];
const k = key[depth]; /*
const slicedKey = key.slice(depth + 1) Skip the current node if the key is the same as the current node's key
depth++; */
if(this.key === k) { if (key === this.key ) {
console.log("same key")
continue continue
} }
if (this.children.has(k)) { /*
console.log("has child", k) If the current node does not have a child with the key, create a new node with the key
this.children.get(k)!.addChild(slicedKey, value); */
if (!currentNode.hasChild(key)) {
currentNode = currentNode.children[key] = new SettingsNode(key, this.coerceValue(value));
} else { } else {
const newNode = new SettingsNode(k); /*
this.children.set(k, newNode); Else move to the child node
if(slicedKey.length > 0) */
newNode.addChild(slicedKey, value); currentNode = currentNode.getChild(key);
else
newNode.value = value;
this.children.get(k)!.addChild(slicedKey, undefined);
} }
} }
} }
public collectFromLeafsUpwards() {
let collected:MapArrayType<any> = {};
for (const key in this.children) {
const child = this.children[key];
if (child.hasChildren()) {
collected[key] = child.collectFromLeafsUpwards();
} else {
collected[key] = child.value;
}
}
return collected;
}
coerceValue = (stringValue: string) => {
// cooked from https://stackoverflow.com/questions/175739/built-in-way-in-javascript-to-check-if-a-string-is-a-valid-number
// @ts-ignore
const isNumeric = !isNaN(stringValue) && !isNaN(parseFloat(stringValue) && isFinite(stringValue));
if (isNumeric) {
// detected numeric string. Coerce to a number
return +stringValue;
}
switch (stringValue) {
case 'true':
return true;
case 'false':
return false;
case 'undefined':
return undefined;
case 'null':
return null;
default:
return stringValue;
}
};
public hasChildren() {
return Object.keys(this.children).length > 0;
}
public getChild(key: string) { public getChild(key: string) {
return this.children.get(key); return this.children[key];
} }
public hasChild(key: string) { public hasChild(key: string) {
return this.children.has(key); return this.children[key] !== undefined;
} }
} }