@itwin/core-quantity 5.0.0-dev.8 → 5.0.0-dev.83
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/CHANGELOG.md +46 -1
- package/README.md +9 -5
- package/lib/cjs/Constants.js +18 -18
- package/lib/cjs/Constants.js.map +1 -1
- package/lib/cjs/Exception.js +1 -0
- package/lib/cjs/Exception.js.map +1 -1
- package/lib/cjs/Formatter/Format.js +23 -13
- package/lib/cjs/Formatter/Format.js.map +1 -1
- package/lib/cjs/Formatter/Formatter.js +8 -8
- package/lib/cjs/Formatter/Formatter.js.map +1 -1
- package/lib/cjs/Formatter/FormatterSpec.js +6 -1
- package/lib/cjs/Formatter/FormatterSpec.js.map +1 -1
- package/lib/cjs/Parser.d.ts +1 -0
- package/lib/cjs/Parser.d.ts.map +1 -1
- package/lib/cjs/Parser.js +66 -29
- package/lib/cjs/Parser.js.map +1 -1
- package/lib/cjs/ParserSpec.d.ts +1 -1
- package/lib/cjs/ParserSpec.js +6 -2
- package/lib/cjs/ParserSpec.js.map +1 -1
- package/lib/cjs/Quantity.js +3 -2
- package/lib/cjs/Quantity.js.map +1 -1
- package/lib/cjs/Unit.js +10 -12
- package/lib/cjs/Unit.js.map +1 -1
- package/lib/esm/Constants.js +18 -18
- package/lib/esm/Constants.js.map +1 -1
- package/lib/esm/Exception.js +1 -0
- package/lib/esm/Exception.js.map +1 -1
- package/lib/esm/Formatter/Format.js +23 -13
- package/lib/esm/Formatter/Format.js.map +1 -1
- package/lib/esm/Formatter/Formatter.js +8 -8
- package/lib/esm/Formatter/Formatter.js.map +1 -1
- package/lib/esm/Formatter/FormatterSpec.js +6 -1
- package/lib/esm/Formatter/FormatterSpec.js.map +1 -1
- package/lib/esm/Parser.d.ts +1 -0
- package/lib/esm/Parser.d.ts.map +1 -1
- package/lib/esm/Parser.js +66 -29
- package/lib/esm/Parser.js.map +1 -1
- package/lib/esm/ParserSpec.d.ts +1 -1
- package/lib/esm/ParserSpec.js +6 -2
- package/lib/esm/ParserSpec.js.map +1 -1
- package/lib/esm/Quantity.js +3 -2
- package/lib/esm/Quantity.js.map +1 -1
- package/lib/esm/Unit.js +10 -12
- package/lib/esm/Unit.js.map +1 -1
- package/package.json +11 -21
|
@@ -13,6 +13,12 @@ import { Formatter } from "./Formatter";
|
|
|
13
13
|
* @beta
|
|
14
14
|
*/
|
|
15
15
|
export class FormatterSpec {
|
|
16
|
+
_name;
|
|
17
|
+
_conversions = []; // max four entries
|
|
18
|
+
_format;
|
|
19
|
+
_persistenceUnit;
|
|
20
|
+
_azimuthBaseConversion; // converts azimuth base unit to persistence unit
|
|
21
|
+
_revolutionConversion; // converts revolution unit to persistence unit
|
|
16
22
|
/** Constructor
|
|
17
23
|
* @param name The name of a format specification.
|
|
18
24
|
* @param format Defines the output format for the quantity value.
|
|
@@ -22,7 +28,6 @@ export class FormatterSpec {
|
|
|
22
28
|
* @param revolutionConversion The conversion used to determine a revolution value (used for bearing and azimuth).
|
|
23
29
|
*/
|
|
24
30
|
constructor(name, format, conversions, persistenceUnit, azimuthBaseConversion, revolutionConversion) {
|
|
25
|
-
this._conversions = []; // max four entries
|
|
26
31
|
if (!persistenceUnit) {
|
|
27
32
|
if (format.units) {
|
|
28
33
|
const [props] = format.units[0];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"FormatterSpec.js","sourceRoot":"","sources":["../../../src/Formatter/FormatterSpec.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAIH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,sFAAsF;AACtF,6JAA6J;AAE7J;;;GAGG;AACH,MAAM,OAAO,aAAa;IAQxB;;;;;;;OAOG;IACH,YAAY,IAAY,EAAE,MAAc,EAAE,WAAkC,EAAE,eAA2B,EAAE,qBAA2C,EAAE,oBAA0C;QAdxL,iBAAY,GAAyB,EAAE,CAAC,CAAE,mBAAmB;QAerE,IAAI,CAAC,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,OAAO,IAAI,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,qBAAqB,EAAE,oBAAoB,CAAC,CAAC;IAC9G,CAAC;IAED,+BAA+B;IACxB,eAAe,CAAC,SAAiB;QACtC,OAAO,SAAS,CAAC,cAAc,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACnD,CAAC;CACF","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\n/** @packageDocumentation\n * @module Quantity\n */\n\nimport { UnitConversionProps, UnitConversionSpec, UnitProps, UnitsProvider } from \"../Interfaces\";\nimport { Format } from \"./Format\";\nimport { Formatter } from \"./Formatter\";\n\n// cSpell:ignore ZERONORMALIZED, nosign, onlynegative, signalways, negativeparentheses\n// cSpell:ignore trailzeroes, keepsinglezero, zeroempty, keepdecimalpoint, applyrounding, fractiondash, showunitlabel, prependunitlabel, exponentonlynegative\n\n/** A class that contains both formatting information and the conversion factors necessary to convert from an input unit to the units specified in the format.\n * Once created the FormatterSpec will be able to format quantity values with synchronous calls.\n * @beta\n */\nexport class FormatterSpec {\n protected _name: string;\n protected _conversions: UnitConversionSpec[] = []; // max four entries\n protected _format: Format;\n protected _persistenceUnit: UnitProps;\n protected _azimuthBaseConversion?: UnitConversionProps; // converts azimuth base unit to persistence unit\n protected _revolutionConversion?: UnitConversionProps; // converts revolution unit to persistence unit\n\n /** Constructor\n * @param name The name of a format specification.\n * @param format Defines the output format for the quantity value.\n * @param conversions An array of conversion factors necessary to convert from an input unit to the units specified in the format.\n * @param persistenceUnit The unit the magnitude value is input.\n * @param azimuthBaseConversion The conversion used to interpret azimuth base values.\n * @param revolutionConversion The conversion used to determine a revolution value (used for bearing and azimuth).\n */\n constructor(name: string, format: Format, conversions?: UnitConversionSpec[], persistenceUnit?: UnitProps, azimuthBaseConversion?: UnitConversionProps, revolutionConversion?: UnitConversionProps) {\n if (!persistenceUnit) {\n if (format.units) {\n const [props] = format.units[0];\n persistenceUnit = props;\n } else {\n throw new Error(\"Formatter Spec needs persistence unit to be specified\");\n }\n }\n\n this._name = name;\n this._format = format;\n this._persistenceUnit = persistenceUnit;\n if (conversions)\n this._conversions = conversions;\n this._azimuthBaseConversion = azimuthBaseConversion;\n this._revolutionConversion = revolutionConversion;\n }\n\n public get name(): string { return this._name; }\n /** Returns an array of UnitConversionSpecs, one for each unit that is to be shown in the formatted quantity string. */\n public get unitConversions(): UnitConversionSpec[] { return this._conversions; }\n public get format(): Format { return this._format; }\n public get persistenceUnit(): UnitProps { return this._persistenceUnit; }\n public get azimuthBaseConversion(): UnitConversionProps | undefined { return this._azimuthBaseConversion; }\n public get revolutionConversion(): UnitConversionProps | undefined { return this._revolutionConversion; }\n\n /** Get an array of UnitConversionSpecs, one for each unit that is to be shown in the formatted quantity string. */\n public static async getUnitConversions(format: Format, unitsProvider: UnitsProvider, inputUnit?: UnitProps): Promise<UnitConversionSpec[]> {\n const conversions: UnitConversionSpec[] = [];\n let persistenceUnit = inputUnit;\n if (!persistenceUnit) {\n if (format.units) {\n const [props] = format.units[0];\n persistenceUnit = props;\n } else {\n throw new Error(\"Formatter Spec needs persistence unit to be specified\");\n }\n }\n\n if (format.units) {\n let convertFromUnit = inputUnit;\n for (const unit of format.units) {\n let unitConversion: UnitConversionProps;\n if (convertFromUnit) {\n unitConversion = await unitsProvider.getConversion(convertFromUnit, unit[0]);\n } else {\n unitConversion = { factor: 1.0, offset: 0.0 };\n }\n const unitLabel = (unit[1] && unit[1].length > 0) ? unit[1] : unit[0].label;\n const spec = ({ name: unit[0].name, label: unitLabel, conversion: unitConversion, system: unit[0].system }) as UnitConversionSpec;\n\n conversions.push(spec);\n convertFromUnit = unit[0];\n }\n } else {\n // if format is only numeric and a input unit is defined set spec to use the input unit as the format unit\n if (inputUnit) {\n const spec: UnitConversionSpec = { name: inputUnit.name, label: inputUnit.label, system: inputUnit.system, conversion: { factor: 1.0, offset: 0.0 } };\n conversions.push(spec);\n }\n }\n\n return conversions;\n }\n\n /** Static async method to create a FormatSpec given the format and unit of the quantity that will be passed to the Formatter. The input unit will\n * be used to generate conversion information for each unit specified in the Format. This method is async due to the fact that the units provider must make\n * async calls to lookup unit definitions.\n * @param name The name of a format specification.\n * @param unitsProvider The units provider is used to look up unit definitions and provide conversion information for converting between units.\n * @param inputUnit The unit the value to be formatted. This unit is often referred to as persistence unit.\n */\n public static async create(name: string, format: Format, unitsProvider: UnitsProvider, inputUnit?: UnitProps): Promise<FormatterSpec> {\n const conversions: UnitConversionSpec[] = await FormatterSpec.getUnitConversions(format, unitsProvider, inputUnit);\n let azimuthBaseConversion: UnitConversionProps | undefined;\n if (format.azimuthBaseUnit !== undefined) {\n if (inputUnit !== undefined) {\n azimuthBaseConversion = await unitsProvider.getConversion(format.azimuthBaseUnit, inputUnit);\n } else {\n azimuthBaseConversion = { factor: 1.0, offset: 0.0 };\n }\n }\n let revolutionConversion: UnitConversionProps | undefined;\n if (format.revolutionUnit !== undefined) {\n if (inputUnit !== undefined) {\n revolutionConversion = await unitsProvider.getConversion(format.revolutionUnit, inputUnit);\n } else {\n revolutionConversion = { factor: 1.0, offset: 0.0 };\n }\n }\n\n return new FormatterSpec(name, format, conversions, inputUnit, azimuthBaseConversion, revolutionConversion);\n }\n\n /** Format a quantity value. */\n public applyFormatting(magnitude: number): string {\n return Formatter.formatQuantity(magnitude, this);\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"FormatterSpec.js","sourceRoot":"","sources":["../../../src/Formatter/FormatterSpec.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAIH,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAExC,sFAAsF;AACtF,6JAA6J;AAE7J;;;GAGG;AACH,MAAM,OAAO,aAAa;IACd,KAAK,CAAS;IACd,YAAY,GAAyB,EAAE,CAAC,CAAE,mBAAmB;IAC7D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C;gBAC5B,oBAAoB,GAAG,MAAM,aAAa,CAAC,aAAa,CAAC,MAAM,CAAC,cAAc,EAAE,SAAS,CAAC,CAAC;YAC7F,CAAC;iBAAM,CAAC;gBACN,oBAAoB,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,CAAC;YACtD,CAAC;QACH,CAAC;QAED,OAAO,IAAI,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,EAAE,qBAAqB,EAAE,oBAAoB,CAAC,CAAC;IAC9G,CAAC;IAED,+BAA+B;IACxB,eAAe,CAAC,SAAiB;QACtC,OAAO,SAAS,CAAC,cAAc,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACnD,CAAC;CACF","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\n/** @packageDocumentation\n * @module Quantity\n */\n\nimport { UnitConversionProps, UnitConversionSpec, UnitProps, UnitsProvider } from \"../Interfaces\";\nimport { Format } from \"./Format\";\nimport { Formatter } from \"./Formatter\";\n\n// cSpell:ignore ZERONORMALIZED, nosign, onlynegative, signalways, negativeparentheses\n// cSpell:ignore trailzeroes, keepsinglezero, zeroempty, keepdecimalpoint, applyrounding, fractiondash, showunitlabel, prependunitlabel, exponentonlynegative\n\n/** A class that contains both formatting information and the conversion factors necessary to convert from an input unit to the units specified in the format.\n * Once created the FormatterSpec will be able to format quantity values with synchronous calls.\n * @beta\n */\nexport class FormatterSpec {\n protected _name: string;\n protected _conversions: UnitConversionSpec[] = []; // max four entries\n protected _format: Format;\n protected _persistenceUnit: UnitProps;\n protected _azimuthBaseConversion?: UnitConversionProps; // converts azimuth base unit to persistence unit\n protected _revolutionConversion?: UnitConversionProps; // converts revolution unit to persistence unit\n\n /** Constructor\n * @param name The name of a format specification.\n * @param format Defines the output format for the quantity value.\n * @param conversions An array of conversion factors necessary to convert from an input unit to the units specified in the format.\n * @param persistenceUnit The unit the magnitude value is input.\n * @param azimuthBaseConversion The conversion used to interpret azimuth base values.\n * @param revolutionConversion The conversion used to determine a revolution value (used for bearing and azimuth).\n */\n constructor(name: string, format: Format, conversions?: UnitConversionSpec[], persistenceUnit?: UnitProps, azimuthBaseConversion?: UnitConversionProps, revolutionConversion?: UnitConversionProps) {\n if (!persistenceUnit) {\n if (format.units) {\n const [props] = format.units[0];\n persistenceUnit = props;\n } else {\n throw new Error(\"Formatter Spec needs persistence unit to be specified\");\n }\n }\n\n this._name = name;\n this._format = format;\n this._persistenceUnit = persistenceUnit;\n if (conversions)\n this._conversions = conversions;\n this._azimuthBaseConversion = azimuthBaseConversion;\n this._revolutionConversion = revolutionConversion;\n }\n\n public get name(): string { return this._name; }\n /** Returns an array of UnitConversionSpecs, one for each unit that is to be shown in the formatted quantity string. */\n public get unitConversions(): UnitConversionSpec[] { return this._conversions; }\n public get format(): Format { return this._format; }\n public get persistenceUnit(): UnitProps { return this._persistenceUnit; }\n public get azimuthBaseConversion(): UnitConversionProps | undefined { return this._azimuthBaseConversion; }\n public get revolutionConversion(): UnitConversionProps | undefined { return this._revolutionConversion; }\n\n /** Get an array of UnitConversionSpecs, one for each unit that is to be shown in the formatted quantity string. */\n public static async getUnitConversions(format: Format, unitsProvider: UnitsProvider, inputUnit?: UnitProps): Promise<UnitConversionSpec[]> {\n const conversions: UnitConversionSpec[] = [];\n let persistenceUnit = inputUnit;\n if (!persistenceUnit) {\n if (format.units) {\n const [props] = format.units[0];\n persistenceUnit = props;\n } else {\n throw new Error(\"Formatter Spec needs persistence unit to be specified\");\n }\n }\n\n if (format.units) {\n let convertFromUnit = inputUnit;\n for (const unit of format.units) {\n let unitConversion: UnitConversionProps;\n if (convertFromUnit) {\n unitConversion = await unitsProvider.getConversion(convertFromUnit, unit[0]);\n } else {\n unitConversion = { factor: 1.0, offset: 0.0 };\n }\n const unitLabel = (unit[1] && unit[1].length > 0) ? unit[1] : unit[0].label;\n const spec = ({ name: unit[0].name, label: unitLabel, conversion: unitConversion, system: unit[0].system }) as UnitConversionSpec;\n\n conversions.push(spec);\n convertFromUnit = unit[0];\n }\n } else {\n // if format is only numeric and a input unit is defined set spec to use the input unit as the format unit\n if (inputUnit) {\n const spec: UnitConversionSpec = { name: inputUnit.name, label: inputUnit.label, system: inputUnit.system, conversion: { factor: 1.0, offset: 0.0 } };\n conversions.push(spec);\n }\n }\n\n return conversions;\n }\n\n /** Static async method to create a FormatSpec given the format and unit of the quantity that will be passed to the Formatter. The input unit will\n * be used to generate conversion information for each unit specified in the Format. This method is async due to the fact that the units provider must make\n * async calls to lookup unit definitions.\n * @param name The name of a format specification.\n * @param unitsProvider The units provider is used to look up unit definitions and provide conversion information for converting between units.\n * @param inputUnit The unit the value to be formatted. This unit is often referred to as persistence unit.\n */\n public static async create(name: string, format: Format, unitsProvider: UnitsProvider, inputUnit?: UnitProps): Promise<FormatterSpec> {\n const conversions: UnitConversionSpec[] = await FormatterSpec.getUnitConversions(format, unitsProvider, inputUnit);\n let azimuthBaseConversion: UnitConversionProps | undefined;\n if (format.azimuthBaseUnit !== undefined) {\n if (inputUnit !== undefined) {\n azimuthBaseConversion = await unitsProvider.getConversion(format.azimuthBaseUnit, inputUnit);\n } else {\n azimuthBaseConversion = { factor: 1.0, offset: 0.0 };\n }\n }\n let revolutionConversion: UnitConversionProps | undefined;\n if (format.revolutionUnit !== undefined) {\n if (inputUnit !== undefined) {\n revolutionConversion = await unitsProvider.getConversion(format.revolutionUnit, inputUnit);\n } else {\n revolutionConversion = { factor: 1.0, offset: 0.0 };\n }\n }\n\n return new FormatterSpec(name, format, conversions, inputUnit, azimuthBaseConversion, revolutionConversion);\n }\n\n /** Format a quantity value. */\n public applyFormatting(magnitude: number): string {\n return Formatter.formatQuantity(magnitude, this);\n }\n}\n"]}
|
package/lib/esm/Parser.d.ts
CHANGED
|
@@ -53,6 +53,7 @@ declare class ParseToken {
|
|
|
53
53
|
constructor(value: string | number | Operator);
|
|
54
54
|
get isString(): boolean;
|
|
55
55
|
get isNumber(): boolean;
|
|
56
|
+
get isSpecialCharacter(): boolean;
|
|
56
57
|
}
|
|
57
58
|
/** A Parser class that is used to break a string that represents a quantity value into tokens.
|
|
58
59
|
* @beta
|
package/lib/esm/Parser.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Parser.d.ts","sourceRoot":"","sources":["../../src/Parser.ts"],"names":[],"mappings":"AAIA;;GAEG;AAIH,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,OAAO,EAAE,2BAA2B,EAAE,kBAAkB,EAAE,aAAa,EAAuB,kBAAkB,EAAE,SAAS,EAAE,aAAa,EAAE,MAAM,cAAc,CAAC;AACjK,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAG1C;;GAEG;AACH,oBAAY,UAAU;IACpB,2BAA2B,IAAI;IAC/B,0BAA0B,IAAA;IAC1B,8BAA8B,IAAA;IAC9B,WAAW,IAAA;IACX,oCAAoC,IAAA;IACpC,iBAAiB,IAAA;IACjB,4BAA4B,IAAA;IAC5B,uCAAuC,IAAA;CACxC;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,uDAAuD;IACvD,EAAE,EAAE,KAAK,CAAC;IACV,uDAAuD;IACvD,KAAK,EAAE,UAAU,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,cAAc;IAC7B,uDAAuD;IACvD,EAAE,EAAE,IAAI,CAAC;IACT,4CAA4C;IAC5C,KAAK,EAAE,MAAM,CAAC;CACf;AAED,aAAK,QAAQ;IACX,QAAQ,MAAM;IACd,WAAW,MAAM;CAClB;AAUD;;;GAGG;AACH,MAAM,MAAM,mBAAmB,GAAG,cAAc,GAAG,kBAAkB,CAAC;AAEtE;;GAEG;AACH,cAAM,UAAU;IACP,KAAK,EAAE,MAAM,GAAG,MAAM,GAAG,QAAQ,CAAC;IAClC,UA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MAAM,EAAE,gBAAgB,EAAE,kBAAkB,EAAE,GAAG,mBAAmB;IAyBjI,OAAO,CAAC,MAAM,CAAC,kBAAkB;IA8EjC,OAAO,CAAC,MAAM,CAAC,kBAAkB;IAuCjC,OAAO,CAAC,MAAM,CAAC,gBAAgB;IA0D/B,OAAO,CAAC,MAAM,CAAC,cAAc;IAS7B,OAAO,CAAC,MAAM,CAAC,aAAa;IAc5B,OAAO,CAAC,MAAM,CAAC,qBAAqB;IAepC,+HAA+H;WAC3G,gCAAgC,CAAC,aAAa,EAAE,aAAa,EAAE,OAAO,EAAE,SAAS,EAAE,qBAAqB,CAAC,EAAE,2BAA2B,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC;IA4B1L,+HAA+H;WAC3G,yBAAyB,CAAC,aAAa,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,EAAE,qBAAqB,CAAC,EAAE,2BAA2B,GAAG,OAAO,CAAC,kBAAkB,EAAE,CAAC;CAgDhO"}
|
package/lib/esm/Parser.js
CHANGED
|
@@ -39,8 +39,9 @@ function isOperator(char) {
|
|
|
39
39
|
* @beta
|
|
40
40
|
*/
|
|
41
41
|
class ParseToken {
|
|
42
|
+
value;
|
|
43
|
+
isOperator = false;
|
|
42
44
|
constructor(value) {
|
|
43
|
-
this.isOperator = false;
|
|
44
45
|
if (typeof value === "string") {
|
|
45
46
|
this.value = value.trim();
|
|
46
47
|
this.isOperator = isOperator(this.value);
|
|
@@ -51,13 +52,18 @@ class ParseToken {
|
|
|
51
52
|
}
|
|
52
53
|
get isString() { return !this.isOperator && typeof this.value === "string"; }
|
|
53
54
|
get isNumber() { return typeof this.value === "number"; }
|
|
55
|
+
get isSpecialCharacter() {
|
|
56
|
+
const format = /^[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]+$/;
|
|
57
|
+
return this.isString && this.value.toString().match(format) !== null;
|
|
58
|
+
}
|
|
54
59
|
}
|
|
55
60
|
/** A ScientificToken holds an index and string representing the exponent.
|
|
56
61
|
* @beta
|
|
57
62
|
*/
|
|
58
63
|
class ScientificToken {
|
|
64
|
+
index;
|
|
65
|
+
exponent = "";
|
|
59
66
|
constructor(index, exponent) {
|
|
60
|
-
this.exponent = "";
|
|
61
67
|
this.index = index;
|
|
62
68
|
if (exponent)
|
|
63
69
|
this.exponent = exponent;
|
|
@@ -67,9 +73,10 @@ class ScientificToken {
|
|
|
67
73
|
* @beta
|
|
68
74
|
*/
|
|
69
75
|
class FractionToken {
|
|
76
|
+
index;
|
|
77
|
+
fraction = 0.0;
|
|
78
|
+
exponent = "";
|
|
70
79
|
constructor(index, fraction) {
|
|
71
|
-
this.fraction = 0.0;
|
|
72
|
-
this.exponent = "";
|
|
73
80
|
this.index = index;
|
|
74
81
|
if (fraction)
|
|
75
82
|
this.fraction = fraction;
|
|
@@ -79,6 +86,7 @@ class FractionToken {
|
|
|
79
86
|
* @beta
|
|
80
87
|
*/
|
|
81
88
|
export class Parser {
|
|
89
|
+
static _log = false;
|
|
82
90
|
static isParsedQuantity(item) {
|
|
83
91
|
return item.ok;
|
|
84
92
|
}
|
|
@@ -266,7 +274,20 @@ export class Parser {
|
|
|
266
274
|
}
|
|
267
275
|
else {
|
|
268
276
|
// not processing a number
|
|
269
|
-
|
|
277
|
+
const isCharOperator = isOperator(charCode);
|
|
278
|
+
const isSpacer = charCode === format.spacerOrDefault.charCodeAt(0) && charCode !== QuantityConstants.CHAR_SPACE;
|
|
279
|
+
if (isSpacer && i > 0 && i < str.length - 1) {
|
|
280
|
+
const prevCharCode = str.charCodeAt(i - 1);
|
|
281
|
+
if (isCharOperator && prevCharCode !== QuantityConstants.CHAR_SPACE) {
|
|
282
|
+
// ignore spacer if it's not at the start or end, not whitespace, and is not in front of a whitespace
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
if (wipToken.length === 0 && charCode === QuantityConstants.CHAR_SPACE) {
|
|
287
|
+
// Don't add space when the wip token is empty.
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
if (isCharOperator) {
|
|
270
291
|
if (wipToken.length > 0) {
|
|
271
292
|
// There is a token is progress, process it now, before adding the new operator token.
|
|
272
293
|
tokens.push(new ParseToken(wipToken));
|
|
@@ -275,10 +296,6 @@ export class Parser {
|
|
|
275
296
|
tokens.push(new ParseToken(str[i])); // Push an Operator Token in the list.
|
|
276
297
|
continue;
|
|
277
298
|
}
|
|
278
|
-
if (wipToken.length === 0 && charCode === QuantityConstants.CHAR_SPACE) {
|
|
279
|
-
// Don't add space when the wip token is empty.
|
|
280
|
-
continue;
|
|
281
|
-
}
|
|
282
299
|
wipToken = wipToken.concat(str[i]);
|
|
283
300
|
}
|
|
284
301
|
}
|
|
@@ -289,7 +306,12 @@ export class Parser {
|
|
|
289
306
|
if (signToken.length > 0) {
|
|
290
307
|
wipToken = signToken + wipToken;
|
|
291
308
|
}
|
|
292
|
-
|
|
309
|
+
if (isNaN(Number(wipToken))) {
|
|
310
|
+
tokens.push(new ParseToken(NaN));
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
tokens.push(new ParseToken(parseFloat(wipToken)));
|
|
314
|
+
}
|
|
293
315
|
}
|
|
294
316
|
else {
|
|
295
317
|
tokens.push(new ParseToken(wipToken));
|
|
@@ -387,8 +409,6 @@ export class Parser {
|
|
|
387
409
|
*/
|
|
388
410
|
static async parseIntoQuantity(inString, format, unitsProvider, altUnitLabelsProvider) {
|
|
389
411
|
const tokens = Parser.parseQuantitySpecification(inString, format);
|
|
390
|
-
if (tokens.length === 0 || (!format.allowMathematicOperations && Parser.isMathematicOperation(tokens)))
|
|
391
|
-
return new Quantity();
|
|
392
412
|
return Parser.createQuantityFromParseTokens(tokens, format, unitsProvider, altUnitLabelsProvider);
|
|
393
413
|
}
|
|
394
414
|
/** method to get the Unit Conversion given a unit label */
|
|
@@ -481,6 +501,14 @@ export class Parser {
|
|
|
481
501
|
* Accumulate the given list of tokens into a single quantity value. Formatting the tokens along the way.
|
|
482
502
|
*/
|
|
483
503
|
static getQuantityValueFromParseTokens(tokens, format, unitsConversions, defaultUnitConversion) {
|
|
504
|
+
if (tokens.length === 0)
|
|
505
|
+
return { ok: false, error: ParseError.UnableToGenerateParseTokens };
|
|
506
|
+
if (!format.allowMathematicOperations && Parser.isMathematicOperation(tokens))
|
|
507
|
+
return { ok: false, error: ParseError.MathematicOperationFoundButIsNotAllowed };
|
|
508
|
+
if (tokens.some((token) => token.isNumber && isNaN(token.value)) ||
|
|
509
|
+
tokens.every((token) => token.isSpecialCharacter)) {
|
|
510
|
+
return { ok: false, error: ParseError.UnableToConvertParseTokensToQuantity };
|
|
511
|
+
}
|
|
484
512
|
const defaultUnit = format.units && format.units.length > 0 ? format.units[0][0] : undefined;
|
|
485
513
|
defaultUnitConversion = defaultUnitConversion ? defaultUnitConversion : Parser.getDefaultUnitConversion(tokens, unitsConversions, defaultUnit);
|
|
486
514
|
let tokenPair;
|
|
@@ -510,10 +538,7 @@ export class Parser {
|
|
|
510
538
|
let value = sign * tokenPair[0].value;
|
|
511
539
|
let conversion;
|
|
512
540
|
if (tokenPair.length === 2 && tokenPair[1].isString) {
|
|
513
|
-
|
|
514
|
-
if (tokenPair[1].value !== spacer) { // ignore spacer
|
|
515
|
-
conversion = Parser.tryFindUnitConversion(tokenPair[1].value, unitsConversions, defaultUnit);
|
|
516
|
-
}
|
|
541
|
+
conversion = Parser.tryFindUnitConversion(tokenPair[1].value, unitsConversions, defaultUnit);
|
|
517
542
|
}
|
|
518
543
|
if (!conversion) {
|
|
519
544
|
if (compositeUnitIndex > 0 && format.units && format.units.length > compositeUnitIndex) {
|
|
@@ -598,6 +623,16 @@ export class Parser {
|
|
|
598
623
|
return this.parseAndProcessTokens(inString, format, unitsConversions);
|
|
599
624
|
}
|
|
600
625
|
static parseBearingFormat(inString, spec) {
|
|
626
|
+
const specialDirections = {
|
|
627
|
+
n: 0,
|
|
628
|
+
ne: 45,
|
|
629
|
+
e: 90,
|
|
630
|
+
se: 135,
|
|
631
|
+
s: 180,
|
|
632
|
+
sw: 225,
|
|
633
|
+
w: 270,
|
|
634
|
+
nw: 315,
|
|
635
|
+
};
|
|
601
636
|
// TODO: at some point we will want to open this for localization, in the first release it's going to be hard coded
|
|
602
637
|
let DirectionLabel;
|
|
603
638
|
(function (DirectionLabel) {
|
|
@@ -609,28 +644,36 @@ export class Parser {
|
|
|
609
644
|
let matchedPrefix = null;
|
|
610
645
|
let matchedSuffix = null;
|
|
611
646
|
// check if input string begins with northLabel or southLabel and strip it off
|
|
612
|
-
if (inString.startsWith(DirectionLabel.North)) {
|
|
647
|
+
if (inString.toUpperCase().startsWith(DirectionLabel.North)) {
|
|
613
648
|
inString = inString.substring(DirectionLabel.North.length);
|
|
614
649
|
matchedPrefix = DirectionLabel.North;
|
|
615
650
|
}
|
|
616
|
-
else if (inString.startsWith(DirectionLabel.South)) {
|
|
651
|
+
else if (inString.toUpperCase().startsWith(DirectionLabel.South)) {
|
|
617
652
|
inString = inString.substring(DirectionLabel.South.length);
|
|
618
653
|
matchedPrefix = DirectionLabel.South;
|
|
619
654
|
}
|
|
620
|
-
// check if input string ends with eastLabel or westLabel and strip it off
|
|
621
|
-
if (inString.endsWith(DirectionLabel.East)) {
|
|
655
|
+
// check if input string ends with eastLabel or westLabel (case-insensitive) and strip it off
|
|
656
|
+
if (inString.toUpperCase().endsWith(DirectionLabel.East)) {
|
|
622
657
|
inString = inString.substring(0, inString.length - DirectionLabel.East.length);
|
|
623
658
|
matchedSuffix = DirectionLabel.East;
|
|
624
659
|
}
|
|
625
|
-
else if (inString.endsWith(DirectionLabel.West)) {
|
|
660
|
+
else if (inString.toUpperCase().endsWith(DirectionLabel.West)) {
|
|
626
661
|
inString = inString.substring(0, inString.length - DirectionLabel.West.length);
|
|
627
662
|
matchedSuffix = DirectionLabel.West;
|
|
628
663
|
}
|
|
664
|
+
// check if the remaining string is a special direction
|
|
665
|
+
if (inString.trim() === "") {
|
|
666
|
+
const prefix = matchedPrefix?.toLowerCase() || "";
|
|
667
|
+
const suffix = matchedSuffix?.toLowerCase() || "";
|
|
668
|
+
const specialDirection = specialDirections[`${prefix}${suffix}`];
|
|
669
|
+
if (specialDirection !== undefined)
|
|
670
|
+
return { ok: true, value: specialDirection };
|
|
671
|
+
}
|
|
629
672
|
if (matchedPrefix === null || matchedSuffix === null) {
|
|
630
673
|
return { ok: false, error: ParseError.BearingPrefixOrSuffixMissing };
|
|
631
674
|
}
|
|
632
675
|
const parsedResult = this.parseAndProcessTokens(inString, spec.format, spec.unitConversions);
|
|
633
|
-
if (this.isParseError(parsedResult)
|
|
676
|
+
if (this.isParseError(parsedResult)) {
|
|
634
677
|
return parsedResult;
|
|
635
678
|
}
|
|
636
679
|
let magnitude = parsedResult.value;
|
|
@@ -655,7 +698,7 @@ export class Parser {
|
|
|
655
698
|
}
|
|
656
699
|
static parseAzimuthFormat(inString, spec) {
|
|
657
700
|
const parsedResult = this.parseAndProcessTokens(inString, spec.format, spec.unitConversions);
|
|
658
|
-
if (this.isParseError(parsedResult)
|
|
701
|
+
if (this.isParseError(parsedResult)) {
|
|
659
702
|
return parsedResult;
|
|
660
703
|
}
|
|
661
704
|
let magnitude = parsedResult.value;
|
|
@@ -754,11 +797,6 @@ export class Parser {
|
|
|
754
797
|
}
|
|
755
798
|
static parseAndProcessTokens(inString, format, unitsConversions) {
|
|
756
799
|
const tokens = Parser.parseQuantitySpecification(inString, format);
|
|
757
|
-
if (tokens.length === 0)
|
|
758
|
-
return { ok: false, error: ParseError.UnableToGenerateParseTokens };
|
|
759
|
-
if (!format.allowMathematicOperations && Parser.isMathematicOperation(tokens)) {
|
|
760
|
-
return { ok: false, error: ParseError.MathematicOperationFoundButIsNotAllowed };
|
|
761
|
-
}
|
|
762
800
|
if (Parser._log) {
|
|
763
801
|
// eslint-disable-next-line no-console
|
|
764
802
|
console.log(`Parse tokens`);
|
|
@@ -842,5 +880,4 @@ export class Parser {
|
|
|
842
880
|
return unitConversionSpecs;
|
|
843
881
|
}
|
|
844
882
|
}
|
|
845
|
-
Parser._log = false;
|
|
846
883
|
//# sourceMappingURL=Parser.js.map
|