@itwin/core-quantity 4.10.0-dev.2 → 4.10.0-dev.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (74) hide show
  1. package/CHANGELOG.md +28 -1
  2. package/lib/cjs/Constants.js.map +1 -1
  3. package/lib/cjs/Exception.d.ts +2 -1
  4. package/lib/cjs/Exception.d.ts.map +1 -1
  5. package/lib/cjs/Exception.js +1 -0
  6. package/lib/cjs/Exception.js.map +1 -1
  7. package/lib/cjs/Formatter/Format.d.ts +5 -2
  8. package/lib/cjs/Formatter/Format.d.ts.map +1 -1
  9. package/lib/cjs/Formatter/Format.js +18 -24
  10. package/lib/cjs/Formatter/Format.js.map +1 -1
  11. package/lib/cjs/Formatter/FormatEnums.d.ts +17 -1
  12. package/lib/cjs/Formatter/FormatEnums.d.ts.map +1 -1
  13. package/lib/cjs/Formatter/FormatEnums.js +33 -1
  14. package/lib/cjs/Formatter/FormatEnums.js.map +1 -1
  15. package/lib/cjs/Formatter/Formatter.d.ts +1 -0
  16. package/lib/cjs/Formatter/Formatter.d.ts.map +1 -1
  17. package/lib/cjs/Formatter/Formatter.js +70 -22
  18. package/lib/cjs/Formatter/Formatter.js.map +1 -1
  19. package/lib/cjs/Formatter/FormatterSpec.js.map +1 -1
  20. package/lib/cjs/Formatter/Interfaces.d.ts +2 -0
  21. package/lib/cjs/Formatter/Interfaces.d.ts.map +1 -1
  22. package/lib/cjs/Formatter/Interfaces.js.map +1 -1
  23. package/lib/cjs/Interfaces.d.ts +13 -0
  24. package/lib/cjs/Interfaces.d.ts.map +1 -1
  25. package/lib/cjs/Interfaces.js +11 -0
  26. package/lib/cjs/Interfaces.js.map +1 -1
  27. package/lib/cjs/Parser.d.ts +2 -0
  28. package/lib/cjs/Parser.d.ts.map +1 -1
  29. package/lib/cjs/Parser.js +65 -16
  30. package/lib/cjs/Parser.js.map +1 -1
  31. package/lib/cjs/ParserSpec.js.map +1 -1
  32. package/lib/cjs/Quantity.d.ts +14 -0
  33. package/lib/cjs/Quantity.d.ts.map +1 -1
  34. package/lib/cjs/Quantity.js +40 -2
  35. package/lib/cjs/Quantity.js.map +1 -1
  36. package/lib/cjs/Unit.js.map +1 -1
  37. package/lib/cjs/core-quantity.js.map +1 -1
  38. package/lib/esm/Constants.js.map +1 -1
  39. package/lib/esm/Exception.d.ts +2 -1
  40. package/lib/esm/Exception.d.ts.map +1 -1
  41. package/lib/esm/Exception.js +1 -0
  42. package/lib/esm/Exception.js.map +1 -1
  43. package/lib/esm/Formatter/Format.d.ts +5 -2
  44. package/lib/esm/Formatter/Format.d.ts.map +1 -1
  45. package/lib/esm/Formatter/Format.js +19 -25
  46. package/lib/esm/Formatter/Format.js.map +1 -1
  47. package/lib/esm/Formatter/FormatEnums.d.ts +17 -1
  48. package/lib/esm/Formatter/FormatEnums.d.ts.map +1 -1
  49. package/lib/esm/Formatter/FormatEnums.js +31 -0
  50. package/lib/esm/Formatter/FormatEnums.js.map +1 -1
  51. package/lib/esm/Formatter/Formatter.d.ts +1 -0
  52. package/lib/esm/Formatter/Formatter.d.ts.map +1 -1
  53. package/lib/esm/Formatter/Formatter.js +72 -24
  54. package/lib/esm/Formatter/Formatter.js.map +1 -1
  55. package/lib/esm/Formatter/FormatterSpec.js.map +1 -1
  56. package/lib/esm/Formatter/Interfaces.d.ts +2 -0
  57. package/lib/esm/Formatter/Interfaces.d.ts.map +1 -1
  58. package/lib/esm/Formatter/Interfaces.js.map +1 -1
  59. package/lib/esm/Interfaces.d.ts +13 -0
  60. package/lib/esm/Interfaces.d.ts.map +1 -1
  61. package/lib/esm/Interfaces.js +10 -1
  62. package/lib/esm/Interfaces.js.map +1 -1
  63. package/lib/esm/Parser.d.ts +2 -0
  64. package/lib/esm/Parser.d.ts.map +1 -1
  65. package/lib/esm/Parser.js +66 -17
  66. package/lib/esm/Parser.js.map +1 -1
  67. package/lib/esm/ParserSpec.js.map +1 -1
  68. package/lib/esm/Quantity.d.ts +14 -0
  69. package/lib/esm/Quantity.d.ts.map +1 -1
  70. package/lib/esm/Quantity.js +37 -1
  71. package/lib/esm/Quantity.js.map +1 -1
  72. package/lib/esm/Unit.js.map +1 -1
  73. package/lib/esm/core-quantity.js.map +1 -1
  74. package/package.json +4 -4
