@perses-dev/components 0.29.0 → 0.30.0

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 +1 -7
  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 +4 -0
  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 +5 -1
  12. package/dist/StatChart/StatChart.js.map +1 -1
  13. package/dist/TimeSeriesTooltip/TimeSeriesTooltip.d.ts +1 -1
  14. package/dist/TimeSeriesTooltip/TimeSeriesTooltip.d.ts.map +1 -1
  15. package/dist/TimeSeriesTooltip/TimeSeriesTooltip.js +6 -5
  16. package/dist/TimeSeriesTooltip/TimeSeriesTooltip.js.map +1 -1
  17. package/dist/TimeSeriesTooltip/TooltipContent.js +2 -2
  18. package/dist/TimeSeriesTooltip/TooltipContent.js.map +1 -1
  19. package/dist/TimeSeriesTooltip/focused-series.d.ts +1 -1
  20. package/dist/TimeSeriesTooltip/focused-series.d.ts.map +1 -1
  21. package/dist/TimeSeriesTooltip/focused-series.js +2 -3
  22. package/dist/TimeSeriesTooltip/focused-series.js.map +1 -1
  23. package/dist/TimeSeriesTooltip/tooltip-model.d.ts +4 -0
  24. package/dist/TimeSeriesTooltip/tooltip-model.d.ts.map +1 -1
  25. package/dist/TimeSeriesTooltip/tooltip-model.js +4 -0
  26. package/dist/TimeSeriesTooltip/tooltip-model.js.map +1 -1
  27. package/dist/TimeSeriesTooltip/utils.d.ts +1 -1
  28. package/dist/TimeSeriesTooltip/utils.d.ts.map +1 -1
  29. package/dist/TimeSeriesTooltip/utils.js +5 -14
  30. package/dist/TimeSeriesTooltip/utils.js.map +1 -1
  31. package/dist/UnitSelector/UnitSelector.d.ts.map +1 -1
  32. package/dist/UnitSelector/UnitSelector.js +40 -17
  33. package/dist/UnitSelector/UnitSelector.js.map +1 -1
  34. package/dist/cjs/DateTimeRangePicker/TimeRangeSelector.js +7 -1
  35. package/dist/cjs/EChart/EChart.js +1 -7
  36. package/dist/cjs/LineChart/LineChart.js +4 -0
  37. package/dist/cjs/StatChart/StatChart.js +5 -1
  38. package/dist/cjs/TimeSeriesTooltip/TimeSeriesTooltip.js +11 -5
  39. package/dist/cjs/TimeSeriesTooltip/TooltipContent.js +2 -2
  40. package/dist/cjs/TimeSeriesTooltip/focused-series.js +1 -2
  41. package/dist/cjs/TimeSeriesTooltip/tooltip-model.js +4 -0
  42. package/dist/cjs/TimeSeriesTooltip/utils.js +5 -14
  43. package/dist/cjs/UnitSelector/UnitSelector.js +39 -16
  44. package/dist/cjs/model/units/bytes.js +25 -21
  45. package/dist/cjs/model/units/constants.js +3 -3
  46. package/dist/cjs/model/units/decimal.js +22 -19
  47. package/dist/cjs/model/units/percent.js +15 -22
  48. package/dist/cjs/model/units/time.js +24 -20
  49. package/dist/cjs/model/units/units.js +2 -2
  50. package/dist/cjs/model/units/utils.js +42 -0
  51. package/dist/model/units/bytes.d.ts +1 -1
  52. package/dist/model/units/bytes.d.ts.map +1 -1
  53. package/dist/model/units/bytes.js +26 -22
  54. package/dist/model/units/bytes.js.map +1 -1
  55. package/dist/model/units/constants.d.ts +1 -1
  56. package/dist/model/units/constants.js +1 -1
  57. package/dist/model/units/constants.js.map +1 -1
  58. package/dist/model/units/decimal.d.ts +2 -2
  59. package/dist/model/units/decimal.d.ts.map +1 -1
  60. package/dist/model/units/decimal.js +22 -19
  61. package/dist/model/units/decimal.js.map +1 -1
  62. package/dist/model/units/percent.d.ts +1 -1
  63. package/dist/model/units/percent.d.ts.map +1 -1
  64. package/dist/model/units/percent.js +14 -21
  65. package/dist/model/units/percent.js.map +1 -1
  66. package/dist/model/units/time.d.ts +1 -1
  67. package/dist/model/units/time.d.ts.map +1 -1
  68. package/dist/model/units/time.js +24 -20
  69. package/dist/model/units/time.js.map +1 -1
  70. package/dist/model/units/units.js +2 -2
  71. package/dist/model/units/units.js.map +1 -1
  72. package/dist/model/units/utils.d.ts +4 -0
  73. package/dist/model/units/utils.d.ts.map +1 -0
  74. package/dist/model/units/utils.js +32 -0
  75. package/dist/model/units/utils.js.map +1 -0
  76. package/package.json +3 -3
