-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunctions.ts
More file actions
246 lines (210 loc) · 6.96 KB
/
functions.ts
File metadata and controls
246 lines (210 loc) · 6.96 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import type { Platforms, PlatformFeatures, PathDef } from '$lib/structure'
export function toLower(str: string) {
return str.trim().toLowerCase()
}
export function toUpper(str: string) {
return str.trim().toUpperCase()
}
export function reverseSortVersions(versions: string[]) {
return versions.sort((a, b) => {
return b.localeCompare(a, undefined, { numeric: true });
});
}
export function toggleSidebar() {
document.getElementById('sidebar')?.classList.toggle('-translate-x-0')
document.getElementById('sidebar')?.classList.toggle('-translate-x-full')
document.getElementById('open-sidebar')?.classList.toggle('hidden')
document.getElementById('close-sidebar')?.classList.toggle('hidden')
}
export function closeSidebar() {
if (document.getElementById('open-sidebar')?.classList.contains("hidden")) {
toggleSidebar();
}
}
export function extractFeatures (data: Platforms): [PlatformFeatures, string[]] {
let platforms: PlatformFeatures = {}
let allFeatures: string[] = []
let uniqueFeatures: string[] = []
if (Object.keys(data)?.length) {
for (const [platform, features] of Object.entries(data)) {
let platformFeatures = features.split(/\s+/)
platforms[platform] = platformFeatures
allFeatures = allFeatures.concat(platformFeatures)
}
uniqueFeatures = [...new Set(allFeatures)].sort()
}
return [platforms, uniqueFeatures]
}
export function escapeText(text: string) {
//return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&')
return text.replace(/[\[\]\*]/g, '\\$&')
}
export function removeKeyDefault(text: string) {
return text.replaceAll("=*", "")
}
export function searchBasedFilter(x: any, searchTerm: string, showPrefix: boolean = false) {
const keys = searchTerm.split(/\s+/)
const searchStr = `${showPrefix ? x["path-with-prefix"] : x.path};${x.type}`
return keys.every(x => searchStr.includes(x))
}
export function markFilter(target: string, term: string, from: string = "table") {
if(term != "") {
const keys = term.split(/\s+/)
const pattern = (new RegExp(escapeText(keys.join('|')), 'g'))
let markClass = "text-nokia-blue dark:text-yellow-400 bg-white dark:bg-gray-800 font-bold"
if(from === "tree") markClass = "bg-green-300 dark:bg-green-400"
const markTerm = (str: string) => str.replace(pattern, (match: any) => `<mark class="${markClass}">${match}</mark>`)
return markTerm(target)
}
return target
}
export function markRender (node: HTMLSpanElement, text:string) {
const action = () => node.innerHTML = text
action()
return {
update(obj: string) {
text = obj
action()
},
}
}
// do not alter the flow in any means
export function featureBasedFilter (x: PathDef, f: string[]): boolean {
const isOperator = (arg: string): boolean => (arg === "|" || arg === "&")
const featureFilter = (data: string[]): string => {
// order of execution is important
let tmp = data.map(i => `(${i})`).join(", ")
tmp = tmp.replaceAll("\n", " ")
if (tmp.includes("not")) {
tmp = tmp.replaceAll("not ", "!")
}
tmp = tmp.replaceAll("srl_nokia-feat:", "")
.replaceAll("srl-feat:", "")
.replaceAll("srl_feat:", "")
.replaceAll(" or ", " | ")
.replaceAll(", ", " & ")
.replaceAll("-", "_")
return tmp
}
if (f.length > 0 && x["if-features"]) {
let exp = featureFilter(x["if-features"])
// Since future-0-0 does not exist, mark as do not exist by default
exp = exp.replace(/future_0_0/g, "=")
for (const feature of f) {
let d2h = feature.replace(/-/g, "_")
if (exp.includes(d2h)) {
exp = exp.replace(new RegExp(`\\b${d2h}\\b`, 'g'), "+")
}
}
exp = exp.replaceAll("!+", "=")
let expSplit = exp.split(" ")
let expResult = []
const validOperators = ["+", "=", "&", "|"]
for (let i = 0; i < expSplit.length; i++) {
if (expSplit[i] === "+" || expSplit[i] === "=") {
expResult.push(expSplit[i])
} else {
let validation = validOperators.some(operator => expSplit[i].includes(operator))
if (!validation) {
let tmpRep = expSplit[i].replace(/[a-z0-9_]+/g, "=").replace("!=", "+")
expResult.push(tmpRep)
} else {
expResult.push(expSplit[i])
}
}
if (isOperator(expSplit[i + 1])) {
expResult.push(expSplit[i + 1])
i++
}
}
if (expResult.length > 0) {
if (isOperator(expResult[expResult.length - 1])) {
expResult.pop()
}
let result = expResult.join(" ")
result = result.replaceAll("+", "1").replaceAll("=", "0")
return evalBoolString(result)
}
}
return true
}
// cloudfare pages dont support eval() or Function()
function evalBoolString(expression: string): boolean {
const tokens = expression.split(/([()&|!])/).filter(token => token.trim() !== '');
let index = 0;
const consume = (expected: string) => {
if (tokens[index] === expected) {
index++;
} else {
throw new Error(`Feature Evaluation: Expected '${expected}' but found '${tokens[index]}'`);
}
}
const parseFactor = () => {
const token = tokens[index++].trim();
if (token === '(') {
const result = parseExpression();
consume(')');
return result;
} else if (token === '1') {
return true;
} else if (token === '0') {
return false;
} else if (token === '!') {
return !parseFactor();
} else {
throw new Error(`Feature Evaluation: Invalid token '${token}'`);
}
}
const parseTerm = () => {
let result = parseFactor();
while (tokens[index] === '&') {
consume('&');
result = result & parseFactor();
}
return result;
}
const parseExpression = () => {
let result = parseTerm();
while (tokens[index] === '|') {
consume('|');
result = result | parseTerm();
}
return result;
}
return Boolean(parseExpression());
}
export function gnmiToJsPath(gnmiPath: string) {
return gnmiPath
.split('/')
.filter(Boolean)
.map(segment => {
const base = segment.split('[')[0]
const keys = [...segment.matchAll(/\[([^=\]]+)=([^\]]+)\]/g)]
const keyStr = keys.map(k => `{.${k[1]}==${k[2]}}`).join('')
return `.${base}${keyStr}`
})
.join('')
}
export function gnmiToModelPath(jsonInstancePath: string) {
return jsonInstancePath
.split('/')
.filter(Boolean)
.map(segment => {
const base = segment.split('[')[0]
// Match [key="value"], [key=value], or [key=*]
const keyMatch = segment.match(/\[.*?=(?:"([^"]+)"|([^\]]+))\]/)
const rawValue = keyMatch ? (keyMatch[1] || keyMatch[2]) : null
const value = rawValue !== null ? encodeURIComponent(rawValue) : null
return value ? `${base}=${value}` : base
})
.join('/')
.replace(/^/, '/')
}
export function copyAnimation() {
const toggle = () => {
document.getElementById("clip")?.classList.toggle("hidden")
document.getElementById("copied")?.classList.toggle("hidden")
}
setTimeout(toggle, 1000)
toggle()
}