/
formatters.ts
152 lines (140 loc) · 4.43 KB
/
formatters.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
import * as moment from 'moment';
export type IFormatter = (val: any, data: object) => any;
/**
* Formatter methods which can be used with {@link Field.getFormatter} and
* {@link Field.setFormatter}.
*
* The methods in this class return a function for use with the formatter
* methods. Each method may define its own parameters that configure how
* the formatter operates. For example the date / time formatters take
* information on the formatting to be used.
*
* @export
*/
export default class Formatter {
/**
* Convert from SQL date / date time format (ISO8601) to a format given
* by the options parameter. Typically used with a get formatter.
*
* Uses MomentJS - formats are defined by Moment.
*
* @static
* @param {string} format Format to convert to.
* @returns {IFormatter} Configured formatter function
*/
public static sqlDateToFormat( format: string ): IFormatter {
return function(val: any, data: object ) {
if ( val === null ) {
return null;
}
return moment( val, 'YYYY-MM-DD' ).format( format );
};
}
/**
* Convert to SQL date / date time format (ISO8601) from a format given
* by the options parameter. Typically used with a set formatter.
*
* Uses MomentJS - formats are defined by Moment.
*
* @static
* @param {string} format Format to convert from.
* @returns {IFormatter} Configured formatter function
*/
public static formatToSqlDate( format: string ): IFormatter {
return function(val: any, data: object ) {
if ( val === null ) {
return null;
}
return moment( val, format ).format( 'YYYY-MM-DD' );
};
}
/**
* Convert from one date time format to another.
*
* Uses MomentJS - formats are defined by Moment.
*
* @static
* @param {string} from From format
* @param {string} to To format.
* @returns {IFormatter} Configured formatter function
*/
public static dateTime( from: string, to: string ): IFormatter {
return function(val: any, data: object ) {
if ( val === null ) {
return null;
}
return moment( val, from ).format( to );
};
}
/**
* Convert a string of values into an array for use with checkboxes.
*
* @static
* @param {string} [delimiter='|'] Delimiter string (i.e. what to split on)
* @returns {IFormatter} Configured formatter function
*/
public static explode( delimiter: string = '|' ): IFormatter {
return function(val: any, data: object ) {
return val.toString().split( delimiter );
};
}
/**
* Convert an array of values from a checkbox into a string which can be
* used to store in a text field in a database.
*
* @static
* @param {string} [delimiter='|'] Delimiter string (i.e. what to join on)
* @returns {IFormatter} Configured formatter function
*/
public static implode( delimiter: string = '|' ): IFormatter {
return function(val: any, data: object ) {
return val.join( delimiter );
};
}
/**
* Convert an empty string to `null`. Null values are very useful in
* databases, but HTTP variables have no way of representing `null` as a
* value, often leading to an empty string and null overlapping. This method
* will check the value to operate on and return null if it is empty.
*
* @static
* @param {*} emptyValue Value to use if an empty value is submitted. `null`
* is the typical value.
* @returns {IFormatter} Configured formatter function
*/
public static ifEmpty( emptyValue: any ): IFormatter {
return function(val: any, data: object ) {
return val === '' ?
emptyValue :
val;
};
}
/**
* Convert a number from using any character other than a period (dot) to
* one which does use a period. This is useful for allowing numeric user
* input in regions where a comma is used as the decimal character. Use with
* a set formatter.
*
* @static
* @param {string} [char=','] Decimal place character
* @returns {IFormatter} Configured formatter function
*/
public static fromDecimalChar( char: string = ',' ): IFormatter {
return function(val: any, data: object ) {
return val.replace( char, '.' );
};
}
/**
* Convert a number with a period (dot) as the decimal character to use
* a different character (typically a comma). Use with a get formatter.
*
* @static
* @param {string} [char=','] Decimal place character
* @returns {IFormatter} Configured formatter function
*/
public static toDecimalChar( char: string = ',' ): IFormatter {
return function(val: any, data: object ) {
return val.replace( '.', char );
};
}
}