@@ -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
  ];
@@ -41,40 +43,42 @@ const BYTES_GROUP_CONFIG = {
41
43
  abbreviate: true
42
44
  };
43
45
  const BYTES_UNIT_CONFIG = {
44
- // This uses units that are powers of 1000.
45
- // In other words, 1KB = 1000 bytes.
46
+ // These units are powers of 1000.
47
+ // In other words, 1 KB = 1000 bytes.
46
48
  Bytes: {
47
49
  group: 'Bytes',
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
- });
70
- return formatter.format(bytes);
61
+ };
62
+ if ((0, _utils.hasDecimalPlaces)(decimal_places)) {
63
+ formatterOptions.minimumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
64
+ formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
65
+ } else {
66
+ // This can happen if bytes is between -1000 and 1000
67
+ if ((0, _utils.shouldAbbreviate)(abbreviate)) {
68
+ formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
69
+ }
70
+ }
71
+ return Intl.NumberFormat('en-US', formatterOptions).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,
77
- trimMantissa: true,
78
- optionalMantissa: true
78
+ mantissa: (0, _utils.hasDecimalPlaces)(decimal_places) ? decimal_places : DEFAULT_NUMBRO_MANTISSA,
79
+ // trimMantissa trims trailing 0s
80
+ trimMantissa: !(0, _utils.hasDecimalPlaces)(decimal_places),
81
+ // optionalMantissa excludes all the decimal places if they're all zeros
82
+ optionalMantissa: !(0, _utils.hasDecimalPlaces)(decimal_places)
79
83
  });
80
84
  }
@@ -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
- });
58
- return formatter.format(value);
49
+ };
50
+ if ((0, _utils.shouldAbbreviate)(abbreviate)) {
51
+ formatterOptions.notation = 'compact';
52
+ }
53
+ if ((0, _utils.hasDecimalPlaces)(decimal_places)) {
54
+ formatterOptions.minimumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
55
+ formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
56
+ } else {
57
+ if ((0, _utils.shouldAbbreviate)(abbreviate)) {
58
+ formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
59
+ }
60
+ }
61
+ return Intl.NumberFormat('en-US', formatterOptions).format(value);
59
62
  }
@@ -21,19 +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
+ const _constants = require("./constants");
29
+ const _utils = require("./utils");
28
30
  const percentUnitKinds = [
29
31
  'Percent',
30
32
  'PercentDecimal',
31
33
  '%'
32
34
  ];
