-
Notifications
You must be signed in to change notification settings - Fork 248
/
theming.mjs
82 lines (72 loc) · 2.47 KB
/
theming.mjs
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
import get from "lodash/get.js";
import has from "lodash/has.js";
import DEFAULT_THEME from "./default-theme.mjs";
function matchesRE(pValue, pRE) {
const lMatchResult = pValue.match && pValue.match(pRE);
return Boolean(lMatchResult) && lMatchResult.length > 0;
}
function matchesCriterion(pModuleKey, pCriterion) {
return pModuleKey === pCriterion || matchesRE(pModuleKey, pCriterion);
}
function moduleOrDependencyMatchesCriteria(pSchemeEntry, pModule) {
return Object.keys(pSchemeEntry.criteria).every((pKey) => {
// we use lodash.get here because in the criteria you can enter
// nested keys like "rules[0].severity" : "error", and lodash.get handles
// that for us
const lCriterion = get(pSchemeEntry.criteria, pKey);
const lModuleKey = get(pModule, pKey);
if (!(lModuleKey || has(pModule, pKey))) {
return false;
}
if (Array.isArray(lModuleKey)) {
if (Array.isArray(lCriterion)) {
return lCriterion.some((pCriterionEntry) =>
lModuleKey.some((pModuleKeyEntry) =>
matchesCriterion(pModuleKeyEntry, pCriterionEntry),
),
);
} else {
return lModuleKey.some((pModuleKeyEntry) =>
matchesCriterion(pModuleKeyEntry, lCriterion),
);
}
}
if (Array.isArray(lCriterion)) {
return lCriterion.some((pCriterionEntry) =>
matchesCriterion(lModuleKey, pCriterionEntry),
);
}
return matchesCriterion(lModuleKey, lCriterion);
});
}
function determineAttributes(pModuleOrDependency, pAttributeCriteria) {
return (pAttributeCriteria || [])
.filter((pSchemeEntry) =>
moduleOrDependencyMatchesCriteria(pSchemeEntry, pModuleOrDependency),
)
.map((pSchemeEntry) => pSchemeEntry.attributes)
.reduce((pAll, pCurrent) => ({ ...pCurrent, ...pAll }), {});
}
function normalizeTheme(pTheme) {
let lReturnValue = structuredClone(DEFAULT_THEME);
if (pTheme) {
if (pTheme.replace) {
lReturnValue = pTheme;
} else {
lReturnValue.graph = { ...DEFAULT_THEME.graph, ...pTheme.graph };
lReturnValue.node = { ...DEFAULT_THEME.node, ...pTheme.node };
lReturnValue.edge = { ...DEFAULT_THEME.edge, ...pTheme.edge };
lReturnValue.modules = (pTheme.modules || []).concat(
DEFAULT_THEME.modules,
);
lReturnValue.dependencies = (pTheme.dependencies || []).concat(
DEFAULT_THEME.dependencies,
);
}
}
return lReturnValue;
}
export default {
normalizeTheme,
determineAttributes,
};