-
Notifications
You must be signed in to change notification settings - Fork 0
/
cpu.js
91 lines (91 loc) · 2.71 KB
/
cpu.js
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
//camel parse utilities
export function lc(s) {
if (!s)
return s;
return s[0].toLowerCase() + s.substring(1);
}
export function uc(s) {
return s[0].toUpperCase() + s.substring(1);
}
export function toLcGrp(groups, declarations = {}) {
const lcGroup = {};
for (const k in groups) {
const val = groups[k];
const rhs = lc(val);
const rhs2 = declarations[rhs] || rhs;
lcGroup[k] = rhs2;
}
return lcGroup;
}
export function unescSplit(val) {
return val.split('\\').map((s, idx) => idx === 0 ? lc(s) : uc(s)).join('');
}
export function tryParse(s, regExpOrRegExpExt, declarations = {}) {
const reArr = arr(regExpOrRegExpExt);
for (const reOrRegExt of reArr) {
let re;
let def;
if (reOrRegExt instanceof RegExp) {
re = reOrRegExt;
}
else {
re = reOrRegExt.regExp;
def = reOrRegExt.defaultVals;
}
const test = re.exec(s);
if (test === null)
continue;
const returnObj = toLcGrp(test.groups, declarations);
if (def !== undefined) {
Object.assign(returnObj, def);
}
return returnObj;
}
return null;
}
export function arr(inp) {
return inp === undefined ? []
: Array.isArray(inp) ? inp : [inp];
}
export function append(inp, camelStrings, regExp) {
const regExps = arr(regExp);
for (const camelString of camelStrings) {
const toDot = camelString.replaceAll(':', '.');
//TODO: regexps
let grp = toDot;
const regExps = arr(regExp);
for (const r of regExps) {
const test = r.exec(toDot);
const grps = test?.groups;
if (grps) {
grp = toLcGrp(grps);
break;
}
}
inp.push(grp);
}
}
const reSet = /^(?<lhs>\w+)(?<!\\)To(?<rhs>[\w\.]+)/;
export function parseSet(Set, camelConfig) {
if (Set !== undefined) {
const setRules = [];
append(setRules, Set, reSet);
for (const rule of setRules) {
camelConfig[rule.lhs] = rule.rhs;
}
}
}
export async function beSplit(s) {
const split = s.split('.');
if (split.length > 1) {
const { camelToLisp } = await import('trans-render/lib/camelToLisp.js');
let firstTokenCamel = camelToLisp(split[0]);
if (firstTokenCamel.startsWith('be-')) {
firstTokenCamel = firstTokenCamel.replace('be-', '');
//const {lc} = await import('be-decorated/cpu.js');
const path = '.beDecorated.' + lc(s.replace('be', ''));
const eventName = 'be-decorated.' + firstTokenCamel + '.resolved';
return { path, eventName };
}
}
}