-
Notifications
You must be signed in to change notification settings - Fork 7
/
datatypes.js
59 lines (49 loc) · 1.16 KB
/
datatypes.js
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
const _ = require('./helper');
const I18N = require('./I18N');
const DataTypes = {
array(arr, sort = false, unit = '') {
return _.toList(arr, sort, v => DataTypes.format(v, unit));
},
object(obj) {
return _.toObject(obj, v => DataTypes.format(v));
},
null(label = null) {
if (label === null) {
label = _.t('n/a');
}
return _.toNothing(label);
},
number(num, unit = '') {
if (typeof num !== 'number') {
num = parseFloat(num);
}
return _.unit(I18N.numberFormatter.format(num), unit);
},
string(str, unit = '') {
return _.unit(_.e(str).replace(/(\r\n|\r|\n){2,}/g, '<br>'), unit);
},
boolean(bool) {
return bool ? '✔️' : '❌';
},
format(value, unit = '') {
if (typeof value === 'boolean') {
return DataTypes.boolean(value);
}
else if (typeof value === 'number') {
return DataTypes.number(value, unit);
}
else if (typeof value === 'string') {
return DataTypes.string(value, unit);
}
else if (Array.isArray(value)) {
return DataTypes.array(value, unit);
}
else if (_.isObject(value)) {
return DataTypes.object(value);
}
else {
return DataTypes.null();
}
}
};
module.exports = DataTypes;