@itwin/core-quantity 4.9.0-dev.12 → 4.9.0-dev.14
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 +6 -1
- package/lib/cjs/Exception.d.ts +3 -1
- package/lib/cjs/Exception.d.ts.map +1 -1
- package/lib/cjs/Exception.js +2 -0
- package/lib/cjs/Exception.js.map +1 -1
- package/lib/cjs/Formatter/Format.d.ts +14 -0
- package/lib/cjs/Formatter/Format.d.ts.map +1 -1
- package/lib/cjs/Formatter/Format.js +58 -0
- package/lib/cjs/Formatter/Format.js.map +1 -1
- package/lib/cjs/Formatter/FormatEnums.d.ts +5 -1
- package/lib/cjs/Formatter/FormatEnums.d.ts.map +1 -1
- package/lib/cjs/Formatter/FormatEnums.js +10 -0
- package/lib/cjs/Formatter/FormatEnums.js.map +1 -1
- package/lib/cjs/Formatter/Formatter.d.ts +4 -0
- package/lib/cjs/Formatter/Formatter.d.ts.map +1 -1
- package/lib/cjs/Formatter/Formatter.js +114 -13
- package/lib/cjs/Formatter/Formatter.js.map +1 -1
- package/lib/cjs/Formatter/FormatterSpec.d.ts +8 -2
- package/lib/cjs/Formatter/FormatterSpec.d.ts.map +1 -1
- package/lib/cjs/Formatter/FormatterSpec.js +26 -2
- package/lib/cjs/Formatter/FormatterSpec.js.map +1 -1
- package/lib/cjs/Formatter/Interfaces.d.ts +11 -0
- package/lib/cjs/Formatter/Interfaces.d.ts.map +1 -1
- package/lib/cjs/Formatter/Interfaces.js.map +1 -1
- package/lib/cjs/Parser.d.ts +7 -1
- package/lib/cjs/Parser.d.ts.map +1 -1
- package/lib/cjs/Parser.js +158 -4
- package/lib/cjs/Parser.js.map +1 -1
- package/lib/cjs/ParserSpec.d.ts +5 -1
- package/lib/cjs/ParserSpec.d.ts.map +1 -1
- package/lib/cjs/ParserSpec.js +20 -1
- package/lib/cjs/ParserSpec.js.map +1 -1
- package/lib/esm/Exception.d.ts +3 -1
- package/lib/esm/Exception.d.ts.map +1 -1
- package/lib/esm/Exception.js +2 -0
- package/lib/esm/Exception.js.map +1 -1
- package/lib/esm/Formatter/Format.d.ts +14 -0
- package/lib/esm/Formatter/Format.d.ts.map +1 -1
- package/lib/esm/Formatter/Format.js +59 -1
- package/lib/esm/Formatter/Format.js.map +1 -1
- package/lib/esm/Formatter/FormatEnums.d.ts +5 -1
- package/lib/esm/Formatter/FormatEnums.d.ts.map +1 -1
- package/lib/esm/Formatter/FormatEnums.js +10 -0
- package/lib/esm/Formatter/FormatEnums.js.map +1 -1
- package/lib/esm/Formatter/Formatter.d.ts +4 -0
- package/lib/esm/Formatter/Formatter.d.ts.map +1 -1
- package/lib/esm/Formatter/Formatter.js +114 -13
- package/lib/esm/Formatter/Formatter.js.map +1 -1
- package/lib/esm/Formatter/FormatterSpec.d.ts +8 -2
- package/lib/esm/Formatter/FormatterSpec.d.ts.map +1 -1
- package/lib/esm/Formatter/FormatterSpec.js +26 -2
- package/lib/esm/Formatter/FormatterSpec.js.map +1 -1
- package/lib/esm/Formatter/Interfaces.d.ts +11 -0
- package/lib/esm/Formatter/Interfaces.d.ts.map +1 -1
- package/lib/esm/Formatter/Interfaces.js.map +1 -1
- package/lib/esm/Parser.d.ts +7 -1
- package/lib/esm/Parser.d.ts.map +1 -1
- package/lib/esm/Parser.js +158 -4
- package/lib/esm/Parser.js.map +1 -1
- package/lib/esm/ParserSpec.d.ts +5 -1
- package/lib/esm/ParserSpec.d.ts.map +1 -1
- package/lib/esm/ParserSpec.js +20 -1
- package/lib/esm/ParserSpec.js.map +1 -1
- package/package.json +4 -4
|
@@ -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,MA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AAS,CAAC,KAAK,EAAE,MAAM,EAAE,SAAS,CAAC,MAAM,EAAE,UAAU,EAAE,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,EAAE,CAAC;gBACtJ,WAAW,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YACzB,CAAC;QACH,CAAC;QAED,OAAO,WAAW,CAAC;IACrB,CAAC;IAED;;;;;;OAMG;IACI,MAAM,CAAC,KAAK,CAAC,MAAM,CAAC,IAAY,EAAE,MAAc,EAAE,aAA4B,EAAE,SAAqB;QAC1G,MAAM,WAAW,GAAyB,MAAM,aAAa,CAAC,kBAAkB,CAAC,MAAM,EAAE,aAAa,EAAE,SAAS,CAAC,CAAC;QACnH,OAAO,IAAI,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,WAAW,EAAE,SAAS,CAAC,CAAC;IACjE,CAAC;IAED,+BAA+B;IACxB,eAAe,CAAC,SAAiB;QACtC,OAAO,SAAS,CAAC,cAAc,CAAC,SAAS,EAAE,IAAI,CAAC,CAAC;IACnD,CAAC;CACF","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Quantity\r\n */\r\n\r\nimport { UnitConversionProps, UnitConversionSpec, UnitProps, UnitsProvider } from \"../Interfaces\";\r\nimport { Format } from \"./Format\";\r\nimport { Formatter } from \"./Formatter\";\r\n\r\n// cSpell:ignore ZERONORMALIZED, nosign, onlynegative, signalways, negativeparentheses\r\n// cSpell:ignore trailzeroes, keepsinglezero, zeroempty, keepdecimalpoint, applyrounding, fractiondash, showunitlabel, prependunitlabel, exponentonlynegative\r\n\r\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.\r\n * Once created the FormatterSpec will be able to format quantity values with synchronous calls.\r\n * @beta\r\n */\r\nexport class FormatterSpec {\r\n protected _name: string;\r\n protected _conversions: UnitConversionSpec[] = []; // max four entries\r\n protected _format: Format;\r\n protected _persistenceUnit: UnitProps;\r\n\r\n /** Constructor\r\n * @param name The name of a format specification.\r\n * @param format Defines the output format for the quantity value.\r\n * @param conversions An array of conversion factors necessary to convert from an input unit to the units specified in the format.\r\n * @param persistenceUnit The unit the magnitude value is input.\r\n */\r\n constructor(name: string, format: Format, conversions?: UnitConversionSpec[], persistenceUnit?: UnitProps) {\r\n if (!persistenceUnit) {\r\n if (format.units) {\r\n const [props] = format.units[0];\r\n persistenceUnit = props;\r\n } else {\r\n throw new Error(\"Formatter Spec needs persistence unit to be specified\");\r\n }\r\n }\r\n\r\n this._name = name;\r\n this._format = format;\r\n this._persistenceUnit = persistenceUnit;\r\n if (conversions)\r\n this._conversions = conversions;\r\n }\r\n\r\n public get name(): string { return this._name; }\r\n /** Returns an array of UnitConversionSpecs, one for each unit that is to be shown in the formatted quantity string. */\r\n public get unitConversions(): UnitConversionSpec[] { return this._conversions; }\r\n public get format(): Format { return this._format; }\r\n public get persistenceUnit(): UnitProps { return this._persistenceUnit; }\r\n\r\n /** Get an array of UnitConversionSpecs, one for each unit that is to be shown in the formatted quantity string. */\r\n public static async getUnitConversions(format: Format, unitsProvider: UnitsProvider, inputUnit?: UnitProps): Promise<UnitConversionSpec[]> {\r\n const conversions: UnitConversionSpec[] = [];\r\n let persistenceUnit = inputUnit;\r\n if (!persistenceUnit) {\r\n if (format.units) {\r\n const [props] = format.units[0];\r\n persistenceUnit = props;\r\n } else {\r\n throw new Error(\"Formatter Spec needs persistence unit to be specified\");\r\n }\r\n }\r\n\r\n if (format.units) {\r\n let convertFromUnit = inputUnit;\r\n for (const unit of format.units) {\r\n let unitConversion: UnitConversionProps;\r\n if (convertFromUnit) {\r\n unitConversion = await unitsProvider.getConversion(convertFromUnit, unit[0]);\r\n } else {\r\n unitConversion = { factor: 1.0, offset: 0.0 };\r\n }\r\n const unitLabel = (unit[1] && unit[1].length > 0) ? unit[1] : unit[0].label;\r\n const spec = ({ name: unit[0].name, label: unitLabel, conversion: unitConversion, system: unit[0].system }) as UnitConversionSpec;\r\n\r\n conversions.push(spec);\r\n convertFromUnit = unit[0];\r\n }\r\n } else {\r\n // if format is only numeric and a input unit is defined set spec to use the input unit as the format unit\r\n if (inputUnit) {\r\n const spec: UnitConversionSpec = { name: inputUnit.name, label: inputUnit.label, system: inputUnit.system, conversion: { factor: 1.0, offset: 0.0 } };\r\n conversions.push(spec);\r\n }\r\n }\r\n\r\n return conversions;\r\n }\r\n\r\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\r\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\r\n * async calls to lookup unit definitions.\r\n * @param name The name of a format specification.\r\n * @param unitsProvider The units provider is used to look up unit definitions and provide conversion information for converting between units.\r\n * @param inputUnit The unit the value to be formatted. This unit is often referred to as persistence unit.\r\n */\r\n public static async create(name: string, format: Format, unitsProvider: UnitsProvider, inputUnit?: UnitProps): Promise<FormatterSpec> {\r\n const conversions: UnitConversionSpec[] = await FormatterSpec.getUnitConversions(format, unitsProvider, inputUnit);\r\n return new FormatterSpec(name, format, conversions, inputUnit);\r\n }\r\n\r\n /** Format a quantity value. */\r\n public applyFormatting(magnitude: number): string {\r\n return Formatter.formatQuantity(magnitude, this);\r\n }\r\n}\r\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;IAQxB;;;;;;;OAOG;IACH,YAAY,IAAY,EAAE,MAAc,EAAE,WAAkC,EAAE,eAA2B,EAAE,qBAA2C,EAAE,oBAA0C;QAdxL,iBAAY,GAAyB,EAAE,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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":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Quantity\r\n */\r\n\r\nimport { UnitConversionProps, UnitConversionSpec, UnitProps, UnitsProvider } from \"../Interfaces\";\r\nimport { Format } from \"./Format\";\r\nimport { Formatter } from \"./Formatter\";\r\n\r\n// cSpell:ignore ZERONORMALIZED, nosign, onlynegative, signalways, negativeparentheses\r\n// cSpell:ignore trailzeroes, keepsinglezero, zeroempty, keepdecimalpoint, applyrounding, fractiondash, showunitlabel, prependunitlabel, exponentonlynegative\r\n\r\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.\r\n * Once created the FormatterSpec will be able to format quantity values with synchronous calls.\r\n * @beta\r\n */\r\nexport class FormatterSpec {\r\n protected _name: string;\r\n protected _conversions: UnitConversionSpec[] = []; // max four entries\r\n protected _format: Format;\r\n protected _persistenceUnit: UnitProps;\r\n protected _azimuthBaseConversion?: UnitConversionProps; // converts azimuth base unit to persistence unit\r\n protected _revolutionConversion?: UnitConversionProps; // converts revolution unit to persistence unit\r\n\r\n /** Constructor\r\n * @param name The name of a format specification.\r\n * @param format Defines the output format for the quantity value.\r\n * @param conversions An array of conversion factors necessary to convert from an input unit to the units specified in the format.\r\n * @param persistenceUnit The unit the magnitude value is input.\r\n * @param azimuthBaseConversion The conversion used to interpret azimuth base values.\r\n * @param revolutionConversion The conversion used to determine a revolution value (used for bearing and azimuth).\r\n */\r\n constructor(name: string, format: Format, conversions?: UnitConversionSpec[], persistenceUnit?: UnitProps, azimuthBaseConversion?: UnitConversionProps, revolutionConversion?: UnitConversionProps) {\r\n if (!persistenceUnit) {\r\n if (format.units) {\r\n const [props] = format.units[0];\r\n persistenceUnit = props;\r\n } else {\r\n throw new Error(\"Formatter Spec needs persistence unit to be specified\");\r\n }\r\n }\r\n\r\n this._name = name;\r\n this._format = format;\r\n this._persistenceUnit = persistenceUnit;\r\n if (conversions)\r\n this._conversions = conversions;\r\n this._azimuthBaseConversion = azimuthBaseConversion;\r\n this._revolutionConversion = revolutionConversion;\r\n }\r\n\r\n public get name(): string { return this._name; }\r\n /** Returns an array of UnitConversionSpecs, one for each unit that is to be shown in the formatted quantity string. */\r\n public get unitConversions(): UnitConversionSpec[] { return this._conversions; }\r\n public get format(): Format { return this._format; }\r\n public get persistenceUnit(): UnitProps { return this._persistenceUnit; }\r\n public get azimuthBaseConversion(): UnitConversionProps | undefined { return this._azimuthBaseConversion; }\r\n public get revolutionConversion(): UnitConversionProps | undefined { return this._revolutionConversion; }\r\n\r\n /** Get an array of UnitConversionSpecs, one for each unit that is to be shown in the formatted quantity string. */\r\n public static async getUnitConversions(format: Format, unitsProvider: UnitsProvider, inputUnit?: UnitProps): Promise<UnitConversionSpec[]> {\r\n const conversions: UnitConversionSpec[] = [];\r\n let persistenceUnit = inputUnit;\r\n if (!persistenceUnit) {\r\n if (format.units) {\r\n const [props] = format.units[0];\r\n persistenceUnit = props;\r\n } else {\r\n throw new Error(\"Formatter Spec needs persistence unit to be specified\");\r\n }\r\n }\r\n\r\n if (format.units) {\r\n let convertFromUnit = inputUnit;\r\n for (const unit of format.units) {\r\n let unitConversion: UnitConversionProps;\r\n if (convertFromUnit) {\r\n unitConversion = await unitsProvider.getConversion(convertFromUnit, unit[0]);\r\n } else {\r\n unitConversion = { factor: 1.0, offset: 0.0 };\r\n }\r\n const unitLabel = (unit[1] && unit[1].length > 0) ? unit[1] : unit[0].label;\r\n const spec = ({ name: unit[0].name, label: unitLabel, conversion: unitConversion, system: unit[0].system }) as UnitConversionSpec;\r\n\r\n conversions.push(spec);\r\n convertFromUnit = unit[0];\r\n }\r\n } else {\r\n // if format is only numeric and a input unit is defined set spec to use the input unit as the format unit\r\n if (inputUnit) {\r\n const spec: UnitConversionSpec = { name: inputUnit.name, label: inputUnit.label, system: inputUnit.system, conversion: { factor: 1.0, offset: 0.0 } };\r\n conversions.push(spec);\r\n }\r\n }\r\n\r\n return conversions;\r\n }\r\n\r\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\r\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\r\n * async calls to lookup unit definitions.\r\n * @param name The name of a format specification.\r\n * @param unitsProvider The units provider is used to look up unit definitions and provide conversion information for converting between units.\r\n * @param inputUnit The unit the value to be formatted. This unit is often referred to as persistence unit.\r\n */\r\n public static async create(name: string, format: Format, unitsProvider: UnitsProvider, inputUnit?: UnitProps): Promise<FormatterSpec> {\r\n const conversions: UnitConversionSpec[] = await FormatterSpec.getUnitConversions(format, unitsProvider, inputUnit);\r\n let azimuthBaseConversion: UnitConversionProps | undefined;\r\n if (format.azimuthBaseUnit !== undefined) {\r\n if (inputUnit !== undefined) {\r\n azimuthBaseConversion = await unitsProvider.getConversion(format.azimuthBaseUnit, inputUnit);\r\n } else {\r\n azimuthBaseConversion = { factor: 1.0, offset: 0.0 };\r\n }\r\n }\r\n let revolutionConversion: UnitConversionProps | undefined;\r\n if (format.revolutionUnit !== undefined) {\r\n if (inputUnit !== undefined) {\r\n revolutionConversion = await unitsProvider.getConversion(format.revolutionUnit, inputUnit);\r\n } else {\r\n revolutionConversion = { factor: 1.0, offset: 0.0 };\r\n }\r\n }\r\n\r\n return new FormatterSpec(name, format, conversions, inputUnit, azimuthBaseConversion, revolutionConversion);\r\n }\r\n\r\n /** Format a quantity value. */\r\n public applyFormatting(magnitude: number): string {\r\n return Formatter.formatQuantity(magnitude, this);\r\n }\r\n}\r\n"]}
|
|
@@ -16,11 +16,22 @@ export interface FormatProps {
|
|
|
16
16
|
readonly decimalSeparator?: string;
|
|
17
17
|
readonly thousandSeparator?: string;
|
|
18
18
|
readonly uomSeparator?: string;
|
|
19
|
+
/** conditionally required. */
|
|
19
20
|
readonly scientificType?: string;
|
|
21
|
+
/** conditionally required. */
|
|
20
22
|
readonly stationOffsetSize?: number;
|
|
21
23
|
readonly stationSeparator?: string;
|
|
24
|
+
/** The base value for azimuth, specified from east counter-clockwise. */
|
|
25
|
+
readonly azimuthBase?: number;
|
|
26
|
+
/** The name of the unit for the azimuth base value. */
|
|
27
|
+
readonly azimuthBaseUnit?: string;
|
|
28
|
+
/** If set to true, azimuth values are returned counter-clockwise from the base. */
|
|
29
|
+
readonly azimuthCounterClockwise?: boolean;
|
|
30
|
+
/** The name of the unit that represents a revolution/perigon. Required for bearing or azimuth types. */
|
|
31
|
+
readonly revolutionUnit?: string;
|
|
22
32
|
readonly allowMathematicOperations?: boolean;
|
|
23
33
|
readonly composite?: {
|
|
34
|
+
/** separates values when formatting composite strings */
|
|
24
35
|
readonly spacer?: string;
|
|
25
36
|
readonly includeZero?: boolean;
|
|
26
37
|
readonly units: Array<{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Interfaces.d.ts","sourceRoot":"","sources":["../../../src/Formatter/Interfaces.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH,OAAO,EAAE,SAAS,EAAE,MAAM,eAAe,CAAC;AAC1C,OAAO,EAAE,gBAAgB,EAAE,YAAY,EAAE,UAAU,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC;AAEhG;;GAEG;AACH,MAAM,WAAW,WAAW;IAC1B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,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W,CAAC,EAAE,SAAS,CAAC;CACzB"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Interfaces.js","sourceRoot":"","sources":["../../../src/Formatter/Interfaces.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;
|
|
1
|
+
{"version":3,"file":"Interfaces.js","sourceRoot":"","sources":["../../../src/Formatter/Interfaces.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAyDH;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,IAAiB,EAA6B,EAAE;IAClF,OAAQ,IAA0B,CAAC,MAAM,KAAK,SAAS,CAAC;AAC1D,CAAC,CAAC","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\n/** @packageDocumentation\r\n * @module Quantity\r\n */\r\n\r\nimport { UnitProps } from \"../Interfaces\";\r\nimport { DecimalPrecision, FormatTraits, FormatType, FractionalPrecision } from \"./FormatEnums\";\r\n\r\n/** This interface defines the persistence format for describing the formatting of quantity values.\r\n * @beta\r\n */\r\nexport interface FormatProps {\r\n readonly type: string;\r\n readonly precision?: number;\r\n readonly roundFactor?: number;\r\n readonly minWidth?: number;\r\n readonly showSignOption?: string;\r\n readonly formatTraits?: string | string[];\r\n readonly decimalSeparator?: string;\r\n readonly thousandSeparator?: string;\r\n readonly uomSeparator?: string;\r\n\r\n /** conditionally required. */\r\n readonly scientificType?: string;\r\n\r\n /** conditionally required. */\r\n readonly stationOffsetSize?: number;\r\n readonly stationSeparator?: string;\r\n\r\n /** The base value for azimuth, specified from east counter-clockwise. */\r\n readonly azimuthBase?: number;\r\n\r\n /** The name of the unit for the azimuth base value. */\r\n readonly azimuthBaseUnit?: string;\r\n\r\n /** If set to true, azimuth values are returned counter-clockwise from the base. */\r\n readonly azimuthCounterClockwise?: boolean;\r\n\r\n /** The name of the unit that represents a revolution/perigon. Required for bearing or azimuth types. */\r\n readonly revolutionUnit?: string;\r\n\r\n readonly allowMathematicOperations?: boolean;\r\n readonly composite?: {\r\n /** separates values when formatting composite strings */\r\n readonly spacer?: string;\r\n readonly includeZero?: boolean;\r\n readonly units: Array<{\r\n readonly name: string;\r\n readonly label?: string;\r\n }>;\r\n };\r\n}\r\n\r\n/** This interface is used when supporting Custom Formatters that need more than the standard set of properties.\r\n * @beta\r\n */\r\nexport interface CustomFormatProps extends FormatProps {\r\n readonly custom: any;\r\n}\r\n\r\n/** CustomFormatProps type guard.\r\n * @beta\r\n */\r\nexport const isCustomFormatProps = (item: FormatProps): item is CustomFormatProps => {\r\n return (item as CustomFormatProps).custom !== undefined;\r\n};\r\n\r\n/** CloneFormat defines unit and label specification if primary unit is to be set during clone.\r\n * @beta\r\n */\r\nexport interface CloneUnit {\r\n unit?: UnitProps;\r\n label?: string;\r\n}\r\n\r\n/** CloneOptions that define modifications that can be made during the cloning of a Format.\r\n * @beta\r\n */\r\nexport interface CloneOptions {\r\n /** allows composite formats to be converted to only show primary unit */\r\n showOnlyPrimaryUnit?: boolean;\r\n /** allow format traits to be set */\r\n traits?: FormatTraits;\r\n /** allows new FormatType to be specified */\r\n type?: FormatType;\r\n /** allows precision to be set, this will throw if value is not valid for FormatType */\r\n precision?: DecimalPrecision | FractionalPrecision;\r\n /** allows primary unit and label to be specified */\r\n primaryUnit?: CloneUnit;\r\n}\r\n"]}
|
package/lib/esm/Parser.d.ts
CHANGED
|
@@ -14,7 +14,8 @@ export declare enum ParseError {
|
|
|
14
14
|
UnknownUnit = 4,
|
|
15
15
|
UnableToConvertParseTokensToQuantity = 5,
|
|
16
16
|
InvalidParserSpec = 6,
|
|
17
|
-
|
|
17
|
+
BearingPrefixOrSuffixMissing = 7,
|
|
18
|
+
MathematicOperationFoundButIsNotAllowed = 8
|
|
18
19
|
}
|
|
19
20
|
/** Parse error result from [[Parser.parseToQuantityValue]] or [[Parser.parseToQuantityValue]].
|
|
20
21
|
* @beta
|
|
@@ -106,6 +107,11 @@ export declare class Parser {
|
|
|
106
107
|
* @param unitsConversions dictionary of conversions used to convert from unit used in inString to output quantity
|
|
107
108
|
*/
|
|
108
109
|
static parseToQuantityValue(inString: string, format: Format, unitsConversions: UnitConversionSpec[]): QuantityParseResult;
|
|
110
|
+
private static parseBearingFormat;
|
|
111
|
+
private static parseAzimuthFormat;
|
|
112
|
+
private static normalizeAngle;
|
|
113
|
+
private static getRevolution;
|
|
114
|
+
private static parseAndProcessTokens;
|
|
109
115
|
/** Async Method used to create an array of UnitConversionSpec entries that can be used in synchronous calls to parse units. */
|
|
110
116
|
static createUnitConversionSpecsForUnit(unitsProvider: UnitsProvider, outUnit: UnitProps, altUnitLabelsProvider?: AlternateUnitLabelsProvider): Promise<UnitConversionSpec[]>;
|
|
111
117
|
/** Async Method used to create an array of UnitConversionSpec entries that can be used in synchronous calls to parse units. */
|
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;
|
|
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,uCA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
|
*/
|
|
8
8
|
import { QuantityConstants } from "./Constants";
|
|
9
|
+
import { QuantityError, QuantityStatus } from "./Exception";
|
|
9
10
|
import { FormatTraits, FormatType } from "./Formatter/FormatEnums";
|
|
10
11
|
import { Quantity } from "./Quantity";
|
|
11
12
|
/** Possible parser errors
|
|
@@ -19,7 +20,8 @@ export var ParseError;
|
|
|
19
20
|
ParseError[ParseError["UnknownUnit"] = 4] = "UnknownUnit";
|
|
20
21
|
ParseError[ParseError["UnableToConvertParseTokensToQuantity"] = 5] = "UnableToConvertParseTokensToQuantity";
|
|
21
22
|
ParseError[ParseError["InvalidParserSpec"] = 6] = "InvalidParserSpec";
|
|
22
|
-
ParseError[ParseError["
|
|
23
|
+
ParseError[ParseError["BearingPrefixOrSuffixMissing"] = 7] = "BearingPrefixOrSuffixMissing";
|
|
24
|
+
ParseError[ParseError["MathematicOperationFoundButIsNotAllowed"] = 8] = "MathematicOperationFoundButIsNotAllowed";
|
|
23
25
|
})(ParseError || (ParseError = {}));
|
|
24
26
|
var Operator;
|
|
25
27
|
(function (Operator) {
|
|
@@ -487,6 +489,7 @@ export class Parser {
|
|
|
487
489
|
let mag = 0.0;
|
|
488
490
|
// The sign is saved outside from the loop for cases like this. '-1m 50cm 10mm + 2m 30cm 40mm' => -1.51m + 2.34m
|
|
489
491
|
let sign = 1;
|
|
492
|
+
let compositeUnitIndex = 0;
|
|
490
493
|
for (let i = 0; i < tokens.length; i = i + increment) {
|
|
491
494
|
tokenPair = this.getNextTokenPair(i, tokens);
|
|
492
495
|
if (!tokenPair || tokenPair.length === 0) {
|
|
@@ -497,6 +500,7 @@ export class Parser {
|
|
|
497
500
|
if (tokenPair[0].isOperator) {
|
|
498
501
|
sign = tokenPair[0].value === Operator.addition ? 1 : -1;
|
|
499
502
|
tokenPair.shift();
|
|
503
|
+
compositeUnitIndex = 0; // Reset the composite unit index, the following tokens begin from start.
|
|
500
504
|
}
|
|
501
505
|
// unit specification comes before value (like currency)
|
|
502
506
|
if (tokenPair.length === 2 && tokenPair[0].isString) {
|
|
@@ -507,13 +511,26 @@ export class Parser {
|
|
|
507
511
|
let value = sign * tokenPair[0].value;
|
|
508
512
|
let conversion;
|
|
509
513
|
if (tokenPair.length === 2 && tokenPair[1].isString) {
|
|
510
|
-
|
|
514
|
+
const spacer = format.spacerOrDefault;
|
|
515
|
+
if (tokenPair[1].value !== spacer) { // ignore spacer
|
|
516
|
+
conversion = Parser.tryFindUnitConversion(tokenPair[1].value, unitsConversions, defaultUnit);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
if (!conversion) {
|
|
520
|
+
if (compositeUnitIndex > 0 && format.units && format.units.length > compositeUnitIndex) {
|
|
521
|
+
// if this is not the first token, and we have a composite spec, look up the unit from the current index
|
|
522
|
+
const presUnitAtIndex = format.units[compositeUnitIndex][0];
|
|
523
|
+
conversion = Parser.tryFindUnitConversion(presUnitAtIndex.label, unitsConversions, presUnitAtIndex);
|
|
524
|
+
}
|
|
525
|
+
else if (defaultUnitConversion) {
|
|
526
|
+
conversion = defaultUnitConversion;
|
|
527
|
+
}
|
|
511
528
|
}
|
|
512
|
-
conversion = conversion ? conversion : defaultUnitConversion;
|
|
513
529
|
if (conversion) {
|
|
514
530
|
value = (value * conversion.factor) + conversion.offset;
|
|
515
531
|
}
|
|
516
532
|
mag = mag + value;
|
|
533
|
+
compositeUnitIndex++;
|
|
517
534
|
}
|
|
518
535
|
else {
|
|
519
536
|
// only the unit label was specified so assume magnitude of 0
|
|
@@ -531,7 +548,25 @@ export class Parser {
|
|
|
531
548
|
* @param parserSpec unit label if not explicitly defined by user. Must have matching entry in supplied array of unitsConversions.
|
|
532
549
|
*/
|
|
533
550
|
static parseQuantityString(inString, parserSpec) {
|
|
534
|
-
|
|
551
|
+
// ensure any labels defined in composite unit definition are specified in unitConversions
|
|
552
|
+
if (parserSpec.format.units) {
|
|
553
|
+
parserSpec.format.units.forEach(([unit, label]) => {
|
|
554
|
+
if (label) {
|
|
555
|
+
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
|
|
556
|
+
const unitConversion = parserSpec.unitConversions.find((conversion) => conversion.name === unit.name);
|
|
557
|
+
if (unitConversion && unitConversion.parseLabels && !unitConversion.parseLabels.find((entry) => entry === label))
|
|
558
|
+
unitConversion.parseLabels.push(label);
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
if (parserSpec.format.type === FormatType.Bearing) {
|
|
564
|
+
return this.parseBearingFormat(inString, parserSpec);
|
|
565
|
+
}
|
|
566
|
+
if (parserSpec.format.type === FormatType.Azimuth) {
|
|
567
|
+
return this.parseAzimuthFormat(inString, parserSpec);
|
|
568
|
+
}
|
|
569
|
+
return this.parseAndProcessTokens(inString, parserSpec.format, parserSpec.unitConversions);
|
|
535
570
|
}
|
|
536
571
|
/** Method to generate a Quantity given a string that represents a quantity value and likely a unit label.
|
|
537
572
|
* @param inString A string that contains text represent a quantity.
|
|
@@ -539,6 +574,8 @@ export class Parser {
|
|
|
539
574
|
* @param unitsConversions dictionary of conversions used to convert from unit used in inString to output quantity
|
|
540
575
|
*/
|
|
541
576
|
static parseToQuantityValue(inString, format, unitsConversions) {
|
|
577
|
+
// TODO: This method is not able to do bearing and azimuth formatting and is overlapping with parseQuantityString.
|
|
578
|
+
// We should consider deprecating and removing it, there do not seem to be any upstream callers at this moment
|
|
542
579
|
// ensure any labels defined in composite unit definition are specified in unitConversions
|
|
543
580
|
if (format.units) {
|
|
544
581
|
format.units.forEach(([unit, label]) => {
|
|
@@ -551,6 +588,123 @@ export class Parser {
|
|
|
551
588
|
}
|
|
552
589
|
});
|
|
553
590
|
}
|
|
591
|
+
if (format.type === FormatType.Bearing || format.type === FormatType.Azimuth) {
|
|
592
|
+
// throw error indicating to call parseQuantityString instead
|
|
593
|
+
throw new QuantityError(QuantityStatus.UnsupportedUnit, `Bearing and Azimuth format must be parsed using a ParserSpec. Call parseQuantityString instead.`);
|
|
594
|
+
}
|
|
595
|
+
return this.parseAndProcessTokens(inString, format, unitsConversions);
|
|
596
|
+
}
|
|
597
|
+
static parseBearingFormat(inString, spec) {
|
|
598
|
+
// TODO: at some point we will want to open this for localization, in the first release it's going to be hard coded
|
|
599
|
+
let DirectionLabel;
|
|
600
|
+
(function (DirectionLabel) {
|
|
601
|
+
DirectionLabel["North"] = "N";
|
|
602
|
+
DirectionLabel["South"] = "S";
|
|
603
|
+
DirectionLabel["East"] = "E";
|
|
604
|
+
DirectionLabel["West"] = "W";
|
|
605
|
+
})(DirectionLabel || (DirectionLabel = {}));
|
|
606
|
+
let matchedPrefix = null;
|
|
607
|
+
let matchedSuffix = null;
|
|
608
|
+
// check if input string begins with northLabel or southLabel and strip it off
|
|
609
|
+
if (inString.startsWith(DirectionLabel.North)) {
|
|
610
|
+
inString = inString.substring(DirectionLabel.North.length);
|
|
611
|
+
matchedPrefix = DirectionLabel.North;
|
|
612
|
+
}
|
|
613
|
+
else if (inString.startsWith(DirectionLabel.South)) {
|
|
614
|
+
inString = inString.substring(DirectionLabel.South.length);
|
|
615
|
+
matchedPrefix = DirectionLabel.South;
|
|
616
|
+
}
|
|
617
|
+
// check if input string ends with eastLabel or westLabel and strip it off
|
|
618
|
+
if (inString.endsWith(DirectionLabel.East)) {
|
|
619
|
+
inString = inString.substring(0, inString.length - DirectionLabel.East.length);
|
|
620
|
+
matchedSuffix = DirectionLabel.East;
|
|
621
|
+
}
|
|
622
|
+
else if (inString.endsWith(DirectionLabel.West)) {
|
|
623
|
+
inString = inString.substring(0, inString.length - DirectionLabel.West.length);
|
|
624
|
+
matchedSuffix = DirectionLabel.West;
|
|
625
|
+
}
|
|
626
|
+
if (matchedPrefix === null && matchedSuffix === null) {
|
|
627
|
+
return { ok: false, error: ParseError.BearingPrefixOrSuffixMissing };
|
|
628
|
+
}
|
|
629
|
+
const parsedResult = this.parseAndProcessTokens(inString, spec.format, spec.unitConversions);
|
|
630
|
+
if (this.isParseError(parsedResult) || !parsedResult.ok) {
|
|
631
|
+
return parsedResult;
|
|
632
|
+
}
|
|
633
|
+
let magnitude = parsedResult.value;
|
|
634
|
+
const revolution = this.getRevolution(spec);
|
|
635
|
+
magnitude = this.normalizeAngle(magnitude, revolution);
|
|
636
|
+
const quarterRevolution = revolution / 4;
|
|
637
|
+
// we have to turn the value into an east base and counter clockwise (NW and SE are already counter clockwise)
|
|
638
|
+
if (matchedPrefix === DirectionLabel.North) {
|
|
639
|
+
if (matchedSuffix === DirectionLabel.West) {
|
|
640
|
+
magnitude = quarterRevolution + magnitude;
|
|
641
|
+
}
|
|
642
|
+
else if (matchedSuffix === DirectionLabel.East) {
|
|
643
|
+
magnitude = quarterRevolution - magnitude;
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
else if (matchedPrefix === DirectionLabel.South) {
|
|
647
|
+
if (matchedSuffix === DirectionLabel.West) {
|
|
648
|
+
magnitude = (3 * quarterRevolution) - magnitude;
|
|
649
|
+
}
|
|
650
|
+
else if (matchedSuffix === DirectionLabel.East) {
|
|
651
|
+
magnitude = (3 * quarterRevolution) + magnitude;
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
return { ok: true, value: magnitude };
|
|
655
|
+
}
|
|
656
|
+
static parseAzimuthFormat(inString, spec) {
|
|
657
|
+
const parsedResult = this.parseAndProcessTokens(inString, spec.format, spec.unitConversions);
|
|
658
|
+
if (this.isParseError(parsedResult) || !parsedResult.ok) {
|
|
659
|
+
return parsedResult;
|
|
660
|
+
}
|
|
661
|
+
let magnitude = parsedResult.value;
|
|
662
|
+
const revolution = this.getRevolution(spec);
|
|
663
|
+
magnitude = this.normalizeAngle(magnitude, revolution);
|
|
664
|
+
const quarterRevolution = revolution / 4;
|
|
665
|
+
let azimuthBase = quarterRevolution;
|
|
666
|
+
if (spec.format.azimuthBase !== undefined) {
|
|
667
|
+
if (spec.azimuthBaseConversion === undefined) {
|
|
668
|
+
throw new QuantityError(QuantityStatus.MissingRequiredProperty, `Missing azimuth base conversion for interpreting ${spec.format.name}'s azimuth base.`);
|
|
669
|
+
}
|
|
670
|
+
const azBaseQuantity = new Quantity(spec.format.azimuthBaseUnit, spec.format.azimuthBase);
|
|
671
|
+
const azBaseConverted = azBaseQuantity.convertTo(spec.outUnit, spec.azimuthBaseConversion);
|
|
672
|
+
if (azBaseConverted === undefined || !azBaseConverted.isValid) {
|
|
673
|
+
throw new QuantityError(QuantityStatus.UnsupportedUnit, `Failed to convert azimuth base unit to ${spec.outUnit.name}.`);
|
|
674
|
+
}
|
|
675
|
+
azimuthBase = this.normalizeAngle(azBaseConverted.magnitude, revolution);
|
|
676
|
+
}
|
|
677
|
+
const azimuthCounterClockwise = spec.format.azimuthCounterClockwiseOrDefault;
|
|
678
|
+
if (azimuthCounterClockwise && azimuthBase === 0) {
|
|
679
|
+
// parsed result already has the same base and orientation as our desired output
|
|
680
|
+
return parsedResult;
|
|
681
|
+
}
|
|
682
|
+
if (azimuthCounterClockwise)
|
|
683
|
+
magnitude = azimuthBase + magnitude;
|
|
684
|
+
else
|
|
685
|
+
magnitude = azimuthBase - magnitude;
|
|
686
|
+
magnitude = this.normalizeAngle(magnitude, revolution);
|
|
687
|
+
return { ok: true, value: magnitude };
|
|
688
|
+
}
|
|
689
|
+
// TODO: The following two methods are redundant with Formatter. We should consider consolidating them.
|
|
690
|
+
static normalizeAngle(magnitude, revolution) {
|
|
691
|
+
magnitude = magnitude % revolution; // Strip anything that goes around more than once
|
|
692
|
+
if (magnitude < 0) // If the value is negative, we want to normalize it to a positive angle
|
|
693
|
+
magnitude += revolution;
|
|
694
|
+
return magnitude;
|
|
695
|
+
}
|
|
696
|
+
static getRevolution(spec) {
|
|
697
|
+
if (spec.revolutionConversion === undefined) {
|
|
698
|
+
throw new QuantityError(QuantityStatus.MissingRequiredProperty, `Missing revolution unit conversion for calculating ${spec.format.name}'s revolution.`);
|
|
699
|
+
}
|
|
700
|
+
const revolution = new Quantity(spec.format.revolutionUnit, 1.0);
|
|
701
|
+
const converted = revolution.convertTo(spec.outUnit, spec.revolutionConversion);
|
|
702
|
+
if (converted === undefined || !converted.isValid) {
|
|
703
|
+
throw new QuantityError(QuantityStatus.UnsupportedUnit, `Failed to convert revolution unit to ${spec.outUnit.name} On format ${spec.format.name}.`);
|
|
704
|
+
}
|
|
705
|
+
return converted.magnitude;
|
|
706
|
+
}
|
|
707
|
+
static parseAndProcessTokens(inString, format, unitsConversions) {
|
|
554
708
|
const tokens = Parser.parseQuantitySpecification(inString, format);
|
|
555
709
|
if (tokens.length === 0)
|
|
556
710
|
return { ok: false, error: ParseError.UnableToGenerateParseTokens };
|