@itwin/core-quantity 4.0.0-dev.52 → 4.0.0-dev.55

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 (66) hide show
  1. package/lib/cjs/Constants.d.ts +27 -27
  2. package/lib/cjs/Constants.js +52 -52
  3. package/lib/cjs/Constants.js.map +1 -1
  4. package/lib/cjs/Exception.d.ts +26 -26
  5. package/lib/cjs/Exception.js +38 -38
  6. package/lib/cjs/Formatter/Format.d.ts +91 -91
  7. package/lib/cjs/Formatter/Format.js +328 -328
  8. package/lib/cjs/Formatter/FormatEnums.d.ts +133 -133
  9. package/lib/cjs/Formatter/FormatEnums.js +318 -318
  10. package/lib/cjs/Formatter/Formatter.d.ts +44 -44
  11. package/lib/cjs/Formatter/Formatter.js +371 -371
  12. package/lib/cjs/Formatter/Formatter.js.map +1 -1
  13. package/lib/cjs/Formatter/FormatterSpec.d.ts +39 -39
  14. package/lib/cjs/Formatter/FormatterSpec.js +101 -101
  15. package/lib/cjs/Formatter/Interfaces.d.ts +62 -62
  16. package/lib/cjs/Formatter/Interfaces.js +17 -17
  17. package/lib/cjs/Interfaces.d.ts +86 -86
  18. package/lib/cjs/Interfaces.d.ts.map +1 -1
  19. package/lib/cjs/Interfaces.js +9 -9
  20. package/lib/cjs/Parser.d.ts +93 -93
  21. package/lib/cjs/Parser.d.ts.map +1 -1
  22. package/lib/cjs/Parser.js +592 -592
  23. package/lib/cjs/Parser.js.map +1 -1
  24. package/lib/cjs/ParserSpec.d.ts +34 -34
  25. package/lib/cjs/ParserSpec.js +47 -47
  26. package/lib/cjs/Quantity.d.ts +27 -27
  27. package/lib/cjs/Quantity.js +46 -46
  28. package/lib/cjs/Quantity.js.map +1 -1
  29. package/lib/cjs/Unit.d.ts +25 -25
  30. package/lib/cjs/Unit.js +44 -44
  31. package/lib/cjs/core-quantity.d.ts +19 -19
  32. package/lib/cjs/core-quantity.js +39 -35
  33. package/lib/cjs/core-quantity.js.map +1 -1
  34. package/lib/esm/Constants.d.ts +27 -27
  35. package/lib/esm/Constants.js +49 -48
  36. package/lib/esm/Constants.js.map +1 -1
  37. package/lib/esm/Exception.d.ts +26 -26
  38. package/lib/esm/Exception.js +34 -34
  39. package/lib/esm/Formatter/Format.d.ts +91 -91
  40. package/lib/esm/Formatter/Format.js +323 -323
  41. package/lib/esm/Formatter/FormatEnums.d.ts +133 -133
  42. package/lib/esm/Formatter/FormatEnums.js +302 -302
  43. package/lib/esm/Formatter/Formatter.d.ts +44 -44
  44. package/lib/esm/Formatter/Formatter.js +368 -367
  45. package/lib/esm/Formatter/Formatter.js.map +1 -1
  46. package/lib/esm/Formatter/FormatterSpec.d.ts +39 -39
  47. package/lib/esm/Formatter/FormatterSpec.js +97 -97
  48. package/lib/esm/Formatter/Interfaces.d.ts +62 -62
  49. package/lib/esm/Formatter/Interfaces.js +13 -13
  50. package/lib/esm/Interfaces.d.ts +86 -86
  51. package/lib/esm/Interfaces.d.ts.map +1 -1
  52. package/lib/esm/Interfaces.js +8 -8
  53. package/lib/esm/Parser.d.ts +93 -93
  54. package/lib/esm/Parser.d.ts.map +1 -1
  55. package/lib/esm/Parser.js +589 -588
  56. package/lib/esm/Parser.js.map +1 -1
  57. package/lib/esm/ParserSpec.d.ts +34 -34
  58. package/lib/esm/ParserSpec.js +43 -43
  59. package/lib/esm/Quantity.d.ts +27 -27
  60. package/lib/esm/Quantity.js +42 -42
  61. package/lib/esm/Quantity.js.map +1 -1
  62. package/lib/esm/Unit.d.ts +25 -25
  63. package/lib/esm/Unit.js +39 -39
  64. package/lib/esm/core-quantity.d.ts +19 -19
  65. package/lib/esm/core-quantity.js +23 -23
  66. package/package.json +6 -6
