@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,89 @@
|
|
|
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 numbro from 'numbro';
|
|
14
|
+
import { MAX_SIGNIFICANT_DIGITS } from './constants';
|
|
15
|
+
import { hasDecimalPlaces, limitDecimalPlaces, shouldShortenValues } from './utils';
|
|
16
|
+
import { getFormatterFromCache } from './formatterCache';
|
|
17
|
+
/**
|
|
18
|
+
* We support both SI (decimal) and IEC (binary) units for bits:
|
|
19
|
+
*
|
|
20
|
+
* SI/decimal (unit: 'decbits'):
|
|
21
|
+
* 1 Kb = 1000 bits (1000^1 bits)
|
|
22
|
+
* 1 Mb = 1,000,000 bits (1000^2 bits)
|
|
23
|
+
* etc.
|
|
24
|
+
*
|
|
25
|
+
* IEC/binary (unit: 'bits'):
|
|
26
|
+
* 1 Kib = 1024 bits (1024^1 bits)
|
|
27
|
+
* 1 Mib = 1,048,576 bits (1024^2 bits)
|
|
28
|
+
* etc.
|
|
29
|
+
*/ const DEFAULT_NUMBRO_MANTISSA = 2;
|
|
30
|
+
export const BITS_GROUP_CONFIG = {
|
|
31
|
+
label: 'Bits',
|
|
32
|
+
decimalPlaces: true,
|
|
33
|
+
shortValues: true
|
|
34
|
+
};
|
|
35
|
+
export const BITS_UNIT_CONFIG = {
|
|
36
|
+
bits: {
|
|
37
|
+
group: 'Bits',
|
|
38
|
+
label: 'Bits (IEC)'
|
|
39
|
+
},
|
|
40
|
+
decbits: {
|
|
41
|
+
group: 'Bits',
|
|
42
|
+
label: 'Bits (SI)'
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
export function formatBits(bits, { unit = 'bits', shortValues, decimalPlaces }) {
|
|
46
|
+
const isDecimal = unit === 'decbits';
|
|
47
|
+
const threshold = isDecimal ? 1000 : 1024;
|
|
48
|
+
// If we're showing the entire value, we can use Intl.NumberFormat.
|
|
49
|
+
if (!shouldShortenValues(shortValues) || Math.abs(bits) < threshold) {
|
|
50
|
+
const formatterOptions = {
|
|
51
|
+
style: 'decimal',
|
|
52
|
+
useGrouping: true
|
|
53
|
+
};
|
|
54
|
+
if (hasDecimalPlaces(decimalPlaces)) {
|
|
55
|
+
formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);
|
|
56
|
+
formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);
|
|
57
|
+
} else {
|
|
58
|
+
// This can happen if bits is between -threshold and threshold (1000 for SI, 1024 for IEC)
|
|
59
|
+
if (shouldShortenValues(shortValues)) {
|
|
60
|
+
formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
const key = [
|
|
64
|
+
formatterOptions.style,
|
|
65
|
+
formatterOptions.useGrouping,
|
|
66
|
+
formatterOptions.maximumSignificantDigits,
|
|
67
|
+
decimalPlaces,
|
|
68
|
+
shortValues,
|
|
69
|
+
unit
|
|
70
|
+
];
|
|
71
|
+
return `${getFormatterFromCache(key, 'bits', formatterOptions, 'en-US')(bits)} bits`;
|
|
72
|
+
}
|
|
73
|
+
// If we're showing the shorten value, we use numbro.
|
|
74
|
+
// numbro is able to add units like Kb, Mb, Gb, etc. correctly.
|
|
75
|
+
const formatted = numbro(bits).format({
|
|
76
|
+
output: 'byte',
|
|
77
|
+
base: isDecimal ? 'decimal' : 'binary',
|
|
78
|
+
spaceSeparated: true,
|
|
79
|
+
mantissa: hasDecimalPlaces(decimalPlaces) ? decimalPlaces : DEFAULT_NUMBRO_MANTISSA,
|
|
80
|
+
// trimMantissa trims trailing 0s
|
|
81
|
+
trimMantissa: !hasDecimalPlaces(decimalPlaces),
|
|
82
|
+
// optionalMantissa excludes all the decimal places if they're all zeros
|
|
83
|
+
optionalMantissa: !hasDecimalPlaces(decimalPlaces)
|
|
84
|
+
});
|
|
85
|
+
// Replace byte units with bit units
|
|
86
|
+
return formatted.replace(/KB/g, 'Kb').replace(/MB/g, 'Mb').replace(/GB/g, 'Gb').replace(/TB/g, 'Tb').replace(/PB/g, 'Pb').replace(/EB/g, 'Eb').replace(/KiB/g, 'Kib').replace(/MiB/g, 'Mib').replace(/GiB/g, 'Gib').replace(/TiB/g, 'Tib').replace(/PiB/g, 'Pib').replace(/EiB/g, 'Eib');
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
//# sourceMappingURL=bits.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/model/bits.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 numbro from 'numbro';\n\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitGroupConfig, UnitConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces, shouldShortenValues } from './utils';\nimport { getFormatterFromCache } from './formatterCache';\n\n/**\n * We support both SI (decimal) and IEC (binary) units for bits:\n *\n * SI/decimal (unit: 'decbits'):\n * 1 Kb = 1000 bits (1000^1 bits)\n * 1 Mb = 1,000,000 bits (1000^2 bits)\n * etc.\n *\n * IEC/binary (unit: 'bits'):\n * 1 Kib = 1024 bits (1024^1 bits)\n * 1 Mib = 1,048,576 bits (1024^2 bits)\n * etc.\n */\n\nconst DEFAULT_NUMBRO_MANTISSA = 2;\n\ntype BitsUnit = 'bits' | 'decbits';\n\nexport type BitsFormatOptions = {\n unit?: BitsUnit;\n decimalPlaces?: number;\n shortValues?: boolean;\n};\n\nexport const BITS_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Bits',\n decimalPlaces: true,\n shortValues: true,\n};\n\nexport const BITS_UNIT_CONFIG: Readonly<Record<BitsUnit, UnitConfig>> = {\n bits: {\n group: 'Bits',\n label: 'Bits (IEC)',\n },\n decbits: {\n group: 'Bits',\n label: 'Bits (SI)',\n },\n};\n\nexport function formatBits(bits: number, { unit = 'bits', shortValues, decimalPlaces }: BitsFormatOptions): string {\n const isDecimal = unit === 'decbits';\n const threshold = isDecimal ? 1000 : 1024;\n\n // If we're showing the entire value, we can use Intl.NumberFormat.\n if (!shouldShortenValues(shortValues) || Math.abs(bits) < threshold) {\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'decimal',\n useGrouping: true,\n };\n\n if (hasDecimalPlaces(decimalPlaces)) {\n formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);\n } else {\n // This can happen if bits is between -threshold and threshold (1000 for SI, 1024 for IEC)\n if (shouldShortenValues(shortValues)) {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n }\n\n const key = [\n formatterOptions.style,\n formatterOptions.useGrouping,\n formatterOptions.maximumSignificantDigits,\n decimalPlaces,\n shortValues,\n unit,\n ];\n\n return `${getFormatterFromCache(key, 'bits', formatterOptions, 'en-US')(bits)} bits`;\n }\n\n // If we're showing the shorten value, we use numbro.\n // numbro is able to add units like Kb, Mb, Gb, etc. correctly.\n const formatted = numbro(bits).format({\n output: 'byte', // numbro uses 'byte' output for bit formatting\n base: isDecimal ? 'decimal' : 'binary',\n spaceSeparated: true,\n mantissa: hasDecimalPlaces(decimalPlaces) ? decimalPlaces : DEFAULT_NUMBRO_MANTISSA,\n // trimMantissa trims trailing 0s\n trimMantissa: !hasDecimalPlaces(decimalPlaces),\n // optionalMantissa excludes all the decimal places if they're all zeros\n optionalMantissa: !hasDecimalPlaces(decimalPlaces),\n });\n\n // Replace byte units with bit units\n return formatted\n .replace(/KB/g, 'Kb')\n .replace(/MB/g, 'Mb')\n .replace(/GB/g, 'Gb')\n .replace(/TB/g, 'Tb')\n .replace(/PB/g, 'Pb')\n .replace(/EB/g, 'Eb')\n .replace(/KiB/g, 'Kib')\n .replace(/MiB/g, 'Mib')\n .replace(/GiB/g, 'Gib')\n .replace(/TiB/g, 'Tib')\n .replace(/PiB/g, 'Pib')\n .replace(/EiB/g, 'Eib');\n}\n"],"names":["numbro","MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","shouldShortenValues","getFormatterFromCache","DEFAULT_NUMBRO_MANTISSA","BITS_GROUP_CONFIG","label","decimalPlaces","shortValues","BITS_UNIT_CONFIG","bits","group","decbits","formatBits","unit","isDecimal","threshold","Math","abs","formatterOptions","style","useGrouping","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","key","formatted","format","output","base","spaceSeparated","mantissa","trimMantissa","optionalMantissa","replace"],"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,OAAOA,YAAY,SAAS;AAE5B,SAASC,sBAAsB,QAAQ,cAAc;AAErD,SAASC,gBAAgB,EAAEC,kBAAkB,EAAEC,mBAAmB,QAAQ,UAAU;AACpF,SAASC,qBAAqB,QAAQ,mBAAmB;AAEzD;;;;;;;;;;;;CAYC,GAED,MAAMC,0BAA0B;AAUhC,OAAO,MAAMC,oBAAqC;IAChDC,OAAO;IACPC,eAAe;IACfC,aAAa;AACf,EAAE;AAEF,OAAO,MAAMC,mBAA2D;IACtEC,MAAM;QACJC,OAAO;QACPL,OAAO;IACT;IACAM,SAAS;QACPD,OAAO;QACPL,OAAO;IACT;AACF,EAAE;AAEF,OAAO,SAASO,WAAWH,IAAY,EAAE,EAAEI,OAAO,MAAM,EAAEN,WAAW,EAAED,aAAa,EAAqB;IACvG,MAAMQ,YAAYD,SAAS;IAC3B,MAAME,YAAYD,YAAY,OAAO;IAErC,mEAAmE;IACnE,IAAI,CAACb,oBAAoBM,gBAAgBS,KAAKC,GAAG,CAACR,QAAQM,WAAW;QACnE,MAAMG,mBAA6C;YACjDC,OAAO;YACPC,aAAa;QACf;QAEA,IAAIrB,iBAAiBO,gBAAgB;YACnCY,iBAAiBG,qBAAqB,GAAGrB,mBAAmBM;YAC5DY,iBAAiBI,qBAAqB,GAAGtB,mBAAmBM;QAC9D,OAAO;YACL,0FAA0F;YAC1F,IAAIL,oBAAoBM,cAAc;gBACpCW,iBAAiBK,wBAAwB,GAAGzB;YAC9C;QACF;QAEA,MAAM0B,MAAM;YACVN,iBAAiBC,KAAK;YACtBD,iBAAiBE,WAAW;YAC5BF,iBAAiBK,wBAAwB;YACzCjB;YACAC;YACAM;SACD;QAED,OAAO,GAAGX,sBAAsBsB,KAAK,QAAQN,kBAAkB,SAAST,MAAM,KAAK,CAAC;IACtF;IAEA,qDAAqD;IACrD,+DAA+D;IAC/D,MAAMgB,YAAY5B,OAAOY,MAAMiB,MAAM,CAAC;QACpCC,QAAQ;QACRC,MAAMd,YAAY,YAAY;QAC9Be,gBAAgB;QAChBC,UAAU/B,iBAAiBO,iBAAiBA,gBAAgBH;QAC5D,iCAAiC;QACjC4B,cAAc,CAAChC,iBAAiBO;QAChC,wEAAwE;QACxE0B,kBAAkB,CAACjC,iBAAiBO;IACtC;IAEA,oCAAoC;IACpC,OAAOmB,UACJQ,OAAO,CAAC,OAAO,MACfA,OAAO,CAAC,OAAO,MACfA,OAAO,CAAC,OAAO,MACfA,OAAO,CAAC,OAAO,MACfA,OAAO,CAAC,OAAO,MACfA,OAAO,CAAC,OAAO,MACfA,OAAO,CAAC,QAAQ,OAChBA,OAAO,CAAC,QAAQ,OAChBA,OAAO,CAAC,QAAQ,OAChBA,OAAO,CAAC,QAAQ,OAChBA,OAAO,CAAC,QAAQ,OAChBA,OAAO,CAAC,QAAQ;AACrB"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { UnitGroupConfig, UnitConfig } from './types';
|
|
2
|
+
type BytesUnit = 'bytes' | 'decbytes';
|
|
3
|
+
export type BytesFormatOptions = {
|
|
4
|
+
unit?: BytesUnit;
|
|
5
|
+
decimalPlaces?: number;
|
|
6
|
+
shortValues?: boolean;
|
|
7
|
+
};
|
|
8
|
+
export declare const BYTES_GROUP_CONFIG: UnitGroupConfig;
|
|
9
|
+
export declare const BYTES_UNIT_CONFIG: Readonly<Record<BytesUnit, UnitConfig>>;
|
|
10
|
+
export declare function formatBytes(bytes: number, { unit, shortValues, decimalPlaces }: BytesFormatOptions): string;
|
|
11
|
+
export {};
|
|
12
|
+
//# sourceMappingURL=bytes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../src/model/bytes.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAoBtD,KAAK,SAAS,GAAG,OAAO,GAAG,UAAU,CAAC;AAEtC,MAAM,MAAM,kBAAkB,GAAG;IAC/B,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,WAAW,CAAC,EAAE,OAAO,CAAC;CACvB,CAAC;AACF,eAAO,MAAM,kBAAkB,EAAE,eAIhC,CAAC;AACF,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,CASrE,CAAC;AAEF,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,IAAc,EAAE,WAAW,EAAE,aAAa,EAAE,EAAE,kBAAkB,GAAG,MAAM,CAiDrH"}
|
|
@@ -0,0 +1,91 @@
|
|
|
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 numbro from 'numbro';
|
|
14
|
+
import { MAX_SIGNIFICANT_DIGITS } from './constants';
|
|
15
|
+
import { hasDecimalPlaces, limitDecimalPlaces, shouldShortenValues } from './utils';
|
|
16
|
+
import { getFormatterFromCache } from './formatterCache';
|
|
17
|
+
/**
|
|
18
|
+
* We support both SI (decimal) and IEC (binary) units for bytes:
|
|
19
|
+
*
|
|
20
|
+
* SI/decimal (unit: 'decbytes'):
|
|
21
|
+
* 1 KB = 1000 bytes (1000^1 bytes)
|
|
22
|
+
* 1 MB = 1,000,000 bytes (1000^2 bytes)
|
|
23
|
+
* etc.
|
|
24
|
+
*
|
|
25
|
+
* IEC/binary (unit: 'bytes'):
|
|
26
|
+
* 1 KiB = 1024 bytes (1024^1 bytes)
|
|
27
|
+
* 1 MiB = 1,048,576 bytes (1024^2 bytes)
|
|
28
|
+
* etc.
|
|
29
|
+
*/ const DEFAULT_NUMBRO_MANTISSA = 2;
|
|
30
|
+
export const BYTES_GROUP_CONFIG = {
|
|
31
|
+
label: 'Bytes',
|
|
32
|
+
decimalPlaces: true,
|
|
33
|
+
shortValues: true
|
|
34
|
+
};
|
|
35
|
+
export const BYTES_UNIT_CONFIG = {
|
|
36
|
+
bytes: {
|
|
37
|
+
group: 'Bytes',
|
|
38
|
+
label: 'Bytes (IEC)'
|
|
39
|
+
},
|
|
40
|
+
decbytes: {
|
|
41
|
+
group: 'Bytes',
|
|
42
|
+
label: 'Bytes (SI)'
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
export function formatBytes(bytes, { unit = 'bytes', shortValues, decimalPlaces }) {
|
|
46
|
+
const isDecimal = unit === 'decbytes';
|
|
47
|
+
const threshold = isDecimal ? 1000 : 1024;
|
|
48
|
+
// If we're showing the entire value, we can use Intl.NumberFormat.
|
|
49
|
+
if (!shouldShortenValues(shortValues) || Math.abs(bytes) < threshold) {
|
|
50
|
+
const formatterOptions = {
|
|
51
|
+
style: 'unit',
|
|
52
|
+
unit: 'byte',
|
|
53
|
+
unitDisplay: 'long',
|
|
54
|
+
useGrouping: true
|
|
55
|
+
};
|
|
56
|
+
if (hasDecimalPlaces(decimalPlaces)) {
|
|
57
|
+
formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);
|
|
58
|
+
formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);
|
|
59
|
+
} else {
|
|
60
|
+
// This can happen if bytes is between -threshold and threshold (1000 for SI, 1024 for IEC)
|
|
61
|
+
if (shouldShortenValues(shortValues)) {
|
|
62
|
+
formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const key = [
|
|
66
|
+
formatterOptions.style,
|
|
67
|
+
formatterOptions.unit,
|
|
68
|
+
formatterOptions.unitDisplay,
|
|
69
|
+
formatterOptions.useGrouping,
|
|
70
|
+
formatterOptions.maximumSignificantDigits,
|
|
71
|
+
decimalPlaces,
|
|
72
|
+
shortValues,
|
|
73
|
+
unit
|
|
74
|
+
];
|
|
75
|
+
return getFormatterFromCache(key, 'bytes', formatterOptions, 'en-US')(bytes);
|
|
76
|
+
}
|
|
77
|
+
// If we're showing the shorten value, we use numbro.
|
|
78
|
+
// numbro is able to add units like KB, MB, GB, etc. correctly.
|
|
79
|
+
return numbro(bytes).format({
|
|
80
|
+
output: 'byte',
|
|
81
|
+
base: isDecimal ? 'decimal' : 'binary',
|
|
82
|
+
spaceSeparated: true,
|
|
83
|
+
mantissa: hasDecimalPlaces(decimalPlaces) ? decimalPlaces : DEFAULT_NUMBRO_MANTISSA,
|
|
84
|
+
// trimMantissa trims trailing 0s
|
|
85
|
+
trimMantissa: !hasDecimalPlaces(decimalPlaces),
|
|
86
|
+
// optionalMantissa excludes all the decimal places if they're all zeros
|
|
87
|
+
optionalMantissa: !hasDecimalPlaces(decimalPlaces)
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
//# sourceMappingURL=bytes.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/model/bytes.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 numbro from 'numbro';\n\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitGroupConfig, UnitConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces, shouldShortenValues } from './utils';\nimport { getFormatterFromCache } from './formatterCache';\n\n/**\n * We support both SI (decimal) and IEC (binary) units for bytes:\n *\n * SI/decimal (unit: 'decbytes'):\n * 1 KB = 1000 bytes (1000^1 bytes)\n * 1 MB = 1,000,000 bytes (1000^2 bytes)\n * etc.\n *\n * IEC/binary (unit: 'bytes'):\n * 1 KiB = 1024 bytes (1024^1 bytes)\n * 1 MiB = 1,048,576 bytes (1024^2 bytes)\n * etc.\n */\n\nconst DEFAULT_NUMBRO_MANTISSA = 2;\n\ntype BytesUnit = 'bytes' | 'decbytes';\n\nexport type BytesFormatOptions = {\n unit?: BytesUnit;\n decimalPlaces?: number;\n shortValues?: boolean;\n};\nexport const BYTES_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Bytes',\n decimalPlaces: true,\n shortValues: true,\n};\nexport const BYTES_UNIT_CONFIG: Readonly<Record<BytesUnit, UnitConfig>> = {\n bytes: {\n group: 'Bytes',\n label: 'Bytes (IEC)',\n },\n decbytes: {\n group: 'Bytes',\n label: 'Bytes (SI)',\n },\n};\n\nexport function formatBytes(bytes: number, { unit = 'bytes', shortValues, decimalPlaces }: BytesFormatOptions): string {\n const isDecimal = unit === 'decbytes';\n const threshold = isDecimal ? 1000 : 1024;\n\n // If we're showing the entire value, we can use Intl.NumberFormat.\n if (!shouldShortenValues(shortValues) || Math.abs(bytes) < threshold) {\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'unit',\n unit: 'byte',\n unitDisplay: 'long',\n useGrouping: true,\n };\n\n if (hasDecimalPlaces(decimalPlaces)) {\n formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);\n } else {\n // This can happen if bytes is between -threshold and threshold (1000 for SI, 1024 for IEC)\n if (shouldShortenValues(shortValues)) {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n }\n\n const key = [\n formatterOptions.style,\n formatterOptions.unit,\n formatterOptions.unitDisplay,\n formatterOptions.useGrouping,\n formatterOptions.maximumSignificantDigits,\n decimalPlaces,\n shortValues,\n unit,\n ];\n\n return getFormatterFromCache(key, 'bytes', formatterOptions, 'en-US')(bytes);\n }\n\n // If we're showing the shorten value, we use numbro.\n // numbro is able to add units like KB, MB, GB, etc. correctly.\n return numbro(bytes).format({\n output: 'byte',\n base: isDecimal ? 'decimal' : 'binary',\n spaceSeparated: true,\n mantissa: hasDecimalPlaces(decimalPlaces) ? decimalPlaces : DEFAULT_NUMBRO_MANTISSA,\n // trimMantissa trims trailing 0s\n trimMantissa: !hasDecimalPlaces(decimalPlaces),\n // optionalMantissa excludes all the decimal places if they're all zeros\n optionalMantissa: !hasDecimalPlaces(decimalPlaces),\n });\n}\n"],"names":["numbro","MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","shouldShortenValues","getFormatterFromCache","DEFAULT_NUMBRO_MANTISSA","BYTES_GROUP_CONFIG","label","decimalPlaces","shortValues","BYTES_UNIT_CONFIG","bytes","group","decbytes","formatBytes","unit","isDecimal","threshold","Math","abs","formatterOptions","style","unitDisplay","useGrouping","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","key","format","output","base","spaceSeparated","mantissa","trimMantissa","optionalMantissa"],"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,OAAOA,YAAY,SAAS;AAE5B,SAASC,sBAAsB,QAAQ,cAAc;AAErD,SAASC,gBAAgB,EAAEC,kBAAkB,EAAEC,mBAAmB,QAAQ,UAAU;AACpF,SAASC,qBAAqB,QAAQ,mBAAmB;AAEzD;;;;;;;;;;;;CAYC,GAED,MAAMC,0BAA0B;AAShC,OAAO,MAAMC,qBAAsC;IACjDC,OAAO;IACPC,eAAe;IACfC,aAAa;AACf,EAAE;AACF,OAAO,MAAMC,oBAA6D;IACxEC,OAAO;QACLC,OAAO;QACPL,OAAO;IACT;IACAM,UAAU;QACRD,OAAO;QACPL,OAAO;IACT;AACF,EAAE;AAEF,OAAO,SAASO,YAAYH,KAAa,EAAE,EAAEI,OAAO,OAAO,EAAEN,WAAW,EAAED,aAAa,EAAsB;IAC3G,MAAMQ,YAAYD,SAAS;IAC3B,MAAME,YAAYD,YAAY,OAAO;IAErC,mEAAmE;IACnE,IAAI,CAACb,oBAAoBM,gBAAgBS,KAAKC,GAAG,CAACR,SAASM,WAAW;QACpE,MAAMG,mBAA6C;YACjDC,OAAO;YACPN,MAAM;YACNO,aAAa;YACbC,aAAa;QACf;QAEA,IAAItB,iBAAiBO,gBAAgB;YACnCY,iBAAiBI,qBAAqB,GAAGtB,mBAAmBM;YAC5DY,iBAAiBK,qBAAqB,GAAGvB,mBAAmBM;QAC9D,OAAO;YACL,2FAA2F;YAC3F,IAAIL,oBAAoBM,cAAc;gBACpCW,iBAAiBM,wBAAwB,GAAG1B;YAC9C;QACF;QAEA,MAAM2B,MAAM;YACVP,iBAAiBC,KAAK;YACtBD,iBAAiBL,IAAI;YACrBK,iBAAiBE,WAAW;YAC5BF,iBAAiBG,WAAW;YAC5BH,iBAAiBM,wBAAwB;YACzClB;YACAC;YACAM;SACD;QAED,OAAOX,sBAAsBuB,KAAK,SAASP,kBAAkB,SAAST;IACxE;IAEA,qDAAqD;IACrD,+DAA+D;IAC/D,OAAOZ,OAAOY,OAAOiB,MAAM,CAAC;QAC1BC,QAAQ;QACRC,MAAMd,YAAY,YAAY;QAC9Be,gBAAgB;QAChBC,UAAU/B,iBAAiBO,iBAAiBA,gBAAgBH;QAC5D,iCAAiC;QACjC4B,cAAc,CAAChC,iBAAiBO;QAChC,wEAAwE;QACxE0B,kBAAkB,CAACjC,iBAAiBO;IACtC;AACF"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../../src/model/constants.ts"],"names":[],"mappings":"AAaA,eAAO,MAAM,sBAAsB,IAAI,CAAC"}
|
|
@@ -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 const MAX_SIGNIFICANT_DIGITS = 3;
|
|
14
|
+
|
|
15
|
+
//# sourceMappingURL=constants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/model/constants.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 const MAX_SIGNIFICANT_DIGITS = 3;\n"],"names":["MAX_SIGNIFICANT_DIGITS"],"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,OAAO,MAAMA,yBAAyB,EAAE"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { UnitConfig, UnitGroupConfig } from './types';
|
|
2
|
+
type CurrencyUnits = 'aud' | 'cad' | 'chf' | 'cny' | 'eur' | 'gbp' | 'hkd' | 'inr' | 'jpy' | 'krw' | 'nok' | 'nzd' | 'sek' | 'sgd' | 'usd';
|
|
3
|
+
export type CurrencyFormatOptions = {
|
|
4
|
+
unit: CurrencyUnits;
|
|
5
|
+
decimalPlaces?: number;
|
|
6
|
+
};
|
|
7
|
+
export declare const CURRENCY_GROUP_CONFIG: UnitGroupConfig;
|
|
8
|
+
export declare const CURRENCY_UNIT_CONFIG: Readonly<Record<CurrencyUnits, UnitConfig>>;
|
|
9
|
+
export declare function formatCurrency(value: number, { unit, decimalPlaces }: CurrencyFormatOptions): string;
|
|
10
|
+
export {};
|
|
11
|
+
//# sourceMappingURL=currency.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"currency.d.ts","sourceRoot":"","sources":["../../src/model/currency.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,SAAS,CAAC;AAMtD,KAAK,aAAa,GACd,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,GACL,KAAK,CAAC;AACV,MAAM,MAAM,qBAAqB,GAAG;IAClC,IAAI,EAAE,aAAa,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAGF,eAAO,MAAM,qBAAqB,EAAE,eAGnC,CAAC;AACF,eAAO,MAAM,oBAAoB,EAAE,QAAQ,CAAC,MAAM,CAAC,aAAa,EAAE,UAAU,CAAC,CA6D5E,CAAC;AAEF,wBAAgB,cAAc,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,aAAa,EAAE,EAAE,qBAAqB,GAAG,MAAM,CAwBpG"}
|
|
@@ -0,0 +1,107 @@
|
|
|
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 { toUpper } from 'lodash';
|
|
14
|
+
import { MAX_SIGNIFICANT_DIGITS } from './constants';
|
|
15
|
+
import { hasDecimalPlaces, limitDecimalPlaces } from './utils';
|
|
16
|
+
import { getFormatterFromCache } from './formatterCache';
|
|
17
|
+
const CURRENCY_GROUP = 'Currency';
|
|
18
|
+
export const CURRENCY_GROUP_CONFIG = {
|
|
19
|
+
label: 'Currency',
|
|
20
|
+
decimalPlaces: true
|
|
21
|
+
};
|
|
22
|
+
export const CURRENCY_UNIT_CONFIG = {
|
|
23
|
+
aud: {
|
|
24
|
+
group: CURRENCY_GROUP,
|
|
25
|
+
label: 'Australian Dollar (A$)'
|
|
26
|
+
},
|
|
27
|
+
cad: {
|
|
28
|
+
group: CURRENCY_GROUP,
|
|
29
|
+
label: 'Canadian Dollar (CA$)'
|
|
30
|
+
},
|
|
31
|
+
chf: {
|
|
32
|
+
group: CURRENCY_GROUP,
|
|
33
|
+
label: 'Swiss Franc (CHF)'
|
|
34
|
+
},
|
|
35
|
+
cny: {
|
|
36
|
+
group: CURRENCY_GROUP,
|
|
37
|
+
label: 'Renminbi (CN¥)'
|
|
38
|
+
},
|
|
39
|
+
eur: {
|
|
40
|
+
group: CURRENCY_GROUP,
|
|
41
|
+
label: 'Euro (€)'
|
|
42
|
+
},
|
|
43
|
+
gbp: {
|
|
44
|
+
group: CURRENCY_GROUP,
|
|
45
|
+
label: 'Pound (£)'
|
|
46
|
+
},
|
|
47
|
+
hkd: {
|
|
48
|
+
group: CURRENCY_GROUP,
|
|
49
|
+
label: 'Hong Kong Dollar (HK$)'
|
|
50
|
+
},
|
|
51
|
+
inr: {
|
|
52
|
+
group: CURRENCY_GROUP,
|
|
53
|
+
label: 'Indian Rupee (₹)'
|
|
54
|
+
},
|
|
55
|
+
jpy: {
|
|
56
|
+
group: CURRENCY_GROUP,
|
|
57
|
+
label: 'Yen (¥)'
|
|
58
|
+
},
|
|
59
|
+
krw: {
|
|
60
|
+
group: CURRENCY_GROUP,
|
|
61
|
+
label: 'South Korean Won (₩)'
|
|
62
|
+
},
|
|
63
|
+
nok: {
|
|
64
|
+
group: CURRENCY_GROUP,
|
|
65
|
+
label: 'Norwegian Krone (NOK)'
|
|
66
|
+
},
|
|
67
|
+
nzd: {
|
|
68
|
+
group: CURRENCY_GROUP,
|
|
69
|
+
label: 'New Zealand Dollar (NZ$)'
|
|
70
|
+
},
|
|
71
|
+
sek: {
|
|
72
|
+
group: CURRENCY_GROUP,
|
|
73
|
+
label: 'Swedish Krona (SEK)'
|
|
74
|
+
},
|
|
75
|
+
sgd: {
|
|
76
|
+
group: CURRENCY_GROUP,
|
|
77
|
+
label: 'Singapore Dollar (S$)'
|
|
78
|
+
},
|
|
79
|
+
usd: {
|
|
80
|
+
group: CURRENCY_GROUP,
|
|
81
|
+
label: 'US Dollar ($)'
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
export function formatCurrency(value, { unit, decimalPlaces }) {
|
|
85
|
+
const formatterOptions = {
|
|
86
|
+
style: 'currency',
|
|
87
|
+
currency: toUpper(unit),
|
|
88
|
+
currencyDisplay: 'symbol'
|
|
89
|
+
};
|
|
90
|
+
if (hasDecimalPlaces(decimalPlaces)) {
|
|
91
|
+
formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimalPlaces);
|
|
92
|
+
formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimalPlaces);
|
|
93
|
+
} else {
|
|
94
|
+
formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
|
|
95
|
+
}
|
|
96
|
+
const key = [
|
|
97
|
+
formatterOptions.style,
|
|
98
|
+
formatterOptions.currency,
|
|
99
|
+
formatterOptions.currencyDisplay,
|
|
100
|
+
formatterOptions.maximumSignificantDigits,
|
|
101
|
+
decimalPlaces,
|
|
102
|
+
unit
|
|
103
|
+
];
|
|
104
|
+
return getFormatterFromCache(key, 'currency', formatterOptions, 'en-US')(value);
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
//# sourceMappingURL=currency.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/model/currency.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 { toUpper } from 'lodash';\nimport { MAX_SIGNIFICANT_DIGITS } from './constants';\nimport { UnitConfig, UnitGroupConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces } from './utils';\nimport { getFormatterFromCache } from './formatterCache';\n\n// See Intl.supportedValuesOf(\"currency\") for valid options, key names will\n// be converted to uppercase to match the expectation of Intl.NumberFormat\ntype CurrencyUnits =\n | 'aud'\n | 'cad'\n | 'chf'\n | 'cny'\n | 'eur'\n | 'gbp'\n | 'hkd'\n | 'inr'\n | 'jpy'\n | 'krw'\n | 'nok'\n | 'nzd'\n | 'sek'\n | 'sgd'\n | 'usd';\nexport type CurrencyFormatOptions = {\n unit: CurrencyUnits;\n decimalPlaces?: number;\n};\n\nconst CURRENCY_GROUP = 'Currency';\nexport const CURRENCY_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Currency',\n decimalPlaces: true,\n};\nexport const CURRENCY_UNIT_CONFIG: Readonly<Record<CurrencyUnits, UnitConfig>> = {\n aud: {\n group: CURRENCY_GROUP,\n label: 'Australian Dollar (A$)',\n },\n cad: {\n group: CURRENCY_GROUP,\n label: 'Canadian Dollar (CA$)',\n },\n chf: {\n group: CURRENCY_GROUP,\n label: 'Swiss Franc (CHF)',\n },\n cny: {\n group: CURRENCY_GROUP,\n label: 'Renminbi (CN¥)',\n },\n eur: {\n group: CURRENCY_GROUP,\n label: 'Euro (€)',\n },\n gbp: {\n group: CURRENCY_GROUP,\n label: 'Pound (£)',\n },\n hkd: {\n group: CURRENCY_GROUP,\n label: 'Hong Kong Dollar (HK$)',\n },\n inr: {\n group: CURRENCY_GROUP,\n label: 'Indian Rupee (₹)',\n },\n jpy: {\n group: CURRENCY_GROUP,\n label: 'Yen (¥)',\n },\n krw: {\n group: CURRENCY_GROUP,\n label: 'South Korean Won (₩)',\n },\n nok: {\n group: CURRENCY_GROUP,\n label: 'Norwegian Krone (NOK)',\n },\n nzd: {\n group: CURRENCY_GROUP,\n label: 'New Zealand Dollar (NZ$)',\n },\n sek: {\n group: CURRENCY_GROUP,\n label: 'Swedish Krona (SEK)',\n },\n sgd: {\n group: CURRENCY_GROUP,\n label: 'Singapore Dollar (S$)',\n },\n usd: {\n group: CURRENCY_GROUP,\n label: 'US Dollar ($)',\n },\n};\n\nexport function formatCurrency(value: number, { unit, decimalPlaces }: CurrencyFormatOptions): string {\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'currency',\n currency: toUpper(unit),\n currencyDisplay: 'symbol',\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.currency,\n formatterOptions.currencyDisplay,\n formatterOptions.maximumSignificantDigits,\n decimalPlaces,\n unit,\n ];\n\n return getFormatterFromCache(key, 'currency', formatterOptions, 'en-US')(value);\n}\n"],"names":["toUpper","MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","getFormatterFromCache","CURRENCY_GROUP","CURRENCY_GROUP_CONFIG","label","decimalPlaces","CURRENCY_UNIT_CONFIG","aud","group","cad","chf","cny","eur","gbp","hkd","inr","jpy","krw","nok","nzd","sek","sgd","usd","formatCurrency","value","unit","formatterOptions","style","currency","currencyDisplay","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,OAAO,QAAQ,SAAS;AACjC,SAASC,sBAAsB,QAAQ,cAAc;AAErD,SAASC,gBAAgB,EAAEC,kBAAkB,QAAQ,UAAU;AAC/D,SAASC,qBAAqB,QAAQ,mBAAmB;AAyBzD,MAAMC,iBAAiB;AACvB,OAAO,MAAMC,wBAAyC;IACpDC,OAAO;IACPC,eAAe;AACjB,EAAE;AACF,OAAO,MAAMC,uBAAoE;IAC/EC,KAAK;QACHC,OAAON;QACPE,OAAO;IACT;IACAK,KAAK;QACHD,OAAON;QACPE,OAAO;IACT;IACAM,KAAK;QACHF,OAAON;QACPE,OAAO;IACT;IACAO,KAAK;QACHH,OAAON;QACPE,OAAO;IACT;IACAQ,KAAK;QACHJ,OAAON;QACPE,OAAO;IACT;IACAS,KAAK;QACHL,OAAON;QACPE,OAAO;IACT;IACAU,KAAK;QACHN,OAAON;QACPE,OAAO;IACT;IACAW,KAAK;QACHP,OAAON;QACPE,OAAO;IACT;IACAY,KAAK;QACHR,OAAON;QACPE,OAAO;IACT;IACAa,KAAK;QACHT,OAAON;QACPE,OAAO;IACT;IACAc,KAAK;QACHV,OAAON;QACPE,OAAO;IACT;IACAe,KAAK;QACHX,OAAON;QACPE,OAAO;IACT;IACAgB,KAAK;QACHZ,OAAON;QACPE,OAAO;IACT;IACAiB,KAAK;QACHb,OAAON;QACPE,OAAO;IACT;IACAkB,KAAK;QACHd,OAAON;QACPE,OAAO;IACT;AACF,EAAE;AAEF,OAAO,SAASmB,eAAeC,KAAa,EAAE,EAAEC,IAAI,EAAEpB,aAAa,EAAyB;IAC1F,MAAMqB,mBAA6C;QACjDC,OAAO;QACPC,UAAU/B,QAAQ4B;QAClBI,iBAAiB;IACnB;IAEA,IAAI9B,iBAAiBM,gBAAgB;QACnCqB,iBAAiBI,qBAAqB,GAAG9B,mBAAmBK;QAC5DqB,iBAAiBK,qBAAqB,GAAG/B,mBAAmBK;IAC9D,OAAO;QACLqB,iBAAiBM,wBAAwB,GAAGlC;IAC9C;IAEA,MAAMmC,MAAM;QACVP,iBAAiBC,KAAK;QACtBD,iBAAiBE,QAAQ;QACzBF,iBAAiBG,eAAe;QAChCH,iBAAiBM,wBAAwB;QACzC3B;QACAoB;KACD;IAED,OAAOxB,sBAAsBgC,KAAK,YAAYP,kBAAkB,SAASF;AAC3E"}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { UnitGroupConfig, UnitConfig } from './types';
|
|
2
|
+
type DateUnits = 'datetime-iso' | 'datetime-us' | 'datetime-local' | 'date-iso' | 'date-us' | 'date-local' | 'time-local' | 'time-iso' | 'time-us' | 'relative-time' | 'unix-timestamp' | 'unix-timestamp-ms';
|
|
3
|
+
export type DateFormatOptions = {
|
|
4
|
+
unit?: DateUnits;
|
|
5
|
+
/**
|
|
6
|
+
* The locale to use for formatting. Defaults to the system's locale.
|
|
7
|
+
*/
|
|
8
|
+
locale?: string;
|
|
9
|
+
/**
|
|
10
|
+
* The timezone to use for formatting. Defaults to the user's local timezone.
|
|
11
|
+
*/
|
|
12
|
+
timeZone?: string;
|
|
13
|
+
/**
|
|
14
|
+
* For relative time formatting, the reference time to compare against.
|
|
15
|
+
* Defaults to current time.
|
|
16
|
+
*/
|
|
17
|
+
referenceTime?: number;
|
|
18
|
+
/**
|
|
19
|
+
* This property is not used for date formatting, but is included for
|
|
20
|
+
* compatibility with the FormatControls UI component.
|
|
21
|
+
*/
|
|
22
|
+
decimalPlaces?: number;
|
|
23
|
+
};
|
|
24
|
+
export declare const DATE_GROUP_CONFIG: UnitGroupConfig;
|
|
25
|
+
export declare const DATE_UNIT_CONFIG: Readonly<Record<DateUnits, UnitConfig>>;
|
|
26
|
+
export declare function formatDate(value: number, options?: DateFormatOptions): string;
|
|
27
|
+
export {};
|
|
28
|
+
//# sourceMappingURL=date.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"date.d.ts","sourceRoot":"","sources":["../../src/model/date.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEtD,KAAK,SAAS,GACV,cAAc,GACd,aAAa,GACb,gBAAgB,GAChB,UAAU,GACV,SAAS,GACT,YAAY,GACZ,YAAY,GACZ,UAAU,GACV,SAAS,GACT,eAAe,GACf,gBAAgB,GAChB,mBAAmB,CAAC;AAExB,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,CAAC,EAAE,SAAS,CAAC;IACjB;;OAEG;IACH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB;;OAEG;IACH,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB;;;OAGG;IACH,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB,CAAC;AAIF,eAAO,MAAM,iBAAiB,EAAE,eAE/B,CAAC;AAEF,eAAO,MAAM,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,CAiDpE,CAAC;AAqGF,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,GAAE,iBAAsB,GAAG,MAAM,CAwHjF"}
|