forked from cloudforet-io/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget-helper.ts
47 lines (37 loc) · 1.99 KB
/
widget-helper.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
import bytes from 'bytes';
import { byteFormatter, customNumberFormatter, numberFormatter } from '@cloudforet/utils';
import { DATE_FIELD } from '@/common/modules/widgets/_constants/widget-constant';
import { NUMBER_FORMAT } from '@/common/modules/widgets/_constants/widget-field-constant';
import type { NumberFormatInfo } from '@/common/modules/widgets/_widget-fields/number-format/type';
import { SIZE_UNITS } from '@/services/asset-inventory/constants/asset-analysis-constant';
export const sortWidgetTableFields = (fields: string[]): string[] => {
const priorityFields = Object.values(DATE_FIELD) as string[];
const prioritySet = new Set(priorityFields);
const priority = fields.filter((field) => prioritySet.has(field));
const others = fields.filter((field) => !prioritySet.has(field));
const sortedPriority = priorityFields.filter((field) => priority.includes(field));
return [...sortedPriority, ...others];
};
/* Widget Number Format */
export const getFormattedNumber = (val: number, numberFormatInfo?: NumberFormatInfo, unit?: string): string => {
if (!numberFormatInfo) return numberFormatter(val) || '--';
const _targetNumberFormat = numberFormatInfo;
switch (_targetNumberFormat?.format) {
case NUMBER_FORMAT.AUTO:
if (unit && SIZE_UNITS.includes(unit)) {
const _originalVal = bytes.parse(`${val}${unit}`);
return byteFormatter(_originalVal);
}
return numberFormatter(val, { notation: 'compact' }) || '--';
// NOTE: temporary remove short number
// case NUMBER_FORMAT.SHORT_NUMBER:
// return numberFormatter(val, { notation: 'compact' }) || '--';
case NUMBER_FORMAT.FULL_NUMBER:
return numberFormatter(val, { minimumFractionDigits: 2 }) || '--';
case NUMBER_FORMAT.CUSTOM:
if (!_targetNumberFormat.customNumberFormat) return val.toString();
return customNumberFormatter(_targetNumberFormat.customNumberFormat, val) || '--';
default:
return val.toString();
}
};