@@ -1 +1 @@
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"]}
1
+ {"version":3,"file":"Interfaces.js","sourceRoot":"","sources":["../../../src/Formatter/Interfaces.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AA4DH;;GAEG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,IAAiB,EAA6B,EAAE;IAClF,OAAQ,IAA0B,CAAC,MAAM,KAAK,SAAS,CAAC;AAC1D,CAAC,CAAC","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 { UnitProps } from \"../Interfaces\";\nimport { DecimalPrecision, FormatTraits, FormatType, FractionalPrecision } from \"./FormatEnums\";\n\n/** This interface defines the persistence format for describing the formatting of quantity values.\n * @beta\n */\nexport interface FormatProps {\n readonly type: string;\n readonly precision?: number;\n readonly roundFactor?: number;\n readonly minWidth?: number;\n readonly showSignOption?: string;\n readonly formatTraits?: string | string[];\n readonly decimalSeparator?: string;\n readonly thousandSeparator?: string;\n readonly uomSeparator?: string;\n\n /** conditionally required. */\n readonly scientificType?: string;\n\n /** conditionally required. */\n readonly ratioType?: string;\n\n /** conditionally required. */\n readonly stationOffsetSize?: number;\n readonly stationSeparator?: string;\n\n /** The base value for azimuth, specified from east counter-clockwise. */\n readonly azimuthBase?: number;\n\n /** The name of the unit for the azimuth base value. */\n readonly azimuthBaseUnit?: string;\n\n /** If set to true, azimuth values are returned counter-clockwise from the base. */\n readonly azimuthCounterClockwise?: boolean;\n\n /** The name of the unit that represents a revolution/perigon. Required for bearing or azimuth types. */\n readonly revolutionUnit?: string;\n\n readonly allowMathematicOperations?: boolean;\n readonly composite?: {\n /** separates values when formatting composite strings */\n readonly spacer?: string;\n readonly includeZero?: boolean;\n readonly units: Array<{\n readonly name: string;\n readonly label?: string;\n }>;\n };\n}\n\n/** This interface is used when supporting Custom Formatters that need more than the standard set of properties.\n * @beta\n */\nexport interface CustomFormatProps extends FormatProps {\n readonly custom: any;\n}\n\n/** CustomFormatProps type guard.\n * @beta\n */\nexport const isCustomFormatProps = (item: FormatProps): item is CustomFormatProps => {\n return (item as CustomFormatProps).custom !== undefined;\n};\n\n/** CloneFormat defines unit and label specification if primary unit is to be set during clone.\n * @beta\n */\nexport interface CloneUnit {\n unit?: UnitProps;\n label?: string;\n}\n\n/** CloneOptions that define modifications that can be made during the cloning of a Format.\n * @beta\n */\nexport interface CloneOptions {\n /** allows composite formats to be converted to only show primary unit */\n showOnlyPrimaryUnit?: boolean;\n /** allow format traits to be set */\n traits?: FormatTraits;\n /** allows new FormatType to be specified */\n type?: FormatType;\n /** allows precision to be set, this will throw if value is not valid for FormatType */\n precision?: DecimalPrecision | FractionalPrecision;\n /** allows primary unit and label to be specified */\n primaryUnit?: CloneUnit;\n}\n"]}
@@ -46,13 +46,26 @@ export interface UnitConversionSpec {
46
46
  /** Labels that may be used to represent the unit in a string that is to be parsed. */
47
47
  parseLabels?: string[];
48
48
  }
