@perses-dev/components 0.54.0-beta.0 → 0.54.0-beta.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ContentWithLegend/ContentWithLegend.js +1 -1
- package/dist/ContentWithLegend/ContentWithLegend.js.map +1 -1
- package/dist/ContentWithLegend/model/content-with-legend-model.d.ts +1 -1
- package/dist/ContentWithLegend/model/content-with-legend-model.d.ts.map +1 -1
- package/dist/ContentWithLegend/model/content-with-legend-model.js +1 -1
- package/dist/ContentWithLegend/model/content-with-legend-model.js.map +1 -1
- package/dist/FormEditor/FormActions.d.ts +1 -1
- package/dist/FormEditor/FormActions.d.ts.map +1 -1
- package/dist/FormEditor/FormActions.js.map +1 -1
- package/dist/FormatControls/FormatControls.d.ts +1 -1
- package/dist/FormatControls/FormatControls.d.ts.map +1 -1
- package/dist/FormatControls/FormatControls.js +1 -1
- package/dist/FormatControls/FormatControls.js.map +1 -1
- package/dist/FormatControls/UnitSelector.d.ts +1 -1
- package/dist/FormatControls/UnitSelector.d.ts.map +1 -1
- package/dist/FormatControls/UnitSelector.js +1 -1
- package/dist/FormatControls/UnitSelector.js.map +1 -1
- package/dist/Legend/Legend.js +1 -1
- package/dist/Legend/Legend.js.map +1 -1
- package/dist/Legend/legend-model.d.ts +1 -1
- package/dist/Legend/legend-model.d.ts.map +1 -1
- package/dist/Legend/legend-model.js.map +1 -1
- package/dist/Table/Table.d.ts +1 -1
- package/dist/Table/Table.d.ts.map +1 -1
- package/dist/Table/Table.js +26 -6
- package/dist/Table/Table.js.map +1 -1
- package/dist/Table/TableToolbar.d.ts +34 -0
- package/dist/Table/TableToolbar.d.ts.map +1 -0
- package/dist/Table/TableToolbar.js +127 -0
- package/dist/Table/TableToolbar.js.map +1 -0
- package/dist/Table/VirtualizedTable.d.ts +3 -1
- package/dist/Table/VirtualizedTable.d.ts.map +1 -1
- package/dist/Table/VirtualizedTable.js +128 -117
- package/dist/Table/VirtualizedTable.js.map +1 -1
- package/dist/Table/VirtualizedTableContainer.d.ts.map +1 -1
- package/dist/Table/VirtualizedTableContainer.js +5 -1
- package/dist/Table/VirtualizedTableContainer.js.map +1 -1
- package/dist/Table/hooks/useFuzzySearch.d.ts +12 -0
- package/dist/Table/hooks/useFuzzySearch.d.ts.map +1 -0
- package/dist/Table/hooks/useFuzzySearch.js +43 -0
- package/dist/Table/hooks/useFuzzySearch.js.map +1 -0
- package/dist/Table/model/table-model.d.ts +37 -3
- package/dist/Table/model/table-model.d.ts.map +1 -1
- package/dist/Table/model/table-model.js.map +1 -1
- package/dist/ThresholdsEditor/ThresholdInput.d.ts +1 -1
- package/dist/ThresholdsEditor/ThresholdInput.d.ts.map +1 -1
- package/dist/ThresholdsEditor/ThresholdInput.js.map +1 -1
- package/dist/ThresholdsEditor/ThresholdsEditor.d.ts +1 -1
- package/dist/ThresholdsEditor/ThresholdsEditor.d.ts.map +1 -1
- package/dist/ThresholdsEditor/ThresholdsEditor.js.map +1 -1
- package/dist/TimeSeriesTooltip/TimeChartTooltip.d.ts +1 -2
- package/dist/TimeSeriesTooltip/TimeChartTooltip.d.ts.map +1 -1
- package/dist/TimeSeriesTooltip/TimeChartTooltip.js.map +1 -1
- package/dist/TimeSeriesTooltip/nearby-series.d.ts +2 -2
- package/dist/TimeSeriesTooltip/nearby-series.d.ts.map +1 -1
- package/dist/TimeSeriesTooltip/nearby-series.js +1 -2
- package/dist/TimeSeriesTooltip/nearby-series.js.map +1 -1
- package/dist/TransformsEditor/TransformEditor.d.ts +1 -1
- package/dist/TransformsEditor/TransformEditor.d.ts.map +1 -1
- package/dist/TransformsEditor/TransformEditor.js.map +1 -1
- package/dist/TransformsEditor/TransformEditorContainer.d.ts.map +1 -1
- package/dist/TransformsEditor/TransformEditorContainer.js +1 -1
- package/dist/TransformsEditor/TransformEditorContainer.js.map +1 -1
- package/dist/TransformsEditor/TransformsEditor.d.ts +1 -1
- package/dist/TransformsEditor/TransformsEditor.d.ts.map +1 -1
- package/dist/TransformsEditor/TransformsEditor.js.map +1 -1
- package/dist/cjs/ContentWithLegend/ContentWithLegend.js +2 -2
- package/dist/cjs/ContentWithLegend/model/content-with-legend-model.js +2 -2
- package/dist/cjs/FormatControls/FormatControls.js +4 -4
- package/dist/cjs/FormatControls/UnitSelector.js +3 -3
- package/dist/cjs/Legend/Legend.js +2 -2
- package/dist/cjs/Table/Table.js +24 -4
- package/dist/cjs/Table/TableToolbar.js +140 -0
- package/dist/cjs/Table/VirtualizedTable.js +126 -115
- package/dist/cjs/Table/VirtualizedTableContainer.js +5 -1
- package/dist/cjs/Table/hooks/useFuzzySearch.js +48 -0
- package/dist/cjs/TimeSeriesTooltip/nearby-series.js +2 -3
- package/dist/cjs/TransformsEditor/TransformEditorContainer.js +2 -2
- package/dist/cjs/model/action.js +43 -0
- package/dist/cjs/model/bits.js +113 -0
- package/dist/cjs/model/bytes.js +115 -0
- package/dist/cjs/model/constants.js +23 -0
- package/dist/cjs/model/currency.js +126 -0
- package/dist/cjs/model/date.js +297 -0
- package/dist/cjs/model/decimal.js +72 -0
- package/dist/cjs/model/formatterCache.js +120 -0
- package/dist/cjs/model/index.js +16 -0
- package/dist/cjs/model/legend.js +101 -0
- package/dist/cjs/model/percent.js +82 -0
- package/dist/cjs/model/temperature.js +72 -0
- package/dist/cjs/model/thresholds.js +16 -0
- package/dist/cjs/model/throughput.js +161 -0
- package/dist/cjs/model/time.js +178 -0
- package/dist/cjs/model/transforms.js +29 -0
- package/dist/cjs/model/types.js +16 -0
- package/dist/cjs/model/units.js +186 -0
- package/dist/cjs/model/utils.js +48 -0
- package/dist/cjs/utils/axis.js +5 -5
- package/dist/cjs/utils/index.js +1 -0
- package/dist/cjs/utils/request-interpolation.js +49 -0
- package/dist/model/action.d.ts +4 -0
- package/dist/model/action.d.ts.map +1 -0
- package/dist/model/action.js +27 -0
- package/dist/model/action.js.map +1 -0
- package/dist/model/bits.d.ts +12 -0
- package/dist/model/bits.d.ts.map +1 -0
- package/dist/model/bits.js +89 -0
- package/dist/model/bits.js.map +1 -0
- package/dist/model/bytes.d.ts +12 -0
- package/dist/model/bytes.d.ts.map +1 -0
- package/dist/model/bytes.js +91 -0
- package/dist/model/bytes.js.map +1 -0
- package/dist/model/constants.d.ts +2 -0
- package/dist/model/constants.d.ts.map +1 -0
- package/dist/model/constants.js +15 -0
- package/dist/model/constants.js.map +1 -0
- package/dist/model/currency.d.ts +11 -0
- package/dist/model/currency.d.ts.map +1 -0
- package/dist/model/currency.js +107 -0
- package/dist/model/currency.js.map +1 -0
- package/dist/model/date.d.ts +28 -0
- package/dist/model/date.d.ts.map +1 -0
- package/dist/model/date.js +278 -0
- package/dist/model/date.js.map +1 -0
- package/dist/model/decimal.d.ts +12 -0
- package/dist/model/decimal.d.ts.map +1 -0
- package/dist/model/decimal.js +53 -0
- package/dist/model/decimal.js.map +1 -0
- package/dist/model/formatterCache.d.ts +11 -0
- package/dist/model/formatterCache.d.ts.map +1 -0
- package/dist/model/formatterCache.js +104 -0
- package/dist/model/formatterCache.js.map +1 -0
- package/dist/model/index.d.ts +16 -0
- package/dist/model/index.d.ts.map +1 -1
- package/dist/model/index.js +16 -0
- package/dist/model/index.js.map +1 -1
- package/dist/model/legend.d.ts +19 -0
- package/dist/model/legend.d.ts.map +1 -0
- package/dist/model/legend.js +61 -0
- package/dist/model/legend.js.map +1 -0
- package/dist/model/percent.d.ts +11 -0
- package/dist/model/percent.d.ts.map +1 -0
- package/dist/model/percent.js +63 -0
- package/dist/model/percent.js.map +1 -0
- package/dist/model/temperature.d.ts +11 -0
- package/dist/model/temperature.d.ts.map +1 -0
- package/dist/model/temperature.js +53 -0
- package/dist/model/temperature.js.map +1 -0
- package/dist/model/theme.d.ts +1 -1
- package/dist/model/theme.d.ts.map +1 -1
- package/dist/model/theme.js.map +1 -1
- package/dist/model/thresholds.d.ts +16 -0
- package/dist/model/thresholds.d.ts.map +1 -0
- package/dist/model/thresholds.js +15 -0
- package/dist/model/thresholds.js.map +1 -0
- package/dist/model/throughput.d.ts +12 -0
- package/dist/model/throughput.d.ts.map +1 -0
- package/dist/model/throughput.js +142 -0
- package/dist/model/throughput.js.map +1 -0
- package/dist/model/time.d.ts +23 -0
- package/dist/model/time.d.ts.map +1 -0
- package/dist/model/time.js +158 -0
- package/dist/model/time.js.map +1 -0
- package/dist/model/transforms.d.ts +43 -0
- package/dist/model/transforms.d.ts.map +1 -0
- package/dist/model/transforms.js +22 -0
- package/dist/model/transforms.js.map +1 -0
- package/dist/model/types.d.ts +56 -0
- package/dist/model/types.d.ts.map +1 -0
- package/dist/model/types.js +15 -0
- package/dist/model/types.js.map +1 -0
- package/dist/model/units.d.ts +105 -0
- package/dist/model/units.d.ts.map +1 -0
- package/dist/model/units.js +132 -0
- package/dist/model/units.js.map +1 -0
- package/dist/model/utils.d.ts +4 -0
- package/dist/model/utils.d.ts.map +1 -0
- package/dist/model/utils.js +32 -0
- package/dist/model/utils.js.map +1 -0
- package/dist/utils/axis.d.ts +1 -1
- package/dist/utils/axis.d.ts.map +1 -1
- package/dist/utils/axis.js +1 -1
- package/dist/utils/axis.js.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/request-interpolation.d.ts +6 -0
- package/dist/utils/request-interpolation.d.ts.map +1 -0
- package/dist/utils/request-interpolation.js +33 -0
- package/dist/utils/request-interpolation.js.map +1 -0
- package/package.json +2 -1
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
// Copyright The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
import { MAX_SIGNIFICANT_DIGITS } from './constants';
|
|
14
|
+
import { getFormatterFromCache } from './formatterCache';
|
|
15
|
+
import { hasDecimalPlaces, limitDecimalPlaces } from './utils';
|
|
16
|
+
const TIME_GROUP = 'Time';
|
|
17
|
+
export const TIME_GROUP_CONFIG = {
|
|
18
|
+
label: 'Time',
|
|
19
|
+
decimalPlaces: true
|
|
20
|
+
};
|
|
21
|
+
export const TIME_UNIT_CONFIG = {
|
|
22
|
+
nanoseconds: {
|
|
23
|
+
group: TIME_GROUP,
|
|
24
|
+
label: 'Nanoseconds'
|
|
25
|
+
},
|
|
26
|
+
microseconds: {
|
|
27
|
+
group: TIME_GROUP,
|
|
28
|
+
label: 'Microseconds'
|
|
29
|
+
},
|
|
30
|
+
milliseconds: {
|
|
31
|
+
group: TIME_GROUP,
|
|
32
|
+
label: 'Milliseconds'
|
|
33
|
+
},
|
|
34
|
+
seconds: {
|
|
35
|
+
group: TIME_GROUP,
|
|
36
|
+
label: 'Seconds'
|
|
37
|
+
},
|
|
38
|
+
minutes: {
|
|
39
|
+
group: TIME_GROUP,
|
|
40
|
+
label: 'Minutes'
|
|
41
|
+
},
|
|
42
|
+
hours: {
|
|
43
|
+
group: TIME_GROUP,
|
|
44
|
+
label: 'Hours'
|
|
45
|
+
},
|
|
46
|
+
days: {
|
|
47
|
+
group: TIME_GROUP,
|
|
48
|
+
label: 'Days'
|
|
49
|
+
},
|
|
50
|
+
weeks: {
|
|
51
|
+
group: TIME_GROUP,
|
|
52
|
+
label: 'Weeks'
|
|
53
|
+
},
|
|
54
|
+
months: {
|
|
55
|
+
group: TIME_GROUP,
|
|
56
|
+
label: 'Months'
|
|
57
|
+
},
|
|
58
|
+
years: {
|
|
59
|
+
group: TIME_GROUP,
|
|
60
|
+
label: 'Years'
|
|
61
|
+
}
|
|
62
|
+
};
|
|
63
|
+
// Mapping of time units to what Intl.NumberFormat formatter expects
|
|
64
|
+
// https://v8.dev/features/intl-numberformat#units
|
|
65
|
+
export var PersesTimeToIntlTime = /*#__PURE__*/ function(PersesTimeToIntlTime) {
|
|
66
|
+
PersesTimeToIntlTime["nanoseconds"] = "nanosecond";
|
|
67
|
+
PersesTimeToIntlTime["microseconds"] = "microsecond";
|
|
68
|
+
PersesTimeToIntlTime["milliseconds"] = "millisecond";
|
|
69
|
+
PersesTimeToIntlTime["seconds"] = "second";
|
|
70
|
+
PersesTimeToIntlTime["minutes"] = "minute";
|
|
71
|
+
PersesTimeToIntlTime["hours"] = "hour";
|
|
72
|
+
PersesTimeToIntlTime["days"] = "day";
|
|
73
|
+
PersesTimeToIntlTime["weeks"] = "week";
|
|
74
|
+
PersesTimeToIntlTime["months"] = "month";
|
|
75
|
+
PersesTimeToIntlTime["years"] = "year";
|
|
76
|
+
return PersesTimeToIntlTime;
|
|
77
|
+
}({});
|
|
78
|
+
/**
|
|
79
|
+
* Note: This conversion will not be exactly accurate for months and years,
|
|
80
|
+
* due variations in the lengths of months (i.e. 28 - 31 days) and years (i.e. leap years).
|
|
81
|
+
* For precision with months and years, we would need more complex algorithms and/or external libraries.
|
|
82
|
+
* However, we expect that measurements in months and years will be rare.
|
|
83
|
+
*/ const TIME_UNITS_IN_SECONDS = {
|
|
84
|
+
years: 31536000,
|
|
85
|
+
months: 2592000,
|
|
86
|
+
weeks: 604800,
|
|
87
|
+
days: 86400,
|
|
88
|
+
hours: 3600,
|
|
89
|
+
minutes: 60,
|
|
90
|
+
seconds: 1,
|
|
91
|
+
milliseconds: 0.001,
|
|
92
|
+
microseconds: 0.000001,
|
|
93
|
+
nanoseconds: 0.000000001
|
|
94
|
+
};
|
|
95
|
+
const LARGEST_TO_SMALLEST_TIME_UNITS = [
|
|
96
|
+
'years',
|
|
97
|
+
'months',
|
|
98
|
+
'weeks',
|
|
99
|
+
'days',
|
|
100
|
+
'hours',
|
|
101
|
+
'minutes',
|
|
102
|
+
'seconds',
|
|
103
|
+
'milliseconds',
|
|
104
|
+
'microseconds',
|
|
105
|
+
'nanoseconds'
|
|
106
|
+
];
|
|
107
|
+
/**
|
|
108
|
+
* Choose the first time unit that produces a number greater than 1, starting from the biggest time unit.
|
|
109
|
+
*/ function getValueAndKindForNaturalNumbers(value, unit) {
|
|
110
|
+
const valueInSeconds = value * TIME_UNITS_IN_SECONDS[unit];
|
|
111
|
+
// Initialize for TS
|
|
112
|
+
const largestTimeUnit = LARGEST_TO_SMALLEST_TIME_UNITS[0] || 'years';
|
|
113
|
+
let timeUnit = largestTimeUnit;
|
|
114
|
+
let valueInTimeUnit = valueInSeconds / TIME_UNITS_IN_SECONDS[largestTimeUnit];
|
|
115
|
+
for (timeUnit of LARGEST_TO_SMALLEST_TIME_UNITS){
|
|
116
|
+
valueInTimeUnit = valueInSeconds / TIME_UNITS_IN_SECONDS[timeUnit];
|
|
117
|
+
if (valueInTimeUnit >= 1) {
|
|
118
|
+
return {
|
|
119
|
+
value: valueInTimeUnit,
|
|
120
|
+
unit: timeUnit
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
// If we didn't find a time unit, we have to settle for the smallest time unit (which is the last time unit).
|
|
125
|
+
return {
|
|
126
|
+
value: valueInTimeUnit,
|
|
127
|
+
unit: timeUnit
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
function isMonthOrYear(unit) {
|
|
131
|
+
return unit === 'months' || unit === 'years';
|
|
132
|
+
}
|
|
133
|
+
export function formatTime(value, { unit, decimalPlaces }) {
|
|
134
|
+
if (value === 0) return '0s';
|
|
135
|
+
const results = getValueAndKindForNaturalNumbers(value, unit ?? 'seconds');
|
|
136
|
+
const formatterOptions = {
|
|
137
|
+
style: 'unit',
|
|
138
|
+
unit: PersesTimeToIntlTime[results.unit],
|
|
139
|
+
unitDisplay: isMonthOrYear(results.unit) ? 'long' : 'narrow'
|
|
140
|
+
};
|
|
141
|
+
if (hasDecimalPlaces(decimalPlaces)) {
|
|
142
|
+
formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);
|
|
143
|
+
formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);
|
|
144
|
+
} else {
|
|
145
|
+
formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
|
|
146
|
+
}
|
|
147
|
+
const key = [
|
|
148
|
+
formatterOptions.style,
|
|
149
|
+
formatterOptions.unit,
|
|
150
|
+
formatterOptions.unitDisplay,
|
|
151
|
+
formatterOptions.maximumSignificantDigits,
|
|
152
|
+
decimalPlaces,
|
|
153
|
+
unit ?? 'seconds'
|
|
154
|
+
];
|
|
155
|
+
return getFormatterFromCache(key, 'time', formatterOptions, 'en-US')(results.value);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
//# sourceMappingURL=time.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/model/time.ts"],"sourcesContent":["// Copyright The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { getFormatterFromCache } from './formatterCache';\nimport { UnitGroupConfig, UnitConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces } from './utils';\n\ntype TimeUnits =\n | 'nanoseconds'\n | 'microseconds'\n | 'milliseconds'\n | 'seconds'\n | 'minutes'\n | 'hours'\n | 'days'\n | 'weeks'\n | 'months'\n | 'years';\nexport type TimeFormatOptions = {\n unit?: TimeUnits;\n decimalPlaces?: number;\n};\nconst TIME_GROUP = 'Time';\nexport const TIME_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Time',\n decimalPlaces: true,\n};\nexport const TIME_UNIT_CONFIG: Readonly<Record<TimeUnits, UnitConfig>> = {\n nanoseconds: {\n group: TIME_GROUP,\n label: 'Nanoseconds',\n },\n microseconds: {\n group: TIME_GROUP,\n label: 'Microseconds',\n },\n milliseconds: {\n group: TIME_GROUP,\n label: 'Milliseconds',\n },\n seconds: {\n group: TIME_GROUP,\n label: 'Seconds',\n },\n minutes: {\n group: TIME_GROUP,\n label: 'Minutes',\n },\n hours: {\n group: TIME_GROUP,\n label: 'Hours',\n },\n days: {\n group: TIME_GROUP,\n label: 'Days',\n },\n weeks: {\n group: TIME_GROUP,\n label: 'Weeks',\n },\n months: {\n group: TIME_GROUP,\n label: 'Months',\n },\n years: {\n group: TIME_GROUP,\n label: 'Years',\n },\n};\n\n// Mapping of time units to what Intl.NumberFormat formatter expects\n// https://v8.dev/features/intl-numberformat#units\nexport enum PersesTimeToIntlTime {\n nanoseconds = 'nanosecond',\n microseconds = 'microsecond',\n milliseconds = 'millisecond',\n seconds = 'second',\n minutes = 'minute',\n hours = 'hour',\n days = 'day',\n weeks = 'week',\n months = 'month',\n years = 'year',\n}\n\n/**\n * Note: This conversion will not be exactly accurate for months and years,\n * due variations in the lengths of months (i.e. 28 - 31 days) and years (i.e. leap years).\n * For precision with months and years, we would need more complex algorithms and/or external libraries.\n * However, we expect that measurements in months and years will be rare.\n */\nconst TIME_UNITS_IN_SECONDS: Record<TimeUnits, number> = {\n years: 31536000, // 365 days\n months: 2592000, // 30 days\n weeks: 604800, // 7 days\n days: 86400,\n hours: 3600,\n minutes: 60,\n seconds: 1,\n milliseconds: 0.001,\n microseconds: 0.000001,\n nanoseconds: 0.000000001,\n};\n\nconst LARGEST_TO_SMALLEST_TIME_UNITS: TimeUnits[] = [\n 'years',\n 'months',\n 'weeks',\n 'days',\n 'hours',\n 'minutes',\n 'seconds',\n 'milliseconds',\n 'microseconds',\n 'nanoseconds',\n];\n\n/**\n * Choose the first time unit that produces a number greater than 1, starting from the biggest time unit.\n */\nfunction getValueAndKindForNaturalNumbers(value: number, unit: TimeUnits): { value: number; unit: TimeUnits } {\n const valueInSeconds = value * TIME_UNITS_IN_SECONDS[unit];\n\n // Initialize for TS\n const largestTimeUnit = LARGEST_TO_SMALLEST_TIME_UNITS[0] || 'years';\n let timeUnit: TimeUnits = largestTimeUnit;\n let valueInTimeUnit: number = valueInSeconds / TIME_UNITS_IN_SECONDS[largestTimeUnit];\n\n for (timeUnit of LARGEST_TO_SMALLEST_TIME_UNITS) {\n valueInTimeUnit = valueInSeconds / TIME_UNITS_IN_SECONDS[timeUnit];\n if (valueInTimeUnit >= 1) {\n return { value: valueInTimeUnit, unit: timeUnit };\n }\n }\n\n // If we didn't find a time unit, we have to settle for the smallest time unit (which is the last time unit).\n return { value: valueInTimeUnit, unit: timeUnit };\n}\n\nfunction isMonthOrYear(unit: TimeUnits): boolean {\n return unit === 'months' || unit === 'years';\n}\n\nexport function formatTime(value: number, { unit, decimalPlaces }: TimeFormatOptions): string {\n if (value === 0) return '0s';\n\n const results = getValueAndKindForNaturalNumbers(value, unit ?? 'seconds');\n\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'unit',\n unit: PersesTimeToIntlTime[results.unit],\n unitDisplay: isMonthOrYear(results.unit) ? 'long' : 'narrow',\n };\n\n if (hasDecimalPlaces(decimalPlaces)) {\n formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);\n } else {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n\n const key = [\n formatterOptions.style,\n formatterOptions.unit,\n formatterOptions.unitDisplay,\n formatterOptions.maximumSignificantDigits,\n decimalPlaces,\n unit ?? 'seconds',\n ];\n\n return getFormatterFromCache(key, 'time', formatterOptions, 'en-US')(results.value);\n}\n"],"names":["MAX_SIGNIFICANT_DIGITS","getFormatterFromCache","hasDecimalPlaces","limitDecimalPlaces","TIME_GROUP","TIME_GROUP_CONFIG","label","decimalPlaces","TIME_UNIT_CONFIG","nanoseconds","group","microseconds","milliseconds","seconds","minutes","hours","days","weeks","months","years","PersesTimeToIntlTime","TIME_UNITS_IN_SECONDS","LARGEST_TO_SMALLEST_TIME_UNITS","getValueAndKindForNaturalNumbers","value","unit","valueInSeconds","largestTimeUnit","timeUnit","valueInTimeUnit","isMonthOrYear","formatTime","results","formatterOptions","style","unitDisplay","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","key"],"mappings":"AAAA,+BAA+B;AAC/B,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,sBAAsB,QAAQ,cAAc;AACrD,SAASC,qBAAqB,QAAQ,mBAAmB;AAEzD,SAASC,gBAAgB,EAAEC,kBAAkB,QAAQ,UAAU;AAiB/D,MAAMC,aAAa;AACnB,OAAO,MAAMC,oBAAqC;IAChDC,OAAO;IACPC,eAAe;AACjB,EAAE;AACF,OAAO,MAAMC,mBAA4D;IACvEC,aAAa;QACXC,OAAON;QACPE,OAAO;IACT;IACAK,cAAc;QACZD,OAAON;QACPE,OAAO;IACT;IACAM,cAAc;QACZF,OAAON;QACPE,OAAO;IACT;IACAO,SAAS;QACPH,OAAON;QACPE,OAAO;IACT;IACAQ,SAAS;QACPJ,OAAON;QACPE,OAAO;IACT;IACAS,OAAO;QACLL,OAAON;QACPE,OAAO;IACT;IACAU,MAAM;QACJN,OAAON;QACPE,OAAO;IACT;IACAW,OAAO;QACLP,OAAON;QACPE,OAAO;IACT;IACAY,QAAQ;QACNR,OAAON;QACPE,OAAO;IACT;IACAa,OAAO;QACLT,OAAON;QACPE,OAAO;IACT;AACF,EAAE;AAEF,oEAAoE;AACpE,kDAAkD;AAClD,OAAO,I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BAAmBI;IAC9D,OAAO;QACL0B,iBAAiBK,wBAAwB,GAAGtC;IAC9C;IAEA,MAAMuC,MAAM;QACVN,iBAAiBC,KAAK;QACtBD,iBAAiBR,IAAI;QACrBQ,iBAAiBE,WAAW;QAC5BF,iBAAiBK,wBAAwB;QACzC/B;QACAkB,QAAQ;KACT;IAED,OAAOxB,sBAAsBsC,KAAK,QAAQN,kBAAkB,SAASD,QAAQR,KAAK;AACpF"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
export interface TransformCommonSpec {
|
|
2
|
+
disabled?: boolean;
|
|
3
|
+
}
|
|
4
|
+
export interface JoinByColumnValueTransform {
|
|
5
|
+
kind: 'JoinByColumnValue';
|
|
6
|
+
spec: TransformCommonSpec & {
|
|
7
|
+
columns: string[];
|
|
8
|
+
};
|
|
9
|
+
}
|
|
10
|
+
export interface ExtractColumnFieldsTransform {
|
|
11
|
+
kind: 'ExtractColumnFields';
|
|
12
|
+
spec: TransformCommonSpec & {
|
|
13
|
+
column: string;
|
|
14
|
+
format: 'JSON' | 'Regex' | 'SplitByDelimiter' | 'KeyValuePairs';
|
|
15
|
+
matcher?: string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
export interface MergeColumnsTransform {
|
|
19
|
+
kind: 'MergeColumns';
|
|
20
|
+
spec: TransformCommonSpec & {
|
|
21
|
+
columns: string[];
|
|
22
|
+
name: string;
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
export interface MergeIndexedColumnsTransform {
|
|
26
|
+
kind: 'MergeIndexedColumns';
|
|
27
|
+
spec: TransformCommonSpec & {
|
|
28
|
+
column: string;
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
export interface MergeSeriesTransform {
|
|
32
|
+
kind: 'MergeSeries';
|
|
33
|
+
spec: TransformCommonSpec;
|
|
34
|
+
}
|
|
35
|
+
export type Transform = JoinByColumnValueTransform | MergeColumnsTransform | MergeIndexedColumnsTransform | MergeSeriesTransform | ExtractColumnFieldsTransform;
|
|
36
|
+
export declare const TRANSFORM_TEXT: {
|
|
37
|
+
JoinByColumnValue: string;
|
|
38
|
+
MergeColumns: string;
|
|
39
|
+
MergeIndexedColumns: string;
|
|
40
|
+
MergeSeries: string;
|
|
41
|
+
ExtractColumnFields: string;
|
|
42
|
+
};
|
|
43
|
+
//# sourceMappingURL=transforms.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"transforms.d.ts","sourceRoot":"","sources":["../../src/model/transforms.ts"],"names":[],"mappings":"AAaA,MAAM,WAAW,mBAAmB;IAClC,QAAQ,CAAC,EAAE,OAAO,CAAC;CACpB;AAED,MAAM,WAAW,0BAA0B;IACzC,IAAI,EAAE,mBAAmB,CAAC;IAC1B,IAAI,EAAE,mBAAmB,GAAG;QAC1B,OAAO,EAAE,MAAM,EAAE,CAAC;KACnB,CAAC;CACH;AAED,MAAM,WAAW,4BAA4B;IAC3C,IAAI,EAAE,qBAAqB,CAAC;IAC5B,IAAI,EAAE,mBAAmB,GAAG;QAC1B,MAAM,EAAE,MAAM,CAAC;QACf,MAAM,EAAE,MAAM,GAAG,OAAO,GAAG,kBAAkB,GAAG,eAAe,CAAC;QAChE,OAAO,CAAC,EAAE,MAAM,CAAC;KAClB,CAAC;CACH;AAED,MAAM,WAAW,qBAAqB;IACpC,IAAI,EAAE,cAAc,CAAC;IACrB,IAAI,EAAE,mBAAmB,GAAG;QAC1B,OAAO,EAAE,MAAM,EAAE,CAAC;QAClB,IAAI,EAAE,MAAM,CAAC;KACd,CAAC;CACH;AAED,MAAM,WAAW,4BAA4B;IAC3C,IAAI,EAAE,qBAAqB,CAAC;IAC5B,IAAI,EAAE,mBAAmB,GAAG;QAC1B,MAAM,EAAE,MAAM,CAAC;KAChB,CAAC;CACH;AAED,MAAM,WAAW,oBAAoB;IACnC,IAAI,EAAE,aAAa,CAAC;IACpB,IAAI,EAAE,mBAAmB,CAAC;CAC3B;AAED,MAAM,MAAM,SAAS,GACjB,0BAA0B,GAC1B,qBAAqB,GACrB,4BAA4B,GAC5B,oBAAoB,GACpB,4BAA4B,CAAC;AAGjC,eAAO,MAAM,cAAc;;;;;;CAM1B,CAAC"}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// Copyright The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
// Can be moved somewhere else
|
|
14
|
+
export const TRANSFORM_TEXT = {
|
|
15
|
+
JoinByColumnValue: 'Join by column value',
|
|
16
|
+
MergeColumns: 'Merge columns',
|
|
17
|
+
MergeIndexedColumns: 'Merge indexed columns',
|
|
18
|
+
MergeSeries: 'Merge series',
|
|
19
|
+
ExtractColumnFields: 'Extract column fields'
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
//# sourceMappingURL=transforms.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/model/transforms.ts"],"sourcesContent":["// Copyright The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nexport interface TransformCommonSpec {\n disabled?: boolean;\n}\n\nexport interface JoinByColumnValueTransform {\n kind: 'JoinByColumnValue';\n spec: TransformCommonSpec & {\n columns: string[];\n };\n}\n\nexport interface ExtractColumnFieldsTransform {\n kind: 'ExtractColumnFields';\n spec: TransformCommonSpec & {\n column: string;\n format: 'JSON' | 'Regex' | 'SplitByDelimiter' | 'KeyValuePairs';\n matcher?: string;\n };\n}\n\nexport interface MergeColumnsTransform {\n kind: 'MergeColumns';\n spec: TransformCommonSpec & {\n columns: string[];\n name: string;\n };\n}\n\nexport interface MergeIndexedColumnsTransform {\n kind: 'MergeIndexedColumns';\n spec: TransformCommonSpec & {\n column: string;\n };\n}\n\nexport interface MergeSeriesTransform {\n kind: 'MergeSeries';\n spec: TransformCommonSpec;\n}\n\nexport type Transform =\n | JoinByColumnValueTransform\n | MergeColumnsTransform\n | MergeIndexedColumnsTransform\n | MergeSeriesTransform\n | ExtractColumnFieldsTransform;\n\n// Can be moved somewhere else\nexport const TRANSFORM_TEXT = {\n JoinByColumnValue: 'Join by column value',\n MergeColumns: 'Merge columns',\n MergeIndexedColumns: 'Merge indexed columns',\n MergeSeries: 'Merge series',\n ExtractColumnFields: 'Extract column fields',\n};\n"],"names":["TRANSFORM_TEXT","JoinByColumnValue","MergeColumns","MergeIndexedColumns","MergeSeries","ExtractColumnFields"],"mappings":"AAAA,+BAA+B;AAC/B,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAiDjC,8BAA8B;AAC9B,OAAO,MAAMA,iBAAiB;IAC5BC,mBAAmB;IACnBC,cAAc;IACdC,qBAAqB;IACrBC,aAAa;IACbC,qBAAqB;AACvB,EAAE"}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { Duration } from 'date-fns';
|
|
2
|
+
import { AbsoluteTimeRange, DurationString } from '@perses-dev/spec';
|
|
3
|
+
import { FormatOptions } from './units';
|
|
4
|
+
export type UnitGroup = 'Time' | 'Percent' | 'Decimal' | 'Bytes' | 'Bits' | 'Throughput' | 'Currency' | 'Temperature' | 'Date';
|
|
5
|
+
/**
|
|
6
|
+
* Configuration for rendering units that are part of a group.
|
|
7
|
+
*/
|
|
8
|
+
export type UnitGroupConfig = {
|
|
9
|
+
/**
|
|
10
|
+
* The label that is shown in the UI.
|
|
11
|
+
*/
|
|
12
|
+
label: string;
|
|
13
|
+
/**
|
|
14
|
+
* When true, the unit group supports setting decimal places.
|
|
15
|
+
*/
|
|
16
|
+
decimalPlaces?: boolean;
|
|
17
|
+
/**
|
|
18
|
+
* When true, the unit group supports enabling shortValues.
|
|
19
|
+
*/
|
|
20
|
+
shortValues?: boolean;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* Configuration for rendering a specific unit.
|
|
24
|
+
*/
|
|
25
|
+
export type UnitConfig = {
|
|
26
|
+
/**
|
|
27
|
+
* The group the unit is part of. This will inform common rendering behavior.
|
|
28
|
+
*/
|
|
29
|
+
group?: UnitGroup;
|
|
30
|
+
/**
|
|
31
|
+
* When true, this unit will not be displayed in the unit selector. This is
|
|
32
|
+
* useful for units that are shorthand variants of other units.
|
|
33
|
+
*/
|
|
34
|
+
disableSelectorOption?: boolean;
|
|
35
|
+
/**
|
|
36
|
+
* The label that is shown in the UI.
|
|
37
|
+
*/
|
|
38
|
+
label: string;
|
|
39
|
+
};
|
|
40
|
+
/**
|
|
41
|
+
* Used in the tests for each type of unit.
|
|
42
|
+
*/
|
|
43
|
+
export interface UnitTestCase {
|
|
44
|
+
value: number;
|
|
45
|
+
format: FormatOptions;
|
|
46
|
+
expected: string;
|
|
47
|
+
}
|
|
48
|
+
export interface IntervalTestCase {
|
|
49
|
+
timeRange: AbsoluteTimeRange;
|
|
50
|
+
expected: Duration;
|
|
51
|
+
}
|
|
52
|
+
export interface FormatTestCase {
|
|
53
|
+
duration: Duration;
|
|
54
|
+
expected: DurationString;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=types.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/model/types.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AACpC,OAAO,EAAE,iBAAiB,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AACrE,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AAExC,MAAM,MAAM,SAAS,GACjB,MAAM,GACN,SAAS,GACT,SAAS,GACT,OAAO,GACP,MAAM,GACN,YAAY,GACZ,UAAU,GACV,aAAa,GACb,MAAM,CAAC;AAEX;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG;IAC5B;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;IACd;;OAEG;IACH,aAAa,CAAC,EAAE,OAAO,CAAC;IACxB;;OAEG;IACH,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,UAAU,GAAG;IACvB;;OAEG;IACH,KAAK,CAAC,EAAE,SAAS,CAAC;IAElB;;;OAGG;IACH,qBAAqB,CAAC,EAAE,OAAO,CAAC;IAEhC;;OAEG;IACH,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAEF;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,aAAa,CAAC;IACtB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,gBAAgB;IAC/B,SAAS,EAAE,iBAAiB,CAAC;IAC7B,QAAQ,EAAE,QAAQ,CAAC;CACpB;AAED,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,QAAQ,CAAC;IACnB,QAAQ,EAAE,cAAc,CAAC;CAC1B"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// Copyright The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
export { };
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=types.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/model/types.ts"],"sourcesContent":["// Copyright The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { Duration } from 'date-fns';\nimport { AbsoluteTimeRange, DurationString } from '@perses-dev/spec';\nimport { FormatOptions } from './units';\n\nexport type UnitGroup =\n | 'Time'\n | 'Percent'\n | 'Decimal'\n | 'Bytes'\n | 'Bits'\n | 'Throughput'\n | 'Currency'\n | 'Temperature'\n | 'Date';\n\n/**\n * Configuration for rendering units that are part of a group.\n */\nexport type UnitGroupConfig = {\n /**\n * The label that is shown in the UI.\n */\n label: string;\n /**\n * When true, the unit group supports setting decimal places.\n */\n decimalPlaces?: boolean;\n /**\n * When true, the unit group supports enabling shortValues.\n */\n shortValues?: boolean;\n};\n\n/**\n * Configuration for rendering a specific unit.\n */\nexport type UnitConfig = {\n /**\n * The group the unit is part of. This will inform common rendering behavior.\n */\n group?: UnitGroup;\n\n /**\n * When true, this unit will not be displayed in the unit selector. This is\n * useful for units that are shorthand variants of other units.\n */\n disableSelectorOption?: boolean;\n\n /**\n * The label that is shown in the UI.\n */\n label: string;\n};\n\n/**\n * Used in the tests for each type of unit.\n */\nexport interface UnitTestCase {\n value: number;\n format: FormatOptions;\n expected: string;\n}\n\nexport interface IntervalTestCase {\n timeRange: AbsoluteTimeRange;\n expected: Duration;\n}\n\nexport interface FormatTestCase {\n duration: Duration;\n expected: DurationString;\n}\n"],"names":[],"mappings":"AAAA,+BAA+B;AAC/B,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAsEjC,WAGC"}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { BytesFormatOptions as BytesFormatOptions } from './bytes';
|
|
2
|
+
import { BitsFormatOptions as BitsFormatOptions } from './bits';
|
|
3
|
+
import { DecimalFormatOptions as DecimalFormatOptions } from './decimal';
|
|
4
|
+
import { PercentFormatOptions as PercentFormatOptions } from './percent';
|
|
5
|
+
import { TemperatureFormatOptions } from './temperature';
|
|
6
|
+
import { TimeFormatOptions as TimeFormatOptions } from './time';
|
|
7
|
+
import { UnitGroup, UnitGroupConfig, UnitConfig } from './types';
|
|
8
|
+
import { ThroughputFormatOptions } from './throughput';
|
|
9
|
+
import { CurrencyFormatOptions } from './currency';
|
|
10
|
+
import { DateFormatOptions } from './date';
|
|
11
|
+
/**
|
|
12
|
+
* Most of the number formatting is based on Intl.NumberFormat, which is built into JavaScript.
|
|
13
|
+
* Prefer Intl.NumbeFormat because it covers most use cases and will continue to be supported with time.
|
|
14
|
+
*
|
|
15
|
+
* To format bytes, we also make use of the `numbro` package,
|
|
16
|
+
* because it can handle adding units like KB, MB, GB, etc. correctly.
|
|
17
|
+
*/
|
|
18
|
+
export declare const UNIT_GROUP_CONFIG: Readonly<Record<UnitGroup, UnitGroupConfig>>;
|
|
19
|
+
export declare const UNIT_CONFIG: {
|
|
20
|
+
readonly "datetime-local": UnitConfig;
|
|
21
|
+
readonly "datetime-iso": UnitConfig;
|
|
22
|
+
readonly "datetime-us": UnitConfig;
|
|
23
|
+
readonly "date-iso": UnitConfig;
|
|
24
|
+
readonly "date-us": UnitConfig;
|
|
25
|
+
readonly "date-local": UnitConfig;
|
|
26
|
+
readonly "time-local": UnitConfig;
|
|
27
|
+
readonly "time-iso": UnitConfig;
|
|
28
|
+
readonly "time-us": UnitConfig;
|
|
29
|
+
readonly "relative-time": UnitConfig;
|
|
30
|
+
readonly "unix-timestamp": UnitConfig;
|
|
31
|
+
readonly "unix-timestamp-ms": UnitConfig;
|
|
32
|
+
readonly celsius: UnitConfig;
|
|
33
|
+
readonly fahrenheit: UnitConfig;
|
|
34
|
+
readonly aud: UnitConfig;
|
|
35
|
+
readonly cad: UnitConfig;
|
|
36
|
+
readonly chf: UnitConfig;
|
|
37
|
+
readonly cny: UnitConfig;
|
|
38
|
+
readonly eur: UnitConfig;
|
|
39
|
+
readonly gbp: UnitConfig;
|
|
40
|
+
readonly hkd: UnitConfig;
|
|
41
|
+
readonly inr: UnitConfig;
|
|
42
|
+
readonly jpy: UnitConfig;
|
|
43
|
+
readonly krw: UnitConfig;
|
|
44
|
+
readonly nok: UnitConfig;
|
|
45
|
+
readonly nzd: UnitConfig;
|
|
46
|
+
readonly sek: UnitConfig;
|
|
47
|
+
readonly sgd: UnitConfig;
|
|
48
|
+
readonly usd: UnitConfig;
|
|
49
|
+
readonly "bits/sec": UnitConfig;
|
|
50
|
+
readonly "decbits/sec": UnitConfig;
|
|
51
|
+
readonly "bytes/sec": UnitConfig;
|
|
52
|
+
readonly "decbytes/sec": UnitConfig;
|
|
53
|
+
readonly "counts/sec": UnitConfig;
|
|
54
|
+
readonly "events/sec": UnitConfig;
|
|
55
|
+
readonly "messages/sec": UnitConfig;
|
|
56
|
+
readonly "ops/sec": UnitConfig;
|
|
57
|
+
readonly "packets/sec": UnitConfig;
|
|
58
|
+
readonly "reads/sec": UnitConfig;
|
|
59
|
+
readonly "records/sec": UnitConfig;
|
|
60
|
+
readonly "requests/sec": UnitConfig;
|
|
61
|
+
readonly "rows/sec": UnitConfig;
|
|
62
|
+
readonly "writes/sec": UnitConfig;
|
|
63
|
+
readonly bits: UnitConfig;
|
|
64
|
+
readonly decbits: UnitConfig;
|
|
65
|
+
readonly bytes: UnitConfig;
|
|
66
|
+
readonly decbytes: UnitConfig;
|
|
67
|
+
readonly decimal: UnitConfig;
|
|
68
|
+
readonly percent: UnitConfig;
|
|
69
|
+
readonly "percent-decimal": UnitConfig;
|
|
70
|
+
readonly "%": UnitConfig;
|
|
71
|
+
readonly years: UnitConfig;
|
|
72
|
+
readonly months: UnitConfig;
|
|
73
|
+
readonly weeks: UnitConfig;
|
|
74
|
+
readonly days: UnitConfig;
|
|
75
|
+
readonly hours: UnitConfig;
|
|
76
|
+
readonly minutes: UnitConfig;
|
|
77
|
+
readonly seconds: UnitConfig;
|
|
78
|
+
readonly nanoseconds: UnitConfig;
|
|
79
|
+
readonly microseconds: UnitConfig;
|
|
80
|
+
readonly milliseconds: UnitConfig;
|
|
81
|
+
};
|
|
82
|
+
export type FormatOptions = TimeFormatOptions | PercentFormatOptions | DecimalFormatOptions | BytesFormatOptions | BitsFormatOptions | ThroughputFormatOptions | CurrencyFormatOptions | TemperatureFormatOptions | DateFormatOptions;
|
|
83
|
+
type HasDecimalPlaces<UnitOpt> = UnitOpt extends {
|
|
84
|
+
decimalPlaces?: number;
|
|
85
|
+
} ? UnitOpt : never;
|
|
86
|
+
type HasShortValues<UnitOpt> = UnitOpt extends {
|
|
87
|
+
shortValues?: boolean;
|
|
88
|
+
} ? UnitOpt : never;
|
|
89
|
+
export declare function formatValue(value: number, formatOptions?: FormatOptions): string;
|
|
90
|
+
export declare function getUnitConfig(formatOptions: FormatOptions): UnitConfig;
|
|
91
|
+
export declare function getUnitGroup(formatOptions: FormatOptions): UnitGroup;
|
|
92
|
+
export declare function getUnitGroupConfig(formatOptions: FormatOptions): UnitGroupConfig;
|
|
93
|
+
export declare function isTimeUnit(formatOptions: FormatOptions): formatOptions is TimeFormatOptions;
|
|
94
|
+
export declare function isPercentUnit(formatOptions: FormatOptions): formatOptions is PercentFormatOptions;
|
|
95
|
+
export declare function isDecimalUnit(formatOptions: FormatOptions): formatOptions is DecimalFormatOptions;
|
|
96
|
+
export declare function isBytesUnit(formatOptions: FormatOptions): formatOptions is BytesFormatOptions;
|
|
97
|
+
export declare function isBitsUnit(formatOptions: FormatOptions): formatOptions is BitsFormatOptions;
|
|
98
|
+
export declare function isUnitWithDecimalPlaces(formatOptions: FormatOptions): formatOptions is HasDecimalPlaces<FormatOptions>;
|
|
99
|
+
export declare function isUnitWithShortValues(formatOptions: FormatOptions): formatOptions is HasShortValues<FormatOptions>;
|
|
100
|
+
export declare function isThroughputUnit(formatOptions: FormatOptions): formatOptions is ThroughputFormatOptions;
|
|
101
|
+
export declare function isCurrencyUnit(formatOptions: FormatOptions): formatOptions is CurrencyFormatOptions;
|
|
102
|
+
export declare function isDateUnit(formatOptions: FormatOptions): formatOptions is DateFormatOptions;
|
|
103
|
+
export declare function isTemperatureUnit(formatOptions: FormatOptions): formatOptions is TemperatureFormatOptions;
|
|
104
|
+
export {};
|
|
105
|
+
//# sourceMappingURL=units.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"units.d.ts","sourceRoot":"","sources":["../../src/model/units.ts"],"names":[],"mappings":"AAaA,OAAO,EAAe,kBAAkB,IAAI,kBAAkB,EAAyC,MAAM,SAAS,CAAC;AACvH,OAAO,EAAc,iBAAiB,IAAI,iBAAiB,EAAuC,MAAM,QAAQ,CAAC;AACjH,OAAO,EAEL,oBAAoB,IAAI,oBAAoB,EAG7C,MAAM,WAAW,CAAC;AACnB,OAAO,EAEL,oBAAoB,IAAI,oBAAoB,EAG7C,MAAM,WAAW,CAAC;AACnB,OAAO,EAIL,w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,CAGhF;AAGD,wBAAgB,UAAU,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,iBAAiB,CAE3F;AAED,wBAAgB,aAAa,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,oBAAoB,CAEjG;AAED,wBAAgB,aAAa,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,oBAAoB,CAEjG;AAED,wBAAgB,WAAW,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,kBAAkB,CAE7F;AAED,wBAAgB,UAAU,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,iBAAiB,CAE3F;AAED,wBAAgB,uBAAuB,CACrC,aAAa,EAAE,aAAa,GAC3B,aAAa,IAAI,gBAAgB,CAAC,aAAa,CAAC,CAIlD;AAED,wBAAgB,qBAAqB,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,cAAc,CAAC,aAAa,CAAC,CAIlH;AAED,wBAAgB,gBAAgB,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,uBAAuB,CAEvG;AAED,wBAAgB,cAAc,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,qBAAqB,CAEnG;AAED,wBAAgB,UAAU,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,iBAAiB,CAE3F;AAED,wBAAgB,iBAAiB,CAAC,aAAa,EAAE,aAAa,GAAG,aAAa,IAAI,wBAAwB,CAEzG"}
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
// Copyright The Perses Authors
|
|
2
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
3
|
+
// you may not use this file except in compliance with the License.
|
|
4
|
+
// You may obtain a copy of the License at
|
|
5
|
+
//
|
|
6
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
7
|
+
//
|
|
8
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
9
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
10
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
11
|
+
// See the License for the specific language governing permissions and
|
|
12
|
+
// limitations under the License.
|
|
13
|
+
import { formatBytes, BYTES_GROUP_CONFIG, BYTES_UNIT_CONFIG } from './bytes';
|
|
14
|
+
import { formatBits, BITS_GROUP_CONFIG, BITS_UNIT_CONFIG } from './bits';
|
|
15
|
+
import { formatDecimal, DECIMAL_GROUP_CONFIG, DECIMAL_UNIT_CONFIG } from './decimal';
|
|
16
|
+
import { formatPercent, PERCENT_GROUP_CONFIG, PERCENT_UNIT_CONFIG } from './percent';
|
|
17
|
+
import { TEMPERATURE_GROUP_CONFIG, formatTemperature, TEMPERATURE_UNIT_CONFIG } from './temperature';
|
|
18
|
+
import { formatTime, TIME_GROUP_CONFIG, TIME_UNIT_CONFIG } from './time';
|
|
19
|
+
import { formatThroughput, THROUGHPUT_GROUP_CONFIG, THROUGHPUT_UNIT_CONFIG } from './throughput';
|
|
20
|
+
import { formatCurrency, CURRENCY_GROUP_CONFIG, CURRENCY_UNIT_CONFIG } from './currency';
|
|
21
|
+
import { formatDate, DATE_GROUP_CONFIG, DATE_UNIT_CONFIG } from './date';
|
|
22
|
+
/**
|
|
23
|
+
* Most of the number formatting is based on Intl.NumberFormat, which is built into JavaScript.
|
|
24
|
+
* Prefer Intl.NumbeFormat because it covers most use cases and will continue to be supported with time.
|
|
25
|
+
*
|
|
26
|
+
* To format bytes, we also make use of the `numbro` package,
|
|
27
|
+
* because it can handle adding units like KB, MB, GB, etc. correctly.
|
|
28
|
+
*/ export const UNIT_GROUP_CONFIG = {
|
|
29
|
+
Time: TIME_GROUP_CONFIG,
|
|
30
|
+
Percent: PERCENT_GROUP_CONFIG,
|
|
31
|
+
Decimal: DECIMAL_GROUP_CONFIG,
|
|
32
|
+
Bytes: BYTES_GROUP_CONFIG,
|
|
33
|
+
Bits: BITS_GROUP_CONFIG,
|
|
34
|
+
Throughput: THROUGHPUT_GROUP_CONFIG,
|
|
35
|
+
Currency: CURRENCY_GROUP_CONFIG,
|
|
36
|
+
Temperature: TEMPERATURE_GROUP_CONFIG,
|
|
37
|
+
Date: DATE_GROUP_CONFIG
|
|
38
|
+
};
|
|
39
|
+
export const UNIT_CONFIG = {
|
|
40
|
+
...TIME_UNIT_CONFIG,
|
|
41
|
+
...PERCENT_UNIT_CONFIG,
|
|
42
|
+
...DECIMAL_UNIT_CONFIG,
|
|
43
|
+
...BYTES_UNIT_CONFIG,
|
|
44
|
+
...BITS_UNIT_CONFIG,
|
|
45
|
+
...THROUGHPUT_UNIT_CONFIG,
|
|
46
|
+
...CURRENCY_UNIT_CONFIG,
|
|
47
|
+
...TEMPERATURE_UNIT_CONFIG,
|
|
48
|
+
...DATE_UNIT_CONFIG
|
|
49
|
+
};
|
|
50
|
+
export function formatValue(value, formatOptions) {
|
|
51
|
+
if (!formatOptions) {
|
|
52
|
+
return value.toString();
|
|
53
|
+
}
|
|
54
|
+
if (isBytesUnit(formatOptions)) {
|
|
55
|
+
return formatBytes(value, formatOptions);
|
|
56
|
+
}
|
|
57
|
+
if (isBitsUnit(formatOptions)) {
|
|
58
|
+
return formatBits(value, formatOptions);
|
|
59
|
+
}
|
|
60
|
+
if (isDecimalUnit(formatOptions)) {
|
|
61
|
+
return formatDecimal(value, formatOptions);
|
|
62
|
+
}
|
|
63
|
+
if (isPercentUnit(formatOptions)) {
|
|
64
|
+
return formatPercent(value, formatOptions);
|
|
65
|
+
}
|
|
66
|
+
if (isTimeUnit(formatOptions)) {
|
|
67
|
+
return formatTime(value, formatOptions);
|
|
68
|
+
}
|
|
69
|
+
if (isThroughputUnit(formatOptions)) {
|
|
70
|
+
return formatThroughput(value, formatOptions);
|
|
71
|
+
}
|
|
72
|
+
if (isCurrencyUnit(formatOptions)) {
|
|
73
|
+
return formatCurrency(value, formatOptions);
|
|
74
|
+
}
|
|
75
|
+
if (isDateUnit(formatOptions)) {
|
|
76
|
+
return formatDate(value, formatOptions);
|
|
77
|
+
}
|
|
78
|
+
if (isTemperatureUnit(formatOptions)) {
|
|
79
|
+
return formatTemperature(value, formatOptions);
|
|
80
|
+
}
|
|
81
|
+
const exhaustive = formatOptions;
|
|
82
|
+
throw new Error(`Unknown unit options ${exhaustive}`);
|
|
83
|
+
}
|
|
84
|
+
export function getUnitConfig(formatOptions) {
|
|
85
|
+
const unit = formatOptions.unit ?? 'decimal';
|
|
86
|
+
return UNIT_CONFIG[unit];
|
|
87
|
+
}
|
|
88
|
+
export function getUnitGroup(formatOptions) {
|
|
89
|
+
return getUnitConfig(formatOptions).group ?? 'Decimal';
|
|
90
|
+
}
|
|
91
|
+
export function getUnitGroupConfig(formatOptions) {
|
|
92
|
+
const unitConfig = getUnitConfig(formatOptions);
|
|
93
|
+
return UNIT_GROUP_CONFIG[unitConfig.group ?? 'Decimal'];
|
|
94
|
+
}
|
|
95
|
+
// Type guards
|
|
96
|
+
export function isTimeUnit(formatOptions) {
|
|
97
|
+
return getUnitGroup(formatOptions) === 'Time';
|
|
98
|
+
}
|
|
99
|
+
export function isPercentUnit(formatOptions) {
|
|
100
|
+
return getUnitGroup(formatOptions) === 'Percent';
|
|
101
|
+
}
|
|
102
|
+
export function isDecimalUnit(formatOptions) {
|
|
103
|
+
return getUnitGroup(formatOptions) === 'Decimal';
|
|
104
|
+
}
|
|
105
|
+
export function isBytesUnit(formatOptions) {
|
|
106
|
+
return getUnitGroup(formatOptions) === 'Bytes';
|
|
107
|
+
}
|
|
108
|
+
export function isBitsUnit(formatOptions) {
|
|
109
|
+
return getUnitGroup(formatOptions) === 'Bits';
|
|
110
|
+
}
|
|
111
|
+
export function isUnitWithDecimalPlaces(formatOptions) {
|
|
112
|
+
const groupConfig = getUnitGroupConfig(formatOptions);
|
|
113
|
+
return !!groupConfig.decimalPlaces;
|
|
114
|
+
}
|
|
115
|
+
export function isUnitWithShortValues(formatOptions) {
|
|
116
|
+
const groupConfig = getUnitGroupConfig(formatOptions);
|
|
117
|
+
return !!groupConfig.shortValues;
|
|
118
|
+
}
|
|
119
|
+
export function isThroughputUnit(formatOptions) {
|
|
120
|
+
return getUnitGroup(formatOptions) === 'Throughput';
|
|
121
|
+
}
|
|
122
|
+
export function isCurrencyUnit(formatOptions) {
|
|
123
|
+
return getUnitGroup(formatOptions) === 'Currency';
|
|
124
|
+
}
|
|
125
|
+
export function isDateUnit(formatOptions) {
|
|
126
|
+
return getUnitGroup(formatOptions) === 'Date';
|
|
127
|
+
}
|
|
128
|
+
export function isTemperatureUnit(formatOptions) {
|
|
129
|
+
return getUnitGroup(formatOptions) === 'Temperature';
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
//# sourceMappingURL=units.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/model/units.ts"],"sourcesContent":["// Copyright The Perses Authors\n// Licensed under the Apache License, Version 2.0 (the \"License\");\n// you may not use this file except in compliance with the License.\n// You may obtain a copy of the License at\n//\n// http://www.apache.org/licenses/LICENSE-2.0\n//\n// Unless required by applicable law or agreed to in writing, software\n// distributed under the License is distributed on an \"AS IS\" BASIS,\n// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n// See the License for the specific language governing permissions and\n// limitations under the License.\n\nimport { formatBytes, BytesFormatOptions as BytesFormatOptions, BYTES_GROUP_CONFIG, BYTES_UNIT_CONFIG } from './bytes';\nimport { formatBits, BitsFormatOptions as BitsFormatOptions, BITS_GROUP_CONFIG, BITS_UNIT_CONFIG } from './bits';\nimport {\n formatDecimal,\n DecimalFormatOptions as DecimalFormatOptions,\n DECIMAL_GROUP_CONFIG,\n DECIMAL_UNIT_CONFIG,\n} from './decimal';\nimport {\n formatPercent,\n PercentFormatOptions as PercentFormatOptions,\n PERCENT_GROUP_CONFIG,\n PERCENT_UNIT_CONFIG,\n} from './percent';\nimport {\n TEMPERATURE_GROUP_CONFIG,\n formatTemperature,\n TEMPERATURE_UNIT_CONFIG,\n TemperatureFormatOptions,\n} from './temperature';\nimport { formatTime, TimeFormatOptions as TimeFormatOptions, TIME_GROUP_CONFIG, TIME_UNIT_CONFIG } from './time';\nimport { UnitGroup, UnitGroupConfig, UnitConfig } from './types';\nimport {\n formatThroughput,\n THROUGHPUT_GROUP_CONFIG,\n THROUGHPUT_UNIT_CONFIG,\n ThroughputFormatOptions,\n} from './throughput';\nimport { formatCurrency, CURRENCY_GROUP_CONFIG, CURRENCY_UNIT_CONFIG, CurrencyFormatOptions } from './currency';\nimport { formatDate, DateFormatOptions, DATE_GROUP_CONFIG, DATE_UNIT_CONFIG } from './date';\n\n/**\n * Most of the number formatting is based on Intl.NumberFormat, which is built into JavaScript.\n * Prefer Intl.NumbeFormat because it covers most use cases and will continue to be supported with time.\n *\n * To format bytes, we also make use of the `numbro` package,\n * because it can handle adding units like KB, MB, GB, etc. correctly.\n */\n\nexport const UNIT_GROUP_CONFIG: Readonly<Record<UnitGroup, UnitGroupConfig>> = {\n Time: TIME_GROUP_CONFIG,\n Percent: PERCENT_GROUP_CONFIG,\n Decimal: DECIMAL_GROUP_CONFIG,\n Bytes: BYTES_GROUP_CONFIG,\n Bits: BITS_GROUP_CONFIG,\n Throughput: THROUGHPUT_GROUP_CONFIG,\n Currency: CURRENCY_GROUP_CONFIG,\n Temperature: TEMPERATURE_GROUP_CONFIG,\n Date: DATE_GROUP_CONFIG,\n};\nexport const UNIT_CONFIG = {\n ...TIME_UNIT_CONFIG,\n ...PERCENT_UNIT_CONFIG,\n ...DECIMAL_UNIT_CONFIG,\n ...BYTES_UNIT_CONFIG,\n ...BITS_UNIT_CONFIG,\n ...THROUGHPUT_UNIT_CONFIG,\n ...CURRENCY_UNIT_CONFIG,\n ...TEMPERATURE_UNIT_CONFIG,\n ...DATE_UNIT_CONFIG,\n} as const;\n\nexport type FormatOptions =\n | TimeFormatOptions\n | PercentFormatOptions\n | DecimalFormatOptions\n | BytesFormatOptions\n | BitsFormatOptions\n | ThroughputFormatOptions\n | CurrencyFormatOptions\n | TemperatureFormatOptions\n | DateFormatOptions;\n\ntype HasDecimalPlaces<UnitOpt> = UnitOpt extends { decimalPlaces?: number } ? UnitOpt : never;\ntype HasShortValues<UnitOpt> = UnitOpt extends { shortValues?: boolean } ? UnitOpt : never;\n\nexport function formatValue(value: number, formatOptions?: FormatOptions): string {\n if (!formatOptions) {\n return value.toString();\n }\n\n if (isBytesUnit(formatOptions)) {\n return formatBytes(value, formatOptions);\n }\n\n if (isBitsUnit(formatOptions)) {\n return formatBits(value, formatOptions);\n }\n\n if (isDecimalUnit(formatOptions)) {\n return formatDecimal(value, formatOptions);\n }\n\n if (isPercentUnit(formatOptions)) {\n return formatPercent(value, formatOptions);\n }\n\n if (isTimeUnit(formatOptions)) {\n return formatTime(value, formatOptions);\n }\n\n if (isThroughputUnit(formatOptions)) {\n return formatThroughput(value, formatOptions);\n }\n\n if (isCurrencyUnit(formatOptions)) {\n return formatCurrency(value, formatOptions);\n }\n\n if (isDateUnit(formatOptions)) {\n return formatDate(value, formatOptions);\n }\n\n if (isTemperatureUnit(formatOptions)) {\n return formatTemperature(value, formatOptions);\n }\n\n const exhaustive: never = formatOptions;\n throw new Error(`Unknown unit options ${exhaustive}`);\n}\n\nexport function getUnitConfig(formatOptions: FormatOptions): UnitConfig {\n const unit = formatOptions.unit ?? 'decimal';\n return UNIT_CONFIG[unit];\n}\n\nexport function getUnitGroup(formatOptions: FormatOptions): UnitGroup {\n return getUnitConfig(formatOptions).group ?? 'Decimal';\n}\n\nexport function getUnitGroupConfig(formatOptions: FormatOptions): UnitGroupConfig {\n const unitConfig = getUnitConfig(formatOptions);\n return UNIT_GROUP_CONFIG[unitConfig.group ?? 'Decimal'];\n}\n\n// Type guards\nexport function isTimeUnit(formatOptions: FormatOptions): formatOptions is TimeFormatOptions {\n return getUnitGroup(formatOptions) === 'Time';\n}\n\nexport function isPercentUnit(formatOptions: FormatOptions): formatOptions is PercentFormatOptions {\n return getUnitGroup(formatOptions) === 'Percent';\n}\n\nexport function isDecimalUnit(formatOptions: FormatOptions): formatOptions is DecimalFormatOptions {\n return getUnitGroup(formatOptions) === 'Decimal';\n}\n\nexport function isBytesUnit(formatOptions: FormatOptions): formatOptions is BytesFormatOptions {\n return getUnitGroup(formatOptions) === 'Bytes';\n}\n\nexport function isBitsUnit(formatOptions: FormatOptions): formatOptions is BitsFormatOptions {\n return getUnitGroup(formatOptions) === 'Bits';\n}\n\nexport function isUnitWithDecimalPlaces(\n formatOptions: FormatOptions\n): formatOptions is HasDecimalPlaces<FormatOptions> {\n const groupConfig = getUnitGroupConfig(formatOptions);\n\n return !!groupConfig.decimalPlaces;\n}\n\nexport function isUnitWithShortValues(formatOptions: FormatOptions): formatOptions is HasShortValues<FormatOptions> {\n const groupConfig = getUnitGroupConfig(formatOptions);\n\n return !!groupConfig.shortValues;\n}\n\nexport function isThroughputUnit(formatOptions: FormatOptions): formatOptions is ThroughputFormatOptions {\n return getUnitGroup(formatOptions) === 'Throughput';\n}\n\nexport function isCurrencyUnit(formatOptions: FormatOptions): formatOptions is CurrencyFormatOptions {\n return getUnitGroup(formatOptions) === 'Currency';\n}\n\nexport function isDateUnit(formatOptions: FormatOptions): formatOptions is DateFormatOptions {\n return getUnitGroup(formatOptions) === 'Date';\n}\n\nexport function isTemperatureUnit(formatOptions: FormatOptions): formatOptions is TemperatureFormatOptions {\n return getUnitGroup(formatOptions) === 'Temperature';\n}\n"],"names":["formatBytes","BYTES_GROUP_CONFIG","BYTES_UNIT_CONFIG","formatBits","BITS_GROUP_CONFIG","BITS_UNIT_CONFIG","formatDecimal","DECIMAL_GROUP_CONFIG","DECIMAL_UNIT_CONFIG","formatPercent","PERCENT_GROUP_CONFIG","PERCENT_UNIT_CONFIG","TEMPERATURE_GROUP_CONFIG","formatTemperature","TEMPERATURE_UNIT_CONFIG","formatTime","TIME_GROUP_CONFIG","TIME_UNIT_CONFIG","formatThroughput","THROUGHPUT_GROUP_CONFIG","THROUGHPUT_UNIT_CONFIG","formatCurrency","CURRENCY_GROUP_CONFIG","CURRENCY_UNIT_CONFIG","formatDate","DATE_GROUP_CONFIG","DATE_UNIT_CONFIG","UNIT_GROUP_CONFIG","Time","Percent","Decimal","Bytes","Bits","Throughput","Currency","Temperature","Date","UNIT_CONFIG","formatValue","value","formatOptions","toString","isBytesUnit","isBitsUnit","isDecimalUnit","isPercentUnit","isTimeUnit","isThroughputUnit","isCurrencyUnit","isDateUnit","isTemperatureUnit","exhaustive","Error","getUnitConfig","unit","getUnitGroup","group","getUnitGroupConfig","unitConfig","isUnitWithDecimalPlaces","groupConfig","decimalPlaces","isUnitWithShortValues","shortValues"],"mappings":"AAAA,+BAA+B;AAC/B,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,SAASA,WAAW,EAA4CC,kBAAkB,EAAEC,iBAAiB,QAAQ,UAAU;AACvH,SAASC,UAAU,EAA0CC,iBAAiB,EAAEC,gBAAgB,QAAQ,SAAS;AACjH,SACEC,aAAa,EAEbC,oBAAoB,EACpBC,mBAAmB,QACd,YAAY;AACnB,SACEC,aAAa,EAEbC,oBAAoB,EACpBC,mBAAmB,QACd,YAAY;AACnB,SACEC,wBAAwB,EACxBC,iBAAiB,EACjBC,uBAAuB,QAElB,gBAAgB;AACvB,SAASC,UAAU,EAA0CC,iBAAiB,EAAEC,gBAAgB,QAAQ,SAAS;AAEjH,SACEC,gBAAgB,EAChBC,uBAAuB,EACvBC,sBAAsB,QAEjB,eAAe;AACtB,SAASC,cAAc,EAAEC,qBAAqB,EAAEC,oBAAoB,QAA+B,aAAa;AAChH,SAASC,UAAU,EAAqBC,iBAAiB,EAAEC,gBAAgB,QAAQ,SAAS;AAE5F;;;;;;CAMC,GAED,OAAO,MAAMC,oBAAkE;IAC7EC,MAAMZ;IACNa,SAASnB;IACToB,SAASvB;IACTwB,OAAO9B;IACP+B,MAAM5B;IACN6B,YAAYd;IACZe,UAAUZ;IACVa,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