-
Notifications
You must be signed in to change notification settings - Fork 4.9k
/
util.ts
157 lines (130 loc) · 2.95 KB
/
util.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import { t } from "ttag";
import { isNotNull } from "metabase/lib/types";
import * as Lib from "metabase-lib";
export type ColumnAndSeparator = {
separator: string;
column: Lib.ColumnMetadata | null;
};
export const label = (index: number) => {
const ordinal = index + 1;
if (ordinal === 1) {
return t`First column`;
}
if (ordinal === 2) {
return t`Second column`;
}
if (ordinal === 3) {
return t`Third column`;
}
if (ordinal === 4) {
return t`Fourth column`;
}
if (ordinal === 5) {
return t`Fifth column`;
}
if (ordinal === 6) {
return t`Sixth column`;
}
if (ordinal === 7) {
return t`Seventh column`;
}
if (ordinal === 8) {
return t`Eighth column`;
}
if (ordinal === 9) {
return t`Ninth column`;
}
return t`Column ${ordinal}`;
};
export type ColumnOption = {
label: string;
value: string;
column: Lib.ColumnMetadata;
};
export const formatSeparator = (separator: string) => {
if (!separator) {
return `(${t`empty`})`;
}
if (separator === " ") {
return `(${t`space`})`;
}
return separator;
};
export const getDefaultSeparator = (column: Lib.ColumnMetadata): string => {
if (Lib.isEmail(column)) {
return "";
}
if (Lib.isURL(column)) {
return "/";
}
return " ";
};
export const getExpressionName = (
query: Lib.Query,
stageIndex: number,
columnsAndSeparators: ColumnAndSeparator[],
): string => {
const columns = columnsAndSeparators
.map(({ column }) => column)
.filter(isNotNull);
const names = columns.map(
column => Lib.displayInfo(query, stageIndex, column).displayName,
);
return t`Combined ${names.join(", ")}`;
};
export const flatten = (
columnsAndSeparators: ColumnAndSeparator[],
): (string | Lib.ColumnMetadata)[] => {
return columnsAndSeparators
.flatMap(({ column, separator }) => [separator, column])
.slice(1)
.filter(
(element): element is string | Lib.ColumnMetadata => element !== "",
);
};
export const getExample = (
columnsAndSeparators: ColumnAndSeparator[],
): string => {
return flatten(columnsAndSeparators).map(getColumnExample).join("");
};
const getColumnExample = (
column: Lib.ColumnMetadata | string | null,
): string => {
if (!column) {
return "";
}
if (typeof column === "string") {
return column;
}
if (Lib.isEmail(column)) {
return "email@example.com";
}
if (Lib.isURL(column)) {
return "https://www.example.com";
}
if (Lib.isBoolean(column)) {
return "true";
}
if (Lib.isID(column)) {
return "12345";
}
if (Lib.isInteger(column)) {
return "123";
}
if (Lib.isNumeric(column)) {
return "123.45678901234567";
}
if (Lib.isDateWithoutTime(column)) {
return "2042-01-01";
}
if (Lib.isDate(column)) {
return "2042-01-01 12:34:56.789";
}
if (Lib.isTime(column)) {
return "12:34:56.789";
}
if (Lib.isLatitude(column) || Lib.isLongitude(column)) {
return "-12.34567";
}
return "text";
};