49
+ /** Indicates the way in which unit values are inverted during conversion
50
+ * @beta
51
+ */
52
+ export declare enum UnitConversionInvert {
53
+ /** Invert value before applying the other conversion steps (the from-unit is the inverted unit) */
54
+ InvertPreConversion = "InvertPreConversion",
55
+ /** Invert value after applying the other conversion steps (the to-unit is the inverted unit) */
56
+ InvertPostConversion = "InvertPostConversion"
57
+ }
49
58
  /** This interface defines the properties required to convert a quantity value from one unit to another such as from meters to feet
50
59
  * or from Celsius to Fahrenheit.
51
60
  * @beta
52
61
  */
53
62
  export interface UnitConversionProps {
63
+ /** The factor to multiply the input value by to convert to the output value. */
54
64
  factor: number;
65
+ /** The offset to add to the input value to convert to the output value. */
55
66
  offset: number;
67
+ /** If set, inverts the unit value (1/x) before or after conversion. */
68
+ inversion?: UnitConversionInvert;
56
69
  }
57
70
  /** Interface that defines potential parse units that may be found in user's string input of a quantity value.
58
71
  * @beta
@@ -1 +1 @@
1
- {"version":3,"file":"Interfaces.d.ts","sourceRoot":"","sources":["../../src/Interfaces.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH;;GAEG;AACH,MAAM,WAAW,2BAA2B;IAC1C,sBAAsB,EAAE,CAAC,IAAI,EAAE,SAAS,KAAK,MAAM,EAAE,GAAG,SAAS,CAAC;CACnE;AAED;;;GAGG;AACH,MAAM,WAAW,SAAS;IACxB,4BAA4B;IAC5B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,8BAA8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AG,UAAU,CAAC"}
@@ -5,5 +5,14 @@
5
5
  /** @packageDocumentation
6
6
  * @module Quantity
7
7
  */
