-
Notifications
You must be signed in to change notification settings - Fork 44
/
formatFieldValue.ts
88 lines (80 loc) · 3.03 KB
/
formatFieldValue.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
import {format as d3NumberFormat} from 'd3-format';
import {timeDay, timeHour, timeMinute, timeMonth, timeSecond, timeWeek, timeYear} from 'd3-time';
import {timeFormat} from 'd3-time-format';
import {isDate, isFunction, isNumber, isString} from 'vega-util';
import {FormatCallback, ScenegraphPrimitive} from './options';
/**
* Format value using formatType and format
* @param value - a field value to be formatted
* @param formatType - the formatType can be: "time", "number", or "string"
* @param format - a time time format specifier, or a time number format specifier, or undefined
* @return the formatted value, or undefined if value or formatType is missing
*/
export function customFormat(value: ScenegraphPrimitive, formatType: string, format: string | FormatCallback): string | undefined {
if (value === undefined || value === null) {
return undefined;
}
if (!formatType && !format) {
return undefined;
}
if (isString(format)) {
switch (formatType) {
case 'time':
return format ? timeFormat(format)(value as Date) : autoTimeFormat(value as Date);
case 'number':
return format ? d3NumberFormat(format)(value as number) : autoNumberFormat(value as number);
case 'string':
default:
return value as string;
}
}
if (isFunction(format)) {
return format(value);
}
return undefined;
}
/**
* Automatically format a time, number or string value
* @return the formatted time, number or string value
*/
export function autoFormat(value: ScenegraphPrimitive): string {
if (isNumber(value)) {
return autoNumberFormat(value);
} else if (isDate(value)) {
return autoTimeFormat(value);
} else {
return value;
}
}
/**
* Automatically format a number based on its decimal.
* @param value number to be formatted
* @return If it's a decimal number, return a fixed two points precision.
* If it's a whole number, return the original value without any format.
*/
export function autoNumberFormat(value: number) {
return value % 1 === 0 ? d3NumberFormat(',')(value) : d3NumberFormat(',.2f')(value);
}
/**
* Automatically format a time based on its date.
* @param date object to be formatted
* @return a formatted time string depending on the time. For example,
* the start of February is formatted as "February", while February second is formatted as "Feb 2".
*/
export function autoTimeFormat(date: Date) {
const formatMillisecond = timeFormat('.%L'),
formatSecond = timeFormat(':%S'),
formatMinute = timeFormat('%I:%M'),
formatHour = timeFormat('%I %p'),
formatDay = timeFormat('%a %d'),
formatWeek = timeFormat('%b %d'),
formatMonth = timeFormat('%B'),
formatYear = timeFormat('%Y');
return (timeSecond(date) < date ? formatMillisecond
: timeMinute(date) < date ? formatSecond
: timeHour(date) < date ? formatMinute
: timeDay(date) < date ? formatHour
: timeMonth(date) < date ? (timeWeek(date) < date ? formatDay : formatWeek)
: timeYear(date) < date ? formatMonth
: formatYear)(date);
}