-
Notifications
You must be signed in to change notification settings - Fork 0
/
highcharts-patch.ts
49 lines (45 loc) · 1.69 KB
/
highcharts-patch.ts
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
import { resolveDefPath } from "./utils";
type Patch<T> = {
[P in keyof T]?: Patch<T[P]> | ((value: T[P]) => T[P])
}
const patch: { [path: string]: Patch<Def> } = {
"plotOptions.series.dataLabels.filter.operator": {
doclet: {
values: values => values.replace(/""/g, `"`) // fixes typo `[">", "<", ">=", "<=", "==", "===""]` (note the double trailing quotes)
}
},
"plotOptions.tilemap.tileShape": {
doclet: {
values: `["hexagon", "circle", "diamond", "square"]` // possible values mentioned in docs
}
},
"yAxis.tooltipValueFormat": {
doclet: {
type: { names: ["String"] } // missing and `tooltipValueFormat` doesn't merge from anywhere
}
},
"chart.parallelAxes.title": {
doclet: {
extends: "xAxis.labels" // missing type and doesn't merge, based on docs: "All options for xAxis.labels applies to parallel coordinates titles" -- https://api.highcharts.com/highcharts/chart.parallelAxes.title
}
}
}
export function applyPatchToDefs(rootDef: Def) {
Object.keys(patch).forEach(key => {
console.log(`Patching "${key.bold}" with`, patch[key])
const path = key.split(".");
const def = resolveDefPath(rootDef, path);
applyPatch(def, patch[key]);
});
}
function applyPatch<T extends object, K extends keyof T>(target: T, patch: Patch<T>): T {
(Object.keys(patch) as K[]).forEach(key => {
const patchValue = patch[key]!; // ! to avoid bogus "variable used before being assigned" error
target[key] = typeof patchValue == "object" && !Array.isArray(patchValue)
? applyPatch(target[key] || {}, patchValue)
: typeof patchValue == "function"
? patchValue(target[key])
: patchValue;
});
return target;
}