@@ -1,329 +1,329 @@
1
- "use strict";
2
- /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
6
- /** @packageDocumentation
7
- * @module Quantity
8
- */
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.Format = exports.BaseFormat = void 0;
11
- const Constants_1 = require("../Constants");
12
- const Exception_1 = require("../Exception");
13
- const FormatEnums_1 = require("./FormatEnums");
14
- const Interfaces_1 = require("./Interfaces");
15
- // cSpell:ignore ZERONORMALIZED, nosign, onlynegative, signalways, negativeparentheses
16
- // cSpell:ignore trailzeroes, keepsinglezero, zeroempty, keepdecimalpoint, applyrounding, fractiondash, showunitlabel, prependunitlabel, exponentonlynegative
17
- /** A base Format class with shared properties and functionality between quantity and ecschema-metadata Format classes
18
- * @beta
19
- */
20
- class BaseFormat {
21
- constructor(name) {
22
- this._name = "";
23
- this._roundFactor = 0.0;
24
- this._type = FormatEnums_1.FormatType.Decimal; // required; options are decimal, fractional, scientific, station
25
- this._precision = FormatEnums_1.DecimalPrecision.Six; // required
26
- this._showSignOption = FormatEnums_1.ShowSignOption.OnlyNegative; // options: noSign, onlyNegative, signAlways, negativeParentheses
27
- this._decimalSeparator = Constants_1.QuantityConstants.LocaleSpecificDecimalSeparator;
28
- this._thousandSeparator = Constants_1.QuantityConstants.LocaleSpecificThousandSeparator;
29
- this._uomSeparator = " "; // optional; default is " "; defined separator between magnitude and the unit
30
- this._stationSeparator = "+"; // optional; default is "+"
31
- this._formatTraits = FormatEnums_1.FormatTraits.Uninitialized;
32
- this._spacer = " "; // optional; default is " "
33
- this._includeZero = true; // optional; default is true
34
- this._name = name;
35
- }
36
- get name() { return this._name; }
37
- get roundFactor() { return this._roundFactor; }
38
- set roundFactor(roundFactor) { this._roundFactor = roundFactor; }
39
- get type() { return this._type; }
40
- set type(formatType) { this._type = formatType; }
41
- get precision() { return this._precision; }
42
- set precision(precision) { this._precision = precision; }
43
- get minWidth() { return this._minWidth; }
44
- set minWidth(minWidth) { this._minWidth = minWidth; }
45
- get scientificType() { return this._scientificType; }
46
- set scientificType(scientificType) { this._scientificType = scientificType; }
47
- get showSignOption() { return this._showSignOption; }
48
- set showSignOption(showSignOption) { this._showSignOption = showSignOption; }
49
- get decimalSeparator() { return this._decimalSeparator; }
50
- set decimalSeparator(decimalSeparator) { this._decimalSeparator = decimalSeparator; }
51
- get thousandSeparator() { return this._thousandSeparator; }
52
- set thousandSeparator(thousandSeparator) { this._thousandSeparator = thousandSeparator; }
53
- get uomSeparator() { return this._uomSeparator; }
54
- set uomSeparator(uomSeparator) { this._uomSeparator = uomSeparator; }
55
- get stationSeparator() { return this._stationSeparator; }
56
- set stationSeparator(stationSeparator) { this._stationSeparator = stationSeparator; }
57
- get stationOffsetSize() { return this._stationOffsetSize; }
58
- set stationOffsetSize(stationOffsetSize) { stationOffsetSize = this._stationOffsetSize = stationOffsetSize; }
59
- get formatTraits() { return this._formatTraits; }
60
- set formatTraits(formatTraits) { this._formatTraits = formatTraits; }
61
- get spacer() { return this._spacer; }
62
- set spacer(spacer) { this._spacer = spacer ?? this._spacer; }
63
- get includeZero() { return this._includeZero; }
64
- set includeZero(includeZero) { this._includeZero = includeZero ?? this._includeZero; }
65
- /** This method parses input string that is typically extracted for persisted JSON data and validates that the string is a valid FormatType. Throws exception if not valid. */
66
- parseFormatTraits(formatTraitsFromJson) {
67
- const formatTraits = (Array.isArray(formatTraitsFromJson)) ? formatTraitsFromJson : formatTraitsFromJson.split(/,|;|\|/);
68
- formatTraits.forEach((formatTraitsString) => {
69
- const formatTrait = (0, FormatEnums_1.parseFormatTrait)(formatTraitsString, this.name);
70
- this._formatTraits = this.formatTraits | formatTrait;
71
- });
72
- }
73
- /** This method returns true if the formatTrait is set in this Format object. */
74
- hasFormatTraitSet(formatTrait) {
75
- return (this._formatTraits & formatTrait) === formatTrait;
76
- }
77
- loadFormatProperties(formatProps) {
78
- this._type = (0, FormatEnums_1.parseFormatType)(formatProps.type, this.name);
79
- if (formatProps.precision !== undefined) {
80
- if (!Number.isInteger(formatProps.precision)) // mut be an integer
81
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'precision' attribute. It should be an integer.`);
82
- this._precision = (0, FormatEnums_1.parsePrecision)(formatProps.precision, this._type, this.name);
83
- }
84
- if (this.type === FormatEnums_1.FormatType.Scientific) {
85
- if (undefined === formatProps.scientificType) // if format type is scientific and scientific type is undefined, throw
86
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} is 'Scientific' type therefore the attribute 'scientificType' is required.`);
87
- this._scientificType = (0, FormatEnums_1.parseScientificType)(formatProps.scientificType, this.name);
88
- }
89
- if (undefined !== formatProps.roundFactor) { // optional; default is 0.0
90
- if (typeof (formatProps.roundFactor) !== "number")
91
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'roundFactor' attribute. It should be of type 'number'.`);
92
- if (formatProps.roundFactor !== this.roundFactor) // if roundFactor isn't default value of 0.0, reassign roundFactor variable
93
- this._roundFactor = formatProps.roundFactor;
94
- }
95
- if (undefined !== formatProps.minWidth) { // optional
96
- if (!Number.isInteger(formatProps.minWidth) || formatProps.minWidth < 0) // must be a positive int
97
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'minWidth' attribute. It should be a positive integer.`);
98
- this._minWidth = formatProps.minWidth;
99
- }
100
- if (FormatEnums_1.FormatType.Station === this.type) {
101
- if (undefined === formatProps.stationOffsetSize)
102
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} is 'Station' type therefore the attribute 'stationOffsetSize' is required.`);
103
- if (!Number.isInteger(formatProps.stationOffsetSize) || formatProps.stationOffsetSize < 0) // must be a positive int > 0
104
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'stationOffsetSize' attribute. It should be a positive integer.`);
105
- this._stationOffsetSize = formatProps.stationOffsetSize;
106
- }
107
- if (undefined !== formatProps.showSignOption) { // optional; default is "onlyNegative"
108
- this._showSignOption = (0, FormatEnums_1.parseShowSignOption)(formatProps.showSignOption, this.name);
109
- }
110
- if (undefined !== formatProps.formatTraits && formatProps.formatTraits.length !== 0) { // FormatTraits is optional
111
- if (!Array.isArray(formatProps.formatTraits) && typeof (formatProps.formatTraits) !== "string") // must be either an array of strings or a string
112
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'formatTraits' attribute. It should be of type 'string' or 'string[]'.`);
113
- this.parseFormatTraits(formatProps.formatTraits); // check that all of the options for formatTraits are valid. If now, throw
114
- }
115
- if (undefined !== formatProps.decimalSeparator) { // optional
116
- if (typeof (formatProps.decimalSeparator) !== "string") // not a string or not a one character string
117
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'decimalSeparator' attribute. It should be of type 'string'.`);
118
- if (formatProps.decimalSeparator.length > 1)
119
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'decimalSeparator' attribute. It should be an empty or one character string.`);
120
- this._decimalSeparator = formatProps.decimalSeparator;
121
- }
122
- if (undefined !== formatProps.thousandSeparator) { // optional
123
- if (typeof (formatProps.thousandSeparator) !== "string")
124
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'thousandSeparator' attribute. It should be of type 'string'.`);
125
- if (formatProps.thousandSeparator.length > 1)
126
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'thousandSeparator' attribute. It should be an empty or one character string.`);
127
- this._thousandSeparator = formatProps.thousandSeparator;
128
- }
129
- if (undefined !== formatProps.uomSeparator) { // optional; default is " "
130
- if (typeof (formatProps.uomSeparator) !== "string")
131
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'uomSeparator' attribute. It should be of type 'string'.`);
132
- if (formatProps.uomSeparator.length < 0 || formatProps.uomSeparator.length > 1)
133
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'uomSeparator' attribute. It should be an empty or one character string.`);
134
- this._uomSeparator = formatProps.uomSeparator;
135
- }
136
- if (undefined !== formatProps.stationSeparator) { // optional; default is "+"
137
- if (typeof (formatProps.stationSeparator) !== "string")
138
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'stationSeparator' attribute. It should be of type 'string'.`);
139
- if (formatProps.stationSeparator.length > 1)
140
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'stationSeparator' attribute. It should be an empty or one character string.`);
141
- this._stationSeparator = formatProps.stationSeparator;
142
- }
143
- }
144
- }
145
- exports.BaseFormat = BaseFormat;
146
- /** A class used to define the specifications for formatting quantity values. This class is typically loaded by reading [[FormatProps]].
147
- * @beta
148
- */
149
- class Format extends BaseFormat {
150
- /** Constructor
151
- * @param name The name of a format specification. TODO: make optional or remove
152
- */
153
- constructor(name) {
154
- super(name);
155
- }
156
- get units() { return this._units; }
157
- get hasUnits() { return this._units !== undefined && this._units.length > 0; }
158
- get customProps() { return this._customProps; }
159
- static isFormatTraitSetInProps(formatProps, trait) {
160
- if (!formatProps.formatTraits)
161
- return false;
162
- const formatTraits = Array.isArray(formatProps.formatTraits) ? formatProps.formatTraits : formatProps.formatTraits.split(/,|;|\|/);
163
- const traitStr = (0, FormatEnums_1.getTraitString)(trait);
164
- return formatTraits.find((traitEntry) => traitStr === traitEntry) ? true : false;
165
- }
166
- async createUnit(unitsProvider, name, label) {
167
- if (name === undefined || typeof (name) !== "string" || (label !== undefined && typeof (label) !== "string")) // throws if name is undefined or name isn't a string or if label is defined and isn't a string
168
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `This Composite has a unit with an invalid 'name' or 'label' attribute.`);
169
- for (const unit of this.units) {
170
- const unitObj = unit[0].name;
171
- if (unitObj.toLowerCase() === name.toLowerCase()) // duplicate names are not allowed
172
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The unit ${unitObj} has a duplicate name.`);
173
- }
174
- const newUnit = await unitsProvider.findUnitByName(name);
175
- if (!newUnit || !newUnit.isValid)
176
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `Invalid unit name '${name}'.`);
177
- this.units.push([newUnit, label]);
178
- }
179
- /**
180
- * Clone Format
181
- */
182
- clone(options) {
183
- const newFormat = new Format(this.name);
184
- newFormat._roundFactor = this._roundFactor;
185
- newFormat._type = this._type;
186
- newFormat._precision = this._precision;
187
- newFormat._minWidth = this._minWidth;
188
- newFormat._scientificType = this._scientificType;
189
- newFormat._showSignOption = this._showSignOption;
190
- newFormat._decimalSeparator = this._decimalSeparator;
191
- newFormat._thousandSeparator = this._thousandSeparator;
192
- newFormat._uomSeparator = this._uomSeparator;
193
- newFormat._stationSeparator = this._stationSeparator;
194
- newFormat._stationOffsetSize = this._stationOffsetSize;
195
- newFormat._formatTraits = this._formatTraits;
196
- newFormat._spacer = this._spacer;
197
- newFormat._includeZero = this._includeZero;
198
- newFormat._customProps = this._customProps;
199
- this._units && (newFormat._units = [...this._units]);
200
- if (newFormat._units) {
201
- if (options?.showOnlyPrimaryUnit) {
202
- if (newFormat._units.length > 1)
203
- newFormat._units.length = 1;
204
- }
205
- }
206
- if (undefined !== options?.traits)
207
- newFormat._formatTraits = options?.traits;
208
- if (undefined !== options?.type)
209
- newFormat._type = options.type;
210
- if (undefined !== options?.precision) {
211
- // ensure specified precision is valid
212
- const precision = (0, FormatEnums_1.parsePrecision)(options?.precision, newFormat._type, newFormat.name);
213
- newFormat._precision = precision;
214
- }
215
- if (undefined !== options?.primaryUnit) {
216
- if (options.primaryUnit.unit) {
217
- const newUnits = new Array();
218
- newUnits.push([options.primaryUnit.unit, options.primaryUnit.label]);
219
- newFormat._units = newUnits;
220
- }
221
- else if (options.primaryUnit.label && newFormat._units?.length) {
222
- // update label only
223
- newFormat._units[0][1] = options.primaryUnit.label;
224
- }
225
- }
226
- return newFormat;
227
- }
228
- /**
229
- * Populates this Format with the values from the provided.
230
- */
231
- async fromJSON(unitsProvider, jsonObj) {
232
- this.loadFormatProperties(jsonObj);
233
- if ((0, Interfaces_1.isCustomFormatProps)(jsonObj))
234
- this._customProps = jsonObj.custom;
235
- if (undefined !== jsonObj.composite) { // optional
236
- this._units = new Array();
237
- if (jsonObj.composite.includeZero !== undefined) {
238
- if (typeof (jsonObj.composite.includeZero) !== "boolean") // includeZero must be a boolean IF it is defined
239
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has a Composite with an invalid 'includeZero' attribute. It should be of type 'boolean'.`);
240
- this._includeZero = jsonObj.composite.includeZero;
241
- }
242
- if (jsonObj.composite.spacer !== undefined) { // spacer must be a string IF it is defined
243
- if (typeof (jsonObj.composite.spacer) !== "string")
244
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has a Composite with an invalid 'spacer' attribute. It must be of type 'string'.`);
245
- if (jsonObj.composite.spacer.length > 1)
246
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has a Composite with an invalid 'spacer' attribute. It should be an empty or one character string.`);
247
- this._spacer = jsonObj.composite.spacer;
248
- }
249
- if (jsonObj.composite.units !== undefined) { // if composite is defined, it must be an array with 1-4 units
250
- if (!Array.isArray(jsonObj.composite.units)) { // must be an array
251
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has a Composite with an invalid 'units' attribute. It must be of type 'array'`);
252
- }
253
- if (jsonObj.composite.units.length > 0 && jsonObj.composite.units.length <= 4) { // Composite requires 1-4 units
254
- try {
255
- const createUnitPromises = [];
256
- for (const unit of jsonObj.composite.units) {
257
- createUnitPromises.push(this.createUnit(unitsProvider, unit.name, unit.label));
258
- }
259
- await Promise.all(createUnitPromises);
260
- }
261
- catch (e) {
262
- throw e;
263
- }
264
- }
265
- }
266
- if (undefined === this.units || this.units.length === 0)
267
- throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has a Composite with no valid 'units'`);
268
- }
269
- }
270
- /** Create a Format from FormatProps */
271
- static async createFromJSON(name, unitsProvider, formatProps) {
272
- const actualFormat = new Format(name);
273
- await actualFormat.fromJSON(unitsProvider, formatProps);
274
- return actualFormat;
275
- }
276
- /**
277
- * Returns a JSON object that contain the specification for this Format.
278
- */
279
- toJSON() {
280
- let composite;
281
- if (this.units) {
282
- const units = this.units.map((value) => {
283
- if (undefined !== value[1])
284
- return { name: value[0].name, label: value[1] };
285
- else
286
- return { name: value[0].name };
287
- });
288
- composite = {
289
- spacer: this.spacer,
290
- includeZero: this.includeZero,
291
- units,
292
- };
293
- }
294
- if (this.customProps)
295
- return {
296
- type: (0, FormatEnums_1.formatTypeToString)(this.type),
297
- precision: this.precision,
298
- roundFactor: this.roundFactor,
299
- minWidth: this.minWidth,
300
- showSignOption: (0, FormatEnums_1.showSignOptionToString)(this.showSignOption),
301
- formatTraits: (0, FormatEnums_1.formatTraitsToArray)(this.formatTraits),
302
- decimalSeparator: this.decimalSeparator,
303
- thousandSeparator: this.thousandSeparator,
304
- uomSeparator: this.uomSeparator,
305
- scientificType: this.scientificType ? (0, FormatEnums_1.scientificTypeToString)(this.scientificType) : undefined,
306
- stationOffsetSize: this.stationOffsetSize,
307
- stationSeparator: this.stationSeparator,
308
- composite,
309
- custom: this.customProps,
310
- };
311
- return {
312
- type: (0, FormatEnums_1.formatTypeToString)(this.type),
313
- precision: this.precision,
314
- roundFactor: this.roundFactor,
315
- minWidth: this.minWidth,
316
- showSignOption: (0, FormatEnums_1.showSignOptionToString)(this.showSignOption),
317
- formatTraits: (0, FormatEnums_1.formatTraitsToArray)(this.formatTraits),
318
- decimalSeparator: this.decimalSeparator,
319
- thousandSeparator: this.thousandSeparator,
320
- uomSeparator: this.uomSeparator,
321
- scientificType: this.scientificType ? (0, FormatEnums_1.scientificTypeToString)(this.scientificType) : undefined,
322
- stationOffsetSize: this.stationOffsetSize,
323
- stationSeparator: this.stationSeparator,
324
- composite,
325
- };
326
- }
327
- }
328
- exports.Format = Format;
1
+ "use strict";
2
+ /*---------------------------------------------------------------------------------------------
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
+ /** @packageDocumentation
7
+ * @module Quantity
8
+ */
9
+ Object.defineProperty(exports, "__esModule", { value: true });
10
+ exports.Format = exports.BaseFormat = void 0;
11
+ const Constants_1 = require("../Constants");
12
+ const Exception_1 = require("../Exception");
13
+ const FormatEnums_1 = require("./FormatEnums");
14
+ const Interfaces_1 = require("./Interfaces");
15
+ // cSpell:ignore ZERONORMALIZED, nosign, onlynegative, signalways, negativeparentheses
16
+ // cSpell:ignore trailzeroes, keepsinglezero, zeroempty, keepdecimalpoint, applyrounding, fractiondash, showunitlabel, prependunitlabel, exponentonlynegative
17
+ /** A base Format class with shared properties and functionality between quantity and ecschema-metadata Format classes
18
+ * @beta
19
+ */
20
+ class BaseFormat {
21
+ constructor(name) {
22
+ this._name = "";
23
+ this._roundFactor = 0.0;
24
+ this._type = FormatEnums_1.FormatType.Decimal; // required; options are decimal, fractional, scientific, station
25
+ this._precision = FormatEnums_1.DecimalPrecision.Six; // required
26
+ this._showSignOption = FormatEnums_1.ShowSignOption.OnlyNegative; // options: noSign, onlyNegative, signAlways, negativeParentheses
27
+ this._decimalSeparator = Constants_1.QuantityConstants.LocaleSpecificDecimalSeparator;
28
+ this._thousandSeparator = Constants_1.QuantityConstants.LocaleSpecificThousandSeparator;
29
+ this._uomSeparator = " "; // optional; default is " "; defined separator between magnitude and the unit
30
+ this._stationSeparator = "+"; // optional; default is "+"
31
+ this._formatTraits = FormatEnums_1.FormatTraits.Uninitialized;
32
+ this._spacer = " "; // optional; default is " "
33
+ this._includeZero = true; // optional; default is true
34
+ this._name = name;
35
+ }
36
+ get name() { return this._name; }
37
+ get roundFactor() { return this._roundFactor; }
38
+ set roundFactor(roundFactor) { this._roundFactor = roundFactor; }
39
+ get type() { return this._type; }
40
+ set type(formatType) { this._type = formatType; }
41
+ get precision() { return this._precision; }
42
+ set precision(precision) { this._precision = precision; }
43
+ get minWidth() { return this._minWidth; }
44
+ set minWidth(minWidth) { this._minWidth = minWidth; }
45
+ get scientificType() { return this._scientificType; }
46
+ set scientificType(scientificType) { this._scientificType = scientificType; }
47
+ get showSignOption() { return this._showSignOption; }
48
+ set showSignOption(showSignOption) { this._showSignOption = showSignOption; }
49
+ get decimalSeparator() { return this._decimalSeparator; }
50
+ set decimalSeparator(decimalSeparator) { this._decimalSeparator = decimalSeparator; }
51
+ get thousandSeparator() { return this._thousandSeparator; }
52
+ set thousandSeparator(thousandSeparator) { this._thousandSeparator = thousandSeparator; }
53
+ get uomSeparator() { return this._uomSeparator; }
54
+ set uomSeparator(uomSeparator) { this._uomSeparator = uomSeparator; }
55
+ get stationSeparator() { return this._stationSeparator; }
56
+ set stationSeparator(stationSeparator) { this._stationSeparator = stationSeparator; }
57
+ get stationOffsetSize() { return this._stationOffsetSize; }
58
+ set stationOffsetSize(stationOffsetSize) { stationOffsetSize = this._stationOffsetSize = stationOffsetSize; }
59
+ get formatTraits() { return this._formatTraits; }
60
+ set formatTraits(formatTraits) { this._formatTraits = formatTraits; }
61
+ get spacer() { return this._spacer; }
62
+ set spacer(spacer) { this._spacer = spacer ?? this._spacer; }
63
+ get includeZero() { return this._includeZero; }
64
+ set includeZero(includeZero) { this._includeZero = includeZero ?? this._includeZero; }
65
+ /** This method parses input string that is typically extracted for persisted JSON data and validates that the string is a valid FormatType. Throws exception if not valid. */
66
+ parseFormatTraits(formatTraitsFromJson) {
67
+ const formatTraits = (Array.isArray(formatTraitsFromJson)) ? formatTraitsFromJson : formatTraitsFromJson.split(/,|;|\|/);
68
+ formatTraits.forEach((formatTraitsString) => {
69
+ const formatTrait = (0, FormatEnums_1.parseFormatTrait)(formatTraitsString, this.name);
70
+ this._formatTraits = this.formatTraits | formatTrait;
71
+ });
72
+ }
73
+ /** This method returns true if the formatTrait is set in this Format object. */
74
+ hasFormatTraitSet(formatTrait) {
75
+ return (this._formatTraits & formatTrait) === formatTrait;
76
+ }
77
+ loadFormatProperties(formatProps) {
78
+ this._type = (0, FormatEnums_1.parseFormatType)(formatProps.type, this.name);
79
+ if (formatProps.precision !== undefined) {
80
+ if (!Number.isInteger(formatProps.precision)) // mut be an integer
81
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'precision' attribute. It should be an integer.`);
82
+ this._precision = (0, FormatEnums_1.parsePrecision)(formatProps.precision, this._type, this.name);
83
+ }
84
+ if (this.type === FormatEnums_1.FormatType.Scientific) {
85
+ if (undefined === formatProps.scientificType) // if format type is scientific and scientific type is undefined, throw
86
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} is 'Scientific' type therefore the attribute 'scientificType' is required.`);
87
+ this._scientificType = (0, FormatEnums_1.parseScientificType)(formatProps.scientificType, this.name);
88
+ }
89
+ if (undefined !== formatProps.roundFactor) { // optional; default is 0.0
90
+ if (typeof (formatProps.roundFactor) !== "number")
91
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'roundFactor' attribute. It should be of type 'number'.`);
92
+ if (formatProps.roundFactor !== this.roundFactor) // if roundFactor isn't default value of 0.0, reassign roundFactor variable
93
+ this._roundFactor = formatProps.roundFactor;
94
+ }
95
+ if (undefined !== formatProps.minWidth) { // optional
96
+ if (!Number.isInteger(formatProps.minWidth) || formatProps.minWidth < 0) // must be a positive int
97
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'minWidth' attribute. It should be a positive integer.`);
98
+ this._minWidth = formatProps.minWidth;
99
+ }
100
+ if (FormatEnums_1.FormatType.Station === this.type) {
101
+ if (undefined === formatProps.stationOffsetSize)
102
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} is 'Station' type therefore the attribute 'stationOffsetSize' is required.`);
103
+ if (!Number.isInteger(formatProps.stationOffsetSize) || formatProps.stationOffsetSize < 0) // must be a positive int > 0
104
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'stationOffsetSize' attribute. It should be a positive integer.`);
105
+ this._stationOffsetSize = formatProps.stationOffsetSize;
106
+ }
107
+ if (undefined !== formatProps.showSignOption) { // optional; default is "onlyNegative"
108
+ this._showSignOption = (0, FormatEnums_1.parseShowSignOption)(formatProps.showSignOption, this.name);
109
+ }
110
+ if (undefined !== formatProps.formatTraits && formatProps.formatTraits.length !== 0) { // FormatTraits is optional
111
+ if (!Array.isArray(formatProps.formatTraits) && typeof (formatProps.formatTraits) !== "string") // must be either an array of strings or a string
112
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'formatTraits' attribute. It should be of type 'string' or 'string[]'.`);
113
+ this.parseFormatTraits(formatProps.formatTraits); // check that all of the options for formatTraits are valid. If now, throw
114
+ }
115
+ if (undefined !== formatProps.decimalSeparator) { // optional
116
+ if (typeof (formatProps.decimalSeparator) !== "string") // not a string or not a one character string
117
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'decimalSeparator' attribute. It should be of type 'string'.`);
118
+ if (formatProps.decimalSeparator.length > 1)
119
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'decimalSeparator' attribute. It should be an empty or one character string.`);
120
+ this._decimalSeparator = formatProps.decimalSeparator;
121
+ }
122
+ if (undefined !== formatProps.thousandSeparator) { // optional
123
+ if (typeof (formatProps.thousandSeparator) !== "string")
124
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'thousandSeparator' attribute. It should be of type 'string'.`);
125
+ if (formatProps.thousandSeparator.length > 1)
126
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'thousandSeparator' attribute. It should be an empty or one character string.`);
127
+ this._thousandSeparator = formatProps.thousandSeparator;
128
+ }
129
+ if (undefined !== formatProps.uomSeparator) { // optional; default is " "
130
+ if (typeof (formatProps.uomSeparator) !== "string")
131
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'uomSeparator' attribute. It should be of type 'string'.`);
132
+ if (formatProps.uomSeparator.length < 0 || formatProps.uomSeparator.length > 1)
133
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'uomSeparator' attribute. It should be an empty or one character string.`);
134
+ this._uomSeparator = formatProps.uomSeparator;
135
+ }
136
+ if (undefined !== formatProps.stationSeparator) { // optional; default is "+"
137
+ if (typeof (formatProps.stationSeparator) !== "string")
138
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'stationSeparator' attribute. It should be of type 'string'.`);
139
+ if (formatProps.stationSeparator.length > 1)
140
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has an invalid 'stationSeparator' attribute. It should be an empty or one character string.`);
141
+ this._stationSeparator = formatProps.stationSeparator;
142
+ }
143
+ }
144
+ }
145
+ exports.BaseFormat = BaseFormat;
146
+ /** A class used to define the specifications for formatting quantity values. This class is typically loaded by reading [[FormatProps]].
147
+ * @beta
148
+ */
149
+ class Format extends BaseFormat {
150
+ /** Constructor
151
+ * @param name The name of a format specification. TODO: make optional or remove
152
+ */
153
+ constructor(name) {
154
+ super(name);
155
+ }
156
+ get units() { return this._units; }
157
+ get hasUnits() { return this._units !== undefined && this._units.length > 0; }
158
+ get customProps() { return this._customProps; }
159
+ static isFormatTraitSetInProps(formatProps, trait) {
160
+ if (!formatProps.formatTraits)
161
+ return false;
162
+ const formatTraits = Array.isArray(formatProps.formatTraits) ? formatProps.formatTraits : formatProps.formatTraits.split(/,|;|\|/);
163
+ const traitStr = (0, FormatEnums_1.getTraitString)(trait);
164
+ return formatTraits.find((traitEntry) => traitStr === traitEntry) ? true : false;
165
+ }
166
+ async createUnit(unitsProvider, name, label) {
167
+ if (name === undefined || typeof (name) !== "string" || (label !== undefined && typeof (label) !== "string")) // throws if name is undefined or name isn't a string or if label is defined and isn't a string
168
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `This Composite has a unit with an invalid 'name' or 'label' attribute.`);
169
+ for (const unit of this.units) {
170
+ const unitObj = unit[0].name;
171
+ if (unitObj.toLowerCase() === name.toLowerCase()) // duplicate names are not allowed
172
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The unit ${unitObj} has a duplicate name.`);
173
+ }
174
+ const newUnit = await unitsProvider.findUnitByName(name);
175
+ if (!newUnit || !newUnit.isValid)
176
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `Invalid unit name '${name}'.`);
177
+ this.units.push([newUnit, label]);
178
+ }
179
+ /**
180
+ * Clone Format
181
+ */
182
+ clone(options) {
183
+ const newFormat = new Format(this.name);
184
+ newFormat._roundFactor = this._roundFactor;
185
+ newFormat._type = this._type;
186
+ newFormat._precision = this._precision;
187
+ newFormat._minWidth = this._minWidth;
188
+ newFormat._scientificType = this._scientificType;
189
+ newFormat._showSignOption = this._showSignOption;
190
+ newFormat._decimalSeparator = this._decimalSeparator;
191
+ newFormat._thousandSeparator = this._thousandSeparator;
192
+ newFormat._uomSeparator = this._uomSeparator;
193
+ newFormat._stationSeparator = this._stationSeparator;
194
+ newFormat._stationOffsetSize = this._stationOffsetSize;
195
+ newFormat._formatTraits = this._formatTraits;
196
+ newFormat._spacer = this._spacer;
197
+ newFormat._includeZero = this._includeZero;
198
+ newFormat._customProps = this._customProps;
199
+ this._units && (newFormat._units = [...this._units]);
200
+ if (newFormat._units) {
201
+ if (options?.showOnlyPrimaryUnit) {
202
+ if (newFormat._units.length > 1)
203
+ newFormat._units.length = 1;
204
+ }
205
+ }
206
+ if (undefined !== options?.traits)
207
+ newFormat._formatTraits = options?.traits;
208
+ if (undefined !== options?.type)
209
+ newFormat._type = options.type;
210
+ if (undefined !== options?.precision) {
211
+ // ensure specified precision is valid
212
+ const precision = (0, FormatEnums_1.parsePrecision)(options?.precision, newFormat._type, newFormat.name);
213
+ newFormat._precision = precision;
214
+ }
215
+ if (undefined !== options?.primaryUnit) {
216
+ if (options.primaryUnit.unit) {
217
+ const newUnits = new Array();
218
+ newUnits.push([options.primaryUnit.unit, options.primaryUnit.label]);
219
+ newFormat._units = newUnits;
220
+ }
221
+ else if (options.primaryUnit.label && newFormat._units?.length) {
222
+ // update label only
223
+ newFormat._units[0][1] = options.primaryUnit.label;
224
+ }
225
+ }
226
+ return newFormat;
227
+ }
228
+ /**
229
+ * Populates this Format with the values from the provided.
230
+ */
231
+ async fromJSON(unitsProvider, jsonObj) {
232
+ this.loadFormatProperties(jsonObj);
233
+ if ((0, Interfaces_1.isCustomFormatProps)(jsonObj))
234
+ this._customProps = jsonObj.custom;
235
+ if (undefined !== jsonObj.composite) { // optional
236
+ this._units = new Array();
237
+ if (jsonObj.composite.includeZero !== undefined) {
238
+ if (typeof (jsonObj.composite.includeZero) !== "boolean") // includeZero must be a boolean IF it is defined
239
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has a Composite with an invalid 'includeZero' attribute. It should be of type 'boolean'.`);
240
+ this._includeZero = jsonObj.composite.includeZero;
241
+ }
242
+ if (jsonObj.composite.spacer !== undefined) { // spacer must be a string IF it is defined
243
+ if (typeof (jsonObj.composite.spacer) !== "string")
244
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has a Composite with an invalid 'spacer' attribute. It must be of type 'string'.`);
245
+ if (jsonObj.composite.spacer.length > 1)
246
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has a Composite with an invalid 'spacer' attribute. It should be an empty or one character string.`);
247
+ this._spacer = jsonObj.composite.spacer;
248
+ }
249
+ if (jsonObj.composite.units !== undefined) { // if composite is defined, it must be an array with 1-4 units
250
+ if (!Array.isArray(jsonObj.composite.units)) { // must be an array
251
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has a Composite with an invalid 'units' attribute. It must be of type 'array'`);
252
+ }
253
+ if (jsonObj.composite.units.length > 0 && jsonObj.composite.units.length <= 4) { // Composite requires 1-4 units
254
+ try {
255
+ const createUnitPromises = [];
256
+ for (const unit of jsonObj.composite.units) {
257
+ createUnitPromises.push(this.createUnit(unitsProvider, unit.name, unit.label));
258
+ }
259
+ await Promise.all(createUnitPromises);
260
+ }
261
+ catch (e) {
262
+ throw e;
263
+ }
264
+ }
265
+ }
266
+ if (undefined === this.units || this.units.length === 0)
267
+ throw new Exception_1.QuantityError(Exception_1.QuantityStatus.InvalidJson, `The Format ${this.name} has a Composite with no valid 'units'`);
268
+ }
269
+ }
270
+ /** Create a Format from FormatProps */
271
+ static async createFromJSON(name, unitsProvider, formatProps) {
272
+ const actualFormat = new Format(name);
273
+ await actualFormat.fromJSON(unitsProvider, formatProps);
274
+ return actualFormat;
275
+ }
276
+ /**
277
+ * Returns a JSON object that contain the specification for this Format.
278
+ */
279
+ toJSON() {
280
+ let composite;
281
+ if (this.units) {
282
+ const units = this.units.map((value) => {
283
+ if (undefined !== value[1])
284
+ return { name: value[0].name, label: value[1] };
285
+ else
286
+ return { name: value[0].name };
287
+ });
288
+ composite = {
289
+ spacer: this.spacer,
290
+ includeZero: this.includeZero,
291
+ units,
292
+ };
293
+ }
294
+ if (this.customProps)
295
+ return {
296
+ type: (0, FormatEnums_1.formatTypeToString)(this.type),
297
+ precision: this.precision,
298
+ roundFactor: this.roundFactor,
299
+ minWidth: this.minWidth,
300
+ showSignOption: (0, FormatEnums_1.showSignOptionToString)(this.showSignOption),
301
+ formatTraits: (0, FormatEnums_1.formatTraitsToArray)(this.formatTraits),
302
+ decimalSeparator: this.decimalSeparator,
303
+ thousandSeparator: this.thousandSeparator,
304
+ uomSeparator: this.uomSeparator,
305
+ scientificType: this.scientificType ? (0, FormatEnums_1.scientificTypeToString)(this.scientificType) : undefined,
306
+ stationOffsetSize: this.stationOffsetSize,
307
+ stationSeparator: this.stationSeparator,
308
+ composite,
309
+ custom: this.customProps,
310
+ };
311
+ return {
312
+ type: (0, FormatEnums_1.formatTypeToString)(this.type),
313
+ precision: this.precision,
314
+ roundFactor: this.roundFactor,
315
+ minWidth: this.minWidth,
316
+ showSignOption: (0, FormatEnums_1.showSignOptionToString)(this.showSignOption),
317
+ formatTraits: (0, FormatEnums_1.formatTraitsToArray)(this.formatTraits),
318
+ decimalSeparator: this.decimalSeparator,
319
+ thousandSeparator: this.thousandSeparator,
320
+ uomSeparator: this.uomSeparator,
321
+ scientificType: this.scientificType ? (0, FormatEnums_1.scientificTypeToString)(this.scientificType) : undefined,
322
+ stationOffsetSize: this.stationOffsetSize,
323
+ stationSeparator: this.stationSeparator,
324
+ composite,
325
+ };
326
+ }
327
+ }
328
+ exports.Format = Format;
329
329
  //# sourceMappingURL=Format.js.map