@malloydata/render 0.0.109-dev231130215708 → 0.0.109-dev231201191617

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.
@@ -0,0 +1,2 @@
1
+ import { AtomicField } from '@malloydata/malloy';
2
+ export declare function renderNumericField(f: AtomicField, value: number): string;
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.renderNumericField = void 0;
4
+ const data_styles_1 = require("../data_styles");
5
+ const ssf_1 = require("ssf");
6
+ // Map of unit to how many units of the make up the following time unit.
7
+ const multiplierMap = new Map([
8
+ [data_styles_1.DurationUnit.Nanoseconds, 1000],
9
+ [data_styles_1.DurationUnit.Microseconds, 1000],
10
+ [data_styles_1.DurationUnit.Milliseconds, 1000],
11
+ [data_styles_1.DurationUnit.Seconds, 60],
12
+ [data_styles_1.DurationUnit.Minutes, 60],
13
+ [data_styles_1.DurationUnit.Hours, 24],
14
+ [data_styles_1.DurationUnit.Days, Number.MAX_VALUE],
15
+ ]);
16
+ function formatTimeUnit(value, unit) {
17
+ let unitString = unit.toString();
18
+ if (value === 1) {
19
+ unitString = unitString.substring(0, unitString.length - 1);
20
+ }
21
+ return `${value} ${unitString}`;
22
+ }
23
+ function renderNumericField(f, value) {
24
+ let displayValue = value;
25
+ const { tag } = f.tagParse();
26
+ if (tag.has('currency')) {
27
+ let unitText = '$';
28
+ switch (tag.text('currency')) {
29
+ case data_styles_1.Currency.Euros:
30
+ unitText = '€';
31
+ break;
32
+ case data_styles_1.Currency.Pounds:
33
+ unitText = '£';
34
+ break;
35
+ case data_styles_1.Currency.Dollars:
36
+ // Do nothing.
37
+ break;
38
+ }
39
+ displayValue = (0, ssf_1.format)(`${unitText}#,##0.00`, value);
40
+ }
41
+ else if (tag.has('percent'))
42
+ displayValue = (0, ssf_1.format)('#,##0.00%', value);
43
+ else if (tag.has('duration')) {
44
+ const duration_unit = tag.text('duration');
45
+ const targetUnit = duration_unit !== null && duration_unit !== void 0 ? duration_unit : data_styles_1.DurationUnit.Seconds;
46
+ let currentDuration = value;
47
+ let currentUnitValue = 0;
48
+ let durationParts = [];
49
+ let foundUnit = false;
50
+ for (const [unit, multiplier] of multiplierMap) {
51
+ if (unit === targetUnit) {
52
+ foundUnit = true;
53
+ }
54
+ if (!foundUnit) {
55
+ continue;
56
+ }
57
+ currentUnitValue = currentDuration % multiplier;
58
+ currentDuration = Math.floor((currentDuration /= multiplier));
59
+ if (currentUnitValue > 0) {
60
+ durationParts = [
61
+ formatTimeUnit(currentUnitValue, unit),
62
+ ...durationParts,
63
+ ];
64
+ }
65
+ if (currentDuration === 0) {
66
+ break;
67
+ }
68
+ }
69
+ if (durationParts.length > 0) {
70
+ displayValue = durationParts.slice(0, 2).join(' ');
71
+ }
72
+ else
73
+ displayValue = formatTimeUnit(0, targetUnit);
74
+ }
75
+ else if (tag.has('number'))
76
+ displayValue = (0, ssf_1.format)(tag.text('number'), value);
77
+ else
78
+ displayValue = value.toLocaleString();
79
+ return displayValue;
80
+ }
81
+ exports.renderNumericField = renderNumericField;
82
+ //# sourceMappingURL=render-numeric-field.js.map
@@ -34,6 +34,7 @@ const decorators_js_1 = require("lit/decorators.js");
34
34
  const class_map_js_1 = require("lit/directives/class-map.js");
35
35
  const context_1 = require("@lit/context");
36
36
  const util_1 = require("./util");
37
+ const render_numeric_field_1 = require("./render-numeric-field");
37
38
  const tableContext = (0, context_1.createContext)('table');
38
39
  // TODO: replace with an estimator per column
39
40
  function getColumnWidth() {
@@ -75,8 +76,9 @@ const renderFieldContent = (row, f, options) => {
75
76
  let value = row.cell(f).value;
76
77
  if (options.pinnedHeader)
77
78
  value = '';
78
- else if (f.isAtomicField() && f.isNumber())
79
- value = value.toLocaleString();
79
+ else if (f.isAtomicField() && f.isNumber()) {
80
+ value = (0, render_numeric_field_1.renderNumericField)(f, value);
81
+ }
80
82
  return renderCell(f, value, {
81
83
  hideStartGutter: (0, util_1.isFirstChild)(f),
82
84
  hideEndGutter: (0, util_1.isLastChild)(f),
@@ -28,3 +28,9 @@ export declare const Nested: {
28
28
  view: string;
29
29
  };
30
30
  };
31
+ export declare const NumberFormatting: {
32
+ args: {
33
+ source: string;
34
+ view: string;
35
+ };
36
+ };
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.Nested = exports.Products2Column = exports.ProductsTableCustomTheme = exports.ProductsTable = void 0;
6
+ exports.NumberFormatting = exports.Nested = exports.Products2Column = exports.ProductsTableCustomTheme = exports.ProductsTable = void 0;
7
7
  const tables_malloy_raw_1 = __importDefault(require("./static/tables.malloy?raw"));
8
8
  const util_1 = require("./util");
9
9
  require("./themes.css");
@@ -50,4 +50,10 @@ exports.Nested = {
50
50
  view: 'nested',
51
51
  },
52
52
  };
53
+ exports.NumberFormatting = {
54
+ args: {
55
+ source: 'products',
56
+ view: 'number_formats',
57
+ },
58
+ };
53
59
  //# sourceMappingURL=tables.stories.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/render",
3
- "version": "0.0.109-dev231130215708",
3
+ "version": "0.0.109-dev231201191617",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -24,7 +24,7 @@
24
24
  },
25
25
  "dependencies": {
26
26
  "@lit/context": "^1.1.0",
27
- "@malloydata/malloy": "^0.0.109-dev231130215708",
27
+ "@malloydata/malloy": "^0.0.109-dev231201191617",
28
28
  "@types/luxon": "^2.4.0",
29
29
  "lit": "^3.0.2",
30
30
  "lodash": "^4.17.20",