8
- export {};
8
+ /** Indicates the way in which unit values are inverted during conversion
9
+ * @beta
10
+ */
11
+ export var UnitConversionInvert;
12
+ (function (UnitConversionInvert) {
13
+ /** Invert value before applying the other conversion steps (the from-unit is the inverted unit) */
14
+ UnitConversionInvert["InvertPreConversion"] = "InvertPreConversion";
15
+ /** Invert value after applying the other conversion steps (the to-unit is the inverted unit) */
16
+ UnitConversionInvert["InvertPostConversion"] = "InvertPostConversion";
17
+ })(UnitConversionInvert || (UnitConversionInvert = {}));
9
18
  //# sourceMappingURL=Interfaces.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"Interfaces.js","sourceRoot":"","sources":["../../src/Interfaces.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG","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\n/** This interface allows a provider to be specified that will define an array of alternate labels for a specific unit.\r\n * @beta\r\n */\r\nexport interface AlternateUnitLabelsProvider {\r\n getAlternateUnitLabels: (unit: UnitProps) => string[] | undefined;\r\n}\r\n\r\n/** This interface provides basic information about a Unit that is return from a UnitProvider. This info\r\n * uniquely identifies a unit by its name.\r\n * @beta\r\n */\r\nexport interface UnitProps {\r\n /** Unique name for unit. */\r\n readonly name: string;\r\n /** Default label for unit. */\r\n readonly label: string;\r\n /** Unique name of unit phenomenon. Example phenomenon names include 'Units.LENGTH', 'Units.AREA', and 'Units.VOLUME' */\r\n readonly phenomenon: string;\r\n /** This is set to true if the Unit is known by the UnitsProvider. */\r\n readonly isValid: boolean;\r\n /** Unique system name. Example \"Units.USCUSTOM\",\" Units.METRIC\", \"Units.USSURVEY\", \"Units.IMPERIAL\" */\r\n readonly system: string;\r\n}\r\n\r\n/** This interface defines the required properties of a Quantity.\r\n * @beta\r\n */\r\nexport interface QuantityProps {\r\n readonly magnitude: number;\r\n readonly unit: UnitProps;\r\n readonly isValid: boolean;\r\n}\r\n\r\n/** Interface that defines how to convert between a specific unit an another in synchronous formatting or parsing processing.\r\n * @beta\r\n */\r\nexport interface UnitConversionSpec {\r\n /** Unit name that was used to locate the unit by the Unit Provider */\r\n name: string;\r\n /** The default label that is used to display unit */\r\n label: string;\r\n /** Unit system name, used to when finding preferred parse unit */\r\n system: string;\r\n /** the information necessary to convert the unit to a specific display unit */\r\n conversion: UnitConversionProps;\r\n /** Labels that may be used to represent the unit in a string that is to be parsed. */\r\n parseLabels?: string[];\r\n}\r\n\r\n/** This interface defines the properties required to convert a quantity value from one unit to another such as from meters to feet\r\n * or from Celsius to Fahrenheit.\r\n * @beta\r\n */\r\nexport interface UnitConversionProps {\r\n factor: number;\r\n offset: number;\r\n}\r\n\r\n/** Interface that defines potential parse units that may be found in user's string input of a quantity value.\r\n * @beta\r\n */\r\nexport interface PotentialParseUnit {\r\n unitName: string;\r\n altLabels?: string[];\r\n}\r\n\r\n/**\r\n * This interface defines extra properties to be associated with Units from Units Schema by name\r\n * @alpha\r\n */\r\nexport interface UnitExtraData {\r\n readonly name: string;\r\n readonly altDisplayLabels: string[];\r\n}\r\n\r\n/** This interface is implemented by the class that is responsible for locating units by name or label and providing conversion values between units.\r\n * The methods to be implemented are async allowing the UnitsProvider to query the backend when necessary to look up unit definition and conversion rules.\r\n * @beta\r\n */\r\nexport interface UnitsProvider {\r\n findUnit(unitLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<UnitProps>;\r\n getUnitsByFamily(phenomenon: string): Promise<UnitProps[]>;\r\n findUnitByName(unitName: string): Promise<UnitProps>;\r\n getConversion(fromUnit: UnitProps, toUnit: UnitProps): Promise<UnitConversionProps>;\r\n}\r\n\r\n/**\r\n * Used to uniquely identify a unit system.\r\n * @beta\r\n */\r\nexport type UnitSystemKey = \"metric\" | \"imperial\" | \"usCustomary\" | \"usSurvey\";\r\n"]}
