UNPKG

1.82 kBJavaScriptView Raw
1import { typeAll } from '@antv/dw-analyzer';
2import { parse } from './parse';
3function rename(originStr, aggType, option) {
4 if (option === void 0) { option = 'brackets'; }
5 if (option === false || option === 'origin') {
6 return originStr;
7 }
8 if (option === true || option === 'brackets' || !option) {
9 return aggType.toUpperCase() + "(" + originStr + ")";
10 }
11 if (option === 'underline') {
12 return aggType.toUpperCase() + "_" + originStr;
13 }
14 if (typeof option === 'function') {
15 return String(option(originStr, aggType));
16 }
17 throw new Error("Invalid rename option " + option);
18}
19/**
20 * @beta
21 */
22export function autoSchema(data, renameOption) {
23 if (renameOption === void 0) { renameOption = 'brackets'; }
24 var schemas = [];
25 var schema = {
26 actions: [],
27 };
28 var dataAnalyze = typeAll(data);
29 var toGroupBy = [];
30 var toNotGroupBy = [];
31 dataAnalyze.forEach(function (col) {
32 if (['string', 'date', 'boolean'].includes(col.recommendation)) {
33 toGroupBy.push(col.name);
34 }
35 else {
36 toNotGroupBy.push(col.name);
37 }
38 });
39 if (toGroupBy.length) {
40 schema.groupBy = toGroupBy;
41 toNotGroupBy.forEach(function (colName) {
42 schema.actions.push({
43 type: 'sum',
44 field: colName,
45 as: rename(colName, 'sum', renameOption),
46 });
47 });
48 schemas.push(schema);
49 }
50 return schemas;
51}
52/**
53 * @beta
54 */
55export function autoTransform(data, renameOption) {
56 if (renameOption === void 0) { renameOption = 'brackets'; }
57 var result = {
58 result: [],
59 schemas: autoSchema(data, renameOption),
60 };
61 result.result = parse(data, result.schemas);
62 return result;
63}