@itwin/core-quantity 4.0.0-dev.44 → 4.0.0-dev.48
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.
- package/lib/cjs/Constants.d.ts +27 -27
- package/lib/cjs/Constants.js +52 -52
- package/lib/cjs/Constants.js.map +1 -1
- package/lib/cjs/Exception.d.ts +26 -26
- package/lib/cjs/Exception.js +38 -38
- package/lib/cjs/Exception.js.map +1 -1
- package/lib/cjs/Formatter/Format.d.ts +91 -91
- package/lib/cjs/Formatter/Format.js +328 -328
- package/lib/cjs/Formatter/Format.js.map +1 -1
- package/lib/cjs/Formatter/FormatEnums.d.ts +133 -133
- package/lib/cjs/Formatter/FormatEnums.js +318 -318
- package/lib/cjs/Formatter/FormatEnums.js.map +1 -1
- package/lib/cjs/Formatter/Formatter.d.ts +44 -44
- package/lib/cjs/Formatter/Formatter.js +371 -371
- package/lib/cjs/Formatter/Formatter.js.map +1 -1
- package/lib/cjs/Formatter/FormatterSpec.d.ts +39 -39
- package/lib/cjs/Formatter/FormatterSpec.js +101 -101
- package/lib/cjs/Formatter/FormatterSpec.js.map +1 -1
- package/lib/cjs/Formatter/Interfaces.d.ts +62 -62
- package/lib/cjs/Formatter/Interfaces.js +17 -17
- package/lib/cjs/Formatter/Interfaces.js.map +1 -1
- package/lib/cjs/Interfaces.d.ts +86 -86
- package/lib/cjs/Interfaces.js +9 -9
- package/lib/cjs/Interfaces.js.map +1 -1
- package/lib/cjs/Parser.d.ts +93 -93
- package/lib/cjs/Parser.js +592 -592
- package/lib/cjs/Parser.js.map +1 -1
- package/lib/cjs/ParserSpec.d.ts +34 -34
- package/lib/cjs/ParserSpec.js +47 -47
- package/lib/cjs/ParserSpec.js.map +1 -1
- package/lib/cjs/Quantity.d.ts +27 -27
- package/lib/cjs/Quantity.js +46 -46
- package/lib/cjs/Quantity.js.map +1 -1
- package/lib/cjs/Unit.d.ts +25 -25
- package/lib/cjs/Unit.js +44 -44
- package/lib/cjs/Unit.js.map +1 -1
- package/lib/cjs/core-quantity.d.ts +19 -19
- package/lib/cjs/core-quantity.js +35 -35
- package/lib/cjs/core-quantity.js.map +1 -1
- package/lib/esm/Constants.d.ts +27 -27
- package/lib/esm/Constants.js +48 -48
- package/lib/esm/Constants.js.map +1 -1
- package/lib/esm/Exception.d.ts +26 -26
- package/lib/esm/Exception.js +34 -34
- package/lib/esm/Exception.js.map +1 -1
- package/lib/esm/Formatter/Format.d.ts +91 -91
- package/lib/esm/Formatter/Format.js +323 -323
- package/lib/esm/Formatter/Format.js.map +1 -1
- package/lib/esm/Formatter/FormatEnums.d.ts +133 -133
- package/lib/esm/Formatter/FormatEnums.js +302 -302
- package/lib/esm/Formatter/FormatEnums.js.map +1 -1
- package/lib/esm/Formatter/Formatter.d.ts +44 -44
- package/lib/esm/Formatter/Formatter.js +367 -367
- package/lib/esm/Formatter/Formatter.js.map +1 -1
- package/lib/esm/Formatter/FormatterSpec.d.ts +39 -39
- package/lib/esm/Formatter/FormatterSpec.js +97 -97
- package/lib/esm/Formatter/FormatterSpec.js.map +1 -1
- package/lib/esm/Formatter/Interfaces.d.ts +62 -62
- package/lib/esm/Formatter/Interfaces.js +13 -13
- package/lib/esm/Formatter/Interfaces.js.map +1 -1
- package/lib/esm/Interfaces.d.ts +86 -86
- package/lib/esm/Interfaces.js +8 -8
- package/lib/esm/Interfaces.js.map +1 -1
- package/lib/esm/Parser.d.ts +93 -93
- package/lib/esm/Parser.js +588 -588
- package/lib/esm/Parser.js.map +1 -1
- package/lib/esm/ParserSpec.d.ts +34 -34
- package/lib/esm/ParserSpec.js +43 -43
- package/lib/esm/ParserSpec.js.map +1 -1
- package/lib/esm/Quantity.d.ts +27 -27
- package/lib/esm/Quantity.js +42 -42
- package/lib/esm/Quantity.js.map +1 -1
- package/lib/esm/Unit.d.ts +25 -25
- package/lib/esm/Unit.js +39 -39
- package/lib/esm/Unit.js.map +1 -1
- package/lib/esm/core-quantity.d.ts +19 -19
- package/lib/esm/core-quantity.js +23 -23
- package/lib/esm/core-quantity.js.map +1 -1
- package/package.json +5 -5
package/lib/esm/Parser.js
CHANGED
|
@@ -1,589 +1,589 @@
|
|
|
1
|
-
/*---------------------------------------------------------------------------------------------
|
|
2
|
-
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
3
|
-
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
4
|
-
*--------------------------------------------------------------------------------------------*/
|
|
5
|
-
/** @packageDocumentation
|
|
6
|
-
* @module Quantity
|
|
7
|
-
*/
|
|
8
|
-
import { QuantityConstants } from "./Constants";
|
|
9
|
-
import { FormatTraits, FormatType } from "./Formatter/FormatEnums";
|
|
10
|
-
import { Quantity } from "./Quantity";
|
|
11
|
-
/** Possible parser errors
|
|
12
|
-
* @beta
|
|
13
|
-
*/
|
|
14
|
-
export var ParseError;
|
|
15
|
-
(function (ParseError) {
|
|
16
|
-
ParseError[ParseError["UnableToGenerateParseTokens"] = 1] = "UnableToGenerateParseTokens";
|
|
17
|
-
ParseError[ParseError["NoValueOrUnitFoundInString"] = 2] = "NoValueOrUnitFoundInString";
|
|
18
|
-
ParseError[ParseError["UnitLabelSuppliedButNotMatched"] = 3] = "UnitLabelSuppliedButNotMatched";
|
|
19
|
-
ParseError[ParseError["UnknownUnit"] = 4] = "UnknownUnit";
|
|
20
|
-
ParseError[ParseError["UnableToConvertParseTokensToQuantity"] = 5] = "UnableToConvertParseTokensToQuantity";
|
|
21
|
-
ParseError[ParseError["InvalidParserSpec"] = 6] = "InvalidParserSpec";
|
|
22
|
-
})(ParseError || (ParseError = {}));
|
|
23
|
-
/** A ParseToken holds either a numeric or string token extracted from a string that represents a quantity value.
|
|
24
|
-
* @beta
|
|
25
|
-
*/
|
|
26
|
-
class ParseToken {
|
|
27
|
-
constructor(value) {
|
|
28
|
-
if (typeof value === "string")
|
|
29
|
-
this.value = value.trim();
|
|
30
|
-
else
|
|
31
|
-
this.value = value;
|
|
32
|
-
}
|
|
33
|
-
get isString() { return typeof this.value === "string"; }
|
|
34
|
-
get isNumber() { return typeof this.value === "number"; }
|
|
35
|
-
}
|
|
36
|
-
/** A ScientificToken holds an index and string representing the exponent.
|
|
37
|
-
* @beta
|
|
38
|
-
*/
|
|
39
|
-
class ScientificToken {
|
|
40
|
-
constructor(index, exponent) {
|
|
41
|
-
this.exponent = "";
|
|
42
|
-
this.index = index;
|
|
43
|
-
if (exponent)
|
|
44
|
-
this.exponent = exponent;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
/** A FractionToken holds an index and the fraction value of numerator / denominator.
|
|
48
|
-
* @beta
|
|
49
|
-
*/
|
|
50
|
-
class FractionToken {
|
|
51
|
-
constructor(index, fraction) {
|
|
52
|
-
this.fraction = 0.0;
|
|
53
|
-
this.exponent = "";
|
|
54
|
-
this.index = index;
|
|
55
|
-
if (fraction)
|
|
56
|
-
this.fraction = fraction;
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
/** A Parser class that is used to break a string that represents a quantity value into tokens.
|
|
60
|
-
* @beta
|
|
61
|
-
*/
|
|
62
|
-
export class Parser {
|
|
63
|
-
static isParsedQuantity(item) {
|
|
64
|
-
return item.ok;
|
|
65
|
-
}
|
|
66
|
-
static isParseError(item) {
|
|
67
|
-
return !item.ok;
|
|
68
|
-
}
|
|
69
|
-
static checkForScientificNotation(index, stringToParse, uomSeparatorToIgnore) {
|
|
70
|
-
let exponentString = "";
|
|
71
|
-
let i = index + 1;
|
|
72
|
-
for (; i < stringToParse.length; i++) {
|
|
73
|
-
const charCode = stringToParse.charCodeAt(i);
|
|
74
|
-
if (Parser.isDigit(charCode) || ((charCode === QuantityConstants.CHAR_MINUS || charCode === QuantityConstants.CHAR_PLUS) && (i === (index + 1)))) {
|
|
75
|
-
exponentString = exponentString.concat(stringToParse[i]);
|
|
76
|
-
}
|
|
77
|
-
else {
|
|
78
|
-
i = uomSeparatorToIgnore === charCode ? i : i - 1;
|
|
79
|
-
break;
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
if (exponentString.length > 1 || ((exponentString.length === 1) && (exponentString.charCodeAt(0) !== QuantityConstants.CHAR_MINUS) && (exponentString.charCodeAt(0) !== QuantityConstants.CHAR_PLUS)))
|
|
83
|
-
return new ScientificToken(i, exponentString);
|
|
84
|
-
return new ScientificToken(index);
|
|
85
|
-
}
|
|
86
|
-
static checkForFractions(index, stringToParse, uomSeparatorToIgnore, numeratorStr) {
|
|
87
|
-
let numeratorToken = "";
|
|
88
|
-
let denominatorToken = "";
|
|
89
|
-
let processingNumerator = true;
|
|
90
|
-
let i = index;
|
|
91
|
-
if (numeratorStr && numeratorStr.length > 0) {
|
|
92
|
-
numeratorToken = numeratorStr;
|
|
93
|
-
processingNumerator = false;
|
|
94
|
-
}
|
|
95
|
-
for (; i < stringToParse.length; i++) {
|
|
96
|
-
const charCode = stringToParse.charCodeAt(i);
|
|
97
|
-
if (Parser.isDigit(charCode)) {
|
|
98
|
-
if (processingNumerator) {
|
|
99
|
-
numeratorToken = numeratorToken.concat(stringToParse[i]);
|
|
100
|
-
}
|
|
101
|
-
else {
|
|
102
|
-
denominatorToken = denominatorToken.concat(stringToParse[i]);
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
else {
|
|
106
|
-
if (processingNumerator && (charCode === QuantityConstants.CHAR_SLASH || charCode === QuantityConstants.CHAR_DIVISION_SLASH || charCode === QuantityConstants.CHAR_DIVISION_SLASH)) {
|
|
107
|
-
processingNumerator = false;
|
|
108
|
-
}
|
|
109
|
-
else {
|
|
110
|
-
if (uomSeparatorToIgnore !== charCode)
|
|
111
|
-
i = i - 1; // skip over uom separator after fraction
|
|
112
|
-
break;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (numeratorToken.length > 0 && denominatorToken.length > 0) {
|
|
117
|
-
const numerator = parseInt(numeratorToken, 10);
|
|
118
|
-
const denominator = parseInt(denominatorToken, 10);
|
|
119
|
-
if (denominator > 0)
|
|
120
|
-
return new FractionToken(i, numerator / denominator);
|
|
121
|
-
return new FractionToken(i);
|
|
122
|
-
}
|
|
123
|
-
return new FractionToken(index + 1);
|
|
124
|
-
}
|
|
125
|
-
static isDigit(charCode) {
|
|
126
|
-
return (charCode >= QuantityConstants.CHAR_DIGIT_ZERO) && (charCode <= QuantityConstants.CHAR_DIGIT_NINE);
|
|
127
|
-
}
|
|
128
|
-
static isDigitOrDecimalSeparator(charCode, format) {
|
|
129
|
-
return (charCode === format.decimalSeparator.charCodeAt(0)) || Parser.isDigit(charCode);
|
|
130
|
-
}
|
|
131
|
-
/** Parse the quantity string and return and array of ParseTokens that represent the component invariant values and unit labels.
|
|
132
|
-
* @param quantitySpecification The quantity string to ba parsed.
|
|
133
|
-
*/
|
|
134
|
-
static parseQuantitySpecification(quantitySpecification, format) {
|
|
135
|
-
const tokens = [];
|
|
136
|
-
const str = quantitySpecification.trim();
|
|
137
|
-
let processingNumber = false;
|
|
138
|
-
let wipToken = "";
|
|
139
|
-
let signToken = "";
|
|
140
|
-
let uomSeparatorToIgnore = 0;
|
|
141
|
-
let fractionDashCode = 0;
|
|
142
|
-
const skipCodes = [format.thousandSeparator.charCodeAt(0)];
|
|
143
|
-
if (format.type === FormatType.Station && format.stationSeparator && format.stationSeparator.length === 1)
|
|
144
|
-
skipCodes.push(format.stationSeparator.charCodeAt(0));
|
|
145
|
-
if (format.type === FormatType.Fractional && format.hasFormatTraitSet(FormatTraits.FractionDash)) {
|
|
146
|
-
fractionDashCode = QuantityConstants.CHAR_MINUS;
|
|
147
|
-
}
|
|
148
|
-
if (format.uomSeparator && format.uomSeparator !== " " && format.uomSeparator.length === 1) {
|
|
149
|
-
uomSeparatorToIgnore = format.uomSeparator.charCodeAt(0);
|
|
150
|
-
skipCodes.push(uomSeparatorToIgnore);
|
|
151
|
-
}
|
|
152
|
-
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
|
153
|
-
for (let i = 0; i < str.length; i++) {
|
|
154
|
-
const charCode = str.charCodeAt(i);
|
|
155
|
-
if (Parser.isDigitOrDecimalSeparator(charCode, format)) {
|
|
156
|
-
if (!processingNumber) {
|
|
157
|
-
if (wipToken.length > 0) {
|
|
158
|
-
tokens.push(new ParseToken(wipToken));
|
|
159
|
-
wipToken = "";
|
|
160
|
-
}
|
|
161
|
-
processingNumber = true;
|
|
162
|
-
}
|
|
163
|
-
wipToken = wipToken.concat(str[i]);
|
|
164
|
-
}
|
|
165
|
-
else {
|
|
166
|
-
if (processingNumber) {
|
|
167
|
-
if (charCode === QuantityConstants.CHAR_SLASH || charCode === QuantityConstants.CHAR_FRACTION_SLASH || charCode === QuantityConstants.CHAR_DIVISION_SLASH) {
|
|
168
|
-
const fractSymbol = Parser.checkForFractions(i + 1, str, uomSeparatorToIgnore, wipToken);
|
|
169
|
-
let fraction = fractSymbol.fraction;
|
|
170
|
-
i = fractSymbol.index;
|
|
171
|
-
if (fractSymbol.fraction !== 0.0) {
|
|
172
|
-
wipToken = "";
|
|
173
|
-
if (signToken.length > 0) {
|
|
174
|
-
if (signToken === "-")
|
|
175
|
-
fraction = 0 - fraction;
|
|
176
|
-
signToken = "";
|
|
177
|
-
}
|
|
178
|
-
tokens.push(new ParseToken(fraction));
|
|
179
|
-
processingNumber = false;
|
|
180
|
-
continue;
|
|
181
|
-
}
|
|
182
|
-
}
|
|
183
|
-
else {
|
|
184
|
-
// a space may signify end of number or start of decimal
|
|
185
|
-
if (charCode === QuantityConstants.CHAR_SPACE || charCode === fractionDashCode) {
|
|
186
|
-
const fractSymbol = Parser.checkForFractions(i + 1, str, uomSeparatorToIgnore);
|
|
187
|
-
let fraction = fractSymbol.fraction;
|
|
188
|
-
if (fractSymbol.fraction !== 0.0) {
|
|
189
|
-
i = fractSymbol.index;
|
|
190
|
-
if (signToken.length > 0) {
|
|
191
|
-
wipToken = signToken + wipToken;
|
|
192
|
-
if (signToken === "-")
|
|
193
|
-
fraction = 0 - fraction;
|
|
194
|
-
signToken = "";
|
|
195
|
-
}
|
|
196
|
-
const valueWithFraction = parseFloat(wipToken) + fraction;
|
|
197
|
-
tokens.push(new ParseToken(valueWithFraction));
|
|
198
|
-
processingNumber = false;
|
|
199
|
-
wipToken = "";
|
|
200
|
-
}
|
|
201
|
-
continue;
|
|
202
|
-
}
|
|
203
|
-
else {
|
|
204
|
-
// an "E" or "e" may signify scientific notation
|
|
205
|
-
if (charCode === QuantityConstants.CHAR_UPPER_E || charCode === QuantityConstants.CHAR_LOWER_E) {
|
|
206
|
-
const exponentSymbol = Parser.checkForScientificNotation(i, str, uomSeparatorToIgnore);
|
|
207
|
-
i = exponentSymbol.index;
|
|
208
|
-
if (exponentSymbol.exponent && exponentSymbol.exponent.length > 0) {
|
|
209
|
-
if (signToken.length > 0) {
|
|
210
|
-
wipToken = signToken + wipToken;
|
|
211
|
-
signToken = "";
|
|
212
|
-
}
|
|
213
|
-
wipToken = `${wipToken}e${exponentSymbol.exponent}`;
|
|
214
|
-
const scientificValue = Number(wipToken);
|
|
215
|
-
tokens.push(new ParseToken(scientificValue));
|
|
216
|
-
processingNumber = false;
|
|
217
|
-
wipToken = "";
|
|
218
|
-
continue;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
// ignore any codes in skipCodes
|
|
224
|
-
if (skipCodes.findIndex((ref) => ref === charCode) !== -1)
|
|
225
|
-
continue;
|
|
226
|
-
if (signToken.length > 0) {
|
|
227
|
-
wipToken = signToken + wipToken;
|
|
228
|
-
signToken = "";
|
|
229
|
-
}
|
|
230
|
-
tokens.push(new ParseToken(parseFloat(wipToken)));
|
|
231
|
-
wipToken = (i < str.length) ? str[i] : "";
|
|
232
|
-
processingNumber = false;
|
|
233
|
-
}
|
|
234
|
-
else {
|
|
235
|
-
// not processing a number
|
|
236
|
-
if ((charCode === QuantityConstants.CHAR_PLUS || charCode === QuantityConstants.CHAR_MINUS)) {
|
|
237
|
-
if (0 === tokens.length) // sign token only needed for left most value
|
|
238
|
-
signToken = str[i];
|
|
239
|
-
continue;
|
|
240
|
-
}
|
|
241
|
-
wipToken = wipToken.concat(str[i]);
|
|
242
|
-
}
|
|
243
|
-
}
|
|
244
|
-
}
|
|
245
|
-
// handle case where end of input string is reached.
|
|
246
|
-
if (wipToken.length > 0) {
|
|
247
|
-
if (processingNumber) {
|
|
248
|
-
if (signToken.length > 0) {
|
|
249
|
-
wipToken = signToken + wipToken;
|
|
250
|
-
}
|
|
251
|
-
tokens.push(new ParseToken(parseFloat(wipToken)));
|
|
252
|
-
}
|
|
253
|
-
else {
|
|
254
|
-
tokens.push(new ParseToken(wipToken));
|
|
255
|
-
}
|
|
256
|
-
}
|
|
257
|
-
return tokens;
|
|
258
|
-
}
|
|
259
|
-
static async lookupUnitByLabel(unitLabel, format, unitsProvider, altUnitLabelsProvider) {
|
|
260
|
-
const defaultUnit = format.units && format.units.length > 0 ? format.units[0][0] : undefined;
|
|
261
|
-
const labelToFind = unitLabel.toLowerCase();
|
|
262
|
-
// First look in format for a label and matches
|
|
263
|
-
if (format.units && format.units.length > 0) {
|
|
264
|
-
const formatUnit = format.units.find(([unit, label]) => {
|
|
265
|
-
if (label && label.toLowerCase() === labelToFind)
|
|
266
|
-
return true;
|
|
267
|
-
const alternateLabels = altUnitLabelsProvider?.getAlternateUnitLabels(unit);
|
|
268
|
-
// check any alternate labels that may be defined for the Unit
|
|
269
|
-
if (alternateLabels && alternateLabels.find((lbl) => lbl.toLowerCase() === labelToFind))
|
|
270
|
-
return true;
|
|
271
|
-
return false;
|
|
272
|
-
});
|
|
273
|
-
if (formatUnit)
|
|
274
|
-
return formatUnit[0];
|
|
275
|
-
}
|
|
276
|
-
// now try to find a unit from the same family and system
|
|
277
|
-
let foundUnit = await unitsProvider.findUnit(unitLabel, defaultUnit ? defaultUnit.phenomenon : undefined, defaultUnit ? defaultUnit.system : undefined);
|
|
278
|
-
// if nothing found yet just limit to family
|
|
279
|
-
if (!foundUnit.isValid && defaultUnit)
|
|
280
|
-
foundUnit = await unitsProvider.findUnit(unitLabel, defaultUnit ? defaultUnit.phenomenon : undefined);
|
|
281
|
-
return foundUnit;
|
|
282
|
-
}
|
|
283
|
-
static async createQuantityFromParseTokens(tokens, format, unitsProvider, altUnitLabelsProvider) {
|
|
284
|
-
let defaultUnit = format.units && format.units.length > 0 ? format.units[0][0] : undefined;
|
|
285
|
-
// common case where single value is supplied
|
|
286
|
-
if (tokens.length === 1) {
|
|
287
|
-
if (tokens[0].isNumber) {
|
|
288
|
-
return new Quantity(defaultUnit, tokens[0].value);
|
|
289
|
-
}
|
|
290
|
-
else {
|
|
291
|
-
const unit = await this.lookupUnitByLabel(tokens[0].value, format, unitsProvider, altUnitLabelsProvider);
|
|
292
|
-
return new Quantity(unit);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
// common case where single value and single label are supplied
|
|
296
|
-
if (tokens.length === 2) {
|
|
297
|
-
if (tokens[0].isNumber && tokens[1].isString) {
|
|
298
|
-
const unit = await this.lookupUnitByLabel(tokens[1].value, format, unitsProvider, altUnitLabelsProvider);
|
|
299
|
-
if (undefined === defaultUnit)
|
|
300
|
-
defaultUnit = unit;
|
|
301
|
-
if (defaultUnit && defaultUnit.name === unit.name) {
|
|
302
|
-
return new Quantity(defaultUnit, tokens[0].value);
|
|
303
|
-
}
|
|
304
|
-
else if (defaultUnit) {
|
|
305
|
-
const conversion = await unitsProvider.getConversion(unit, defaultUnit);
|
|
306
|
-
const mag = ((tokens[0].value * conversion.factor)) + conversion.offset;
|
|
307
|
-
return new Quantity(defaultUnit, mag);
|
|
308
|
-
}
|
|
309
|
-
}
|
|
310
|
-
else { // unit specification comes before value (like currency)
|
|
311
|
-
if (tokens[1].isNumber && tokens[0].isString) {
|
|
312
|
-
const unit = await this.lookupUnitByLabel(tokens[0].value, format, unitsProvider, altUnitLabelsProvider);
|
|
313
|
-
if (undefined === defaultUnit)
|
|
314
|
-
defaultUnit = unit;
|
|
315
|
-
if (defaultUnit && defaultUnit.name === unit.name) {
|
|
316
|
-
return new Quantity(defaultUnit, tokens[1].value);
|
|
317
|
-
}
|
|
318
|
-
else if (defaultUnit) {
|
|
319
|
-
const conversion = await unitsProvider.getConversion(unit, defaultUnit);
|
|
320
|
-
const mag = ((tokens[1].value * conversion.factor)) + conversion.offset;
|
|
321
|
-
return new Quantity(defaultUnit, mag);
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
// common case where there are multiple value/label pairs
|
|
327
|
-
if (tokens.length % 2 === 0) {
|
|
328
|
-
let mag = 0.0;
|
|
329
|
-
for (let i = 0; i < tokens.length; i = i + 2) {
|
|
330
|
-
if (tokens[i].isNumber && tokens[i + 1].isString) {
|
|
331
|
-
const value = tokens[i].value;
|
|
332
|
-
const unit = await this.lookupUnitByLabel(tokens[i + 1].value, format, unitsProvider, altUnitLabelsProvider);
|
|
333
|
-
if (undefined === defaultUnit)
|
|
334
|
-
defaultUnit = unit;
|
|
335
|
-
if (0 === i) {
|
|
336
|
-
if (defaultUnit.name === unit.name)
|
|
337
|
-
mag = value;
|
|
338
|
-
else {
|
|
339
|
-
const conversion = await unitsProvider.getConversion(unit, defaultUnit);
|
|
340
|
-
mag = ((value * conversion.factor)) + conversion.offset;
|
|
341
|
-
}
|
|
342
|
-
}
|
|
343
|
-
else {
|
|
344
|
-
if (defaultUnit) {
|
|
345
|
-
const conversion = await unitsProvider.getConversion(unit, defaultUnit);
|
|
346
|
-
if (mag < 0.0)
|
|
347
|
-
mag = mag - ((value * conversion.factor)) + conversion.offset;
|
|
348
|
-
else
|
|
349
|
-
mag = mag + ((value * conversion.factor)) + conversion.offset;
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
}
|
|
353
|
-
}
|
|
354
|
-
return new Quantity(defaultUnit, mag);
|
|
355
|
-
}
|
|
356
|
-
return new Quantity(defaultUnit);
|
|
357
|
-
}
|
|
358
|
-
/** Async method to generate a Quantity given a string that represents a quantity value and likely a unit label.
|
|
359
|
-
* @param inString A string that contains text represent a quantity.
|
|
360
|
-
* @param format Defines the likely format of inString.
|
|
361
|
-
* @param unitsProvider required to look up units that may be specified in inString
|
|
362
|
-
*/
|
|
363
|
-
static async parseIntoQuantity(inString, format, unitsProvider, altUnitLabelsProvider) {
|
|
364
|
-
const tokens = Parser.parseQuantitySpecification(inString, format);
|
|
365
|
-
if (tokens.length === 0)
|
|
366
|
-
return new Quantity();
|
|
367
|
-
return Parser.createQuantityFromParseTokens(tokens, format, unitsProvider, altUnitLabelsProvider);
|
|
368
|
-
}
|
|
369
|
-
/** method to get the Unit Conversion given a unit label */
|
|
370
|
-
static tryFindUnitConversion(unitLabel, unitsConversions, preferredUnit) {
|
|
371
|
-
if (unitsConversions.length > 0) {
|
|
372
|
-
const label = unitLabel.toLocaleLowerCase();
|
|
373
|
-
/* A preferred unit is used to target a unit if a unit label is used in more that one unit definition from the same unit family.
|
|
374
|
-
* An example is if "ft" is used as the unitLabel and the preferredUnit is "SURVEY_FT" since that unit has an alternate label of "ft" the
|
|
375
|
-
* conversion to "SURVEY_FT" is returned. If no preferredUnit is specified then the unit "FT" would likely to have been found first.
|
|
376
|
-
* If "in" is the unit label and "SURVEY_FT" is the preferredUnit then conversion to "SURVEY_IN" would be returned.
|
|
377
|
-
*/
|
|
378
|
-
if (preferredUnit) {
|
|
379
|
-
// if there is a preferred unit defined see if unit label matched it or one of its alternates
|
|
380
|
-
const preferredConversion = unitsConversions.find((conversion) => conversion.name === preferredUnit.name);
|
|
381
|
-
if (preferredConversion && preferredConversion.parseLabels) {
|
|
382
|
-
if (-1 !== preferredConversion.parseLabels.findIndex((lbl) => lbl === label))
|
|
383
|
-
return preferredConversion.conversion;
|
|
384
|
-
}
|
|
385
|
-
// see if we can find a matching unitLabel in any unit within the same system as the preferred unit
|
|
386
|
-
const preferredSystemConversions = unitsConversions.filter((conversion) => conversion.system === preferredUnit.system);
|
|
387
|
-
for (const conversion of preferredSystemConversions) {
|
|
388
|
-
if (conversion.parseLabels) {
|
|
389
|
-
if (-1 !== conversion.parseLabels.findIndex((lbl) => lbl === label))
|
|
390
|
-
return conversion.conversion;
|
|
391
|
-
}
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
// if no unit found based on preferredUnit see if an unit label matches
|
|
395
|
-
for (const conversion of unitsConversions) {
|
|
396
|
-
if (conversion.parseLabels) {
|
|
397
|
-
if (-1 !== conversion.parseLabels.findIndex((lbl) => lbl === label))
|
|
398
|
-
return conversion.conversion;
|
|
399
|
-
}
|
|
400
|
-
else {
|
|
401
|
-
// eslint-disable-next-line no-console
|
|
402
|
-
console.log("ERROR: Parser expects to find parseLabels array populate with all possible unit labels for the unit.");
|
|
403
|
-
}
|
|
404
|
-
}
|
|
405
|
-
}
|
|
406
|
-
return undefined;
|
|
407
|
-
}
|
|
408
|
-
static getQuantityValueFromParseTokens(tokens, format, unitsConversions) {
|
|
409
|
-
const defaultUnit = format.units && format.units.length > 0 ? format.units[0][0] : undefined;
|
|
410
|
-
// common case where single value is supplied
|
|
411
|
-
if (tokens.length === 1) {
|
|
412
|
-
if (tokens[0].isNumber) {
|
|
413
|
-
if (defaultUnit) {
|
|
414
|
-
const conversion = Parser.tryFindUnitConversion(defaultUnit.label, unitsConversions, defaultUnit);
|
|
415
|
-
if (conversion) {
|
|
416
|
-
const value = tokens[0].value * conversion.factor + conversion.offset;
|
|
417
|
-
return { ok: true, value };
|
|
418
|
-
}
|
|
419
|
-
}
|
|
420
|
-
else {
|
|
421
|
-
// if no conversion or no defaultUnit, just return parsed number
|
|
422
|
-
return { ok: true, value: tokens[0].value };
|
|
423
|
-
}
|
|
424
|
-
}
|
|
425
|
-
else {
|
|
426
|
-
// only the unit label was specified so assume magnitude of 1
|
|
427
|
-
const conversion = Parser.tryFindUnitConversion(tokens[0].value, unitsConversions, defaultUnit);
|
|
428
|
-
if (undefined !== conversion)
|
|
429
|
-
return { ok: true, value: conversion.factor + conversion.offset };
|
|
430
|
-
else
|
|
431
|
-
return { ok: false, error: ParseError.NoValueOrUnitFoundInString };
|
|
432
|
-
}
|
|
433
|
-
}
|
|
434
|
-
// common case where single value and single label are supplied
|
|
435
|
-
if (tokens.length === 2) {
|
|
436
|
-
if (tokens[0].isNumber && tokens[1].isString) {
|
|
437
|
-
const conversion = Parser.tryFindUnitConversion(tokens[1].value, unitsConversions, defaultUnit);
|
|
438
|
-
if (conversion) {
|
|
439
|
-
const value = tokens[0].value * conversion.factor + conversion.offset;
|
|
440
|
-
return { ok: true, value };
|
|
441
|
-
}
|
|
442
|
-
// if no conversion, just return parsed number and ignore value in second token
|
|
443
|
-
return { ok: true, value: tokens[0].value };
|
|
444
|
-
}
|
|
445
|
-
else { // unit specification comes before value (like currency)
|
|
446
|
-
if (tokens[1].isNumber && tokens[0].isString) {
|
|
447
|
-
const conversion = Parser.tryFindUnitConversion(tokens[0].value, unitsConversions, defaultUnit);
|
|
448
|
-
if (conversion) {
|
|
449
|
-
const value = tokens[1].value * conversion.factor + conversion.offset;
|
|
450
|
-
return { ok: true, value };
|
|
451
|
-
}
|
|
452
|
-
// if no conversion, just return parsed number and ignore value in second token
|
|
453
|
-
return { ok: true, value: tokens[1].value };
|
|
454
|
-
}
|
|
455
|
-
}
|
|
456
|
-
}
|
|
457
|
-
// common case where there are multiple value/label pairs
|
|
458
|
-
if (tokens.length % 2 === 0) {
|
|
459
|
-
let mag = 0.0;
|
|
460
|
-
for (let i = 0; i < tokens.length; i = i + 2) {
|
|
461
|
-
if (tokens[i].isNumber && tokens[i + 1].isString) {
|
|
462
|
-
const value = tokens[i].value;
|
|
463
|
-
const conversion = Parser.tryFindUnitConversion(tokens[i + 1].value, unitsConversions, defaultUnit);
|
|
464
|
-
if (conversion) {
|
|
465
|
-
if (mag < 0.0)
|
|
466
|
-
mag = mag - ((value * conversion.factor)) + conversion.offset;
|
|
467
|
-
else
|
|
468
|
-
mag = mag + ((value * conversion.factor)) + conversion.offset;
|
|
469
|
-
}
|
|
470
|
-
}
|
|
471
|
-
}
|
|
472
|
-
return { ok: true, value: mag };
|
|
473
|
-
}
|
|
474
|
-
return { ok: false, error: ParseError.UnableToConvertParseTokensToQuantity };
|
|
475
|
-
}
|
|
476
|
-
/** Method to generate a Quantity given a string that represents a quantity value.
|
|
477
|
-
* @param inString A string that contains text represent a quantity.
|
|
478
|
-
* @param parserSpec unit label if not explicitly defined by user. Must have matching entry in supplied array of unitsConversions.
|
|
479
|
-
* @param defaultValue default value to return if parsing is un successful
|
|
480
|
-
*/
|
|
481
|
-
static parseQuantityString(inString, parserSpec) {
|
|
482
|
-
return Parser.parseToQuantityValue(inString, parserSpec.format, parserSpec.unitConversions);
|
|
483
|
-
}
|
|
484
|
-
/** Method to generate a Quantity given a string that represents a quantity value and likely a unit label.
|
|
485
|
-
* @param inString A string that contains text represent a quantity.
|
|
486
|
-
* @param format Defines the likely format of inString. Primary unit serves as a default unit if no unit label found in string.
|
|
487
|
-
* @param unitsConversions dictionary of conversions used to convert from unit used in inString to output quantity
|
|
488
|
-
*/
|
|
489
|
-
static parseToQuantityValue(inString, format, unitsConversions) {
|
|
490
|
-
// ensure any labels defined in composite unit definition are specified in unitConversions
|
|
491
|
-
if (format.units) {
|
|
492
|
-
format.units.forEach(([unit, label]) => {
|
|
493
|
-
if (label) {
|
|
494
|
-
if (unit.label !== label) { // if default unit label does not match composite label ensure the label is in the list of parse labels for the conversion
|
|
495
|
-
const unitConversion = unitsConversions.find((conversion) => conversion.name === unit.name);
|
|
496
|
-
if (unitConversion && unitConversion.parseLabels && !unitConversion.parseLabels.find((entry) => entry === label))
|
|
497
|
-
unitConversion.parseLabels.push(label);
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
});
|
|
501
|
-
}
|
|
502
|
-
const tokens = Parser.parseQuantitySpecification(inString, format);
|
|
503
|
-
if (tokens.length === 0)
|
|
504
|
-
return { ok: false, error: ParseError.UnableToGenerateParseTokens };
|
|
505
|
-
if (Parser._log) {
|
|
506
|
-
// eslint-disable-next-line no-console
|
|
507
|
-
console.log(`Parse tokens`);
|
|
508
|
-
let i = 0;
|
|
509
|
-
for (const token of tokens) {
|
|
510
|
-
// eslint-disable-next-line no-console
|
|
511
|
-
console.log(` [${i++}] isNumber=${token.isNumber} isString=${token.isString} token=${token.value}`);
|
|
512
|
-
}
|
|
513
|
-
}
|
|
514
|
-
return Parser.getQuantityValueFromParseTokens(tokens, format, unitsConversions);
|
|
515
|
-
}
|
|
516
|
-
/** Async Method used to create an array of UnitConversionSpec entries that can be used in synchronous calls to parse units. */
|
|
517
|
-
static async createUnitConversionSpecsForUnit(unitsProvider, outUnit, altUnitLabelsProvider) {
|
|
518
|
-
const unitConversionSpecs = [];
|
|
519
|
-
const familyUnits = await unitsProvider.getUnitsByFamily(outUnit.phenomenon);
|
|
520
|
-
for (const unit of familyUnits) {
|
|
521
|
-
const conversion = await unitsProvider.getConversion(unit, outUnit);
|
|
522
|
-
const parseLabels = [unit.label.toLocaleLowerCase()];
|
|
523
|
-
const alternateLabels = altUnitLabelsProvider?.getAlternateUnitLabels(unit);
|
|
524
|
-
// add any alternate labels that may be defined for the Unit
|
|
525
|
-
if (alternateLabels) {
|
|
526
|
-
alternateLabels.forEach((label) => {
|
|
527
|
-
const potentialLabel = label.toLocaleLowerCase();
|
|
528
|
-
if (-1 === parseLabels.findIndex((lbl) => lbl === potentialLabel))
|
|
529
|
-
parseLabels.push(label.toLocaleLowerCase());
|
|
530
|
-
});
|
|
531
|
-
}
|
|
532
|
-
unitConversionSpecs.push({
|
|
533
|
-
name: unit.name,
|
|
534
|
-
label: unit.label,
|
|
535
|
-
conversion,
|
|
536
|
-
parseLabels,
|
|
537
|
-
system: unit.system,
|
|
538
|
-
});
|
|
539
|
-
}
|
|
540
|
-
return unitConversionSpecs;
|
|
541
|
-
}
|
|
542
|
-
/** Async Method used to create an array of UnitConversionSpec entries that can be used in synchronous calls to parse units. */
|
|
543
|
-
static async createUnitConversionSpecs(unitsProvider, outUnitName, potentialParseUnits, altUnitLabelsProvider) {
|
|
544
|
-
const unitConversionSpecs = [];
|
|
545
|
-
const outUnit = await unitsProvider.findUnitByName(outUnitName);
|
|
546
|
-
if (!outUnit || !outUnit.name || 0 === outUnit.name.length) {
|
|
547
|
-
// eslint-disable-next-line no-console
|
|
548
|
-
console.log(`[Parser.createUnitConversionSpecs] ERROR: Unable to locate out unit ${outUnitName}.`);
|
|
549
|
-
return unitConversionSpecs;
|
|
550
|
-
}
|
|
551
|
-
for (const potentialParseUnit of potentialParseUnits) {
|
|
552
|
-
const unit = await unitsProvider.findUnitByName(potentialParseUnit.unitName);
|
|
553
|
-
if (!unit || !unit.name || 0 === unit.name.length) {
|
|
554
|
-
// eslint-disable-next-line no-console
|
|
555
|
-
console.log(`[Parser.createUnitConversionSpecs] ERROR: Unable to locate potential unit ${potentialParseUnit.unitName}.`);
|
|
556
|
-
continue;
|
|
557
|
-
}
|
|
558
|
-
const conversion = await unitsProvider.getConversion(unit, outUnit);
|
|
559
|
-
const parseLabels = [unit.label.toLocaleLowerCase()];
|
|
560
|
-
const alternateLabels = altUnitLabelsProvider?.getAlternateUnitLabels(unit);
|
|
561
|
-
// add any alternate labels that may be defined for the Unit
|
|
562
|
-
if (alternateLabels) {
|
|
563
|
-
alternateLabels.forEach((label) => {
|
|
564
|
-
const potentialLabel = label.toLocaleLowerCase();
|
|
565
|
-
if (-1 === parseLabels.findIndex((lbl) => lbl === potentialLabel))
|
|
566
|
-
parseLabels.push(label.toLocaleLowerCase());
|
|
567
|
-
});
|
|
568
|
-
}
|
|
569
|
-
// add any alternate labels that where provided by caller
|
|
570
|
-
if (potentialParseUnit.altLabels) {
|
|
571
|
-
potentialParseUnit.altLabels.forEach((label) => {
|
|
572
|
-
const potentialLabel = label.toLocaleLowerCase();
|
|
573
|
-
if (-1 === parseLabels.findIndex((lbl) => lbl === potentialLabel))
|
|
574
|
-
parseLabels.push(label.toLocaleLowerCase());
|
|
575
|
-
});
|
|
576
|
-
}
|
|
577
|
-
unitConversionSpecs.push({
|
|
578
|
-
name: unit.name,
|
|
579
|
-
label: unit.label,
|
|
580
|
-
conversion,
|
|
581
|
-
parseLabels,
|
|
582
|
-
system: unit.system,
|
|
583
|
-
});
|
|
584
|
-
}
|
|
585
|
-
return unitConversionSpecs;
|
|
586
|
-
}
|
|
587
|
-
}
|
|
588
|
-
Parser._log = false;
|
|
1
|
+
/*---------------------------------------------------------------------------------------------
|
|
2
|
+
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
|
|
3
|
+
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
4
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
/** @packageDocumentation
|
|
6
|
+
* @module Quantity
|
|
7
|
+
*/
|
|
8
|
+
import { QuantityConstants } from "./Constants";
|
|
9
|
+
import { FormatTraits, FormatType } from "./Formatter/FormatEnums";
|
|
10
|
+
import { Quantity } from "./Quantity";
|
|
11
|
+
/** Possible parser errors
|
|
12
|
+
* @beta
|
|
13
|
+
*/
|
|
14
|
+
export var ParseError;
|
|
15
|
+
(function (ParseError) {
|
|
16
|
+
ParseError[ParseError["UnableToGenerateParseTokens"] = 1] = "UnableToGenerateParseTokens";
|
|
17
|
+
ParseError[ParseError["NoValueOrUnitFoundInString"] = 2] = "NoValueOrUnitFoundInString";
|
|
18
|
+
ParseError[ParseError["UnitLabelSuppliedButNotMatched"] = 3] = "UnitLabelSuppliedButNotMatched";
|
|
19
|
+
ParseError[ParseError["UnknownUnit"] = 4] = "UnknownUnit";
|
|
20
|
+
ParseError[ParseError["UnableToConvertParseTokensToQuantity"] = 5] = "UnableToConvertParseTokensToQuantity";
|
|
21
|
+
ParseError[ParseError["InvalidParserSpec"] = 6] = "InvalidParserSpec";
|
|
22
|
+
})(ParseError || (ParseError = {}));
|
|
23
|
+
/** A ParseToken holds either a numeric or string token extracted from a string that represents a quantity value.
|
|
24
|
+
* @beta
|
|
25
|
+
*/
|
|
26
|
+
class ParseToken {
|
|
27
|
+
constructor(value) {
|
|
28
|
+
if (typeof value === "string")
|
|
29
|
+
this.value = value.trim();
|
|
30
|
+
else
|
|
31
|
+
this.value = value;
|
|
32
|
+
}
|
|
33
|
+
get isString() { return typeof this.value === "string"; }
|
|
34
|
+
get isNumber() { return typeof this.value === "number"; }
|
|
35
|
+
}
|
|
36
|
+
/** A ScientificToken holds an index and string representing the exponent.
|
|
37
|
+
* @beta
|
|
38
|
+
*/
|
|
39
|
+
class ScientificToken {
|
|
40
|
+
constructor(index, exponent) {
|
|
41
|
+
this.exponent = "";
|
|
42
|
+
this.index = index;
|
|
43
|
+
if (exponent)
|
|
44
|
+
this.exponent = exponent;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** A FractionToken holds an index and the fraction value of numerator / denominator.
|
|
48
|
+
* @beta
|
|
49
|
+
*/
|
|
50
|
+
class FractionToken {
|
|
51
|
+
constructor(index, fraction) {
|
|
52
|
+
this.fraction = 0.0;
|
|
53
|
+
this.exponent = "";
|
|
54
|
+
this.index = index;
|
|
55
|
+
if (fraction)
|
|
56
|
+
this.fraction = fraction;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
/** A Parser class that is used to break a string that represents a quantity value into tokens.
|
|
60
|
+
* @beta
|
|
61
|
+
*/
|
|
62
|
+
export class Parser {
|
|
63
|
+
static isParsedQuantity(item) {
|
|
64
|
+
return item.ok;
|
|
65
|
+
}
|
|
66
|
+
static isParseError(item) {
|
|
67
|
+
return !item.ok;
|
|
68
|
+
}
|
|
69
|
+
static checkForScientificNotation(index, stringToParse, uomSeparatorToIgnore) {
|
|
70
|
+
let exponentString = "";
|
|
71
|
+
let i = index + 1;
|
|
72
|
+
for (; i < stringToParse.length; i++) {
|
|
73
|
+
const charCode = stringToParse.charCodeAt(i);
|
|
74
|
+
if (Parser.isDigit(charCode) || ((charCode === QuantityConstants.CHAR_MINUS || charCode === QuantityConstants.CHAR_PLUS) && (i === (index + 1)))) {
|
|
75
|
+
exponentString = exponentString.concat(stringToParse[i]);
|
|
76
|
+
}
|
|
77
|
+
else {
|
|
78
|
+
i = uomSeparatorToIgnore === charCode ? i : i - 1;
|
|
79
|
+
break;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
if (exponentString.length > 1 || ((exponentString.length === 1) && (exponentString.charCodeAt(0) !== QuantityConstants.CHAR_MINUS) && (exponentString.charCodeAt(0) !== QuantityConstants.CHAR_PLUS)))
|
|
83
|
+
return new ScientificToken(i, exponentString);
|
|
84
|
+
return new ScientificToken(index);
|
|
85
|
+
}
|
|
86
|
+
static checkForFractions(index, stringToParse, uomSeparatorToIgnore, numeratorStr) {
|
|
87
|
+
let numeratorToken = "";
|
|
88
|
+
let denominatorToken = "";
|
|
89
|
+
let processingNumerator = true;
|
|
90
|
+
let i = index;
|
|
91
|
+
if (numeratorStr && numeratorStr.length > 0) {
|
|
92
|
+
numeratorToken = numeratorStr;
|
|
93
|
+
processingNumerator = false;
|
|
94
|
+
}
|
|
95
|
+
for (; i < stringToParse.length; i++) {
|
|
96
|
+
const charCode = stringToParse.charCodeAt(i);
|
|
97
|
+
if (Parser.isDigit(charCode)) {
|
|
98
|
+
if (processingNumerator) {
|
|
99
|
+
numeratorToken = numeratorToken.concat(stringToParse[i]);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
denominatorToken = denominatorToken.concat(stringToParse[i]);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
if (processingNumerator && (charCode === QuantityConstants.CHAR_SLASH || charCode === QuantityConstants.CHAR_DIVISION_SLASH || charCode === QuantityConstants.CHAR_DIVISION_SLASH)) {
|
|
107
|
+
processingNumerator = false;
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
if (uomSeparatorToIgnore !== charCode)
|
|
111
|
+
i = i - 1; // skip over uom separator after fraction
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
if (numeratorToken.length > 0 && denominatorToken.length > 0) {
|
|
117
|
+
const numerator = parseInt(numeratorToken, 10);
|
|
118
|
+
const denominator = parseInt(denominatorToken, 10);
|
|
119
|
+
if (denominator > 0)
|
|
120
|
+
return new FractionToken(i, numerator / denominator);
|
|
121
|
+
return new FractionToken(i);
|
|
122
|
+
}
|
|
123
|
+
return new FractionToken(index + 1);
|
|
124
|
+
}
|
|
125
|
+
static isDigit(charCode) {
|
|
126
|
+
return (charCode >= QuantityConstants.CHAR_DIGIT_ZERO) && (charCode <= QuantityConstants.CHAR_DIGIT_NINE);
|
|
127
|
+
}
|
|
128
|
+
static isDigitOrDecimalSeparator(charCode, format) {
|
|
129
|
+
return (charCode === format.decimalSeparator.charCodeAt(0)) || Parser.isDigit(charCode);
|
|
130
|
+
}
|
|
131
|
+
/** Parse the quantity string and return and array of ParseTokens that represent the component invariant values and unit labels.
|
|
132
|
+
* @param quantitySpecification The quantity string to ba parsed.
|
|
133
|
+
*/
|
|
134
|
+
static parseQuantitySpecification(quantitySpecification, format) {
|
|
135
|
+
const tokens = [];
|
|
136
|
+
const str = quantitySpecification.trim();
|
|
137
|
+
let processingNumber = false;
|
|
138
|
+
let wipToken = "";
|
|
139
|
+
let signToken = "";
|
|
140
|
+
let uomSeparatorToIgnore = 0;
|
|
141
|
+
let fractionDashCode = 0;
|
|
142
|
+
const skipCodes = [format.thousandSeparator.charCodeAt(0)];
|
|
143
|
+
if (format.type === FormatType.Station && format.stationSeparator && format.stationSeparator.length === 1)
|
|
144
|
+
skipCodes.push(format.stationSeparator.charCodeAt(0));
|
|
145
|
+
if (format.type === FormatType.Fractional && format.hasFormatTraitSet(FormatTraits.FractionDash)) {
|
|
146
|
+
fractionDashCode = QuantityConstants.CHAR_MINUS;
|
|
147
|
+
}
|
|
148
|
+
if (format.uomSeparator && format.uomSeparator !== " " && format.uomSeparator.length === 1) {
|
|
149
|
+
uomSeparatorToIgnore = format.uomSeparator.charCodeAt(0);
|
|
150
|
+
skipCodes.push(uomSeparatorToIgnore);
|
|
151
|
+
}
|
|
152
|
+
// eslint-disable-next-line @typescript-eslint/prefer-for-of
|
|
153
|
+
for (let i = 0; i < str.length; i++) {
|
|
154
|
+
const charCode = str.charCodeAt(i);
|
|
155
|
+
if (Parser.isDigitOrDecimalSeparator(charCode, format)) {
|
|
156
|
+
if (!processingNumber) {
|
|
157
|
+
if (wipToken.length > 0) {
|
|
158
|
+
tokens.push(new ParseToken(wipToken));
|
|
159
|
+
wipToken = "";
|
|
160
|
+
}
|
|
161
|
+
processingNumber = true;
|
|
162
|
+
}
|
|
163
|
+
wipToken = wipToken.concat(str[i]);
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
if (processingNumber) {
|
|
167
|
+
if (charCode === QuantityConstants.CHAR_SLASH || charCode === QuantityConstants.CHAR_FRACTION_SLASH || charCode === QuantityConstants.CHAR_DIVISION_SLASH) {
|
|
168
|
+
const fractSymbol = Parser.checkForFractions(i + 1, str, uomSeparatorToIgnore, wipToken);
|
|
169
|
+
let fraction = fractSymbol.fraction;
|
|
170
|
+
i = fractSymbol.index;
|
|
171
|
+
if (fractSymbol.fraction !== 0.0) {
|
|
172
|
+
wipToken = "";
|
|
173
|
+
if (signToken.length > 0) {
|
|
174
|
+
if (signToken === "-")
|
|
175
|
+
fraction = 0 - fraction;
|
|
176
|
+
signToken = "";
|
|
177
|
+
}
|
|
178
|
+
tokens.push(new ParseToken(fraction));
|
|
179
|
+
processingNumber = false;
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
// a space may signify end of number or start of decimal
|
|
185
|
+
if (charCode === QuantityConstants.CHAR_SPACE || charCode === fractionDashCode) {
|
|
186
|
+
const fractSymbol = Parser.checkForFractions(i + 1, str, uomSeparatorToIgnore);
|
|
187
|
+
let fraction = fractSymbol.fraction;
|
|
188
|
+
if (fractSymbol.fraction !== 0.0) {
|
|
189
|
+
i = fractSymbol.index;
|
|
190
|
+
if (signToken.length > 0) {
|
|
191
|
+
wipToken = signToken + wipToken;
|
|
192
|
+
if (signToken === "-")
|
|
193
|
+
fraction = 0 - fraction;
|
|
194
|
+
signToken = "";
|
|
195
|
+
}
|
|
196
|
+
const valueWithFraction = parseFloat(wipToken) + fraction;
|
|
197
|
+
tokens.push(new ParseToken(valueWithFraction));
|
|
198
|
+
processingNumber = false;
|
|
199
|
+
wipToken = "";
|
|
200
|
+
}
|
|
201
|
+
continue;
|
|
202
|
+
}
|
|
203
|
+
else {
|
|
204
|
+
// an "E" or "e" may signify scientific notation
|
|
205
|
+
if (charCode === QuantityConstants.CHAR_UPPER_E || charCode === QuantityConstants.CHAR_LOWER_E) {
|
|
206
|
+
const exponentSymbol = Parser.checkForScientificNotation(i, str, uomSeparatorToIgnore);
|
|
207
|
+
i = exponentSymbol.index;
|
|
208
|
+
if (exponentSymbol.exponent && exponentSymbol.exponent.length > 0) {
|
|
209
|
+
if (signToken.length > 0) {
|
|
210
|
+
wipToken = signToken + wipToken;
|
|
211
|
+
signToken = "";
|
|
212
|
+
}
|
|
213
|
+
wipToken = `${wipToken}e${exponentSymbol.exponent}`;
|
|
214
|
+
const scientificValue = Number(wipToken);
|
|
215
|
+
tokens.push(new ParseToken(scientificValue));
|
|
216
|
+
processingNumber = false;
|
|
217
|
+
wipToken = "";
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
// ignore any codes in skipCodes
|
|
224
|
+
if (skipCodes.findIndex((ref) => ref === charCode) !== -1)
|
|
225
|
+
continue;
|
|
226
|
+
if (signToken.length > 0) {
|
|
227
|
+
wipToken = signToken + wipToken;
|
|
228
|
+
signToken = "";
|
|
229
|
+
}
|
|
230
|
+
tokens.push(new ParseToken(parseFloat(wipToken)));
|
|
231
|
+
wipToken = (i < str.length) ? str[i] : "";
|
|
232
|
+
processingNumber = false;
|
|
233
|
+
}
|
|
234
|
+
else {
|
|
235
|
+
// not processing a number
|
|
236
|
+
if ((charCode === QuantityConstants.CHAR_PLUS || charCode === QuantityConstants.CHAR_MINUS)) {
|
|
237
|
+
if (0 === tokens.length) // sign token only needed for left most value
|
|
238
|
+
signToken = str[i];
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
wipToken = wipToken.concat(str[i]);
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
// handle case where end of input string is reached.
|
|
246
|
+
if (wipToken.length > 0) {
|
|
247
|
+
if (processingNumber) {
|
|
248
|
+
if (signToken.length > 0) {
|
|
249
|
+
wipToken = signToken + wipToken;
|
|
250
|
+
}
|
|
251
|
+
tokens.push(new ParseToken(parseFloat(wipToken)));
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
tokens.push(new ParseToken(wipToken));
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
return tokens;
|
|
258
|
+
}
|
|
259
|
+
static async lookupUnitByLabel(unitLabel, format, unitsProvider, altUnitLabelsProvider) {
|
|
260
|
+
const defaultUnit = format.units && format.units.length > 0 ? format.units[0][0] : undefined;
|
|
261
|
+
const labelToFind = unitLabel.toLowerCase();
|
|
262
|
+
// First look in format for a label and matches
|
|
263
|
+
if (format.units && format.units.length > 0) {
|
|
264
|
+
const formatUnit = format.units.find(([unit, label]) => {
|
|
265
|
+
if (label && label.toLowerCase() === labelToFind)
|
|
266
|
+
return true;
|
|
267
|
+
const alternateLabels = altUnitLabelsProvider?.getAlternateUnitLabels(unit);
|
|
268
|
+
// check any alternate labels that may be defined for the Unit
|
|
269
|
+
if (alternateLabels && alternateLabels.find((lbl) => lbl.toLowerCase() === labelToFind))
|
|
270
|
+
return true;
|
|
271
|
+
return false;
|
|
272
|
+
});
|
|
273
|
+
if (formatUnit)
|
|
274
|
+
return formatUnit[0];
|
|
275
|
+
}
|
|
276
|
+
// now try to find a unit from the same family and system
|
|
277
|
+
let foundUnit = await unitsProvider.findUnit(unitLabel, defaultUnit ? defaultUnit.phenomenon : undefined, defaultUnit ? defaultUnit.system : undefined);
|
|
278
|
+
// if nothing found yet just limit to family
|
|
279
|
+
if (!foundUnit.isValid && defaultUnit)
|
|
280
|
+
foundUnit = await unitsProvider.findUnit(unitLabel, defaultUnit ? defaultUnit.phenomenon : undefined);
|
|
281
|
+
return foundUnit;
|
|
282
|
+
}
|
|
283
|
+
static async createQuantityFromParseTokens(tokens, format, unitsProvider, altUnitLabelsProvider) {
|
|
284
|
+
let defaultUnit = format.units && format.units.length > 0 ? format.units[0][0] : undefined;
|
|
285
|
+
// common case where single value is supplied
|
|
286
|
+
if (tokens.length === 1) {
|
|
287
|
+
if (tokens[0].isNumber) {
|
|
288
|
+
return new Quantity(defaultUnit, tokens[0].value);
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
const unit = await this.lookupUnitByLabel(tokens[0].value, format, unitsProvider, altUnitLabelsProvider);
|
|
292
|
+
return new Quantity(unit);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
// common case where single value and single label are supplied
|
|
296
|
+
if (tokens.length === 2) {
|
|
297
|
+
if (tokens[0].isNumber && tokens[1].isString) {
|
|
298
|
+
const unit = await this.lookupUnitByLabel(tokens[1].value, format, unitsProvider, altUnitLabelsProvider);
|
|
299
|
+
if (undefined === defaultUnit)
|
|
300
|
+
defaultUnit = unit;
|
|
301
|
+
if (defaultUnit && defaultUnit.name === unit.name) {
|
|
302
|
+
return new Quantity(defaultUnit, tokens[0].value);
|
|
303
|
+
}
|
|
304
|
+
else if (defaultUnit) {
|
|
305
|
+
const conversion = await unitsProvider.getConversion(unit, defaultUnit);
|
|
306
|
+
const mag = ((tokens[0].value * conversion.factor)) + conversion.offset;
|
|
307
|
+
return new Quantity(defaultUnit, mag);
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
else { // unit specification comes before value (like currency)
|
|
311
|
+
if (tokens[1].isNumber && tokens[0].isString) {
|
|
312
|
+
const unit = await this.lookupUnitByLabel(tokens[0].value, format, unitsProvider, altUnitLabelsProvider);
|
|
313
|
+
if (undefined === defaultUnit)
|
|
314
|
+
defaultUnit = unit;
|
|
315
|
+
if (defaultUnit && defaultUnit.name === unit.name) {
|
|
316
|
+
return new Quantity(defaultUnit, tokens[1].value);
|
|
317
|
+
}
|
|
318
|
+
else if (defaultUnit) {
|
|
319
|
+
const conversion = await unitsProvider.getConversion(unit, defaultUnit);
|
|
320
|
+
const mag = ((tokens[1].value * conversion.factor)) + conversion.offset;
|
|
321
|
+
return new Quantity(defaultUnit, mag);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
// common case where there are multiple value/label pairs
|
|
327
|
+
if (tokens.length % 2 === 0) {
|
|
328
|
+
let mag = 0.0;
|
|
329
|
+
for (let i = 0; i < tokens.length; i = i + 2) {
|
|
330
|
+
if (tokens[i].isNumber && tokens[i + 1].isString) {
|
|
331
|
+
const value = tokens[i].value;
|
|
332
|
+
const unit = await this.lookupUnitByLabel(tokens[i + 1].value, format, unitsProvider, altUnitLabelsProvider);
|
|
333
|
+
if (undefined === defaultUnit)
|
|
334
|
+
defaultUnit = unit;
|
|
335
|
+
if (0 === i) {
|
|
336
|
+
if (defaultUnit.name === unit.name)
|
|
337
|
+
mag = value;
|
|
338
|
+
else {
|
|
339
|
+
const conversion = await unitsProvider.getConversion(unit, defaultUnit);
|
|
340
|
+
mag = ((value * conversion.factor)) + conversion.offset;
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
else {
|
|
344
|
+
if (defaultUnit) {
|
|
345
|
+
const conversion = await unitsProvider.getConversion(unit, defaultUnit);
|
|
346
|
+
if (mag < 0.0)
|
|
347
|
+
mag = mag - ((value * conversion.factor)) + conversion.offset;
|
|
348
|
+
else
|
|
349
|
+
mag = mag + ((value * conversion.factor)) + conversion.offset;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
return new Quantity(defaultUnit, mag);
|
|
355
|
+
}
|
|
356
|
+
return new Quantity(defaultUnit);
|
|
357
|
+
}
|
|
358
|
+
/** Async method to generate a Quantity given a string that represents a quantity value and likely a unit label.
|
|
359
|
+
* @param inString A string that contains text represent a quantity.
|
|
360
|
+
* @param format Defines the likely format of inString.
|
|
361
|
+
* @param unitsProvider required to look up units that may be specified in inString
|
|
362
|
+
*/
|
|
363
|
+
static async parseIntoQuantity(inString, format, unitsProvider, altUnitLabelsProvider) {
|
|
364
|
+
const tokens = Parser.parseQuantitySpecification(inString, format);
|
|
365
|
+
if (tokens.length === 0)
|
|
366
|
+
return new Quantity();
|
|
367
|
+
return Parser.createQuantityFromParseTokens(tokens, format, unitsProvider, altUnitLabelsProvider);
|
|
368
|
+
}
|
|
369
|
+
/** method to get the Unit Conversion given a unit label */
|
|
370
|
+
static tryFindUnitConversion(unitLabel, unitsConversions, preferredUnit) {
|
|
371
|
+
if (unitsConversions.length > 0) {
|
|
372
|
+
const label = unitLabel.toLocaleLowerCase();
|
|
373
|
+
/* A preferred unit is used to target a unit if a unit label is used in more that one unit definition from the same unit family.
|
|
374
|
+
* An example is if "ft" is used as the unitLabel and the preferredUnit is "SURVEY_FT" since that unit has an alternate label of "ft" the
|
|
375
|
+
* conversion to "SURVEY_FT" is returned. If no preferredUnit is specified then the unit "FT" would likely to have been found first.
|
|
376
|
+
* If "in" is the unit label and "SURVEY_FT" is the preferredUnit then conversion to "SURVEY_IN" would be returned.
|
|
377
|
+
*/
|
|
378
|
+
if (preferredUnit) {
|
|
379
|
+
// if there is a preferred unit defined see if unit label matched it or one of its alternates
|
|
380
|
+
const preferredConversion = unitsConversions.find((conversion) => conversion.name === preferredUnit.name);
|
|
381
|
+
if (preferredConversion && preferredConversion.parseLabels) {
|
|
382
|
+
if (-1 !== preferredConversion.parseLabels.findIndex((lbl) => lbl === label))
|
|
383
|
+
return preferredConversion.conversion;
|
|
384
|
+
}
|
|
385
|
+
// see if we can find a matching unitLabel in any unit within the same system as the preferred unit
|
|
386
|
+
const preferredSystemConversions = unitsConversions.filter((conversion) => conversion.system === preferredUnit.system);
|
|
387
|
+
for (const conversion of preferredSystemConversions) {
|
|
388
|
+
if (conversion.parseLabels) {
|
|
389
|
+
if (-1 !== conversion.parseLabels.findIndex((lbl) => lbl === label))
|
|
390
|
+
return conversion.conversion;
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
// if no unit found based on preferredUnit see if an unit label matches
|
|
395
|
+
for (const conversion of unitsConversions) {
|
|
396
|
+
if (conversion.parseLabels) {
|
|
397
|
+
if (-1 !== conversion.parseLabels.findIndex((lbl) => lbl === label))
|
|
398
|
+
return conversion.conversion;
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
// eslint-disable-next-line no-console
|
|
402
|
+
console.log("ERROR: Parser expects to find parseLabels array populate with all possible unit labels for the unit.");
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
return undefined;
|
|
407
|
+
}
|
|
408
|
+
static getQuantityValueFromParseTokens(tokens, format, unitsConversions) {
|
|
409
|
+
const defaultUnit = format.units && format.units.length > 0 ? format.units[0][0] : undefined;
|
|
410
|
+
// common case where single value is supplied
|
|
411
|
+
if (tokens.length === 1) {
|
|
412
|
+
if (tokens[0].isNumber) {
|
|
413
|
+
if (defaultUnit) {
|
|
414
|
+
const conversion = Parser.tryFindUnitConversion(defaultUnit.label, unitsConversions, defaultUnit);
|
|
415
|
+
if (conversion) {
|
|
416
|
+
const value = tokens[0].value * conversion.factor + conversion.offset;
|
|
417
|
+
return { ok: true, value };
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
else {
|
|
421
|
+
// if no conversion or no defaultUnit, just return parsed number
|
|
422
|
+
return { ok: true, value: tokens[0].value };
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
// only the unit label was specified so assume magnitude of 1
|
|
427
|
+
const conversion = Parser.tryFindUnitConversion(tokens[0].value, unitsConversions, defaultUnit);
|
|
428
|
+
if (undefined !== conversion)
|
|
429
|
+
return { ok: true, value: conversion.factor + conversion.offset };
|
|
430
|
+
else
|
|
431
|
+
return { ok: false, error: ParseError.NoValueOrUnitFoundInString };
|
|
432
|
+
}
|
|
433
|
+
}
|
|
434
|
+
// common case where single value and single label are supplied
|
|
435
|
+
if (tokens.length === 2) {
|
|
436
|
+
if (tokens[0].isNumber && tokens[1].isString) {
|
|
437
|
+
const conversion = Parser.tryFindUnitConversion(tokens[1].value, unitsConversions, defaultUnit);
|
|
438
|
+
if (conversion) {
|
|
439
|
+
const value = tokens[0].value * conversion.factor + conversion.offset;
|
|
440
|
+
return { ok: true, value };
|
|
441
|
+
}
|
|
442
|
+
// if no conversion, just return parsed number and ignore value in second token
|
|
443
|
+
return { ok: true, value: tokens[0].value };
|
|
444
|
+
}
|
|
445
|
+
else { // unit specification comes before value (like currency)
|
|
446
|
+
if (tokens[1].isNumber && tokens[0].isString) {
|
|
447
|
+
const conversion = Parser.tryFindUnitConversion(tokens[0].value, unitsConversions, defaultUnit);
|
|
448
|
+
if (conversion) {
|
|
449
|
+
const value = tokens[1].value * conversion.factor + conversion.offset;
|
|
450
|
+
return { ok: true, value };
|
|
451
|
+
}
|
|
452
|
+
// if no conversion, just return parsed number and ignore value in second token
|
|
453
|
+
return { ok: true, value: tokens[1].value };
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
// common case where there are multiple value/label pairs
|
|
458
|
+
if (tokens.length % 2 === 0) {
|
|
459
|
+
let mag = 0.0;
|
|
460
|
+
for (let i = 0; i < tokens.length; i = i + 2) {
|
|
461
|
+
if (tokens[i].isNumber && tokens[i + 1].isString) {
|
|
462
|
+
const value = tokens[i].value;
|
|
463
|
+
const conversion = Parser.tryFindUnitConversion(tokens[i + 1].value, unitsConversions, defaultUnit);
|
|
464
|
+
if (conversion) {
|
|
465
|
+
if (mag < 0.0)
|
|
466
|
+
mag = mag - ((value * conversion.factor)) + conversion.offset;
|
|
467
|
+
else
|
|
468
|
+
mag = mag + ((value * conversion.factor)) + conversion.offset;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return { ok: true, value: mag };
|
|
473
|
+
}
|
|
474
|
+
return { ok: false, error: ParseError.UnableToConvertParseTokensToQuantity };
|
|
475
|
+
}
|
|
476
|
+
/** Method to generate a Quantity given a string that represents a quantity value.
|
|
477
|
+
* @param inString A string that contains text represent a quantity.
|
|
478
|
+
* @param parserSpec unit label if not explicitly defined by user. Must have matching entry in supplied array of unitsConversions.
|
|
479
|
+
* @param defaultValue default value to return if parsing is un successful
|
|
480
|
+
*/
|
|
481
|
+
static parseQuantityString(inString, parserSpec) {
|
|
482
|
+
return Parser.parseToQuantityValue(inString, parserSpec.format, parserSpec.unitConversions);
|
|
483
|
+
}
|
|
484
|
+
/** Method to generate a Quantity given a string that represents a quantity value and likely a unit label.
|
|
485
|
+
* @param inString A string that contains text represent a quantity.
|
|
486
|
+
* @param format Defines the likely format of inString. Primary unit serves as a default unit if no unit label found in string.
|
|
487
|
+
* @param unitsConversions dictionary of conversions used to convert from unit used in inString to output quantity
|
|
488
|
+
*/
|
|
489
|
+
static parseToQuantityValue(inString, format, unitsConversions) {
|
|
490
|
+
// ensure any labels defined in composite unit definition are specified in unitConversions
|
|
491
|
+
if (format.units) {
|
|
492
|
+
format.units.forEach(([unit, label]) => {
|
|
493
|
+
if (label) {
|
|
494
|
+
if (unit.label !== label) { // if default unit label does not match composite label ensure the label is in the list of parse labels for the conversion
|
|
495
|
+
const unitConversion = unitsConversions.find((conversion) => conversion.name === unit.name);
|
|
496
|
+
if (unitConversion && unitConversion.parseLabels && !unitConversion.parseLabels.find((entry) => entry === label))
|
|
497
|
+
unitConversion.parseLabels.push(label);
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
});
|
|
501
|
+
}
|
|
502
|
+
const tokens = Parser.parseQuantitySpecification(inString, format);
|
|
503
|
+
if (tokens.length === 0)
|
|
504
|
+
return { ok: false, error: ParseError.UnableToGenerateParseTokens };
|
|
505
|
+
if (Parser._log) {
|
|
506
|
+
// eslint-disable-next-line no-console
|
|
507
|
+
console.log(`Parse tokens`);
|
|
508
|
+
let i = 0;
|
|
509
|
+
for (const token of tokens) {
|
|
510
|
+
// eslint-disable-next-line no-console
|
|
511
|
+
console.log(` [${i++}] isNumber=${token.isNumber} isString=${token.isString} token=${token.value}`);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
return Parser.getQuantityValueFromParseTokens(tokens, format, unitsConversions);
|
|
515
|
+
}
|
|
516
|
+
/** Async Method used to create an array of UnitConversionSpec entries that can be used in synchronous calls to parse units. */
|
|
517
|
+
static async createUnitConversionSpecsForUnit(unitsProvider, outUnit, altUnitLabelsProvider) {
|
|
518
|
+
const unitConversionSpecs = [];
|
|
519
|
+
const familyUnits = await unitsProvider.getUnitsByFamily(outUnit.phenomenon);
|
|
520
|
+
for (const unit of familyUnits) {
|
|
521
|
+
const conversion = await unitsProvider.getConversion(unit, outUnit);
|
|
522
|
+
const parseLabels = [unit.label.toLocaleLowerCase()];
|
|
523
|
+
const alternateLabels = altUnitLabelsProvider?.getAlternateUnitLabels(unit);
|
|
524
|
+
// add any alternate labels that may be defined for the Unit
|
|
525
|
+
if (alternateLabels) {
|
|
526
|
+
alternateLabels.forEach((label) => {
|
|
527
|
+
const potentialLabel = label.toLocaleLowerCase();
|
|
528
|
+
if (-1 === parseLabels.findIndex((lbl) => lbl === potentialLabel))
|
|
529
|
+
parseLabels.push(label.toLocaleLowerCase());
|
|
530
|
+
});
|
|
531
|
+
}
|
|
532
|
+
unitConversionSpecs.push({
|
|
533
|
+
name: unit.name,
|
|
534
|
+
label: unit.label,
|
|
535
|
+
conversion,
|
|
536
|
+
parseLabels,
|
|
537
|
+
system: unit.system,
|
|
538
|
+
});
|
|
539
|
+
}
|
|
540
|
+
return unitConversionSpecs;
|
|
541
|
+
}
|
|
542
|
+
/** Async Method used to create an array of UnitConversionSpec entries that can be used in synchronous calls to parse units. */
|
|
543
|
+
static async createUnitConversionSpecs(unitsProvider, outUnitName, potentialParseUnits, altUnitLabelsProvider) {
|
|
544
|
+
const unitConversionSpecs = [];
|
|
545
|
+
const outUnit = await unitsProvider.findUnitByName(outUnitName);
|
|
546
|
+
if (!outUnit || !outUnit.name || 0 === outUnit.name.length) {
|
|
547
|
+
// eslint-disable-next-line no-console
|
|
548
|
+
console.log(`[Parser.createUnitConversionSpecs] ERROR: Unable to locate out unit ${outUnitName}.`);
|
|
549
|
+
return unitConversionSpecs;
|
|
550
|
+
}
|
|
551
|
+
for (const potentialParseUnit of potentialParseUnits) {
|
|
552
|
+
const unit = await unitsProvider.findUnitByName(potentialParseUnit.unitName);
|
|
553
|
+
if (!unit || !unit.name || 0 === unit.name.length) {
|
|
554
|
+
// eslint-disable-next-line no-console
|
|
555
|
+
console.log(`[Parser.createUnitConversionSpecs] ERROR: Unable to locate potential unit ${potentialParseUnit.unitName}.`);
|
|
556
|
+
continue;
|
|
557
|
+
}
|
|
558
|
+
const conversion = await unitsProvider.getConversion(unit, outUnit);
|
|
559
|
+
const parseLabels = [unit.label.toLocaleLowerCase()];
|
|
560
|
+
const alternateLabels = altUnitLabelsProvider?.getAlternateUnitLabels(unit);
|
|
561
|
+
// add any alternate labels that may be defined for the Unit
|
|
562
|
+
if (alternateLabels) {
|
|
563
|
+
alternateLabels.forEach((label) => {
|
|
564
|
+
const potentialLabel = label.toLocaleLowerCase();
|
|
565
|
+
if (-1 === parseLabels.findIndex((lbl) => lbl === potentialLabel))
|
|
566
|
+
parseLabels.push(label.toLocaleLowerCase());
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
// add any alternate labels that where provided by caller
|
|
570
|
+
if (potentialParseUnit.altLabels) {
|
|
571
|
+
potentialParseUnit.altLabels.forEach((label) => {
|
|
572
|
+
const potentialLabel = label.toLocaleLowerCase();
|
|
573
|
+
if (-1 === parseLabels.findIndex((lbl) => lbl === potentialLabel))
|
|
574
|
+
parseLabels.push(label.toLocaleLowerCase());
|
|
575
|
+
});
|
|
576
|
+
}
|
|
577
|
+
unitConversionSpecs.push({
|
|
578
|
+
name: unit.name,
|
|
579
|
+
label: unit.label,
|
|
580
|
+
conversion,
|
|
581
|
+
parseLabels,
|
|
582
|
+
system: unit.system,
|
|
583
|
+
});
|
|
584
|
+
}
|
|
585
|
+
return unitConversionSpecs;
|
|
586
|
+
}
|
|
587
|
+
}
|
|
588
|
+
Parser._log = false;
|
|
589
589
|
//# sourceMappingURL=Parser.js.map
|