1
+ {"version":3,"file":"Interfaces.js","sourceRoot":"","sources":["../../src/Interfaces.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAmDH;;GAEG;AACH,MAAM,CAAN,IAAY,oBAKX;AALD,WAAY,oBAAoB;IAC9B,mGAAmG;IACnG,mEAA2C,CAAA;IAC3C,gGAAgG;IAChG,qEAA6C,CAAA;AAC/C,CAAC,EALW,oBAAoB,KAApB,oBAAoB,QAK/B","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\n/** This interface allows a provider to be specified that will define an array of alternate labels for a specific unit.\n * @beta\n */\nexport interface AlternateUnitLabelsProvider {\n getAlternateUnitLabels: (unit: UnitProps) => string[] | undefined;\n}\n\n/** This interface provides basic information about a Unit that is return from a UnitProvider. This info\n * uniquely identifies a unit by its name.\n * @beta\n */\nexport interface UnitProps {\n /** Unique name for unit. */\n readonly name: string;\n /** Default label for unit. */\n readonly label: string;\n /** Unique name of unit phenomenon. Example phenomenon names include 'Units.LENGTH', 'Units.AREA', and 'Units.VOLUME' */\n readonly phenomenon: string;\n /** This is set to true if the Unit is known by the UnitsProvider. */\n readonly isValid: boolean;\n /** Unique system name. Example \"Units.USCUSTOM\",\" Units.METRIC\", \"Units.USSURVEY\", \"Units.IMPERIAL\" */\n readonly system: string;\n}\n\n/** This interface defines the required properties of a Quantity.\n * @beta\n */\nexport interface QuantityProps {\n readonly magnitude: number;\n readonly unit: UnitProps;\n readonly isValid: boolean;\n}\n\n/** Interface that defines how to convert between a specific unit an another in synchronous formatting or parsing processing.\n * @beta\n */\nexport interface UnitConversionSpec {\n /** Unit name that was used to locate the unit by the Unit Provider */\n name: string;\n /** The default label that is used to display unit */\n label: string;\n /** Unit system name, used to when finding preferred parse unit */\n system: string;\n /** the information necessary to convert the unit to a specific display unit */\n conversion: UnitConversionProps;\n /** Labels that may be used to represent the unit in a string that is to be parsed. */\n parseLabels?: string[];\n}\n\n/** Indicates the way in which unit values are inverted during conversion\n * @beta\n */\nexport enum UnitConversionInvert {\n /** Invert value before applying the other conversion steps (the from-unit is the inverted unit) */\n InvertPreConversion = \"InvertPreConversion\",\n /** Invert value after applying the other conversion steps (the to-unit is the inverted unit) */\n InvertPostConversion = \"InvertPostConversion\"\n}\n\n/** This interface defines the properties required to convert a quantity value from one unit to another such as from meters to feet\n * or from Celsius to Fahrenheit.\n * @beta\n */\nexport interface UnitConversionProps {\n /** The factor to multiply the input value by to convert to the output value. */\n factor: number;\n /** The offset to add to the input value to convert to the output value. */\n offset: number;\n /** If set, inverts the unit value (1/x) before or after conversion. */\n inversion?: UnitConversionInvert;\n}\n\n/** Interface that defines potential parse units that may be found in user's string input of a quantity value.\n * @beta\n */\nexport interface PotentialParseUnit {\n unitName: string;\n altLabels?: string[];\n}\n\n/**\n * This interface defines extra properties to be associated with Units from Units Schema by name\n * @alpha\n */\nexport interface UnitExtraData {\n readonly name: string;\n readonly altDisplayLabels: string[];\n}\n\n/** This interface is implemented by the class that is responsible for locating units by name or label and providing conversion values between units.\n * The methods to be implemented are async allowing the UnitsProvider to query the backend when necessary to look up unit definition and conversion rules.\n * @beta\n */\nexport interface UnitsProvider {\n findUnit(unitLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<UnitProps>;\n getUnitsByFamily(phenomenon: string): Promise<UnitProps[]>;\n findUnitByName(unitName: string): Promise<UnitProps>;\n getConversion(fromUnit: UnitProps, toUnit: UnitProps): Promise<UnitConversionProps>;\n}\n\n/**\n * Used to uniquely identify a unit system.\n * @beta\n */\nexport type UnitSystemKey = \"metric\" | \"imperial\" | \"usCustomary\" | \"usSurvey\";\n"]}
@@ -105,10 +105,12 @@ export declare class Parser {
105
105
  * @param inString A string that contains text represent a quantity.
106
106
  * @param format Defines the likely format of inString. Primary unit serves as a default unit if no unit label found in string.
107
107
  * @param unitsConversions dictionary of conversions used to convert from unit used in inString to output quantity
108
+ * @deprecated in 4.10. Check [[Parser.parseQuantityString]] for replacements.
108
109
  */
109
110
  static parseToQuantityValue(inString: string, format: Format, unitsConversions: UnitConversionSpec[]): QuantityParseResult;
110
111
  private static parseBearingFormat;
111
112
  private static parseAzimuthFormat;
113
+ private static parseRatioFormat;
112
114
  private static normalizeAngle;
113
115
  private static getRevolution;
114
116
  private static parseAndProcessTokens;
@@ -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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
package/lib/esm/Parser.js CHANGED
@@ -8,7 +8,7 @@
8
8
  import { QuantityConstants } from "./Constants";
9
9
  import { QuantityError, QuantityStatus } from "./Exception";
10
10
  import { FormatTraits, FormatType } from "./Formatter/FormatEnums";
11
- import { Quantity } from "./Quantity";
11
+ import { applyConversion, Quantity } from "./Quantity";
12
12
  /** Possible parser errors
13
13
  * @beta
14
14
  */
@@ -30,7 +30,7 @@ var Operator;
30
30
  })(Operator || (Operator = {}));
