@perses-dev/components 0.0.0-snapshot-color-palette-gen-test-0ebddd6 → 0.0.0-snapshot-saving-defaults-refinements-1b25cec

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 (238) hide show
  1. package/dist/ContentWithLegend/ContentWithLegend.d.ts +11 -0
  2. package/dist/ContentWithLegend/ContentWithLegend.d.ts.map +1 -0
  3. package/dist/ContentWithLegend/ContentWithLegend.js +65 -0
  4. package/dist/ContentWithLegend/ContentWithLegend.js.map +1 -0
  5. package/dist/ContentWithLegend/index.d.ts +2 -0
  6. package/dist/ContentWithLegend/index.d.ts.map +1 -0
  7. package/dist/ContentWithLegend/index.js +15 -0
  8. package/dist/ContentWithLegend/index.js.map +1 -0
  9. package/dist/ContentWithLegend/model/content-with-legend-model.d.ts +68 -0
  10. package/dist/ContentWithLegend/model/content-with-legend-model.d.ts.map +1 -0
  11. package/dist/ContentWithLegend/model/content-with-legend-model.js +90 -0
  12. package/dist/ContentWithLegend/model/content-with-legend-model.js.map +1 -0
  13. package/dist/DateTimeRangePicker/TimeRangeSelector.d.ts.map +1 -1
  14. package/dist/DateTimeRangePicker/TimeRangeSelector.js +7 -1
  15. package/dist/DateTimeRangePicker/TimeRangeSelector.js.map +1 -1
  16. package/dist/EChart/EChart.d.ts.map +1 -1
  17. package/dist/EChart/EChart.js +9 -1
  18. package/dist/EChart/EChart.js.map +1 -1
  19. package/dist/Legend/CompactLegend.d.ts +6 -4
  20. package/dist/Legend/CompactLegend.d.ts.map +1 -1
  21. package/dist/Legend/CompactLegend.js +4 -1
  22. package/dist/Legend/CompactLegend.js.map +1 -1
  23. package/dist/Legend/Legend.d.ts +11 -9
  24. package/dist/Legend/Legend.d.ts.map +1 -1
  25. package/dist/Legend/Legend.js +70 -21
  26. package/dist/Legend/Legend.js.map +1 -1
  27. package/dist/Legend/ListLegend.d.ts +5 -8
  28. package/dist/Legend/ListLegend.d.ts.map +1 -1
  29. package/dist/Legend/ListLegend.js +17 -103
  30. package/dist/Legend/ListLegend.js.map +1 -1
  31. package/dist/Legend/ListLegendItem.d.ts +8 -8
  32. package/dist/Legend/ListLegendItem.d.ts.map +1 -1
  33. package/dist/Legend/ListLegendItem.js +9 -12
  34. package/dist/Legend/ListLegendItem.js.map +1 -1
  35. package/dist/Legend/TableLegend.d.ts +12 -0
  36. package/dist/Legend/TableLegend.d.ts.map +1 -0
  37. package/dist/Legend/TableLegend.js +61 -0
  38. package/dist/Legend/TableLegend.js.map +1 -0
  39. package/dist/LegendOptionsEditor/LegendOptionsEditor.d.ts.map +1 -1
  40. package/dist/LegendOptionsEditor/LegendOptionsEditor.js +35 -3
  41. package/dist/LegendOptionsEditor/LegendOptionsEditor.js.map +1 -1
  42. package/dist/LineChart/LineChart.d.ts +2 -1
  43. package/dist/LineChart/LineChart.d.ts.map +1 -1
  44. package/dist/LineChart/LineChart.js +83 -36
  45. package/dist/LineChart/LineChart.js.map +1 -1
  46. package/dist/StatChart/StatChart.d.ts.map +1 -1
  47. package/dist/StatChart/StatChart.js +23 -15
  48. package/dist/StatChart/StatChart.js.map +1 -1
  49. package/dist/Table/InnerTable.d.ts +9 -0
  50. package/dist/Table/InnerTable.d.ts.map +1 -0
  51. package/dist/Table/InnerTable.js +38 -0
  52. package/dist/Table/InnerTable.js.map +1 -0
  53. package/dist/Table/Table.d.ts +10 -0
  54. package/dist/Table/Table.d.ts.map +1 -0
  55. package/dist/Table/Table.js +101 -0
  56. package/dist/Table/Table.js.map +1 -0
  57. package/dist/Table/TableBody.d.ts +6 -0
  58. package/dist/Table/TableBody.d.ts.map +1 -0
  59. package/dist/Table/TableBody.js +23 -0
  60. package/dist/Table/TableBody.js.map +1 -0
  61. package/dist/Table/TableCell.d.ts +18 -0
  62. package/dist/Table/TableCell.d.ts.map +1 -0
  63. package/dist/Table/TableCell.js +91 -0
  64. package/dist/Table/TableCell.js.map +1 -0
  65. package/dist/Table/TableCheckbox.d.ts +9 -0
  66. package/dist/Table/TableCheckbox.d.ts.map +1 -0
  67. package/dist/Table/TableCheckbox.js +49 -0
  68. package/dist/Table/TableCheckbox.js.map +1 -0
  69. package/dist/Table/TableHead.d.ts +6 -0
  70. package/dist/Table/TableHead.d.ts.map +1 -0
  71. package/dist/Table/TableHead.js +23 -0
  72. package/dist/Table/TableHead.js.map +1 -0
  73. package/dist/Table/TableRow.d.ts +9 -0
  74. package/dist/Table/TableRow.d.ts.map +1 -0
  75. package/dist/Table/TableRow.js +29 -0
  76. package/dist/Table/TableRow.js.map +1 -0
  77. package/dist/Table/VirtualizedTable.d.ts +11 -0
  78. package/dist/Table/VirtualizedTable.d.ts.map +1 -0
  79. package/dist/Table/VirtualizedTable.js +152 -0
  80. package/dist/Table/VirtualizedTable.js.map +1 -0
  81. package/dist/Table/VirtualizedTableContainer.d.ts +6 -0
  82. package/dist/Table/VirtualizedTableContainer.d.ts.map +1 -0
  83. package/dist/Table/VirtualizedTableContainer.js +24 -0
  84. package/dist/Table/VirtualizedTableContainer.js.map +1 -0
  85. package/dist/Table/hooks/useTableKeyboardNav.d.ts +32 -0
  86. package/dist/Table/hooks/useTableKeyboardNav.d.ts.map +1 -0
  87. package/dist/Table/hooks/useTableKeyboardNav.js +98 -0
  88. package/dist/Table/hooks/useTableKeyboardNav.js.map +1 -0
  89. package/dist/Table/hooks/useVirtualizedTableKeyboardNav.d.ts +29 -0
  90. package/dist/Table/hooks/useVirtualizedTableKeyboardNav.d.ts.map +1 -0
  91. package/dist/Table/hooks/useVirtualizedTableKeyboardNav.js +89 -0
  92. package/dist/Table/hooks/useVirtualizedTableKeyboardNav.js.map +1 -0
  93. package/dist/Table/index.d.ts +4 -0
  94. package/dist/Table/index.d.ts.map +1 -0
  95. package/dist/Table/index.js +16 -0
  96. package/dist/Table/index.js.map +1 -0
  97. package/dist/Table/model/table-model.d.ts +83 -0
  98. package/dist/Table/model/table-model.d.ts.map +1 -0
  99. package/dist/Table/model/table-model.js +73 -0
  100. package/dist/Table/model/table-model.js.map +1 -0
  101. package/dist/TimeSeriesTooltip/SeriesInfo.d.ts +1 -0
  102. package/dist/TimeSeriesTooltip/SeriesInfo.d.ts.map +1 -1
  103. package/dist/TimeSeriesTooltip/SeriesInfo.js +11 -9
  104. package/dist/TimeSeriesTooltip/SeriesInfo.js.map +1 -1
  105. package/dist/TimeSeriesTooltip/TimeSeriesTooltip.d.ts +5 -4
  106. package/dist/TimeSeriesTooltip/TimeSeriesTooltip.d.ts.map +1 -1
  107. package/dist/TimeSeriesTooltip/TimeSeriesTooltip.js +50 -22
  108. package/dist/TimeSeriesTooltip/TimeSeriesTooltip.js.map +1 -1
  109. package/dist/TimeSeriesTooltip/TooltipContent.d.ts +3 -3
  110. package/dist/TimeSeriesTooltip/TooltipContent.d.ts.map +1 -1
  111. package/dist/TimeSeriesTooltip/TooltipContent.js +28 -67
  112. package/dist/TimeSeriesTooltip/TooltipContent.js.map +1 -1
  113. package/dist/TimeSeriesTooltip/TooltipHeader.d.ts +12 -0
  114. package/dist/TimeSeriesTooltip/TooltipHeader.d.ts.map +1 -0
  115. package/dist/TimeSeriesTooltip/TooltipHeader.js +160 -0
  116. package/dist/TimeSeriesTooltip/TooltipHeader.js.map +1 -0
  117. package/dist/TimeSeriesTooltip/index.d.ts +2 -1
  118. package/dist/TimeSeriesTooltip/index.d.ts.map +1 -1
  119. package/dist/TimeSeriesTooltip/index.js +2 -1
  120. package/dist/TimeSeriesTooltip/index.js.map +1 -1
  121. package/dist/TimeSeriesTooltip/nearby-series.d.ts +46 -0
  122. package/dist/TimeSeriesTooltip/nearby-series.d.ts.map +1 -0
  123. package/dist/TimeSeriesTooltip/nearby-series.js +200 -0
  124. package/dist/TimeSeriesTooltip/nearby-series.js.map +1 -0
  125. package/dist/TimeSeriesTooltip/tooltip-model.d.ts +17 -13
  126. package/dist/TimeSeriesTooltip/tooltip-model.d.ts.map +1 -1
  127. package/dist/TimeSeriesTooltip/tooltip-model.js +11 -2
  128. package/dist/TimeSeriesTooltip/tooltip-model.js.map +1 -1
  129. package/dist/TimeSeriesTooltip/utils.d.ts +1 -1
  130. package/dist/TimeSeriesTooltip/utils.d.ts.map +1 -1
  131. package/dist/TimeSeriesTooltip/utils.js +9 -17
  132. package/dist/TimeSeriesTooltip/utils.js.map +1 -1
  133. package/dist/UnitSelector/UnitSelector.d.ts.map +1 -1
  134. package/dist/UnitSelector/UnitSelector.js +40 -17
  135. package/dist/UnitSelector/UnitSelector.js.map +1 -1
  136. package/dist/YAxisLabel.d.ts.map +1 -1
  137. package/dist/YAxisLabel.js +1 -0
  138. package/dist/YAxisLabel.js.map +1 -1
  139. package/dist/cjs/ContentWithLegend/ContentWithLegend.js +70 -0
  140. package/dist/cjs/ContentWithLegend/index.js +28 -0
  141. package/dist/cjs/ContentWithLegend/model/content-with-legend-model.js +100 -0
  142. package/dist/cjs/DateTimeRangePicker/TimeRangeSelector.js +7 -1
  143. package/dist/cjs/EChart/EChart.js +9 -1
  144. package/dist/cjs/Legend/CompactLegend.js +4 -1
  145. package/dist/cjs/Legend/Legend.js +70 -21
  146. package/dist/cjs/Legend/ListLegend.js +17 -103
  147. package/dist/cjs/Legend/ListLegendItem.js +8 -11
  148. package/dist/cjs/Legend/TableLegend.js +67 -0
  149. package/dist/cjs/LegendOptionsEditor/LegendOptionsEditor.js +34 -2
  150. package/dist/cjs/LineChart/LineChart.js +83 -36
  151. package/dist/cjs/StatChart/StatChart.js +23 -15
  152. package/dist/cjs/Table/InnerTable.js +44 -0
  153. package/dist/cjs/Table/Table.js +102 -0
  154. package/dist/cjs/Table/TableBody.js +29 -0
  155. package/dist/cjs/Table/TableCell.js +97 -0
  156. package/dist/cjs/Table/TableCheckbox.js +55 -0
  157. package/dist/cjs/Table/TableHead.js +29 -0
  158. package/dist/cjs/Table/TableRow.js +35 -0
  159. package/dist/cjs/Table/VirtualizedTable.js +155 -0
  160. package/dist/cjs/Table/VirtualizedTableContainer.js +30 -0
  161. package/dist/cjs/Table/hooks/useTableKeyboardNav.js +99 -0
  162. package/dist/cjs/Table/hooks/useVirtualizedTableKeyboardNav.js +93 -0
  163. package/dist/cjs/Table/index.js +33 -0
  164. package/dist/cjs/Table/model/table-model.js +80 -0
  165. package/dist/cjs/TimeSeriesTooltip/SeriesInfo.js +10 -8
  166. package/dist/cjs/TimeSeriesTooltip/TimeSeriesTooltip.js +54 -21
  167. package/dist/cjs/TimeSeriesTooltip/TooltipContent.js +26 -65
  168. package/dist/cjs/TimeSeriesTooltip/TooltipHeader.js +171 -0
  169. package/dist/cjs/TimeSeriesTooltip/index.js +2 -1
  170. package/dist/cjs/TimeSeriesTooltip/nearby-series.js +206 -0
  171. package/dist/cjs/TimeSeriesTooltip/tooltip-model.js +17 -3
  172. package/dist/cjs/TimeSeriesTooltip/utils.js +8 -16
  173. package/dist/cjs/UnitSelector/UnitSelector.js +39 -16
  174. package/dist/cjs/YAxisLabel.js +1 -0
  175. package/dist/cjs/context/SnackbarProvider.js +66 -0
  176. package/dist/cjs/index.js +3 -0
  177. package/dist/cjs/model/legend.js +37 -2
  178. package/dist/cjs/model/units/bytes.js +25 -21
  179. package/dist/cjs/model/units/constants.js +3 -3
  180. package/dist/cjs/model/units/decimal.js +22 -19
  181. package/dist/cjs/model/units/percent.js +20 -11
  182. package/dist/cjs/model/units/time.js +26 -22
  183. package/dist/cjs/model/units/units.js +2 -2
  184. package/dist/cjs/model/units/utils.js +42 -0
  185. package/dist/cjs/theme/palette/grey.js +6 -2
  186. package/dist/cjs/utils/theme-gen.js +8 -13
  187. package/dist/context/SnackbarProvider.d.ts +23 -0
  188. package/dist/context/SnackbarProvider.d.ts.map +1 -0
  189. package/dist/context/SnackbarProvider.js +59 -0
  190. package/dist/context/SnackbarProvider.js.map +1 -0
  191. package/dist/index.d.ts +3 -0
  192. package/dist/index.d.ts.map +1 -1
  193. package/dist/index.js +3 -0
  194. package/dist/index.js.map +1 -1
  195. package/dist/model/legend.d.ts +19 -5
  196. package/dist/model/legend.d.ts.map +1 -1
  197. package/dist/model/legend.js +31 -1
  198. package/dist/model/legend.js.map +1 -1
  199. package/dist/model/units/bytes.d.ts +1 -1
  200. package/dist/model/units/bytes.d.ts.map +1 -1
  201. package/dist/model/units/bytes.js +26 -22
  202. package/dist/model/units/bytes.js.map +1 -1
  203. package/dist/model/units/constants.d.ts +1 -1
  204. package/dist/model/units/constants.js +1 -1
  205. package/dist/model/units/constants.js.map +1 -1
  206. package/dist/model/units/decimal.d.ts +2 -2
  207. package/dist/model/units/decimal.d.ts.map +1 -1
  208. package/dist/model/units/decimal.js +22 -19
  209. package/dist/model/units/decimal.js.map +1 -1
  210. package/dist/model/units/percent.d.ts +2 -2
  211. package/dist/model/units/percent.d.ts.map +1 -1
  212. package/dist/model/units/percent.js +20 -11
  213. package/dist/model/units/percent.js.map +1 -1
  214. package/dist/model/units/time.d.ts +2 -2
  215. package/dist/model/units/time.d.ts.map +1 -1
  216. package/dist/model/units/time.js +26 -22
  217. package/dist/model/units/time.js.map +1 -1
  218. package/dist/model/units/types.d.ts +9 -0
  219. package/dist/model/units/types.d.ts.map +1 -1
  220. package/dist/model/units/types.js.map +1 -1
  221. package/dist/model/units/units.js +2 -2
  222. package/dist/model/units/units.js.map +1 -1
  223. package/dist/model/units/utils.d.ts +4 -0
  224. package/dist/model/units/utils.d.ts.map +1 -0
  225. package/dist/model/units/utils.js +32 -0
  226. package/dist/model/units/utils.js.map +1 -0
  227. package/dist/theme/palette/grey.d.ts.map +1 -1
  228. package/dist/theme/palette/grey.js +6 -2
  229. package/dist/theme/palette/grey.js.map +1 -1
  230. package/dist/utils/theme-gen.d.ts.map +1 -1
  231. package/dist/utils/theme-gen.js +8 -13
  232. package/dist/utils/theme-gen.js.map +1 -1
  233. package/package.json +6 -6
  234. package/dist/TimeSeriesTooltip/focused-series.d.ts +0 -25
  235. package/dist/TimeSeriesTooltip/focused-series.d.ts.map +0 -1
  236. package/dist/TimeSeriesTooltip/focused-series.js +0 -111
  237. package/dist/TimeSeriesTooltip/focused-series.js.map +0 -1
  238. package/dist/cjs/TimeSeriesTooltip/focused-series.js +0 -117
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.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\nexport * from './ColorPicker';\nexport * from './DateTimeRangePicker';\nexport * from './Dialog';\nexport * from './Drawer';\nexport * from './EChart';\nexport * from './ErrorAlert';\nexport * from './ErrorBoundary';\nexport * from './GaugeChart';\nexport * from './InfoTooltip';\nexport * from './JSONEditor';\nexport * from './Legend';\nexport * from './LegendOptionsEditor';\nexport * from './LineChart';\nexport * from './OptionsEditorLayout';\nexport * from './StatChart';\nexport * from './ThresholdsEditor';\nexport * from './TimeSeriesTooltip';\nexport * from './UnitSelector';\nexport * from './YAxisLabel';\nexport * from './context/ChartsThemeProvider';\nexport * from './context/TimeZoneProvider';\nexport * from './utils';\nexport * from './model';\nexport * from './test-utils';\nexport * from './theme';\n"],"names":[],"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,cAAc,eAAe,CAAC;AAC9B,cAAc,uBAAuB,CAAC;AACtC,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC;AAC5B,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC"}
1
+ {"version":3,"sources":["../src/index.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\nexport * from './ColorPicker';\nexport * from './ContentWithLegend';\nexport * from './DateTimeRangePicker';\nexport * from './Dialog';\nexport * from './Drawer';\nexport * from './EChart';\nexport * from './ErrorAlert';\nexport * from './ErrorBoundary';\nexport * from './GaugeChart';\nexport * from './InfoTooltip';\nexport * from './JSONEditor';\nexport * from './Legend';\nexport * from './LegendOptionsEditor';\nexport * from './LineChart';\nexport * from './OptionsEditorLayout';\nexport * from './StatChart';\nexport * from './Table';\nexport * from './ThresholdsEditor';\nexport * from './TimeSeriesTooltip';\nexport * from './UnitSelector';\nexport * from './YAxisLabel';\nexport * from './context/ChartsThemeProvider';\nexport * from './context/TimeZoneProvider';\nexport * from './context/SnackbarProvider';\nexport * from './utils';\nexport * from './model';\nexport * from './test-utils';\nexport * from './theme';\n"],"names":[],"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,cAAc,eAAe,CAAC;AAC9B,cAAc,qBAAqB,CAAC;AACpC,cAAc,uBAAuB,CAAC;AACtC,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,iBAAiB,CAAC;AAChC,cAAc,cAAc,CAAC;AAC7B,cAAc,eAAe,CAAC;AAC9B,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC;AAC5B,cAAc,uBAAuB,CAAC;AACtC,cAAc,aAAa,CAAC;AAC5B,cAAc,SAAS,CAAC;AACxB,cAAc,oBAAoB,CAAC;AACnC,cAAc,qBAAqB,CAAC;AACpC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,cAAc,CAAC;AAC7B,cAAc,+BAA+B,CAAC;AAC9C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,4BAA4B,CAAC;AAC3C,cAAc,SAAS,CAAC;AACxB,cAAc,SAAS,CAAC;AACxB,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC"}
@@ -1,22 +1,36 @@
1
1
  import { MouseEventHandler } from 'react';
