/
ranking.ts
114 lines (104 loc) · 2.66 KB
/
ranking.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
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
import type { Ranking } from '../../model';
import type { LocalDataProvider } from '../../provider';
import {
INestedBuilder,
IReduceBuilder,
IScriptedBuilder,
IWeightedSumBuilder,
buildRanking as buildRankingImpl,
} from '../RankingBuilder';
import type {
IBuilderAdapterImposeColumnProps,
IBuilderAdapterNestedColumnProps,
IBuilderAdapterRankingProps,
IBuilderAdapterReduceColumnProps,
IBuilderAdapterScriptColumnProps,
IBuilderAdapterSupportColumnProps,
IBuilderAdapterWeightedSumColumnProps,
} from './interfaces';
/*
* build the column description
*/
export function buildRanking(props: IBuilderAdapterRankingProps, data: LocalDataProvider): Ranking {
const r = buildRankingImpl();
if (props.sortBy) {
const s = Array.isArray(props.sortBy) ? props.sortBy : [props.sortBy];
s.forEach((si) => {
if (typeof si === 'string') {
r.sortBy(si);
} else {
r.sortBy(si.column, si.asc);
}
});
}
if (props.groupBy) {
const s = Array.isArray(props.groupBy) ? props.groupBy : [props.groupBy];
r.groupBy(...s);
}
if (props.columns) {
props.columns.forEach((c) => r.column(c));
}
return r.build(data);
}
export function buildGeneric(props: { column: '*' | string }) {
return props.column;
}
export function buildImposeRanking(props: IBuilderAdapterImposeColumnProps) {
return Object.assign(
{
type: 'impose',
},
props
);
}
export function buildNestedRanking(props: IBuilderAdapterNestedColumnProps, children: string[]) {
const r: INestedBuilder = {
type: 'nested',
columns: children,
};
if (props.label) {
r.label = props.label;
}
return r;
}
export function buildWeightedSumRanking(
props: IBuilderAdapterWeightedSumColumnProps,
children: { column: string; weight: number }[]
) {
const r: IWeightedSumBuilder = {
type: 'weightedSum',
columns: children.map((d) => d.column),
weights: children.map((d) => d.weight),
};
if (props.label) {
r.label = props.label;
}
return r;
}
export function buildReduceRanking(props: IBuilderAdapterReduceColumnProps, children: string[]) {
const r: IReduceBuilder = {
type: props.type,
columns: children,
};
if (props.label) {
r.label = props.label;
}
return r;
}
export function buildScriptRanking(props: IBuilderAdapterScriptColumnProps, children: string[]) {
const r: IScriptedBuilder = {
type: 'script',
code: props.code,
columns: children,
};
if (props.label) {
r.label = props.label;
}
return r;
}
export function buildSupportRanking(props: IBuilderAdapterSupportColumnProps) {
return `_${props.type}`;
}
export function buildAllColumnsRanking() {
return '*';
}