@perses-dev/components 0.0.0-snapshot-color-palette-gen-test-0ebddd6 → 0.0.0-snapshot-tooltip-highlight-c995fc8

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.
Files changed (76) hide show
  1. package/dist/DateTimeRangePicker/TimeRangeSelector.d.ts.map +1 -1
  2. package/dist/DateTimeRangePicker/TimeRangeSelector.js +7 -1
  3. package/dist/DateTimeRangePicker/TimeRangeSelector.js.map +1 -1
  4. package/dist/EChart/EChart.d.ts.map +1 -1
  5. package/dist/EChart/EChart.js +7 -1
  6. package/dist/EChart/EChart.js.map +1 -1
  7. package/dist/LineChart/LineChart.d.ts.map +1 -1
  8. package/dist/LineChart/LineChart.js +9 -9
  9. package/dist/LineChart/LineChart.js.map +1 -1
  10. package/dist/StatChart/StatChart.d.ts.map +1 -1
  11. package/dist/StatChart/StatChart.js +12 -15
  12. package/dist/StatChart/StatChart.js.map +1 -1
  13. package/dist/TimeSeriesTooltip/TooltipContent.js +1 -1
  14. package/dist/TimeSeriesTooltip/TooltipContent.js.map +1 -1
  15. package/dist/TimeSeriesTooltip/focused-series.d.ts +5 -2
  16. package/dist/TimeSeriesTooltip/focused-series.d.ts.map +1 -1
  17. package/dist/TimeSeriesTooltip/focused-series.js +36 -7
  18. package/dist/TimeSeriesTooltip/focused-series.js.map +1 -1
  19. package/dist/TimeSeriesTooltip/tooltip-model.d.ts +2 -2
  20. package/dist/TimeSeriesTooltip/tooltip-model.js +2 -2
  21. package/dist/TimeSeriesTooltip/tooltip-model.js.map +1 -1
  22. package/dist/UnitSelector/UnitSelector.d.ts.map +1 -1
  23. package/dist/UnitSelector/UnitSelector.js +38 -16
  24. package/dist/UnitSelector/UnitSelector.js.map +1 -1
  25. package/dist/YAxisLabel.d.ts.map +1 -1
  26. package/dist/YAxisLabel.js +1 -0
  27. package/dist/YAxisLabel.js.map +1 -1
  28. package/dist/cjs/DateTimeRangePicker/TimeRangeSelector.js +7 -1
  29. package/dist/cjs/EChart/EChart.js +7 -1
  30. package/dist/cjs/LineChart/LineChart.js +9 -9
  31. package/dist/cjs/StatChart/StatChart.js +12 -15
  32. package/dist/cjs/TimeSeriesTooltip/TooltipContent.js +1 -1
  33. package/dist/cjs/TimeSeriesTooltip/focused-series.js +37 -6
  34. package/dist/cjs/TimeSeriesTooltip/tooltip-model.js +2 -2
  35. package/dist/cjs/UnitSelector/UnitSelector.js +37 -15
  36. package/dist/cjs/YAxisLabel.js +1 -0
  37. package/dist/cjs/model/units/bytes.js +18 -16
  38. package/dist/cjs/model/units/constants.js +3 -3
  39. package/dist/cjs/model/units/decimal.js +21 -18
  40. package/dist/cjs/model/units/percent.js +20 -11
  41. package/dist/cjs/model/units/time.js +25 -21
  42. package/dist/cjs/model/units/units.js +2 -2
  43. package/dist/cjs/model/units/utils.js +42 -0
  44. package/dist/cjs/utils/theme-gen.js +8 -13
  45. package/dist/model/units/bytes.d.ts +1 -1
  46. package/dist/model/units/bytes.d.ts.map +1 -1
  47. package/dist/model/units/bytes.js +19 -17
  48. package/dist/model/units/bytes.js.map +1 -1
  49. package/dist/model/units/constants.d.ts +1 -1
  50. package/dist/model/units/constants.js +1 -1
  51. package/dist/model/units/constants.js.map +1 -1
  52. package/dist/model/units/decimal.d.ts +2 -2
  53. package/dist/model/units/decimal.d.ts.map +1 -1
  54. package/dist/model/units/decimal.js +21 -18
  55. package/dist/model/units/decimal.js.map +1 -1
  56. package/dist/model/units/percent.d.ts +2 -2
  57. package/dist/model/units/percent.d.ts.map +1 -1
  58. package/dist/model/units/percent.js +20 -11
  59. package/dist/model/units/percent.js.map +1 -1
  60. package/dist/model/units/time.d.ts +2 -2
  61. package/dist/model/units/time.d.ts.map +1 -1
  62. package/dist/model/units/time.js +25 -21
  63. package/dist/model/units/time.js.map +1 -1
  64. package/dist/model/units/types.d.ts +9 -0
  65. package/dist/model/units/types.d.ts.map +1 -1
  66. package/dist/model/units/types.js.map +1 -1
  67. package/dist/model/units/units.js +2 -2
  68. package/dist/model/units/units.js.map +1 -1
  69. package/dist/model/units/utils.d.ts +4 -0
  70. package/dist/model/units/utils.d.ts.map +1 -0
  71. package/dist/model/units/utils.js +32 -0
  72. package/dist/model/units/utils.js.map +1 -0
  73. package/dist/utils/theme-gen.d.ts.map +1 -1
  74. package/dist/utils/theme-gen.js +8 -13
  75. package/dist/utils/theme-gen.js.map +1 -1
  76. package/package.json +4 -4
@@ -34,6 +34,7 @@ function YAxisLabel({ name , height }) {
34
34
  },