2
2
  export declare const legendPositions: readonly ["Bottom", "Right"];
3
3
  export declare type LegendPositions = (typeof legendPositions)[number];
4
+ export declare const legendModes: readonly ["List", "Table"];
5
+ export declare type LegendMode = (typeof legendModes)[number];
4
6
  export interface LegendOptions {
5
7
  position: LegendPositions;
8
+ mode?: LegendMode;
6
9
  }
7
10
  export interface LegendItem {
8
11
  id: string;
9
12
  label: string;
10
- isSelected: boolean;
11
13
  color: string;
12
- onClick: MouseEventHandler<HTMLElement>;
14
+ onClick?: MouseEventHandler<HTMLElement>;
13
15
  }
14
- export declare type LegendPositionConfig = {
16
+ export declare type LegendSingleSelectConfig = {
15
17
  label: string;
16
18
  };
17
- export declare const LEGEND_POSITIONS_CONFIG: Readonly<Record<LegendPositions, LegendPositionConfig>>;
18
- export declare const DEFAULT_LEGEND: LegendOptions;
19
+ /**
20
+ * State of selected items in the legend.
21
+ * - When "ALL", all legend items are selected, but not visually highlighted.
22
+ * - Otherwise, it is a Record that associates legend item ids with a boolean
23
+ * value. When the associated entry for a legend item is `true`, that item
24
+ * will be treated as selected and visually highlighted.
25
+ */
26
+ export declare type SelectedLegendItemState = Record<LegendItem['id'], boolean> | 'ALL';
27
+ export declare const LEGEND_POSITIONS_CONFIG: Readonly<Record<LegendPositions, LegendSingleSelectConfig>>;
28
+ export declare const LEGEND_MODE_CONFIG: Readonly<Record<LegendMode, LegendSingleSelectConfig>>;
29
+ export declare const DEFAULT_LEGEND: Required<LegendOptions>;
19
30
  export declare function getLegendPosition(position?: LegendPositions): "Bottom" | "Right";
20
31
  export declare function isValidLegendPosition(position: LegendPositions): boolean;
32
+ export declare function isValidLegendMode(mode: LegendMode): boolean;
33
+ export declare function getLegendMode(mode?: LegendMode): "List" | "Table";
21
34
  export declare function validateLegendSpec(legend?: LegendOptions): boolean;
35
+ export declare function isLegendItemVisuallySelected(item: LegendItem, selectedItems: SelectedLegendItemState): boolean;
22
36
  //# sourceMappingURL=legend.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"legend.d.ts","sourceRoot":"","sources":["../../src/model/legend.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,iBAAiB,EAAE,MAAM,OAAO,CAAC;AAE1C,eAAO,MAAM,eAAe,8BAA+B,CAAC;AAE5D,oBAAY,eAAe,GAAG,CAAC,OAAO,eAAe,CAAC,CAAC,MAAM,CAAC,CAAC;AAE/D,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,eAAe,CAAC;CAC3B;AAED,MAAM,WAAW,UAAU;I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CAAC;CACf,CAAC;AAEF;;;;;;GAMG;AACH,oBAAY,uBAAuB,GAAG,MAAM,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC,GAAG,KAAK,CAAC;AAEhF,eAAO,MAAM,uBAAuB,EAAE,QAAQ,CAAC,MAAM,CAAC,eAAe,EAAE,wBAAwB,CAAC,CAG/F,CAAC;AAEF,eAAO,MAAM,kBAAkB,EAAE,QAAQ,CAAC,MAAM,CAAC,UAAU,EAAE,wBAAwB,CAAC,CAGrF,CAAC;AAEF,eAAO,MAAM,cAAc,EAAE,QAAQ,CAAC,aAAa,CAGlD,CAAC;AAEF,wBAAgB,iBAAiB,CAAC,QAAQ,CAAC,EAAE,eAAe,sBAQ3D;AAED,wBAAgB,qBAAqB,CAAC,QAAQ,EAAE,eAAe,WAE9D;AAED,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,UAAU,WAEjD;AAED,wBAAgB,aAAa,CAAC,IAAI,CAAC,EAAE,UAAU,oBAM9C;AAED,wBAAgB,kBAAkB,CAAC,MAAM,CAAC,EAAE,aAAa,WAaxD;AAED,wBAAgB,4BAA4B,CAAC,IAAI,EAAE,UAAU,EAAE,aAAa,EAAE,uBAAuB,WAIpG"}
@@ -14,6 +14,10 @@ export const legendPositions = [
14
14
  'Bottom',
15
15
  'Right'
16
16
  ];
17
+ export const legendModes = [
18
+ 'List',
19
+ 'Table'
20
+ ];
17
21
  export const LEGEND_POSITIONS_CONFIG = {
18
22
  Bottom: {
19
23
  label: 'Bottom'
@@ -22,8 +26,17 @@ export const LEGEND_POSITIONS_CONFIG = {
22
26
  label: 'Right'
23
27
  }
24
28
  };
29
+ export const LEGEND_MODE_CONFIG = {
30
+ List: {
31
+ label: 'List'
32
+ },
33
+ Table: {
34
+ label: 'Table'
35
+ }
36
+ };
25
37
  export const DEFAULT_LEGEND = {
26
- position: 'Bottom'
38
+ position: 'Bottom',
39
+ mode: 'List'
27
40
  };
28
41
  export function getLegendPosition(position) {
29
42
  if (position === undefined) {
@@ -37,6 +50,15 @@ export function getLegendPosition(position) {
37
50
  export function isValidLegendPosition(position) {
38
51
  return legendPositions.includes(position);
39
52
  }
53
+ export function isValidLegendMode(mode) {
54
+ return legendModes.includes(mode);
55
+ }
56
+ export function getLegendMode(mode) {
57
+ if (!mode || !isValidLegendMode(mode)) {
58
+ return DEFAULT_LEGEND.mode;
59
+ }
60
+ return mode;
61
+ }
40
62
  export function validateLegendSpec(legend) {
41
63
  if (legend === undefined) {
42
64
  // undefined is valid since this is how legend is hidden by default
@@ -45,7 +67,15 @@ export function validateLegendSpec(legend) {
45
67
  if (!isValidLegendPosition(legend.position)) {
46
68
  return false;
47
69
  }
70
+ if (legend.mode && !isValidLegendMode(legend.mode)) {
71
+ return false;
72
+ }
48
73
  return true;
49
74
  }
75
+ export function isLegendItemVisuallySelected(item, selectedItems) {
76
+ // In the "ALL" case, technically all legend items are selected, but we do
77
+ // not render them differently.
78
+ return selectedItems !== 'ALL' && !!selectedItems[item.id];
79
+ }
50
80
 
51
81
  //# sourceMappingURL=legend.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../src/model/legend.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 { MouseEventHandler } from 'react';\n\nexport const legendPositions = ['Bottom', 'Right'] as const;\n\nexport type LegendPositions = (typeof legendPositions)[number];\n\nexport interface LegendOptions {\n position: LegendPositions;\n}\n\nexport interface LegendItem {\n id: string;\n label: string;\n isSelected: boolean;\n color: string;\n onClick: MouseEventHandler<HTMLElement>;\n}\n\nexport type LegendPositionConfig = {\n label: string;\n};\n\nexport const LEGEND_POSITIONS_CONFIG: Readonly<Record<LegendPositions, LegendPositionConfig>> = {\n Bottom: { label: 'Bottom' },\n Right: { label: 'Right' },\n};\n\nexport const DEFAULT_LEGEND: LegendOptions = {\n position: 'Bottom',\n};\n\nexport function getLegendPosition(position?: LegendPositions) {\n if (position === undefined) {\n return DEFAULT_LEGEND.position;\n }\n if (isValidLegendPosition(position)) {\n return position;\n }\n return DEFAULT_LEGEND.position;\n}\n\nexport function isValidLegendPosition(position: LegendPositions) {\n return (legendPositions as readonly string[]).includes(position);\n}\n\nexport function validateLegendSpec(legend?: LegendOptions) {\n if (legend === undefined) {\n // undefined is valid since this is how legend is hidden by default\n return true;\n }\n if (!isValidLegendPosition(legend.position)) {\n return false;\n }\n return true;\n}\n"],"names":["legendPositions","LEGEND_POSITIONS_CONFIG","Bottom","label","Right","DEFAULT_LEGEND","position","getLegendPosition","undefined","isValidLegendPosition","includes","validateLegendSpec","legend"],"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;AAIjC,OAAO,MAAMA,eAAe,GAAG;IAAC,QAAQ;IAAE,OAAO;CAAC,AAAS,CAAC;AAoB5D,OAAO,MAAMC,uBAAuB,GAA4D;IAC9FC,MAAM,EAAE;QAAEC,KAAK,EAAE,QAAQ;KAAE;IAC3BC,KAAK,EAAE;QAAED,KAAK,EAAE,OAAO;KAAE;CAC1B,CAAC;AAEF,OAAO,MAAME,cAAc,GAAkB;IAC3CC,QAAQ,EAAE,QAAQ;CACnB,CAAC;AAEF,OAAO,SAASC,iBAAiB,CAACD,QAA0B,EAAE;IAC5D,IAAIA,QAAQ,KAAKE,SAAS,EAAE;QAC1B,OAAOH,cAAc,CAACC,QAAQ,CAAC;IACjC,CAAC;IACD,IAAIG,qBAAqB,CAACH,QAAQ,CAAC,EAAE;QACnC,OAAOA,QAAQ,CAAC;IAClB,CAAC;IACD,OAAOD,cAAc,CAACC,QAAQ,CAAC;AACjC,CAAC;AAED,OAAO,SAASG,qBAAqB,CAACH,QAAyB,EAAE;IAC/D,OAAO,AAACN,eAAe,CAAuBU,QAAQ,CAACJ,QAAQ,CAAC,CAAC;AACnE,CAAC;AAED,OAAO,SAASK,kBAAkB,CAACC,MAAsB,EAAE;IACzD,IAAIA,MAAM,KAAKJ,SAAS,EAAE;QACxB,mEAAmE;QACnE,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,CAACC,qBAAqB,CAACG,MAAM,CAACN,QAAQ,CAAC,EAAE;QAC3C,OAAO,KAAK,CAAC;IACf,CAAC;IACD,OAAO,IAAI,CAAC;AACd,CAAC"}
1
+ {"version":3,"sources":["../../src/model/legend.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 { MouseEventHandler } from 'react';\n\nexport const legendPositions = ['Bottom', 'Right'] as const;\nexport type LegendPositions = (typeof legendPositions)[number];\n\nexport const legendModes = ['List', 'Table'] as const;\nexport type LegendMode = (typeof legendModes)[number];\n\nexport interface LegendOptions {\n position: LegendPositions;\n mode?: LegendMode;\n}\n\nexport interface LegendItem {\n id: string;\n label: string;\n color: string;\n onClick?: MouseEventHandler<HTMLElement>;\n}\n\nexport type LegendSingleSelectConfig = {\n label: string;\n};\n\n/**\n * State of selected items in the legend.\n * - When \"ALL\", all legend items are selected, but not visually highlighted.\n * - Otherwise, it is a Record that associates legend item ids with a boolean\n * value. When the associated entry for a legend item is `true`, that item\n * will be treated as selected and visually highlighted.\n */\nexport type SelectedLegendItemState = Record<LegendItem['id'], boolean> | 'ALL';\n\nexport const LEGEND_POSITIONS_CONFIG: Readonly<Record<LegendPositions, LegendSingleSelectConfig>> = {\n Bottom: { label: 'Bottom' },\n Right: { label: 'Right' },\n};\n\nexport const LEGEND_MODE_CONFIG: Readonly<Record<LegendMode, LegendSingleSelectConfig>> = {\n List: { label: 'List' },\n Table: { label: 'Table' },\n};\n\nexport const DEFAULT_LEGEND: Required<LegendOptions> = {\n position: 'Bottom',\n mode: 'List',\n};\n\nexport function getLegendPosition(position?: LegendPositions) {\n if (position === undefined) {\n return DEFAULT_LEGEND.position;\n }\n if (isValidLegendPosition(position)) {\n return position;\n }\n return DEFAULT_LEGEND.position;\n}\n\nexport function isValidLegendPosition(position: LegendPositions) {\n return (legendPositions as readonly string[]).includes(position);\n}\n\nexport function isValidLegendMode(mode: LegendMode) {\n return (legendModes as readonly string[]).includes(mode);\n}\n\nexport function getLegendMode(mode?: LegendMode) {\n if (!mode || !isValidLegendMode(mode)) {\n return DEFAULT_LEGEND.mode;\n }\n\n return mode;\n}\n\nexport function validateLegendSpec(legend?: LegendOptions) {\n if (legend === undefined) {\n // undefined is valid since this is how legend is hidden by default\n return true;\n }\n if (!isValidLegendPosition(legend.position)) {\n return false;\n }\n if (legend.mode && !isValidLegendMode(legend.mode)) {\n return false;\n }\n\n return true;\n}\n\nexport function isLegendItemVisuallySelected(item: LegendItem, selectedItems: SelectedLegendItemState) {\n // In the \"ALL\" case, technically all legend items are selected, but we do\n // not render them differently.\n return selectedItems !== 'ALL' && !!selectedItems[item.id];\n}\n"],"names":["legendPositions","legendModes","LEGEND_POSITIONS_CONFIG","Bottom","label","Right","LEGEND_MODE_CONFIG","List","Table","DEFAULT_LEGEND","position","mode","getLegendPosition","undefined","isValidLegendPosition","includes","isValidLegendMode","getLegendMode","validateLegendSpec","legend","isLegendItemVisuallySelected","item","selectedItems","id"],"mappings":"AAAA,oCA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,OAAO,SAASM,aAAa,CAACN,IAAiB,EAAE;IAC/C,IAAI,CAACA,IAAI,IAAI,CAACK,iBAAiB,CAACL,IAAI,CAAC,EAAE;QACrC,OAAOF,cAAc,CAACE,IAAI,CAAC;IAC7B,CAAC;IAED,OAAOA,IAAI,CAAC;AACd,CAAC;AAED,OAAO,SAASO,kBAAkB,CAACC,MAAsB,EAAE;IACzD,IAAIA,MAAM,KAAKN,SAAS,EAAE;QACxB,mEAAmE;QACnE,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,CAACC,qBAAqB,CAACK,MAAM,CAACT,QAAQ,CAAC,EAAE;QAC3C,OAAO,KAAK,CAAC;IACf,CAAC;IACD,IAAIS,MAAM,CAACR,IAAI,IAAI,CAACK,iBAAiB,CAACG,MAAM,CAACR,IAAI,CAAC,EAAE;QAClD,OAAO,KAAK,CAAC;IACf,CAAC;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAED,OAAO,SAASS,4BAA4B,CAACC,IAAgB,EAAEC,aAAsC,EAAE;IACrG,0EAA0E;IAC1E,+BAA+B;IAC/B,OAAOA,aAAa,KAAK,KAAK,IAAI,CAAC,CAACA,aAAa,CAACD,IAAI,CAACE,EAAE,CAAC,CAAC;AAC7D,CAAC"}
@@ -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;AA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Y,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,KA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AACO,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,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"}
@@ -10,16 +10,16 @@
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 } from './utils';
14
15
  const percentUnitKinds = [
15
16
  'Percent',
16
17
  'PercentDecimal',
17
18
  '%'
18
19
  ];
19
- export const DECIMAL_GROUP_CONFIG = {
20
- label: 'Decimal',
21
- decimal_places: true,
22
- abbreviate: true
20
+ export const PERCENT_GROUP_CONFIG = {
21
+ label: 'Percent',
22
+ decimal_places: true
23
23
  };
24
24
  const PERCENT_GROUP = 'Percent';
25
25
  export const PERCENT_UNIT_CONFIG = {
@@ -39,13 +39,22 @@ export const PERCENT_UNIT_CONFIG = {
39
39
  label: '%'
40
40
  }
41
41
  };
42
- export function formatPercent(value, unitOptions) {
43
- var _decimal_places;
44
- const decimals = (_decimal_places = unitOptions.decimal_places) !== null && _decimal_places !== void 0 ? _decimal_places : DEFAULT_DECIMAL_PLACES;
45
- if (unitOptions.kind === 'PercentDecimal') {
46
- value = value * 100;
42
+ export function formatPercent(value, { kind , decimal_places }) {
43
+ // Intl.NumberFormat translates 0 -> 0%, 0.5 -> 50%, 1 -> 100%
44
+ if (kind === 'Percent') {
45
+ value = value / 100;
47
46
  }
48
- return value.toFixed(decimals) + '%';
47
+ const formatterOptions = {
48
+ style: 'percent',
49
+ useGrouping: true
50
+ };
51
+ if (hasDecimalPlaces(decimal_places)) {
52
+ formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimal_places);
53
+ formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimal_places);
54
+ } else {
55
+ formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
56
+ }
57
+ return Intl.NumberFormat('en-US', formatterOptions).format(value);
49
58
  }
50
59
 
51
60
  //# sourceMappingURL=percent.js.map
@@ -1 +1 @@
1
- {"version":3,"sources":["../../../src/model/units/percent.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 { UnitGroupConfig, UnitConfig } from './types';\nimport { DEFAULT_DECIMAL_PLACES } from './constants';\n\nconst percentUnitKinds = ['Percent', 'PercentDecimal', '%'] as const;\ntype PercentUnitKind = (typeof percentUnitKinds)[number];\nexport type PercentUnitOptions = {\n kind: PercentUnitKind;\n decimal_places?: number;\n};\nexport const DECIMAL_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Decimal',\n decimal_places: true,\n abbreviate: true,\n};\nconst PERCENT_GROUP = 'Percent';\nexport const PERCENT_UNIT_CONFIG: Readonly<Record<PercentUnitKind, UnitConfig>> = {\n Percent: {\n group: PERCENT_GROUP,\n label: 'Percent (0-100)',\n },\n PercentDecimal: {\n group: PERCENT_GROUP,\n label: 'Percent (0.0-1.0)',\n },\n '%': {\n // This option is not shown in the selector because it is a shorthand\n // duplicate of `Percent`.\n disableSelectorOption: true,\n group: PERCENT_GROUP,\n label: '%',\n },\n};\n\nexport function formatPercent(value: number, unitOptions: PercentUnitOptions): string {\n const decimals = unitOptions.decimal_places ?? DEFAULT_DECIMAL_PLACES;\n\n if (unitOptions.kind === 'PercentDecimal') {\n value = value * 100;\n }\n\n return value.toFixed(decimals) + '%';\n}\n"],"names":["DEFAULT_DECIMAL_PLACES","percentUnitKinds","DECIMAL_GROUP_CONFIG","label","decimal_places","abbreviate","PERCENT_GROUP","PERCENT_UNIT_CONFIG","Percent","group","PercentDecimal","disableSelectorOption","formatPercent","value","unitOptions","decimals","kind","toFixed"],"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;AAGjC,SAASA,sBAAsB,QAAQ,aAAa,CAAC;AAErD,MAAMC,gBAAgB,GAAG;IAAC,SAAS;IAAE,gBAAgB;IAAE,GAAG;CAAC,AAAS,AAAC;AAMrE,OAAO,MAAMC,oBAAoB,GAAoB;IACnDC,KAAK,EAAE,SAAS;IAChBC,cAAc,EAAE,IAAI;IACpBC,UAAU,EAAE,IAAI;CACjB,CAAC;AACF,MAAMC,aAAa,GAAG,SAAS,AAAC;AAChC,OAAO,MAAMC,mBAAmB,GAAkD;IAChFC,OAAO,EAAE;QACPC,KAAK,EAAEH,aAAa;QACpBH,KAAK,EAAE,iBAAiB;KACzB;IACDO,cAAc,EAAE;QACdD,KAAK,EAAEH,aAAa;QACpBH,KAAK,EAAE,mBAAmB;KAC3B;IACD,GAAG,EAAE;QACH,qEAAqE;QACrE,0BAA0B;QAC1BQ,qBAAqB,EAAE,IAAI;QAC3BF,KAAK,EAAEH,aAAa;QACpBH,KAAK,EAAE,GAAG;KACX;CACF,CAAC;AAEF,OAAO,SAASS,aAAa,CAACC,KAAa,EAAEC,WAA+B,EAAU;QACnEA,eAA0B;IAA3C,MAAMC,QAAQ,GAAGD,CAAAA,eAA0B,GAA1BA,WAAW,CAACV,cAAc,cAA1BU,eAA0B,cAA1BA,eAA0B,GAAId,sBAAsB,AAAC;IAEtE,IAAIc,WAAW,CAACE,IAAI,KAAK,gBAAgB,EAAE;QACzCH,KAAK,GAAGA,KAAK,GAAG,GAAG,CAAC;IACtB,CAAC;IAED,OAAOA,KAAK,CAACI,OAAO,CAACF,QAAQ,CAAC,GAAG,GAAG,CAAC;AACvC,CAAC"}
1
+ {"version":3,"sources":["../../../src/model/units/percent.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 } from './utils';\n\nconst percentUnitKinds = ['Percent', 'PercentDecimal', '%'] as const;\ntype PercentUnitKind = (typeof percentUnitKinds)[number];\nexport type PercentUnitOptions = {\n kind: PercentUnitKind;\n decimal_places?: number;\n};\nexport const PERCENT_GROUP_CONFIG: UnitGroupConfig = {\n label: 'Percent',\n decimal_places: true,\n};\nconst PERCENT_GROUP = 'Percent';\nexport const PERCENT_UNIT_CONFIG: Readonly<Record<PercentUnitKind, UnitConfig>> = {\n Percent: {\n group: PERCENT_GROUP,\n label: 'Percent (0-100)',\n },\n PercentDecimal: {\n group: PERCENT_GROUP,\n label: 'Percent (0.0-1.0)',\n },\n '%': {\n // This option is not shown in the selector because it is a shorthand\n // duplicate of `Percent`.\n disableSelectorOption: true,\n group: PERCENT_GROUP,\n label: '%',\n },\n};\n\nexport function formatPercent(value: number, { kind, decimal_places }: PercentUnitOptions): string {\n // Intl.NumberFormat translates 0 -> 0%, 0.5 -> 50%, 1 -> 100%\n if (kind === 'Percent') {\n value = value / 100;\n }\n\n const formatterOptions: Intl.NumberFormatOptions = {\n style: 'percent',\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 formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;\n }\n\n return Intl.NumberFormat('en-US', formatterOptions).format(value);\n}\n"],"names":["MAX_SIGNIFICANT_DIGITS","hasDecimalPlaces","limitDecimalPlaces","percentUnitKinds","PERCENT_GROUP_CONFIG","label","decimal_places","PERCENT_GROUP","PERCENT_UNIT_CONFIG","Percent","group","PercentDecimal","disableSelectorOption","formatPercent","value","kind","formatterOptions","style","useGrouping","minimumFractionDigits","maximumFractionDigits","maximumSignificantDigits","Intl","NumberFormat","format"],"mappings":"AAAA,oCAAoC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,CAACI,qBAAqB,GAAGlB,kBAAkB,CAACI,cAAc,CAAC,CAAC;IAC9E,OAAO;QACLU,gBAAgB,CAACK,wBAAwB,GAAGrB,sBAAsB,CAAC;IACrE,CAAC;IAED,OAAOsB,IAAI,CAACC,YAAY,CAAC,OAAO,EAAEP,gBAAgB,CAAC,CAACQ,MAAM,CAACV,KAAK,CAAC,CAAC;AACpE,CAAC"}
@@ -7,7 +7,7 @@ export declare type TimeUnitOptions = {
7
7
  };
8
8
  export declare const TIME_GROUP_CONFIG: UnitGroupConfig;
9
9
  export declare const TIME_UNIT_CONFIG: Readonly<Record<TimeUnitKind, UnitConfig>>;
10
- export declare enum TimeIntlDuration {
10
+ export declare enum PersesTimeToIntlTime {
11
11
  Milliseconds = "millisecond",
12
12
  Seconds = "second",
13
13
  Minutes = "minute",
@@ -17,6 +17,6 @@ export declare enum TimeIntlDuration {
17
17
  Months = "month",
18
18
  Years = "year"
19
19
  }
20
- export declare function formatTime(value: number, unitOptions: TimeUnitOptions): string;
20
+ export declare function formatTime(value: number, { kind, decimal_places }: TimeUnitOptions): string;
21
21
  export {};
22
22
  //# sourceMappingURL=time.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../../../src/model/units/time.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAEtD,QAAA,MAAM,aAAa,8FAA+F,CAAC;AACnH,aAAK,YAAY,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;AACnD,oBAAY,eAAe,GAAG;IAC5B,IAAI,EAAE,YAAY,CAAC;IACnB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,eAAO,MAAM,iBAAiB,EAAE,eAG/B,CAAC;AACF,eAAO,MAAM,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,YAAY,EAAE,UAAU,CAAC,CAiCvE,CAAC;AAIF,oBAAY,gBAAgB;IAC1B,YAAY,gBAAgB;IAC5B,OAAO,WAAW;IAClB,OAAO,WAAW;IAClB,KAAK,SAAS;IACd,IAAI,QAAQ;IACZ,KAAK,SAAS;IACd,MAAM,UAAU;IAChB,KAAK,SAAS;CACf;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,eAAe,GAAG,MAAM,CAU9E"}
1
+ {"version":3,"file":"time.d.ts","sourceRoot":"","sources":["../../../src/model/units/time.ts"],"names":[],"mappings":"AAcA,OAAO,EAAE,eAAe,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAGtD,QAAA,MAAM,aAAa,8FAA+F,CAAC;AACnH,aAAK,YAAY,GAAG,CAAC,OAAO,aAAa,CAAC,CAAC,MAAM,CAAC,CAAC;AACnD,oBAAY,eAAe,GAAG;IAC5B,IAAI,EAAE,YAAY,CAAC;IACnB,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF,eAAO,MAAM,iBAAiB,EAAE,eAG/B,CAAC;AACF,eAAO,MAAM,gBAAgB,EAAE,QAAQ,CAAC,MAAM,CAAC,YAAY,EAAE,UAAU,CAAC,CAiCvE,CAAC;AAIF,oBAAY,oBAAoB;IAC9B,YAAY,gBAAgB;IAC5B,OAAO,WAAW;IAClB,OAAO,WAAW;IAClB,KAAK,SAAS;IACd,IAAI,QAAQ;IACZ,KAAK,SAAS;IACd,MAAM,UAAU;IAChB,KAAK,SAAS;CACf;AAED,wBAAgB,UAAU,CAAC,KAAK,EAAE,MAAM,EAAE,EAAE,IAAI,EAAE,cAAc,EAAE,EAAE,eAAe,GAAG,MAAM,CAiB3F"}
@@ -10,7 +10,8 @@
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 } from './utils';
14
15
  const timeUnitKinds = [
15
16
  'Milliseconds',
16
17
  'Seconds',
@@ -60,28 +61,31 @@ export const TIME_UNIT_CONFIG = {
60
61
  label: 'Years'
61
62
  }
62
63
  };
63
- export var TimeIntlDuration;
64
- (function(TimeIntlDuration) {
65
- TimeIntlDuration["Milliseconds"] = 'millisecond';
66
- TimeIntlDuration["Seconds"] = 'second';
67
- TimeIntlDuration["Minutes"] = 'minute';
68
- TimeIntlDuration["Hours"] = 'hour';
69
- TimeIntlDuration["Days"] = 'day';
70
- TimeIntlDuration["Weeks"] = 'week';
71
- TimeIntlDuration["Months"] = 'month';
72
- TimeIntlDuration["Years"] = 'year';
73
- })(TimeIntlDuration || (TimeIntlDuration = {}));
74
- export function formatTime(value, unitOptions) {
75
- var _decimal_places;
76
- const decimalPlaces = (_decimal_places = unitOptions.decimal_places) !== null && _decimal_places !== void 0 ? _decimal_places : DEFAULT_DECIMAL_PLACES;
77
- const timeUnit = TimeIntlDuration[unitOptions.kind];
78
- const formatter = new Intl.NumberFormat('en-US', {
64
+ export var PersesTimeToIntlTime;
65
+ (function(PersesTimeToIntlTime) {
66
+ PersesTimeToIntlTime["Milliseconds"] = 'millisecond';
67
+ PersesTimeToIntlTime["Seconds"] = 'second';
68
+ PersesTimeToIntlTime["Minutes"] = 'minute';
69
+ PersesTimeToIntlTime["Hours"] = 'hour';
70
+ PersesTimeToIntlTime["Days"] = 'day';
71
+ PersesTimeToIntlTime["Weeks"] = 'week';
72
+ PersesTimeToIntlTime["Months"] = 'month';
73
+ PersesTimeToIntlTime["Years"] = 'year';
74
+ })(PersesTimeToIntlTime || (PersesTimeToIntlTime = {}));
75
+ export function formatTime(value, { kind , decimal_places }) {
76
+ const isMonthOrYear = kind === 'Months' || kind === 'Years';
77
+ const formatterOptions = {
79
78
  style: 'unit',
80
- unit: timeUnit,
81
- unitDisplay: 'long',
82
- maximumFractionDigits: decimalPlaces
83
- });
84
- return formatter.format(value);
79
+ unit: PersesTimeToIntlTime[kind],
80
+ unitDisplay: isMonthOrYear ? 'long' : 'narrow'
81
+ };
82
+ if (hasDecimalPlaces(decimal_places)) {
83
+ formatterOptions.minimumFractionDigits = limitDecimalPlaces(decimal_places);
84
+ formatterOptions.maximumFractionDigits = limitDecimalPlaces(decimal_places);
85
+ } else {
86
+ formatterOptions.maximumSignificantDigits = MAX_SIGNIFICANT_DIGITS;
87
+ }
88
+ return Intl.NumberFormat('en-US', formatterOptions).format(value);
85
89
  }
86
90
 
87
91
  //# sourceMappingURL=time.js.map