-
-
Notifications
You must be signed in to change notification settings - Fork 3k
Expand file tree
/
Copy pathSettingsTree.ts
More file actions
112 lines (94 loc) · 2.71 KB
/
Copy pathSettingsTree.ts
File metadata and controls
112 lines (94 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import {MapArrayType} from "../types/MapType";
export class SettingsTree {
private children: Map<string, SettingsNode>;
constructor() {
this.children = new Map();
}
public addChild(key: string, value: string) {
this.children.set(key, new SettingsNode(key, value));
}
public removeChild(key: string) {
this.children.delete(key);
}
public getChild(key: string) {
return this.children.get(key);
}
public hasChild(key: string) {
return this.children.has(key);
}
}
export class SettingsNode {
private readonly key: string;
private value: string | number | boolean | null | undefined;
private children: MapArrayType<SettingsNode>;
constructor(key: string, value?: string | number | boolean | null | undefined) {
this.key = key;
this.value = value;
this.children = {}
}
public addChild(path: string[], value: string) {
let currentNode:SettingsNode = this;
for (let i = 0; i < path.length; i++) {
const key = path[i];
/*
Skip the current node if the key is the same as the current node's key
*/
if (key === this.key ) {
continue
}
/*
If the current node does not have a child with the key, create a new node with the key
*/
if (!currentNode.hasChild(key)) {
currentNode = currentNode.children[key] = new SettingsNode(key, this.coerceValue(value));
} else {
/*
Else move to the child node
*/
currentNode = currentNode.getChild(key);
}
}
}
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) {
return this.children[key];
}
public hasChild(key: string) {
return this.children[key] !== undefined;
}
}