35
35
  children: /*#__PURE__*/ (0, _jsxRuntime.jsx)(_material.Typography, {
36
36
  variant: "body1",
37
+ "aria-label": "y axis label",
37
38
  sx: {
38
39
  whiteSpace: 'nowrap',
39
40
  overflow: 'hidden',
@@ -27,11 +27,13 @@ _export(exports, {
27
27
  });
28
28
  const _numbro = /*#__PURE__*/ _interopRequireDefault(require("numbro"));
29
29
  const _constants = require("./constants");
30
+ const _utils = require("./utils");
30
31
  function _interopRequireDefault(obj) {
31
32
  return obj && obj.__esModule ? obj : {
32
33
  default: obj
33
34
  };
34
35
  }
36
+ const DEFAULT_NUMBRO_MANTISSA = 2;
35
37
  const bytesUnitKinds = [
36
38
  'Bytes'
37
39
  ];
@@ -48,32 +50,32 @@ const BYTES_UNIT_CONFIG = {
48
50
  label: 'Bytes'
49
51
  }
50
52
  };
51
- function formatBytes(bytes, { abbreviate , decimal_places }) {
52
- if (bytes === 0) return '0 bytes';
53
- decimal_places = decimal_places !== null && decimal_places !== void 0 ? decimal_places : _constants.DEFAULT_DECIMAL_PLACES;
54
- // Avoids maximumFractionDigits value is out of range error. Possible values are 0 to 20.
55
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#minimumfractiondigits
56
- if (decimal_places < 0) {
57
- decimal_places = 0;
58
- } else if (decimal_places > 20) {
59
- decimal_places = 20;
60
- }
61
- const showFullNumber = abbreviate == false || bytes < 1000;
62
- if (showFullNumber) {
63
- const formatter = new Intl.NumberFormat('en-US', {
53
+ function formatBytes(bytes, options) {
54
+ const { abbreviate , decimal_places } = options;
55
+ if (!(0, _utils.shouldAbbreviate)(abbreviate) || Math.abs(bytes) < 1000) {
56
+ const formatterOptions = {
64
57
  style: 'unit',
65
58
  unit: 'byte',
66
59
  unitDisplay: 'long',
67
- maximumFractionDigits: decimal_places,
68
60
  useGrouping: true
69
- });
61
+ };
62
+ if ((0, _utils.hasDecimalPlaces)(decimal_places)) {
63
+ formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
64
+ } else {
65
+ // This can happen if bytes is between -1000 and 1000
66
+ if ((0, _utils.shouldAbbreviate)(abbreviate)) {
67
+ formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
68
+ }
69
+ }
70
+ const formatter = Intl.NumberFormat('en-US', formatterOptions);
70
71
  return formatter.format(bytes);
71
72
  }
73
+ // numbro is able to add units like KB, MB, GB, etc. correctly
72
74
  return (0, _numbro.default)(bytes).format({
73
75
  output: 'byte',
74
76
  base: 'decimal',
75
77
  spaceSeparated: true,
76
- mantissa: decimal_places,
78
+ mantissa: decimal_places !== null && decimal_places !== void 0 ? decimal_places : DEFAULT_NUMBRO_MANTISSA,
77
79
  trimMantissa: true,
78
80
  optionalMantissa: true
79
81
  });
@@ -16,8 +16,8 @@
16
16
  Object.defineProperty(exports, "__esModule", {
17
17
  value: true
18
18
  });
19
- Object.defineProperty(exports, "DEFAULT_DECIMAL_PLACES", {
19
+ Object.defineProperty(exports, "MAX_SIGNIFICANT_DIGITS", {
20
20
  enumerable: true,
21
- get: ()=>DEFAULT_DECIMAL_PLACES
21
+ get: ()=>MAX_SIGNIFICANT_DIGITS
22
22
  });
23
- const DEFAULT_DECIMAL_PLACES = 2;
23
+ const MAX_SIGNIFICANT_DIGITS = 3;
@@ -21,17 +21,19 @@ function _export(target, all) {
21
21
  });
22
22
  }
23
23
  _export(exports, {
24
- PERCENT_GROUP_CONFIG: ()=>PERCENT_GROUP_CONFIG,
24
+ DECIMAL_GROUP_CONFIG: ()=>DECIMAL_GROUP_CONFIG,
25
25
  DECIMAL_UNIT_CONFIG: ()=>DECIMAL_UNIT_CONFIG,
26
26
  formatDecimal: ()=>formatDecimal
27
27
  });
28
28
  const _constants = require("./constants");
29
+ const _utils = require("./utils");
29
30
  const decimalUnitKinds = [
30
31
  'Decimal'
31
32
  ];
32
- const PERCENT_GROUP_CONFIG = {
33
- label: 'Percent',
34
- decimal_places: true
33
+ const DECIMAL_GROUP_CONFIG = {
34
+ label: 'Decimal',
35
+ decimal_places: true,
36
+ abbreviate: true
35
37
  };
36
38
  const DECIMAL_UNIT_CONFIG = {
37
39
  Decimal: {
@@ -39,21 +41,22 @@ const DECIMAL_UNIT_CONFIG = {
39
41
  label: 'Decimal'
40
42
  }
41
43
  };
42
- function formatDecimal(value, { abbreviate , decimal_places }) {
43
- decimal_places = decimal_places !== null && decimal_places !== void 0 ? decimal_places : _constants.DEFAULT_DECIMAL_PLACES;
44
- // Avoids maximumFractionDigits value is out of range error. Possible values are 0 to 20.
45
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#minimumfractiondigits
46
- if (decimal_places < 0) {
47
- decimal_places = 0;
48
- } else if (decimal_places > 20) {
49
- decimal_places = 20;
50
- }
51
- const showFullNumber = abbreviate == false || value < 1000;
52
- const formatter = new Intl.NumberFormat('en-US', {
44
+ function formatDecimal(value, options) {
45
+ const { abbreviate , decimal_places } = options;
46
+ const formatterOptions = {
53
47
  style: 'decimal',
54
- notation: showFullNumber ? 'standard' : 'compact',
55
- maximumFractionDigits: decimal_places,
56
48
  useGrouping: true
57
- });
49
+ };
50
+ if ((0, _utils.shouldAbbreviate)(abbreviate)) {
51
+ formatterOptions.notation = 'compact';
52
+ }
53
+ if ((0, _utils.hasDecimalPlaces)(decimal_places)) {
54
+ formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
55
+ } else {
56
+ if ((0, _utils.shouldAbbreviate)(abbreviate)) {
57
+ formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
58
+ }
59
+ }
60
+ const formatter = Intl.NumberFormat('en-US', formatterOptions);
58
61
  return formatter.format(value);
59
62
  }
@@ -21,20 +21,20 @@ function _export(target, all) {
21
21
  });
22
22
  }
23
23
  _export(exports, {
24
- DECIMAL_GROUP_CONFIG: ()=>DECIMAL_GROUP_CONFIG,
24
+ PERCENT_GROUP_CONFIG: ()=>PERCENT_GROUP_CONFIG,
25
25
  PERCENT_UNIT_CONFIG: ()=>PERCENT_UNIT_CONFIG,
26
26
  formatPercent: ()=>formatPercent
27
27
  });
28
28
  const _constants = require("./constants");
29
+ const _utils = require("./utils");
29
30
  const percentUnitKinds = [
30
31
  'Percent',
31
32
  'PercentDecimal',
32
33
  '%'
33
34
  ];
34
- const DECIMAL_GROUP_CONFIG = {
35
- label: 'Decimal',
36
- decimal_places: true,
37
- abbreviate: true
35
+ const PERCENT_GROUP_CONFIG = {
36
+ label: 'Percent',
37
+ decimal_places: true
38
38
  };
39
39
  const PERCENT_GROUP = 'Percent';
40
40
  const PERCENT_UNIT_CONFIG = {
@@ -54,11 +54,20 @@ const PERCENT_UNIT_CONFIG = {
54
54
  label: '%'
55
55
  }
56
56
  };
57
- function formatPercent(value, unitOptions) {
58
- var _decimal_places;
59
- const decimals = (_decimal_places = unitOptions.decimal_places) !== null && _decimal_places !== void 0 ? _decimal_places : _constants.DEFAULT_DECIMAL_PLACES;
60
- if (unitOptions.kind === 'PercentDecimal') {
61
- value = value * 100;
57
+ function formatPercent(value, { kind , decimal_places }) {
58
+ // Intl.NumberFormat translates 0 -> 0%, 0.5 -> 50%, 1 -> 100%
59
+ if (kind === 'Percent') {
60
+ value = value / 100;
62
61
  }
63
- return value.toFixed(decimals) + '%';
62
+ const formatterOptions = {
63
+ style: 'percent',
64
+ useGrouping: true
65
+ };
66
+ if ((0, _utils.hasDecimalPlaces)(decimal_places)) {
67
+ formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
68
+ } else {
69
+ formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
70
+ }
71
+ const formatter = Intl.NumberFormat('en-us', formatterOptions);
72
+ return formatter.format(value);
64
73
  }
@@ -21,12 +21,13 @@ function _export(target, all) {
21
21
  });
22
22
  }
23
23
  _export(exports, {
24
- TimeIntlDuration: ()=>TimeIntlDuration,
24
+ PersesTimeToIntlTime: ()=>PersesTimeToIntlTime,
25
25
  TIME_GROUP_CONFIG: ()=>TIME_GROUP_CONFIG,
26
26
  TIME_UNIT_CONFIG: ()=>TIME_UNIT_CONFIG,
27
27
  formatTime: ()=>formatTime
28
28
  });
29
29
  const _constants = require("./constants");
30
+ const _utils = require("./utils");
30
31
  const timeUnitKinds = [
31
32
  'Milliseconds',
32
33
  'Seconds',
@@ -76,26 +77,29 @@ const TIME_UNIT_CONFIG = {
76
77
  label: 'Years'
77
78
  }
78
79
  };
79
- var TimeIntlDuration;
80
- (function(TimeIntlDuration) {
81
- TimeIntlDuration["Milliseconds"] = 'millisecond';
82
- TimeIntlDuration["Seconds"] = 'second';
83
- TimeIntlDuration["Minutes"] = 'minute';
84
- TimeIntlDuration["Hours"] = 'hour';
85
- TimeIntlDuration["Days"] = 'day';
86
- TimeIntlDuration["Weeks"] = 'week';
87
- TimeIntlDuration["Months"] = 'month';
88
- TimeIntlDuration["Years"] = 'year';
89
- })(TimeIntlDuration || (TimeIntlDuration = {}));
90
- function formatTime(value, unitOptions) {
91
- var _decimal_places;
92
- const decimalPlaces = (_decimal_places = unitOptions.decimal_places) !== null && _decimal_places !== void 0 ? _decimal_places : _constants.DEFAULT_DECIMAL_PLACES;
93
- const timeUnit = TimeIntlDuration[unitOptions.kind];
94
- const formatter = new Intl.NumberFormat('en-US', {
80
+ var PersesTimeToIntlTime;
81
+ (function(PersesTimeToIntlTime) {
82
+ PersesTimeToIntlTime["Milliseconds"] = 'millisecond';
83
+ PersesTimeToIntlTime["Seconds"] = 'second';
84
+ PersesTimeToIntlTime["Minutes"] = 'minute';
85
+ PersesTimeToIntlTime["Hours"] = 'hour';
86
+ PersesTimeToIntlTime["Days"] = 'day';
87
+ PersesTimeToIntlTime["Weeks"] = 'week';
88
+ PersesTimeToIntlTime["Months"] = 'month';
89
+ PersesTimeToIntlTime["Years"] = 'year';
90
+ })(PersesTimeToIntlTime || (PersesTimeToIntlTime = {}));
91
+ function formatTime(value, { kind , decimal_places }) {
92
+ const isMonthOrYear = kind === 'Months' || kind === 'Years';
93
+ const formatterOptions = {
95
94
  style: 'unit',
96
- unit: timeUnit,
97
- unitDisplay: 'long',
98
- maximumFractionDigits: decimalPlaces
99
- });
95
+ unit: PersesTimeToIntlTime[kind],
96
+ unitDisplay: isMonthOrYear ? 'long' : 'narrow'
97
+ };
98
+ if ((0, _utils.hasDecimalPlaces)(decimal_places)) {
99
+ formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
100
+ } else {
101
+ formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
102
+ }
103
+ const formatter = Intl.NumberFormat('en-US', formatterOptions);
100
104
  return formatter.format(value);
101
105
  }
@@ -40,8 +40,8 @@ const _percent = require("./percent");
40
40
  const _time = require("./time");
41
41
  const UNIT_GROUP_CONFIG = {
42
42
  Time: _time.TIME_GROUP_CONFIG,
43
- Percent: _decimal.PERCENT_GROUP_CONFIG,
44
- Decimal: _percent.DECIMAL_GROUP_CONFIG,
43
+ Percent: _percent.PERCENT_GROUP_CONFIG,
44
+ Decimal: _decimal.DECIMAL_GROUP_CONFIG,
45
45
  Bytes: _bytes.BYTES_GROUP_CONFIG
46
46
  };
47
47
  const UNIT_CONFIG = {
@@ -0,0 +1,42 @@
1
+ // Copyright 2023 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
+ "use strict";
14
+ Object.defineProperty(exports, "__esModule", {
15
+ value: true
16
+ });
17
+ function _export(target, all) {
18
+ for(var name in all)Object.defineProperty(target, name, {
19
+ enumerable: true,
20
+ get: all[name]
21
+ });
22
+ }
23
+ _export(exports, {
24
+ shouldAbbreviate: ()=>shouldAbbreviate,
25
+ hasDecimalPlaces: ()=>hasDecimalPlaces,
26
+ limitDecimalPlaces: ()=>limitDecimalPlaces
27
+ });
28
+ function shouldAbbreviate(abbreviate) {
29
+ return abbreviate !== false;
30
+ }
31
+ function hasDecimalPlaces(decimal_places) {
32
+ return typeof decimal_places === 'number';
33
+ }
34
+ function limitDecimalPlaces(num) {
35
+ if (!num) return num;
36
+ if (num < 0) {
37
+ num = 0;
38
+ } else if (num > 20) {
39
+ num = 20;
40
+ }
41
+ return num;
42
+ }
@@ -40,20 +40,15 @@ function generateChartsTheme(muiTheme, echartsTheme) {
40
40
  left: 20,
41
41
  containLabel: true
42
42
  },
43
- // Accessible categorical palette from: https://colorbrewer2.org/#type=qualitative&scheme=Paired&n=12
43
+ // Accessible categorical palette from: https://davidmathlogic.com/colorblind
44
44
  color: [
45
- '#a6cee3',
46
- '#1f78b4',
47
- '#b2df8a',
48
- '#33a02c',
49
- '#fb9a99',
50
- '#e31a1c',
51
- '#fdbf6f',
52
- '#ff7f00',
53
- '#cab2d6',
54
- '#6a3d9a',
55
- '#ffff99',
56
- '#b15928'
45
+ '#56B4E9',
46
+ '#009E73',
47
+ '#0072B2',
48
+ '#CC79A7',
49
+ '#F0E442',
50
+ '#E69F00',
51
+ '#D55E00'
57
52
  ],
58
53
  categoryAxis: {
59
54
  show: true,
@@ -8,6 +8,6 @@ export declare type BytesUnitOptions = {
8
8
  };
9
9
  export declare const BYTES_GROUP_CONFIG: UnitGroupConfig;
10
10
  export declare const BYTES_UNIT_CONFIG: Readonly<Record<BytesUnitKind, UnitConfig>>;
11
- export declare function formatBytes(bytes: number, { abbreviate, decimal_places }: BytesUnitOptions): string;
11
+ export declare function formatBytes(bytes: number, options: BytesUnitOptions): string;
12
12
  export {};
13
13
  //# sourceMappingURL=bytes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../../src/model/units/bytes.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEtD,QAAA,MAAM,cAAc,oBAAqB,CAAC;AAC1C,aAAK,aAAa,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AACrD,oBAAY,gBAAgB,GAAG;IAC7B,IAAI,EAAE,aAAa,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AACF,eAAO,MAAM,kBAAkB,EAAE,eAIhC,CAAC;AACF,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CAAC,MAAM,CAAC,aAAa,EAAE,UAAU,CAAC,CAOzE,CAAC;AAEF,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,UAAU,EAAE,cAAc,EAAE,EAAE,gBAAgB,UAiC1F"}
1
+ {"version":3,"file":"bytes.d.ts","sourceRoot":"","sources":["../../../src/model/units/bytes.ts"],"names":[],"mappings":"AAgBA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAKtD,QAAA,MAAM,cAAc,oBAAqB,CAAC;AAC1C,aAAK,aAAa,GAAG,CAAC,OAAO,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC;AACrD,oBAAY,gBAAgB,GAAG;IAC7B,IAAI,EAAE,aAAa,CAAC;IACpB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AACF,eAAO,MAAM,kBAAkB,EAAE,eAIhC,CAAC;AACF,eAAO,MAAM,iBAAiB,EAAE,QAAQ,CAAC,MAAM,CAAC,aAAa,EAAE,UAAU,CAAC,CAOzE,CAAC;AAEF,wBAAgB,WAAW,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,gBAAgB,UAiCnE"}
@@ -11,7 +11,9 @@
11
11
  // See the License for the specific language governing permissions and
12
12
  // limitations under the License.
13
13
  import numbro from 'numbro';
14
- import { DEFAULT_DECIMAL_PLACES } from './constants';
14
+ import { MAX_SIGNIFICANT_DIGITS } from './constants';
15
+ import { hasDecimalPlaces, limitDecimalPlaces, shouldAbbreviate } from './utils';
16
+ const DEFAULT_NUMBRO_MANTISSA = 2;
15
17
  const bytesUnitKinds = [
16
18
  'Bytes'
17
19
  ];
@@ -28,32 +30,32 @@ export const BYTES_UNIT_CONFIG = {
28
30
  label: 'Bytes'
29
31
  }
30
32
  };
31
- export function formatBytes(bytes, { abbreviate , decimal_places }) {
32
- if (bytes === 0) return '0 bytes';
33
- decimal_places = decimal_places !== null && decimal_places !== void 0 ? decimal_places : DEFAULT_DECIMAL_PLACES;
34
- // Avoids maximumFractionDigits value is out of range error. Possible values are 0 to 20.
35
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#minimumfractiondigits
36
- if (decimal_places < 0) {
37
- decimal_places = 0;
38
- } else if (decimal_places > 20) {
39
- decimal_places = 20;
40
- }
41
- const showFullNumber = abbreviate == false || bytes < 1000;
42
- if (showFullNumber) {
43
- const formatter = new Intl.NumberFormat('en-US', {
33
+ export function formatBytes(bytes, options) {
34
+ const { abbreviate , decimal_places } = options;
35
+ if (!shouldAbbreviate(abbreviate) || Math.abs(bytes) < 1000) {
36
+ const formatterOptions = {
44
37
  style: 'unit',
45
38
  unit: 'byte',
46
39
  unitDisplay: 'long',
47
- maximumFractionDigits: decimal_places,
48
40
  useGrouping: true
49
- });
41
+ };
42
+ if (hasDecimalPlaces(decimal_places)) {
43
+ formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimal_places);
44
+ } else {
45
+ // This can happen if bytes is between -1000 and 1000
46
+ if (shouldAbbreviate(abbreviate)) {
47
+ formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
48
+ }
49
+ }
50
+ const formatter = Intl.NumberFormat('en-US', formatterOptions);
50
51
  return formatter.format(bytes);
51
52
  }
53
+ // numbro is able to add units like KB, MB, GB, etc. correctly
52
54
  return numbro(bytes).format({
53
55
  output: 'byte',
54
56
  base: 'decimal',
55
57
  spaceSeparated: true,
56
- mantissa: decimal_places,
58
+ mantissa: decimal_places !== null && decimal_places !== void 0 ? decimal_places : DEFAULT_NUMBRO_MANTISSA,
57
59
  trimMantissa: true,
58
60
  optionalMantissa: true
59
61
  });
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/model/units/bytes.ts"],"sourcesContent":["// Copyright 2023 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 { DEFAULT_DECIMAL_PLACES } from './constants';\nimport { UnitGroupConfig, UnitConfig } from './types';\n\nconst bytesUnitKinds = ['Bytes'] as const;\ntype BytesUnitKind = (typeof bytesUnitKinds)[number];\nexport type BytesUnitOptions = {\n kind: BytesUnitKind;\n decimal_places?: number;\n abbreviate?: boolean;\n};\nexport const BYTES_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Bytes',\n decimal_places: true,\n abbreviate: true,\n};\nexport const BYTES_UNIT_CONFIG: Readonly<Record<BytesUnitKind, UnitConfig>> = {\n // This uses units that are powers of 1000.\n // In other words, 1KB = 1000 bytes.\n Bytes: {\n group: 'Bytes',\n label: 'Bytes',\n },\n};\n\nexport function formatBytes(bytes: number, { abbreviate, decimal_places }: BytesUnitOptions) {\n if (bytes === 0) return '0 bytes';\n\n decimal_places = decimal_places ?? DEFAULT_DECIMAL_PLACES;\n // Avoids maximumFractionDigits value is out of range error. Possible values are 0 to 20.\n // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#minimumfractiondigits\n if (decimal_places < 0) {\n decimal_places = 0;\n } else if (decimal_places > 20) {\n decimal_places = 20;\n }\n\n const showFullNumber = abbreviate == false || bytes < 1000;\n\n if (showFullNumber) {\n const formatter = new Intl.NumberFormat('en-US', {\n style: 'unit',\n unit: 'byte',\n unitDisplay: 'long',\n maximumFractionDigits: decimal_places,\n useGrouping: true,\n });\n return formatter.format(bytes);\n }\n\n return numbro(bytes).format({\n output: 'byte',\n base: 'decimal',\n spaceSeparated: true,\n mantissa: decimal_places,\n trimMantissa: true,\n optionalMantissa: true,\n });\n}\n"],"names":["numbro","DEFAULT_DECIMAL_PLACES","bytesUnitKinds","BYTES_GROUP_CONFIG","label","decimal_places","abbreviate","BYTES_UNIT_CONFIG","Bytes","group","formatBytes","bytes","showFullNumber","formatter","Intl","NumberFormat","style","unit","unitDisplay","maximumFractionDigits","useGrouping","format","output","base","spaceSeparated","mantissa","trimMantissa","optionalMantissa"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,OAAOA,MAAM,MAAM,QAAQ,CAAC;AAE5B,SAASC,sBAAsB,QAAQ,aA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AS;QACfC,cAAc,EAAE,IAAI;QACpBC,QAAQ,EAAEpB,cAAc;QACxBqB,YAAY,EAAE,IAAI;QAClBC,gBAAgB,EAAE,IAAI;KACvB,CAAC,CAAC;AACL,CAAC"}
1
+ {"version":3,"sources":["../../../src/model/units/bytes.ts"],"sourcesContent":["// Copyright 2023 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, shouldAbbreviate } from './utils';\n\nconst DEFAULT_NUMBRO_MANTISSA = 2;\n\nconst bytesUnitKinds = ['Bytes'] as const;\ntype BytesUnitKind = (typeof bytesUnitKinds)[number];\nexport type BytesUnitOptions = {\n kind: BytesUnitKind;\n decimal_places?: number;\n abbreviate?: boolean;\n};\nexport const BYTES_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Bytes',\n decimal_places: true,\n abbreviate: true,\n};\nexport const BYTES_UNIT_CONFIG: Readonly<Record<BytesUnitKind, UnitConfig>> = {\n // This uses units that are powers of 1000.\n // In other words, 1KB = 1000 bytes.\n Bytes: {\n group: 'Bytes',\n label: 'Bytes',\n },\n};\n\nexport function formatBytes(bytes: number, options: BytesUnitOptions) {\n const { abbreviate, decimal_places } = options;\n\n if (!shouldAbbreviate(abbreviate) || Math.abs(bytes) < 1000) {\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'unit',\n unit: 'byte',\n unitDisplay: 'long',\n useGrouping: true,\n };\n\n if (hasDecimalPlaces(decimal_places)) {\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimal_places);\n } else {\n // This can happen if bytes is between -1000 and 1000\n if (shouldAbbreviate(abbreviate)) {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n }\n\n const formatter = Intl.NumberFormat('en-US', formatterOptions);\n return formatter.format(bytes);\n }\n\n // numbro is able to add units like KB, MB, GB, etc. correctly\n return numbro(bytes).format({\n output: 'byte',\n base: 'decimal',\n spaceSeparated: true,\n mantissa: decimal_places ?? DEFAULT_NUMBRO_MANTISSA,\n trimMantissa: true,\n optionalMantissa: true,\n });\n}\n"],"names":["numbro","MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","shouldAbbreviate","DEFAULT_NUMBRO_MANTISSA","bytesUnitKinds","BYTES_GROUP_CONFIG","label","decimal_places","abbreviate","BYTES_UNIT_CONFIG","Bytes","group","formatBytes","bytes","options","Math","abs","formatterOptions","style","unit","unitDisplay","useGrouping","maximumFractionDigits","maximumSignificantDigits","formatter","Intl","NumberFormat","format","output","base","spaceSeparated","mantissa","trimMantissa","optionalMantissa"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,OAAOA,MAAM,MAAM,QAAQ,CAAC;AAE5B,SAASC,sBAAsB,QAAQ,aAAa,CAAC;AAErD,SAASC,gBAAgB,EAAEC,kBAAkB,EAAEC,gBAAgB,QAAQ,SAAS,CAAC;AAEjF,MAAMC,uBAAuB,GAAG,CAAC,AAAC;AAElC,MAAMC,cAAc,GAAG;IAAC,OAAO;CAAC,AAAS,AAAC;AAO1C,OAAO,MAAMC,kBAAkB,GAAoB;IACjDC,KAAK,EAAE,OAAO;IACdC,cAAc,EAAE,IAAI;IACpBC,UAAU,EAAE,IAAI;CACjB,CAAC;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C;AACL,CAAC"}
@@ -1,2 +1,2 @@
1
- export declare const DEFAULT_DECIMAL_PLACES = 2;
1
+ export declare const MAX_SIGNIFICANT_DIGITS = 3;
2
2
  //# sourceMappingURL=constants.d.ts.map
@@ -12,6 +12,6 @@
12
12
  // limitations under the License.
13
13
  // Common constants needed across individual unit groups and the overall
14
14
  // combined units.
15
- export const DEFAULT_DECIMAL_PLACES = 2;
15
+ export const MAX_SIGNIFICANT_DIGITS = 3;
16
16
 
17
17
  //# sourceMappingURL=constants.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/model/units/constants.ts"],"sourcesContent":["// Copyright 2023 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\n// Common constants needed across individual unit groups and the overall\n// combined units.\nexport const DEFAULT_DECIMAL_PLACES = 2;\n"],"names":["DEFAULT_DECIMAL_PLACES"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,wEAAwE;AACxE,kBAAkB;AAClB,OAAO,MAAMA,sBAAsB,GAAG,CAAC,CAAC"}
1
+ {"version":3,"sources":["../../../src/model/units/constants.ts"],"sourcesContent":["// Copyright 2023 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\n// Common constants needed across individual unit groups and the overall\n// combined units.\nexport const MAX_SIGNIFICANT_DIGITS = 3;\n"],"names":["MAX_SIGNIFICANT_DIGITS"],"mappings":"AAAA,oCAAoC;AACpC,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,6CAA6C;AAC7C,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;AAEjC,wEAAwE;AACxE,kBAAkB;AAClB,OAAO,MAAMA,sBAAsB,GAAG,CAAC,CAAC"}
@@ -6,8 +6,8 @@ export declare type DecimalUnitOptions = {
6
6
  decimal_places?: number;
7
7
  abbreviate?: boolean;
8
8
  };
9
- export declare const PERCENT_GROUP_CONFIG: UnitGroupConfig;
9
+ export declare const DECIMAL_GROUP_CONFIG: UnitGroupConfig;
10
10
  export declare const DECIMAL_UNIT_CONFIG: Readonly<Record<DecimalUnitKind, UnitConfig>>;
11
- export declare function formatDecimal(value: number, { abbreviate, decimal_places }: DecimalUnitOptions): string;
11
+ export declare function formatDecimal(value: number, options: DecimalUnitOptions): string;
12
12
  export {};
13
13
  //# sourceMappingURL=decimal.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"decimal.d.ts","sourceRoot":"","sources":["../../../src/model/units/decimal.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEtD,QAAA,MAAM,gBAAgB,sBAAuB,CAAC;AAC9C,aAAK,eAAe,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC;AACzD,oBAAY,kBAAkB,GAAG;IAC/B,IAAI,EAAE,eAAe,CAAC;IACtB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AACF,eAAO,MAAM,oBAAoB,EAAE,eAGlC,CAAC;AACF,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,UAAU,CAAC,CAK7E,CAAC;AAEF,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,UAAU,EAAE,cAAc,EAAE,EAAE,kBAAkB,GAAG,MAAM,CAoBvG"}
1
+ {"version":3,"file":"decimal.d.ts","sourceRoot":"","sources":["../../../src/model/units/decimal.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGtD,QAAA,MAAM,gBAAgB,sBAAuB,CAAC;AAC9C,aAAK,eAAe,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC;AACzD,oBAAY,kBAAkB,GAAG;IAC/B,IAAI,EAAE,eAAe,CAAC;IACtB,cAAc,CAAC,EAAE,MAAM,CAAC;IACxB,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AACF,eAAO,MAAM,oBAAoB,EAAE,eAIlC,CAAC;AACF,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,UAAU,CAAC,CAK7E,CAAC;AAEF,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,kBAAkB,GAAG,MAAM,CAsBhF"}
@@ -10,13 +10,15 @@
10
10
  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
11
  // See the License for the specific language governing permissions and
12
12
  // limitations under the License.
13
- import { DEFAULT_DECIMAL_PLACES } from './constants';
13
+ import { MAX_SIGNIFICANT_DIGITS } from './constants';
14
+ import { hasDecimalPlaces, limitDecimalPlaces, shouldAbbreviate } from './utils';
14
15
  const decimalUnitKinds = [
15
16
  'Decimal'
16
17
  ];
17
- export const PERCENT_GROUP_CONFIG = {
18
- label: 'Percent',
19
- decimal_places: true
18
+ export const DECIMAL_GROUP_CONFIG = {
19
+ label: 'Decimal',
20
+ decimal_places: true,
21
+ abbreviate: true
20
22
  };
21
23
  export const DECIMAL_UNIT_CONFIG = {
22
24
  Decimal: {
@@ -24,22 +26,23 @@ export const DECIMAL_UNIT_CONFIG = {
24
26
  label: 'Decimal'
25
27
  }
26
28
  };
27
- export function formatDecimal(value, { abbreviate , decimal_places }) {
28
- decimal_places = decimal_places !== null && decimal_places !== void 0 ? decimal_places : DEFAULT_DECIMAL_PLACES;
29
- // Avoids maximumFractionDigits value is out of range error. Possible values are 0 to 20.
30
- // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#minimumfractiondigits
31
- if (decimal_places < 0) {
32
- decimal_places = 0;
33
- } else if (decimal_places > 20) {
34
- decimal_places = 20;
35
- }
36
- const showFullNumber = abbreviate == false || value < 1000;
37
- const formatter = new Intl.NumberFormat('en-US', {
29
+ export function formatDecimal(value, options) {
30
+ const { abbreviate , decimal_places } = options;
31
+ const formatterOptions = {
38
32
  style: 'decimal',
39
- notation: showFullNumber ? 'standard' : 'compact',
40
- maximumFractionDigits: decimal_places,
41
33
  useGrouping: true
42
- });
34
+ };
35
+ if (shouldAbbreviate(abbreviate)) {
36
+ formatterOptions.notation = 'compact';
37
+ }
38
+ if (hasDecimalPlaces(decimal_places)) {
39
+ formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimal_places);
40
+ } else {
41
+ if (shouldAbbreviate(abbreviate)) {
42
+ formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
43
+ }
44
+ }
45
+ const formatter = Intl.NumberFormat('en-US', formatterOptions);
43
46
  return formatter.format(value);
44
47
  }
45
48
 
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/model/units/decimal.ts"],"sourcesContent":["// Copyright 2023 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 { DEFAULT_DECIMAL_PLACES } from './constants';\nimport { UnitGroupConfig, UnitConfig } from './types';\n\nconst decimalUnitKinds = ['Decimal'] as const;\ntype DecimalUnitKind = (typeof decimalUnitKinds)[number];\nexport type DecimalUnitOptions = {\n kind: DecimalUnitKind;\n decimal_places?: number;\n abbreviate?: boolean;\n};\nexport const PERCENT_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Percent',\n decimal_places: true,\n};\nexport const DECIMAL_UNIT_CONFIG: Readonly<Record<DecimalUnitKind, UnitConfig>> = {\n Decimal: {\n group: 'Decimal',\n label: 'Decimal',\n },\n};\n\nexport function formatDecimal(value: number, { abbreviate, decimal_places }: DecimalUnitOptions): string {\n decimal_places = decimal_places ?? DEFAULT_DECIMAL_PLACES;\n\n // Avoids maximumFractionDigits value is out of range error. Possible values are 0 to 20.\n // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat#minimumfractiondigits\n if (decimal_places < 0) {\n decimal_places = 0;\n } else if (decimal_places > 20) {\n decimal_places = 20;\n }\n\n const showFullNumber = abbreviate == false || value < 1000;\n\n const formatter = new Intl.NumberFormat('en-US', {\n style: 'decimal',\n notation: showFullNumber ? 'standard' : 'compact',\n maximumFractionDigits: decimal_places,\n useGrouping: true,\n });\n return formatter.format(value);\n}\n"],"names":["DEFAULT_DECIMAL_PLACES","decimalUnitKinds","PERCENT_GROUP_CONFIG","label","decimal_places","DECIMAL_UNIT_CONFIG","Decimal","group","formatDecimal","value","abbreviate","showFullNumber","formatter","Intl","NumberFormat","style","notation","maximumFractionDigits","useGrouping","format"],"mappings":"AAAA,oCAAoC;AACpC,kEAAk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right 2023 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 { UnitGroupConfig, UnitConfig } from './types';\nimport { hasDecimalPlaces, limitDecimalPlaces, shouldAbbreviate } from './utils';\n\nconst decimalUnitKinds = ['Decimal'] as const;\ntype DecimalUnitKind = (typeof decimalUnitKinds)[number];\nexport type DecimalUnitOptions = {\n kind: DecimalUnitKind;\n decimal_places?: number;\n abbreviate?: boolean;\n};\nexport const DECIMAL_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Decimal',\n decimal_places: true,\n abbreviate: true,\n};\nexport const DECIMAL_UNIT_CONFIG: Readonly<Record<DecimalUnitKind, UnitConfig>> = {\n Decimal: {\n group: 'Decimal',\n label: 'Decimal',\n },\n};\n\nexport function formatDecimal(value: number, options: DecimalUnitOptions): string {\n const { abbreviate, decimal_places } = options;\n\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'decimal',\n useGrouping: true,\n };\n\n if (shouldAbbreviate(abbreviate)) {\n formatterOptions.notation = 'compact';\n }\n\n if (hasDecimalPlaces(decimal_places)) {\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimal_places);\n } else {\n if (shouldAbbreviate(abbreviate)) {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n }\n\n const formatter = Intl.NumberFormat('en-US', formatterOptions);\n return formatter.format(value);\n}\n"],"names":["MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","shouldAbbreviate","decimalUnitKinds","DECIMAL_GROUP_CONFIG","label","decimal_places","abbreviate","DECIMAL_UNIT_CONFIG","Decimal","group","formatDecimal","value","options","formatterOptions","style","useGrouping","notation","maximumFractionDigits","maximumSignificantDigits","formatter","Intl","NumberFormat","format"],"mappings":"AAAA,oCAAoC;AACpC,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,aAAa,CAAC;AAErD,SAASC,gBAAgB,EAAEC,kBAAkB,EAAEC,gBAAgB,QAAQ,SAAS,CAAC;AAEjF,MAAMC,gBAAgB,GAAG;IAAC,SAAS;CAAC,AAAS,AAAC;AAO9C,OAAO,MAAMC,oBAAoB,GAAoB;IACnDC,KAAK,EAAE,SAAS;IAChBC,cAAc,EAAE,IAAI;IACpBC,UAAU,EAAE,IAAI;CACjB,CAAC;AACF,OAAO,MAAMC,mBAAmB,GAAkD;IAChFC,OAAO,EAAE;QACPC,KAAK,EAAE,SAAS;QAChBL,KAAK,EAAE,SAAS;KACjB;CACF,CAAC;AAEF,OAAO,SAASM,aAAa,CAACC,KAAa,EAAEC,OAA2B,EAAU;IAChF,MAAM,EAAEN,UAAU,CAAA,EAAED,cAAc,CAAA,EAAE,GAAGO,OAAO,AAAC;IAE/C,MAAMC,gBAAgB,GAA6B;QACjDC,KAAK,EAAE,SAAS;QAChBC,WAAW,EAAE,IAAI;KAClB,AAAC;IAEF,IAAId,gBAAgB,CAACK,UAAU,CAAC,EAAE;QAChCO,gBAAgB,CAACG,QAAQ,GAAG,SAAS,CAAC;IACxC,CAAC;IAED,IAAIjB,gBAAgB,CAACM,cAAc,CAAC,EAAE;QACpCQ,gBAAgB,CAACI,qBAAqB,GAAGjB,kBAAkB,CAACK,cAAc,CAAC,CAAC;IAC9E,OAAO;QACL,IAAIJ,gBAAgB,CAACK,UAAU,CAAC,EAAE;YAChCO,gBAAgB,CAACK,wBAAwB,GAAGpB,sBAAsB,CAAC;QACrE,CAAC;IACH,CAAC;IAED,MAAMqB,SAAS,GAAGC,IAAI,CAACC,YAAY,CAAC,OAAO,EAAER,gBAAgB,CAAC,AAAC;IAC/D,OAAOM,SAAS,CAACG,MAAM,CAACX,KAAK,CAAC,CAAC;AACjC,CAAC"}
@@ -5,8 +5,8 @@ export declare type PercentUnitOptions = {
5
5
  kind: PercentUnitKind;
6
6
  decimal_places?: number;
7
7
  };
8
- export declare const DECIMAL_GROUP_CONFIG: UnitGroupConfig;
8
+ export declare const PERCENT_GROUP_CONFIG: UnitGroupConfig;
9
9
  export declare const PERCENT_UNIT_CONFIG: Readonly<Record<PercentUnitKind, UnitConfig>>;
10
- export declare function formatPercent(value: number, unitOptions: PercentUnitOptions): string;
10
+ export declare function formatPercent(value: number, { kind, decimal_places }: PercentUnitOptions): string;
11
11
  export {};
12
12
  //# sourceMappingURL=percent.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"percent.d.ts","sourceRoot":"","sources":["../../../src/model/units/percent.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGtD,QAAA,MAAM,gBAAgB,6CAA8C,CAAC;AACrE,aAAK,eAAe,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC;AACzD,oBAAY,kBAAkB,GAAG;IAC/B,IAAI,EAAE,eAAe,CAAC;IACtB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AACF,eAAO,MAAM,oBAAoB,EAAE,eAIlC,CAAC;AAEF,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,UAAU,CAAC,CAgB7E,CAAC;AAEF,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,kBAAkB,GAAG,MAAM,CAQpF"}
1
+ {"version":3,"file":"percent.d.ts","sourceRoot":"","sources":["../../../src/model/units/percent.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGtD,QAAA,MAAM,gBAAgB,6CAA8C,CAAC;AACrE,aAAK,eAAe,GAAG,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,CAAC,CAAC;AACzD,oBAAY,kBAAkB,GAAG;IAC/B,IAAI,EAAE,eAAe,CAAC;IACtB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AACF,eAAO,MAAM,oBAAoB,EAAE,eAGlC,CAAC;AAEF,eAAO,MAAM,mBAAmB,EAAE,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,UAAU,CAAC,CAgB7E,CAAC;AAEF,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE,EAAE,kBAAkB,GAAG,MAAM,CAmBjG"}