31
31
  function isOperator(char) {
32
32
  if (typeof char === "number") {
33
- // Convert the charcode to string.
33
+ // Convert the CharCode to string.
34
34
  char = String.fromCharCode(char);
35
35
  }
36
36
  return Object.values(Operator).includes(char);
@@ -277,7 +277,7 @@ export class Parser {
277
277
  continue;
278
278
  }
279
279
  if (wipToken.length === 0 && charCode === QuantityConstants.CHAR_SPACE) {
280
- // Dont add space when the wip token is empty.
280
+ // Don't add space when the wip token is empty.
281
281
  continue;
282
282
  }
283
283
  wipToken = wipToken.concat(str[i]);
@@ -527,7 +527,7 @@ export class Parser {
527
527
  }
528
528
  }
529
529
  if (conversion) {
530
- value = (value * conversion.factor) + conversion.offset;
530
+ value = applyConversion(value, conversion);
531
531
  }
532
532
  mag = mag + value;
533
533
  compositeUnitIndex++;
@@ -566,12 +566,16 @@ export class Parser {
566
566
  if (parserSpec.format.type === FormatType.Azimuth) {
567
567
  return this.parseAzimuthFormat(inString, parserSpec);
568
568
  }
569
+ if (parserSpec.format.type === FormatType.Ratio) {
570
+ return this.parseRatioFormat(inString, parserSpec);
571
+ }
569
572
  return this.parseAndProcessTokens(inString, parserSpec.format, parserSpec.unitConversions);
570
573
  }
571
574
  /** Method to generate a Quantity given a string that represents a quantity value and likely a unit label.
572
575
  * @param inString A string that contains text represent a quantity.
573
576
  * @param format Defines the likely format of inString. Primary unit serves as a default unit if no unit label found in string.
574
577
  * @param unitsConversions dictionary of conversions used to convert from unit used in inString to output quantity
578
+ * @deprecated in 4.10. Check [[Parser.parseQuantityString]] for replacements.
575
579
  */
576
580
  static parseToQuantityValue(inString, format, unitsConversions) {
577
581
  // TODO: This method is not able to do bearing and azimuth formatting and is overlapping with parseQuantityString.
@@ -588,9 +592,9 @@ export class Parser {
588
592
  }
589
593
  });
590
594
  }
591
- if (format.type === FormatType.Bearing || format.type === FormatType.Azimuth) {
595
+ if (format.type === FormatType.Bearing || format.type === FormatType.Azimuth || format.type === FormatType.Ratio) {
592
596
  // 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.`);
597
+ throw new QuantityError(QuantityStatus.UnsupportedUnit, `Bearing, Azimuth or Ratio format must be parsed using a ParserSpec. Call parseQuantityString instead.`);
594
598
  }
595
599
  return this.parseAndProcessTokens(inString, format, unitsConversions);
596
600
  }