33
- const DECIMAL_GROUP_CONFIG = {
34
- label: 'Decimal',
35
- decimal_places: true,
36
- abbreviate: true
35
+ const PERCENT_GROUP_CONFIG = {
36
+ label: 'Percent',
37
+ decimal_places: true
37
38
  };
38
39
  const PERCENT_GROUP = 'Percent';
39
40
  const PERCENT_UNIT_CONFIG = {
@@ -53,28 +54,20 @@ const PERCENT_UNIT_CONFIG = {
53
54
  label: '%'
54
55
  }
55
56
  };
56
- const MAX_SIGNIFICANT_DIGITS = 3;
57
57
  function formatPercent(value, { kind , decimal_places }) {
58
58
  // Intl.NumberFormat translates 0 -> 0%, 0.5 -> 50%, 1 -> 100%
59
59
  if (kind === 'Percent') {
60
60
  value = value / 100;
61
61
  }
62
- const hasDecimalPlaces = decimal_places !== undefined;
63
- let formatter;
64
- if (hasDecimalPlaces) {
65
- // If there is a specified # of decimal places, use maximumFractionDigits
66
- formatter = new Intl.NumberFormat('en-US', {
67
- style: 'percent',
68
- maximumFractionDigits: decimal_places,
69
- useGrouping: true
70
- });
62
+ const formatterOptions = {
63
+ style: 'percent',
64
+ useGrouping: true
65
+ };
66
+ if ((0, _utils.hasDecimalPlaces)(decimal_places)) {
67
+ formatterOptions.minimumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
68
+ formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
71
69
  } else {
72
- // By default, use maximumSignificantDigits
73
- formatter = new Intl.NumberFormat('en-US', {
74
- style: 'percent',
75
- maximumSignificantDigits: MAX_SIGNIFICANT_DIGITS,
76
- useGrouping: true
77
- });
70
+ formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
78
71
  }
79
- return formatter.format(value);
72
+ return Intl.NumberFormat('en-US', formatterOptions).format(value);
80
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 = {}));
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 = {}));
90
91
  function formatTime(value, { kind , decimal_places }) {
91
- const decimalPlaces = decimal_places !== null && decimal_places !== void 0 ? decimal_places : _constants.DEFAULT_DECIMAL_PLACES;
92
- const timeUnit = TimeIntlDuration[kind];
93
92
  const isMonthOrYear = kind === 'Months' || kind === 'Years';
94
- const formatter = new Intl.NumberFormat('en-US', {
93
+ const formatterOptions = {
95
94
  style: 'unit',
96
- unit: timeUnit,
97
- unitDisplay: isMonthOrYear ? 'long' : 'narrow',
98
- maximumFractionDigits: decimalPlaces
99
- });
100
- return formatter.format(value);
95
+ unit: PersesTimeToIntlTime[kind],
96
+ unitDisplay: isMonthOrYear ? 'long' : 'narrow'
97
+ };
98
+ if ((0, _utils.hasDecimalPlaces)(decimal_places)) {
99
+ formatterOptions.minimumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
100
+ formatterOptions.maximumFractionDigits = (0, _utils.limitDecimalPlaces)(decimal_places);
101
+ } else {
102
+ formatterOptions.maximumSignificantDigits = _constants.MAX_SIGNIFICANT_DIGITS;
103
+ }
104
+ return Intl.NumberFormat('en-US', formatterOptions).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
+ }
@@ -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,UAmCnE"}
@@ -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
  ];
@@ -21,41 +23,43 @@ export const BYTES_GROUP_CONFIG = {
21
23
  abbreviate: true
22
24
  };
23
25
  export const BYTES_UNIT_CONFIG = {
24
- // This uses units that are powers of 1000.
25
- // In other words, 1KB = 1000 bytes.
26
+ // These units are powers of 1000.
27
+ // In other words, 1 KB = 1000 bytes.
26
28
  Bytes: {
27
29
  group: 'Bytes',
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
- });
50
- return formatter.format(bytes);
41
+ };
42
+ if (hasDecimalPlaces(decimal_places)) {
43
+ formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimal_places);
44
+ formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimal_places);
45
+ } else {
46
+ // This can happen if bytes is between -1000 and 1000
47
+ if (shouldAbbreviate(abbreviate)) {
48
+ formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
49
+ }
50
+ }
51
+ return Intl.NumberFormat('en-US', formatterOptions).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,
57
- trimMantissa: true,
58
- optionalMantissa: true
58
+ mantissa: hasDecimalPlaces(decimal_places) ? decimal_places : DEFAULT_NUMBRO_MANTISSA,
59
+ // trimMantissa trims trailing 0s
60
+ trimMantissa: !hasDecimalPlaces(decimal_places),
61
+ // optionalMantissa excludes all the decimal places if they're all zeros
62
+ optionalMantissa: !hasDecimalPlaces(decimal_places)
59
63
  });
60
64
  }
61
65
 
