@malloydata/render 0.0.56-dev230710183220 → 0.0.56-dev230710205111

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.
@@ -104,6 +104,18 @@ export declare enum DataVolumeUnit {
104
104
  export interface DataVolumeRenderOptions extends TextRenderOptions {
105
105
  data_volume_unit?: DataVolumeUnit;
106
106
  }
107
+ export declare enum DurationUnit {
108
+ Nanoseconds = "nanoseconds",
109
+ Microseconds = "microseconds",
110
+ Milliseconds = "milliseconds",
111
+ Seconds = "seconds",
112
+ Minutes = "minutes",
113
+ Hours = "hours",
114
+ Days = "days"
115
+ }
116
+ export interface DurationRenderOptions extends TextRenderOptions {
117
+ duration_unit?: DurationUnit;
118
+ }
107
119
  export interface ImageRenderOptions extends TextRenderOptions {
108
120
  border?: boolean;
109
121
  }
@@ -22,7 +22,7 @@
22
22
  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
23
  */
24
24
  Object.defineProperty(exports, "__esModule", { value: true });
25
- exports.DataVolumeUnit = exports.Currency = void 0;
25
+ exports.DurationUnit = exports.DataVolumeUnit = exports.Currency = void 0;
26
26
  var Currency;
27
27
  (function (Currency) {
28
28
  Currency["Dollars"] = "usd";
@@ -37,4 +37,14 @@ var DataVolumeUnit;
37
37
  DataVolumeUnit["Gigabytes"] = "gb";
38
38
  DataVolumeUnit["Terabytes"] = "tb";
39
39
  })(DataVolumeUnit = exports.DataVolumeUnit || (exports.DataVolumeUnit = {}));
40
+ var DurationUnit;
41
+ (function (DurationUnit) {
42
+ DurationUnit["Nanoseconds"] = "nanoseconds";
43
+ DurationUnit["Microseconds"] = "microseconds";
44
+ DurationUnit["Milliseconds"] = "milliseconds";
45
+ DurationUnit["Seconds"] = "seconds";
46
+ DurationUnit["Minutes"] = "minutes";
47
+ DurationUnit["Hours"] = "hours";
48
+ DurationUnit["Days"] = "days";
49
+ })(DurationUnit = exports.DurationUnit || (exports.DurationUnit = {}));
40
50
  //# sourceMappingURL=data_styles.js.map
@@ -0,0 +1,19 @@
1
+ import { DataColumn, Explore, Field } from '@malloydata/malloy';
2
+ import { HTMLTextRenderer } from './text';
3
+ import { DurationRenderOptions, DurationUnit, StyleDefaults } from '../data_styles';
4
+ import { RendererOptions } from '../renderer_types';
5
+ import { Renderer } from '../renderer';
6
+ import { RendererFactory } from '../renderer_factory';
7
+ export declare class HTMLDurationRenderer extends HTMLTextRenderer {
8
+ readonly options: DurationRenderOptions;
9
+ constructor(document: Document, options: DurationRenderOptions);
10
+ static multiplierMap: Map<DurationUnit, number>;
11
+ getText(data: DataColumn): string | null;
12
+ formatTimeUnit(value: number, unit: DurationUnit): string;
13
+ }
14
+ export declare class DurationRendererFactory extends RendererFactory<DurationRenderOptions> {
15
+ static readonly instance: DurationRendererFactory;
16
+ constructor();
17
+ create(document: Document, _styleDefaults: StyleDefaults, _rendererOptions: RendererOptions, _field: Field | Explore, options: DurationRenderOptions): Renderer;
18
+ get rendererName(): string;
19
+ }
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ /*
3
+ * Copyright 2023 Google LLC
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining
6
+ * a copy of this software and associated documentation files
7
+ * (the "Software"), to deal in the Software without restriction,
8
+ * including without limitation the rights to use, copy, modify, merge,
9
+ * publish, distribute, sublicense, and/or sell copies of the Software,
10
+ * and to permit persons to whom the Software is furnished to do so,
11
+ * subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be
14
+ * included in all copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23
+ */
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.DurationRendererFactory = exports.HTMLDurationRenderer = void 0;
26
+ const text_1 = require("./text");
27
+ const data_styles_1 = require("../data_styles");
28
+ const renderer_factory_1 = require("../renderer_factory");
29
+ class HTMLDurationRenderer extends text_1.HTMLTextRenderer {
30
+ constructor(document, options) {
31
+ super(document);
32
+ this.options = options;
33
+ }
34
+ getText(data) {
35
+ var _a;
36
+ if (data.isNull()) {
37
+ return null;
38
+ }
39
+ if (!data.isNumber()) {
40
+ throw new Error(`Cannot format field ${data.field.name} as a duration unit since its not a number`);
41
+ }
42
+ const targetUnit = (_a = this.options.duration_unit) !== null && _a !== void 0 ? _a : data_styles_1.DurationUnit.Seconds;
43
+ let currentDuration = data.number.value;
44
+ let currentUnitValue = 0;
45
+ let durationParts = [];
46
+ let foundUnit = false;
47
+ for (const [unit, multiplier] of HTMLDurationRenderer.multiplierMap) {
48
+ if (unit === targetUnit) {
49
+ foundUnit = true;
50
+ }
51
+ if (!foundUnit) {
52
+ continue;
53
+ }
54
+ currentUnitValue = currentDuration % multiplier;
55
+ currentDuration = Math.floor((currentDuration /= multiplier));
56
+ if (currentUnitValue > 0) {
57
+ durationParts = [
58
+ this.formatTimeUnit(currentUnitValue, unit),
59
+ ...durationParts,
60
+ ];
61
+ }
62
+ if (currentDuration === 0) {
63
+ break;
64
+ }
65
+ }
66
+ if (durationParts.length > 0) {
67
+ return durationParts.slice(0, 2).join(' ');
68
+ }
69
+ return this.formatTimeUnit(0, targetUnit);
70
+ }
71
+ formatTimeUnit(value, unit) {
72
+ let unitString = unit.toString();
73
+ if (value === 1) {
74
+ unitString = unitString.substring(0, unitString.length - 1);
75
+ }
76
+ return `${value} ${unitString}`;
77
+ }
78
+ }
79
+ exports.HTMLDurationRenderer = HTMLDurationRenderer;
80
+ // Map of unit to how many units of the make up the following time unit.
81
+ HTMLDurationRenderer.multiplierMap = new Map([
82
+ [data_styles_1.DurationUnit.Nanoseconds, 1000],
83
+ [data_styles_1.DurationUnit.Microseconds, 1000],
84
+ [data_styles_1.DurationUnit.Milliseconds, 1000],
85
+ [data_styles_1.DurationUnit.Seconds, 60],
86
+ [data_styles_1.DurationUnit.Minutes, 60],
87
+ [data_styles_1.DurationUnit.Hours, 24],
88
+ [data_styles_1.DurationUnit.Days, Number.MAX_VALUE],
89
+ ]);
90
+ class DurationRendererFactory extends renderer_factory_1.RendererFactory {
91
+ constructor() {
92
+ super();
93
+ this.addExtractor((options, value) => {
94
+ var _a;
95
+ options.duration_unit = (_a = value) !== null && _a !== void 0 ? _a : data_styles_1.DurationUnit.Seconds;
96
+ }, this.rendererName);
97
+ }
98
+ create(document, _styleDefaults, _rendererOptions, _field, options) {
99
+ return new HTMLDurationRenderer(document, options);
100
+ }
101
+ get rendererName() {
102
+ return 'duration';
103
+ }
104
+ }
105
+ exports.DurationRendererFactory = DurationRendererFactory;
106
+ DurationRendererFactory.instance = new DurationRendererFactory();
107
+ //# sourceMappingURL=duration.js.map
@@ -7,7 +7,6 @@ import { RendererFactory } from '../renderer_factory';
7
7
  export declare class HTMLNumberRenderer extends HTMLTextRenderer {
8
8
  readonly options: NumberRenderOptions;
9
9
  constructor(document: Document, options: NumberRenderOptions);
10
- getNumber(data: DataColumn): number | null;
11
10
  getText(data: DataColumn): string | null;
12
11
  }
13
12
  export declare class NumberRendererFactory extends RendererFactory<NumberRenderOptions> {
@@ -35,7 +35,7 @@ class HTMLNumberRenderer extends text_1.HTMLTextRenderer {
35
35
  super(document);
36
36
  this.options = options;
37
37
  }
38
- getNumber(data) {
38
+ getText(data) {
39
39
  if (data.isNull()) {
40
40
  return null;
41
41
  }
@@ -45,14 +45,10 @@ class HTMLNumberRenderer extends text_1.HTMLTextRenderer {
45
45
  }
46
46
  catch {
47
47
  // TODO: explore surfacing invalid format error, ignoring it for now.
48
- return data.number.value;
48
+ throw new Error(`Invalid value format: ${this.options.value_format}`);
49
49
  }
50
50
  }
51
- return data.number.value;
52
- }
53
- getText(data) {
54
- const num = this.getNumber(data);
55
- return num === null ? num : num.toLocaleString();
51
+ return data.number.value.toLocaleString();
56
52
  }
57
53
  }
58
54
  exports.HTMLNumberRenderer = HTMLNumberRenderer;
@@ -27,7 +27,10 @@ const number_1 = require("./number");
27
27
  const renderer_factory_1 = require("../renderer_factory");
28
28
  class HTMLPercentRenderer extends number_1.HTMLNumberRenderer {
29
29
  getText(data) {
30
- const num = this.getNumber(data);
30
+ if (data.isNull()) {
31
+ return null;
32
+ }
33
+ const num = data.number.value;
31
34
  return num === null
32
35
  ? num
33
36
  : (num * 100).toLocaleString('en', { maximumFractionDigits: 2 }) + '%';
package/dist/html/text.js CHANGED
@@ -38,7 +38,7 @@ class HTMLTextRenderer {
38
38
  text = this.getText(data);
39
39
  }
40
40
  catch (e) {
41
- (0, utils_1.createErrorElement)(this.document, e);
41
+ return (0, utils_1.createErrorElement)(this.document, e);
42
42
  }
43
43
  if (text === null) {
44
44
  return (0, utils_1.createNullElement)(this.document);
@@ -26,8 +26,9 @@ import { LinkRendererFactory } from './html/link';
26
26
  import { UnsupportedRendererFactory } from './html/unsupported';
27
27
  import { TextRendererFactory } from './html/text';
28
28
  import { DataVolumeRendererFactory } from './html/data_volume';
29
+ import { DurationRendererFactory } from './html/duration';
29
30
  export declare class MainRendererFactory {
30
- static renderFactories: (JSONRendererFactory | TextRendererFactory | NumberRendererFactory | ShapeMapRendererFactory | PointMapRendererFactory | ImageRendererFactory | SegmentMapRendererFactory | VegaRendererFactory | LineChartRendererFactory | SparkLineRendererFactory | BarChartRendererFactory | BarSparkLineRendererFactory | AreaSparkLineRendererFactory | ColumnSparkLineRendererFactory | ScatterChartRendererFactory | DateRendererFactory | CurrencyRendererFactory | PercentRendererFactory | BytesRendererFactory | BooleanRendererFactory | LinkRendererFactory | UnsupportedRendererFactory | DataVolumeRendererFactory)[];
31
+ static renderFactories: (JSONRendererFactory | TextRendererFactory | NumberRendererFactory | ShapeMapRendererFactory | PointMapRendererFactory | ImageRendererFactory | SegmentMapRendererFactory | VegaRendererFactory | LineChartRendererFactory | SparkLineRendererFactory | BarChartRendererFactory | BarSparkLineRendererFactory | AreaSparkLineRendererFactory | ColumnSparkLineRendererFactory | ScatterChartRendererFactory | DateRendererFactory | CurrencyRendererFactory | PercentRendererFactory | BytesRendererFactory | BooleanRendererFactory | LinkRendererFactory | UnsupportedRendererFactory | DataVolumeRendererFactory | DurationRendererFactory)[];
31
32
  create(renderDef: RenderDef | undefined, tags: MalloyTags | undefined, document: Document, styleDefaults: StyleDefaults, rendererOptions: RendererOptions, field: Field | Explore, timezone?: string | undefined): Renderer | undefined;
32
33
  matchesRenderDef(renderDef: RenderDef | undefined, factory: RendererFactory<DataRenderOptions>): boolean | "" | undefined;
33
34
  matchesTag(properties: MalloyTagProperties, factory: RendererFactory<DataRenderOptions>): string | boolean | undefined;
@@ -24,6 +24,7 @@ const link_1 = require("./html/link");
24
24
  const unsupported_1 = require("./html/unsupported");
25
25
  const text_1 = require("./html/text");
26
26
  const data_volume_1 = require("./html/data_volume");
27
+ const duration_1 = require("./html/duration");
27
28
  class MainRendererFactory {
28
29
  create(renderDef, tags, document, styleDefaults, rendererOptions, field, timezone) {
29
30
  var _a;
@@ -72,6 +73,7 @@ MainRendererFactory.renderFactories = [
72
73
  data_volume_1.DataVolumeRendererFactory.instance,
73
74
  bytes_1.BytesRendererFactory.instance,
74
75
  link_1.LinkRendererFactory.instance,
76
+ duration_1.DurationRendererFactory.instance,
75
77
  boolean_1.BooleanRendererFactory.instance,
76
78
  number_1.NumberRendererFactory.instance,
77
79
  unsupported_1.UnsupportedRendererFactory.instance,
@@ -2,7 +2,7 @@ import { Explore, Field, MalloyTagProperties } from '@malloydata/malloy';
2
2
  import { Renderer } from './renderer';
3
3
  import { DataRenderOptions, RenderDef, StyleDefaults } from './data_styles';
4
4
  import { RendererOptions } from './renderer_types';
5
- declare type TagPropertyExtractor<T extends DataRenderOptions> = (options: T, extractedValue: string) => void;
5
+ declare type TagPropertyExtractor<T extends DataRenderOptions> = (options: T, extractedValue: string | undefined) => void;
6
6
  export declare abstract class RendererFactory<T extends DataRenderOptions> {
7
7
  readonly tagOptionExtractors: Record<string, TagPropertyExtractor<T>>;
8
8
  protected addExtractor(extractor: TagPropertyExtractor<T>, ...tags: string[]): void;
@@ -23,9 +23,13 @@ class RendererFactory {
23
23
  parseTagParameters(tagProperties) {
24
24
  const options = {};
25
25
  for (const tag in this.tagOptionExtractors) {
26
- if (tagProperties[tag] !== undefined &&
27
- typeof tagProperties[tag] === 'string')
28
- this.tagOptionExtractors[tag](options, tagProperties[tag]);
26
+ let tagValue = undefined;
27
+ if (tagProperties[tag]) {
28
+ if (typeof tagProperties[tag] === 'string') {
29
+ tagValue = tagProperties[tag];
30
+ }
31
+ this.tagOptionExtractors[tag](options, tagValue);
32
+ }
29
33
  }
30
34
  return options;
31
35
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@malloydata/render",
3
- "version": "0.0.56-dev230710183220",
3
+ "version": "0.0.56-dev230710205111",
4
4
  "license": "MIT",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -18,7 +18,7 @@
18
18
  "prepublishOnly": "npm run build"
19
19
  },
20
20
  "dependencies": {
21
- "@malloydata/malloy": "^0.0.56-dev230710183220",
21
+ "@malloydata/malloy": "^0.0.56-dev230710205111",
22
22
  "@types/luxon": "^2.4.0",
23
23
  "lodash": "^4.17.20",
24
24
  "luxon": "^2.4.0",