@@ -637,18 +641,15 @@ export class Parser {
637
641
  // we have to turn the value into an east base and counter clockwise (NW and SE are already counter clockwise)
638
642
  if (matchedPrefix === DirectionLabel.North) {
639
643
  if (matchedSuffix === DirectionLabel.West) {
640
- magnitude = quarterRevolution + magnitude;
641
- }
642
- else if (matchedSuffix === DirectionLabel.East) {
643
- magnitude = quarterRevolution - magnitude;
644
+ magnitude = revolution - magnitude;
644
645
  }
645
646
  }
646
647
  else if (matchedPrefix === DirectionLabel.South) {
647
648
  if (matchedSuffix === DirectionLabel.West) {
648
- magnitude = (3 * quarterRevolution) - magnitude;
649
+ magnitude = (2 * quarterRevolution) + magnitude;
649
650
  }
650
651
  else if (matchedSuffix === DirectionLabel.East) {
651
- magnitude = (3 * quarterRevolution) + magnitude;
652
+ magnitude = (2 * quarterRevolution) - magnitude;
652
653
  }
653
654
  }
654
655
  return { ok: true, value: magnitude };
@@ -661,8 +662,7 @@ export class Parser {
661
662
  let magnitude = parsedResult.value;
662
663
  const revolution = this.getRevolution(spec);
663
664
  magnitude = this.normalizeAngle(magnitude, revolution);
664
- const quarterRevolution = revolution / 4;
665
- let azimuthBase = quarterRevolution;
665
+ let azimuthBase = 0.0;
666
666
  if (spec.format.azimuthBase !== undefined) {
667
667
  if (spec.azimuthBaseConversion === undefined) {
668
668
  throw new QuantityError(QuantityStatus.MissingRequiredProperty, `Missing azimuth base conversion for interpreting ${spec.format.name}'s azimuth base.`);
@@ -674,18 +674,67 @@ export class Parser {
674
674
  }
675
675
  azimuthBase = this.normalizeAngle(azBaseConverted.magnitude, revolution);
676
676
  }
677
- const azimuthCounterClockwise = spec.format.azimuthCounterClockwiseOrDefault;
678
- if (azimuthCounterClockwise && azimuthBase === 0) {
677
+ const inputIsClockwise = spec.format.azimuthClockwiseOrDefault;
678
+ if (inputIsClockwise && azimuthBase === 0) {
679
679
  // parsed result already has the same base and orientation as our desired output
680
680
  return parsedResult;
681
681
  }
682
- if (azimuthCounterClockwise)
682
+ if (inputIsClockwise)
683
683
  magnitude = azimuthBase + magnitude;
684
684
  else
685
685
  magnitude = azimuthBase - magnitude;
686
686
  magnitude = this.normalizeAngle(magnitude, revolution);
687
687
  return { ok: true, value: magnitude };
688
688
  }
689
+ static parseRatioFormat(inString, spec) {
690
+ if (!inString)
691
+ return { ok: false, error: ParseError.NoValueOrUnitFoundInString };
692
+ const parts = inString.split(":");
693
+ if (parts.length > 2)
694
+ return { ok: false, error: ParseError.UnableToConvertParseTokensToQuantity };
695
+ const numerator = parseFloat(parts[0]);
696
+ let denominator;
697
+ if (parts.length === 1) {
698
+ denominator = 1.0;
699
+ }
700
+ else {
701
+ denominator = parseFloat(parts[1]);
702
+ }
703
+ if (isNaN(numerator) || isNaN(denominator))
704
+ return { ok: false, error: ParseError.NoValueOrUnitFoundInString };
705
+ const defaultUnit = spec.format.units && spec.format.units.length > 0 ? spec.format.units[0][0] : undefined;
706
+ const unitConversion = defaultUnit ? Parser.tryFindUnitConversion(defaultUnit.label, spec.unitConversions, defaultUnit) : undefined;
707
+ if (!unitConversion) {
708
+ throw new QuantityError(QuantityStatus.MissingRequiredProperty, `Missing input unit or unit conversion for interpreting ${spec.format.name}.`);
709
+ }
710
+ if (denominator === 0) {
711
+ if (unitConversion.inversion && numerator === 1)
712
+ return { ok: true, value: 0.0 };
713
+ else
714
+ return { ok: false, error: ParseError.MathematicOperationFoundButIsNotAllowed };
715
+ }
716
+ let quantity;
717
+ if (spec.format.units && spec.outUnit) {
718
+ quantity = new Quantity(spec.format.units[0][0], numerator / denominator);
719
+ }
720
+ else {
721
+ throw new QuantityError(QuantityStatus.MissingRequiredProperty, "Missing presentation unit or persistence unit for ratio format.");
722
+ }
723
+ let converted;
724
+ try {
725
+ converted = quantity.convertTo(spec.outUnit, unitConversion);
726
+ }
727
+ catch (err) {
728
+ // for input of "0:N" with reversed unit
729
+ if (err instanceof QuantityError && err.errorNumber === QuantityStatus.InvertingZero) {
730
+ return { ok: false, error: ParseError.MathematicOperationFoundButIsNotAllowed };
731
+ }
732
+ }
733
+ if (converted === undefined || !converted.isValid) {
734
+ throw new QuantityError(QuantityStatus.UnsupportedUnit, `Failed to convert from ${spec.format.units[0][0].name} to ${spec.outUnit.name} On format ${spec.format.name}.`);
735
+ }
736
+ return { ok: true, value: converted.magnitude };
737
+ }
689
738
  // TODO: The following two methods are redundant with Formatter. We should consider consolidating them.
690
739
  static normalizeAngle(magnitude, revolution) {
691
740
  magnitude = magnitude % revolution; // Strip anything that goes around more than once