@@ -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,aAAa,CAAC;AAGrD,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;AACF,OAAO,MAAMC,iBAAiB,GAAgD;IAC5E,2CAA2C;IAC3C,oCAAoC;IACpCC,KAAK,EAAE;QACLC,KAAK,EAAE,OAAO;QACdL,KAAK,EAAE,OAAO;KACf;CACF,CAAC;AAEF,OAAO,SAASM,WAAW,CAACC,KAAa,EAAE,EAAEL,UAAU,CAAA,EAAED,cAAc,CAAA,EAAoB,EAAE;IAC3F,IAAIM,KAAK,KAAK,CAAC,EAAE,OAAO,SAAS,CAAC;IAElCN,cAAc,GAAGA,cAAc,aAAdA,cAAc,cAAdA,cAAc,GAAIJ,sBAAsB,CAAC;IAC1D,yFAAyF;IACzF,wIAAwI;IACxI,IAAII,cAAc,GAAG,CAAC,EAAE;QACtBA,cAAc,GAAG,CAAC,CAAC;IACrB,OAAO,IAAIA,cAAc,GAAG,EAAE,EAAE;QAC9BA,cAAc,GAAG,EAAE,CAAC;IACtB,CAAC;IAED,MAAMO,cAAc,GAAGN,UAAU,IAAI,KAAK,IAAIK,KAAK,GAAG,IAAI,AAAC;IAE3D,IAAIC,cAAc,EAAE;QAClB,MAAMC,SAAS,GAAG,IAAIC,IAAI,CAACC,YAAY,CAAC,OAAO,EAAE;YAC/CC,KAAK,EAAE,MAAM;YACbC,IAAI,EAAE,MAAM;YACZC,WAAW,EAAE,MAAM;YACnBC,qBAAqB,EAAEd,cAAc;YACrCe,WAAW,EAAE,IAAI;SAClB,CAAC,AAAC;QACH,OAAOP,SAAS,CAACQ,MAAM,CAACV,KAAK,CAAC,CAAC;IACjC,CAAC;IAED,OAAOX,MAAM,CAACW,KAAK,CAAC,CAACU,MAAM,CAAC;QAC1BC,MAAM,EAAE,MAAM;QACdC,IAAI,EAAE,SAAS;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 // These units are powers of 1000.\n // In other words, 1 KB = 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.minimumFractionDigits = limitDecimalPlaces(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 return Intl.NumberFormat('en-US', formatterOptions).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: hasDecimalPlaces(decimal_places) ? decimal_places : DEFAULT_NUMBRO_MANTISSA,\n // trimMantissa trims trailing 0s\n trimMantissa: !hasDecimalPlaces(decimal_places),\n // optionalMantissa excludes all the decimal places if they're all zeros\n optionalMantissa: !hasDecimalPlaces(decimal_places),\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","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","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;AACF,OAAO,MAAMC,iBAAiB,GAAgD;IAC5E,kCAAkC;IAClC,qCAAqC;IACrCC,KAAK,EAAE;QACLC,KAAK,EAAE,OAAO;QACdL,KAAK,EAAE,OAAO;KACf;CACF,CAAC;AAEF,OAAO,SAASM,WAAW,CAACC,KAAa,EAAEC,OAAyB,EAAE;IACpE,MAAM,EAAEN,UAAU,CAAA,EAAED,cAAc,CAAA,EAAE,GAAGO,OAAO,AAAC;IAE/C,IAAI,CAACZ,gBAAgB,CAACM,UAAU,CAAC,IAAIO,IAAI,CAACC,GAAG,CAACH,KAAK,CAAC,GAAG,IAAI,EAAE;QAC3D,MAAMI,gBAAgB,GAA6B;YACjDC,KAAK,EAAE,MAAM;YACbC,IAAI,EAAE,MAAM;YACZC,WAAW,EAAE,MAAM;YACnBC,WAAW,EAAE,IAAI;SAClB,AAAC;QAEF,IAAIrB,gBAAgB,CAACO,cAAc,CAAC,EAAE;YACpCU,gBAAgB,CAACK,qBAAqB,GAAGrB,kBAAkB,CAACM,cAAc,CAAC,CAAC;YAC5EU,gBAAgB,CAACM,qBAAqB,GAAGtB,kBAAkB,CAACM,cAAc,CAAC,CAAC;QAC9E,OAAO;YACL,qDAAqD;YACrD,IAAIL,gBAAgB,CAACM,UAAU,CAAC,EAAE;gBAChCS,gBAAgB,CAACO,wBAAwB,GAAGzB,sBAAsB,CAAC;YACrE,CAAC;QACH,CAAC;QAED,OAAO0B,IAAI,CAACC,YAAY,CAAC,OAAO,EAAET,gBAAgB,CAAC,CAACU,MAAM,CAACd,KAAK,CAAC,CAAC;IACpE,CAAC;IAED,8DAA8D;IAC9D,OAAOf,MAAM,CAACe,KAAK,CAAC,CAACc,MAAM,CAAC;QAC1BC,MAAM,EAAE,MAAM;QACdC,IAAI,EAAE,SAAS;QACfC,cAAc,EAAE,IAAI;QACpBC,QAAQ,EAAE/B,gBAAgB,CAACO,cAAc,CAAC,GAAGA,cAAc,GAAGJ,uBAAuB;QACrF,iCAAiC;QACjC6B,YAAY,EAAE,CAAChC,gBAAgB,CAACO,cAAc,CAAC;QAC/C,wEAAwE;QACxE0B,gBAAgB,EAAE,CAACjC,gBAAgB,CAACO,cAAc,CAAC;KACpD,CAAC,CAAC;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,23 +26,24 @@ 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
- });
43
- return formatter.format(value);
34
+ };
35
+ if (shouldAbbreviate(abbreviate)) {
36
+ formatterOptions.notation = 'compact';
37
+ }
38
+ if (hasDecimalPlaces(decimal_places)) {
39
+ formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimal_places);
40
+ formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimal_places);
41
+ } else {
42
+ if (shouldAbbreviate(abbreviate)) {
43
+ formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
44
+ }
45
+ }
46
+ return Intl.NumberFormat('en-US', formatterOptions).format(value);
44
47
  }
45
48
 
46
49
  //# sourceMappingURL=decimal.js.map
@@ -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,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;AAGrD,MAAMC,gBAAgB,GAAG;IAAC,SAAS;CAAC,AAAS,AAAC;AAO9C,OAAO,MAAMC,oBAAoB,GAAoB;IACnDC,KAAK,EAAE,SAAS;IAChBC,cAAc,EAAE,IAAI;CACrB,CAAC;AACF,OAAO,MAAMC,mBAAmB,GAAkD;IAChFC,OAAO,EAAE;QACPC,KAAK,EAAE,SAAS;QAChBJ,KAAK,EAAE,SAAS;KACjB;CACF,CAAC;AAEF,OAAO,SAASK,aAAa,CAACC,KAAa,EAAE,EAAEC,UAAU,CAAA,EAAEN,cAAc,CAAA,EAAsB,EAAU;IACvGA,cAAc,GAAGA,cAAc,aAAdA,cAAc,cAAdA,cAAc,GAAIJ,sBAAsB,CAAC;IAE1D,yFAAyF;IACzF,wIAAwI;IACxI,IAAII,cAAc,GAAG,CAAC,EAAE;QACtBA,cAAc,GAAG,CAAC,CAAC;IACrB,OAAO,IAAIA,cAAc,GAAG,EAAE,EAAE;QAC9BA,cAAc,GAAG,EAAE,CAAC;IACtB,CAAC;IAED,MAAMO,cAAc,GAAGD,UAAU,IAAI,KAAK,IAAID,KAAK,GAAG,IAAI,AAAC;IAE3D,MAAMG,SAAS,GAAG,IAAIC,IAAI,CAACC,YAAY,CAAC,OAAO,EAAE;QAC/CC,KAAK,EAAE,SAAS;QAChBC,QAAQ,EAAEL,cAAc,GAAG,UAAU,GAAG,SAAS;QACjDM,qBAAqB,EAAEb,cAAc;QACrCc,WAAW,EAAE,IAAI;KAClB,CAAC,AAAC;IACH,OAAON,SAAS,CAACO,MAAM,CAACV,KAAK,CAAC,CAAC;AACjC,CAAC"}
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 { 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.minimumFractionDigits = limitDecimalPlaces(decimal_places);\n formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimal_places);\n } else {\n if (shouldAbbreviate(abbreviate)) {\n formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n }\n\n return Intl.NumberFormat('en-US', formatterOptions).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","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","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;QAC5EQ,gBAAgB,CAACK,qBAAqB,GAAGlB,kBAAkB,CAACK,cAAc,CAAC,CAAC;IAC9E,OAAO;QACL,IAAIJ,gBAAgB,CAACK,UAAU,CAAC,EAAE;YAChCO,gBAAgB,CAACM,wBAAwB,GAAGrB,sBAAsB,CAAC;QACrE,CAAC;IACH,CAAC;IAED,OAAOsB,IAAI,CAACC,YAAY,CAAC,OAAO,EAAER,gBAAgB,CAAC,CAACS,MAAM,CAACX,KAAK,CAAC,CAAC;AACpE,CAAC"}
@@ -5,7 +5,7 @@ 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
10
  export declare function formatPercent(value: number, { kind, decimal_places }: PercentUnitOptions): string;
11
11
  export {};
@@ -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;AAEtD,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;AAIF,wBAAgB,aAAa,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE,EAAE,kBAAkB,GAAG,MAAM,CA0BjG"}
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"}