@itwin/core-quantity 5.6.0-dev.13 → 5.6.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.
Files changed (55) hide show
  1. package/lib/cjs/Formatter/Format.d.ts +7 -1
  2. package/lib/cjs/Formatter/Format.d.ts.map +1 -1
  3. package/lib/cjs/Formatter/Format.js +49 -11
  4. package/lib/cjs/Formatter/Format.js.map +1 -1
  5. package/lib/cjs/Formatter/FormatEnums.d.ts +11 -0
  6. package/lib/cjs/Formatter/FormatEnums.d.ts.map +1 -1
  7. package/lib/cjs/Formatter/FormatEnums.js +33 -2
  8. package/lib/cjs/Formatter/FormatEnums.js.map +1 -1
  9. package/lib/cjs/Formatter/Formatter.d.ts +3 -0
  10. package/lib/cjs/Formatter/Formatter.d.ts.map +1 -1
  11. package/lib/cjs/Formatter/Formatter.js +70 -16
  12. package/lib/cjs/Formatter/Formatter.js.map +1 -1
  13. package/lib/cjs/Formatter/FormatterSpec.d.ts +2 -0
  14. package/lib/cjs/Formatter/FormatterSpec.d.ts.map +1 -1
  15. package/lib/cjs/Formatter/FormatterSpec.js +48 -0
  16. package/lib/cjs/Formatter/FormatterSpec.js.map +1 -1
  17. package/lib/cjs/Formatter/Interfaces.d.ts +25 -10
  18. package/lib/cjs/Formatter/Interfaces.d.ts.map +1 -1
  19. package/lib/cjs/Formatter/Interfaces.js.map +1 -1
  20. package/lib/cjs/Parser.d.ts +16 -1
  21. package/lib/cjs/Parser.d.ts.map +1 -1
  22. package/lib/cjs/Parser.js +87 -15
  23. package/lib/cjs/Parser.js.map +1 -1
  24. package/lib/cjs/ParserSpec.d.ts +2 -0
  25. package/lib/cjs/ParserSpec.d.ts.map +1 -1
  26. package/lib/cjs/ParserSpec.js +54 -1
  27. package/lib/cjs/ParserSpec.js.map +1 -1
  28. package/lib/esm/Formatter/Format.d.ts +7 -1
  29. package/lib/esm/Formatter/Format.d.ts.map +1 -1
  30. package/lib/esm/Formatter/Format.js +50 -12
  31. package/lib/esm/Formatter/Format.js.map +1 -1
  32. package/lib/esm/Formatter/FormatEnums.d.ts +11 -0
  33. package/lib/esm/Formatter/FormatEnums.d.ts.map +1 -1
  34. package/lib/esm/Formatter/FormatEnums.js +31 -1
  35. package/lib/esm/Formatter/FormatEnums.js.map +1 -1
  36. package/lib/esm/Formatter/Formatter.d.ts +3 -0
  37. package/lib/esm/Formatter/Formatter.d.ts.map +1 -1
  38. package/lib/esm/Formatter/Formatter.js +70 -16
  39. package/lib/esm/Formatter/Formatter.js.map +1 -1
  40. package/lib/esm/Formatter/FormatterSpec.d.ts +2 -0
  41. package/lib/esm/Formatter/FormatterSpec.d.ts.map +1 -1
  42. package/lib/esm/Formatter/FormatterSpec.js +48 -0
  43. package/lib/esm/Formatter/FormatterSpec.js.map +1 -1
  44. package/lib/esm/Formatter/Interfaces.d.ts +25 -10
  45. package/lib/esm/Formatter/Interfaces.d.ts.map +1 -1
  46. package/lib/esm/Formatter/Interfaces.js.map +1 -1
  47. package/lib/esm/Parser.d.ts +16 -1
  48. package/lib/esm/Parser.d.ts.map +1 -1
  49. package/lib/esm/Parser.js +87 -15
  50. package/lib/esm/Parser.js.map +1 -1
  51. package/lib/esm/ParserSpec.d.ts +2 -0
  52. package/lib/esm/ParserSpec.d.ts.map +1 -1
  53. package/lib/esm/ParserSpec.js +54 -1
  54. package/lib/esm/ParserSpec.js.map +1 -1
  55. package/package.json +5 -5
@@ -5,6 +5,7 @@
5
5
  /** @packageDocumentation
6
6
  * @module Quantity
7
7
  */
8
+ import { FormatType } from "./FormatEnums";
8
9
  import { Formatter } from "./Formatter";
9
10
  // cSpell:ignore ZERONORMALIZED, nosign, onlynegative, signalways, negativeparentheses
10
11
  // cSpell:ignore trailzeroes, keepsinglezero, zeroempty, keepdecimalpoint, applyrounding, fractiondash, showunitlabel, prependunitlabel, exponentonlynegative
@@ -52,6 +53,49 @@ export class FormatterSpec {
52
53
  get persistenceUnit() { return this._persistenceUnit; }
53
54
  get azimuthBaseConversion() { return this._azimuthBaseConversion; }
54
55
  get revolutionConversion() { return this._revolutionConversion; }
56
+ /** Build conversion specs for ratio format with 2 composite units (numerator/denominator). */
57
+ static async getRatioUnitConversions(units, unitsProvider, persistenceUnit) {
58
+ const conversions = [];
59
+ const [numeratorUnit, numeratorLabel] = units[0];
60
+ const [denominatorUnit, denominatorLabel] = units[1];
61
+ // Compute ratio scale: how many numerator units per denominator unit (e.g., IN:FT = 12)
62
+ const denominatorToNumerator = await unitsProvider.getConversion(denominatorUnit, numeratorUnit);
63
+ const displayRatioScale = denominatorToNumerator.factor;
64
+ // Avoid double-scaling: if persistence unit already encodes the display ratio, use factor 1.
65
+ // Check by name heuristic (e.g., IN_PER_FT with ratioUnits [IN, FT] → no scaling needed)
66
+ const persistenceName = persistenceUnit.name.toUpperCase();
67
+ const numName = numeratorUnit.name.toUpperCase().split(".").pop() ?? "";
68
+ const denName = denominatorUnit.name.toUpperCase().split(".").pop() ?? "";
69
+ // Split by word boundaries (underscores, dots) and check for exact token matches
70
+ const persistenceTokens = persistenceName.split(/[._]/);
71
+ const isPersistenceMatchingRatio = persistenceTokens.includes(numName) && persistenceTokens.includes(denName);
72
+ const ratioScaleFactor = isPersistenceMatchingRatio ? 1.0 : displayRatioScale;
73
+ // First conversion spec: effective ratio unit conversion
74
+ const ratioConversionSpec = {
75
+ name: `${numeratorUnit.name}_per_${denominatorUnit.name}`,
76
+ label: "",
77
+ system: numeratorUnit.system,
78
+ conversion: { factor: ratioScaleFactor, offset: 0.0 },
79
+ };
80
+ conversions.push(ratioConversionSpec);
81
+ // Numerator unit for label lookup
82
+ const numeratorSpec = {
83
+ name: numeratorUnit.name,
84
+ label: numeratorLabel?.length ? numeratorLabel : numeratorUnit.label,
85
+ system: numeratorUnit.system,
86
+ conversion: { factor: 1.0, offset: 0.0 },
87
+ };
88
+ conversions.push(numeratorSpec);
89
+ // Denominator unit for label lookup
90
+ const denominatorSpec = {
91
+ name: denominatorUnit.name,
92
+ label: denominatorLabel?.length ? denominatorLabel : denominatorUnit.label,
93
+ system: denominatorUnit.system,
94
+ conversion: { factor: 1.0, offset: 0.0 },
95
+ };
96
+ conversions.push(denominatorSpec);
97
+ return conversions;
98
+ }
55
99
  /** Get an array of UnitConversionSpecs, one for each unit that is to be shown in the formatted quantity string. */
56
100
  static async getUnitConversions(format, unitsProvider, inputUnit) {
57
101
  const conversions = [];
@@ -65,6 +109,10 @@ export class FormatterSpec {
65
109
  throw new Error("Formatter Spec needs persistence unit to be specified");
66
110
  }
67
111
  }
112
+ // Handle 2-unit composite for ratio formats (scale factors)
113
+ if (format.type === FormatType.Ratio && format.units && format.units.length === 2) {
114
+ return FormatterSpec.getRatioUnitConversions(format.units, unitsProvider, persistenceUnit);
115
+ }
68
116
  if (format.units) {
69
117
  let convertFromUnit = inputUnit;
70
118
  for (const unit of format.units) {
@@ -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;IACd,KAAK,CAAS;IACd,YAAY,GAAyB,EAAE,CAAC,CAAE,mBAAmB;IAC7D,OAAO,CAAS;IAChB,gBAAgB,CAAY;IAC5B,sBAAsB,CAAuB,CAAC,iDAAiD;IAC/F,qBAAqB,CAAuB,CAAC,+CAA+C;IAEtG;;;;;;;OAOG;IACH,YAAY,IAAY,EAAE,MAAc,EAAE,WAAkC,EAAE,eAA2B,EAAE,qBAA2C,EAAE,oBAA0C;QAChM,IAAI,CAAC,eAAe,EAAE,CAAC;YACrB,IAAI,MAAM,CAAC,KAAK,EAAE,CAAC;gBACjB,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;gBAChC,eAAe,GA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AiB;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"]}
1
+ {"version":3,"file":"FormatterSpec.js","sourceRoot":"","sources":["../../../src/Formatter/FormatterSpec.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAIH,OAAO,EAAE,UA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CAAC;QACH,CAAC;QACD,IAAI,oBAAqD,CAAC;QAC1D,IAAI,MAAM,CAAC,cAAc,KAAK,SAAS,EAAE,CAAC;YACxC,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;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":["/*---------------------------------------------------------------------------------------------\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 { FormatType } from \"./FormatEnums\";\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 /** Build conversion specs for ratio format with 2 composite units (numerator/denominator). */\r\n private static async getRatioUnitConversions(units: ReadonlyArray<[UnitProps, string | undefined]>, unitsProvider: UnitsProvider, persistenceUnit: UnitProps): Promise<UnitConversionSpec[]> {\r\n const conversions: UnitConversionSpec[] = [];\r\n\r\n const [numeratorUnit, numeratorLabel] = units[0];\r\n const [denominatorUnit, denominatorLabel] = units[1];\r\n\r\n // Compute ratio scale: how many numerator units per denominator unit (e.g., IN:FT = 12)\r\n const denominatorToNumerator = await unitsProvider.getConversion(denominatorUnit, numeratorUnit);\r\n const displayRatioScale = denominatorToNumerator.factor;\r\n\r\n // Avoid double-scaling: if persistence unit already encodes the display ratio, use factor 1.\r\n // Check by name heuristic (e.g., IN_PER_FT with ratioUnits [IN, FT] → no scaling needed)\r\n const persistenceName = persistenceUnit.name.toUpperCase();\r\n const numName = numeratorUnit.name.toUpperCase().split(\".\").pop() ?? \"\";\r\n const denName = denominatorUnit.name.toUpperCase().split(\".\").pop() ?? \"\";\r\n // Split by word boundaries (underscores, dots) and check for exact token matches\r\n const persistenceTokens = persistenceName.split(/[._]/);\r\n const isPersistenceMatchingRatio = persistenceTokens.includes(numName) && persistenceTokens.includes(denName);\r\n const ratioScaleFactor = isPersistenceMatchingRatio ? 1.0 : displayRatioScale;\r\n\r\n // First conversion spec: effective ratio unit conversion\r\n const ratioConversionSpec: UnitConversionSpec = {\r\n name: `${numeratorUnit.name}_per_${denominatorUnit.name}`,\r\n label: \"\",\r\n system: numeratorUnit.system,\r\n conversion: { factor: ratioScaleFactor, offset: 0.0 },\r\n };\r\n conversions.push(ratioConversionSpec);\r\n\r\n // Numerator unit for label lookup\r\n const numeratorSpec: UnitConversionSpec = {\r\n name: numeratorUnit.name,\r\n label: numeratorLabel?.length ? numeratorLabel : numeratorUnit.label,\r\n system: numeratorUnit.system,\r\n conversion: { factor: 1.0, offset: 0.0 },\r\n };\r\n conversions.push(numeratorSpec);\r\n\r\n // Denominator unit for label lookup\r\n const denominatorSpec: UnitConversionSpec = {\r\n name: denominatorUnit.name,\r\n label: denominatorLabel?.length ? denominatorLabel : denominatorUnit.label,\r\n system: denominatorUnit.system,\r\n conversion: { factor: 1.0, offset: 0.0 },\r\n };\r\n conversions.push(denominatorSpec);\r\n\r\n return conversions;\r\n }\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 // Handle 2-unit composite for ratio formats (scale factors)\r\n if (format.type === FormatType.Ratio && format.units && format.units.length === 2) {\r\n return FormatterSpec.getRatioUnitConversions(format.units, unitsProvider, persistenceUnit);\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"]}
@@ -4,6 +4,25 @@
4
4
  import { BeEvent } from "@itwin/core-bentley";
5
5
  import { UnitProps } from "../Interfaces";
6
6
  import { DecimalPrecision, FormatTraits, FormatType, FractionalPrecision } from "./FormatEnums";
7
+ /** Defines a unit specification with a name and optional label override.
8
+ * Used in composite formats and ratio unit specifications.
9
+ * @beta
10
+ */
11
+ export interface FormatUnitSpec {
12
+ /** The name of the unit (e.g., "Units.FT", "Units.IN") */
13
+ readonly name: string;
14
+ /** Optional custom label that overrides the unit's default label (e.g., '"' for inches, "'" for feet) */
15
+ readonly label?: string;
16
+ }
17
+ /** A resolved [[FormatUnitSpec]] with the unit name replaced with the resolved UnitProps.
18
+ * @beta
19
+ */
20
+ export interface ResolvedFormatUnitSpec {
21
+ /** The resolved unit */
22
+ readonly unit: UnitProps;
23
+ /** Optional custom label that overrides the unit's default label */
24
+ readonly label?: string;
25
+ }
7
26
  /** Defines the units that make up a composite format and their display properties.
8
27
  * A composite format allows displaying a single quantity value across multiple units,
9
28
  * such as displaying length as "5 feet 6 inches" or angle as "45° 30' 15"".
@@ -15,12 +34,7 @@ export interface FormatCompositeProps {
15
34
  readonly includeZero?: boolean;
16
35
  /** Array of units this format is comprised of. Each unit specifies the unit name and
17
36
  * an optional custom label that will override the unit's default label when displaying values. */
18
- readonly units: Array<{
19
- /** The name of the unit (e.g., "Units.FT", "Units.IN") */
20
- readonly name: string;
21
- /** Optional custom label that overrides the unit's default label (e.g., "ft" for feet, 'in' for inches) */
22
- readonly label?: string;
23
- }>;
37
+ readonly units: FormatUnitSpec[];
24
38
  }
25
39
  /** This interface defines the persistence format for describing the formatting of quantity values.
26
40
  * @beta
@@ -39,6 +53,10 @@ export interface FormatProps {
39
53
  readonly scientificType?: string;
40
54
  /** conditionally required. */
41
55
  readonly ratioType?: string;
56
+ /** The separator character for ratio formatting. Defaults to ':' if not specified. */
57
+ readonly ratioSeparator?: string;
58
+ /** The format type for the numbers within a ratio. Defaults to "Decimal". */
59
+ readonly ratioFormatType?: string;
42
60
  /** conditionally required. */
43
61
  readonly stationOffsetSize?: number;
44
62
  readonly stationSeparator?: string;
@@ -69,10 +87,7 @@ export declare const isCustomFormatProps: (item: FormatProps) => item is CustomF
69
87
  * @beta
70
88
  */
71
89
  export type ResolvedFormatCompositeProps = Omit<FormatCompositeProps, "units"> & {
72
- readonly units: Array<{
73
- readonly unit: UnitProps;
74
- readonly label?: string;
75
- }>;
90
+ readonly units: ResolvedFormatUnitSpec[];
76
91
  };
77
92
  /** A [[FormatProps]] with all the references to units replaced with JSON representations of those units.
78
93
  * @beta
@@ -1 +1 @@
1
- {"version":3,"file":"Interfaces.d.ts","sourceRoot":"","sources":["../../../src/Formatter/Interfaces.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,MA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AO,CAAC,gBAAgB,GAAG,SAAS,CAAC,CAAC;IAE/D;;;OAGG;IACH,gBAAgB,EAAE,OAAO,CAAC,CAAC,IAAI,EAAE,kBAAkB,KAAK,IAAI,CAAC,CAAC;CAC/D;AAED;;GAEG;AACH,MAAM,WAAW,sBAAuB,SAAQ,eAAe;IAC7D;;OAEG;IACH,SAAS,CAAC,IAAI,EAAE,MAAM,EAAE,MAAM,EAAE,gBAAgB,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IACjE;;;OAGG;IACH,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC3C"}
@@ -1 +1 @@
1
- {"version":3,"file":"Interfaces.js","sourceRoot":"","sources":["../../../src/Formatter/Interfaces.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AA2EH;;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 { BeEvent } from \"@itwin/core-bentley\";\r\nimport { UnitProps } from \"../Interfaces\";\r\nimport { DecimalPrecision, FormatTraits, FormatType, FractionalPrecision } from \"./FormatEnums\";\r\n\r\n/** Defines the units that make up a composite format and their display properties.\r\n * A composite format allows displaying a single quantity value across multiple units,\r\n * such as displaying length as \"5 feet 6 inches\" or angle as \"45° 30' 15\"\".\r\n * @beta\r\n */\r\nexport interface FormatCompositeProps {\r\n /** separates values when formatting composite strings */\r\n readonly spacer?: string;\r\n readonly includeZero?: boolean;\r\n /** Array of units this format is comprised of. Each unit specifies the unit name and\r\n * an optional custom label that will override the unit's default label when displaying values. */\r\n readonly units: Array<{\r\n /** The name of the unit (e.g., \"Units.FT\", \"Units.IN\") */\r\n readonly name: string;\r\n /** Optional custom label that overrides the unit's default label (e.g., \"ft\" for feet, 'in' for inches) */\r\n readonly label?: string;\r\n }>;\r\n}\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 ratioType?: string;\r\n\r\n /** conditionally required. */\r\n readonly stationOffsetSize?: number;\r\n readonly stationSeparator?: string;\r\n\r\n /** Optional base factor for station formatting. A positive integer, defaults to 1. */\r\n readonly stationBaseFactor?: number;\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?: FormatCompositeProps;\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/** A [[FormatCompositeProps]] with unit names replaced with JSON representations of those units.\r\n * @beta\r\n */\r\nexport type ResolvedFormatCompositeProps = Omit<FormatCompositeProps, \"units\"> & {\r\n readonly units: Array<{\r\n readonly unit: UnitProps;\r\n readonly label?: string;\r\n }>;\r\n};\r\n\r\n/** A [[FormatProps]] with all the references to units replaced with JSON representations of those units.\r\n * @beta\r\n */\r\nexport type ResolvedFormatProps = Omit<FormatDefinition, \"azimuthBaseUnit\" | \"revolutionUnit\" | \"composite\"> & {\r\n readonly azimuthBaseUnit?: UnitProps;\r\n readonly revolutionUnit?: UnitProps;\r\n readonly composite?: ResolvedFormatCompositeProps;\r\n readonly custom?: any;\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\r\n/** An extension of FormatProps to help identify formats.\r\n * @beta\r\n */\r\nexport interface FormatDefinition extends FormatProps {\r\n readonly name?: string;\r\n readonly label?: string;\r\n readonly description?: string;\r\n}\r\n\r\n/** Argument for [[FormatsProvider.onFormatsChanged]]\r\n * @beta\r\n */\r\nexport interface FormatsChangedArgs {\r\n /**\r\n * If `all` - all formats within the `FormatsProvider` have changed.\r\n * If array, the array items list the names of formats that were changed or removed.\r\n */\r\n formatsChanged: \"all\" | string[];\r\n}\r\n\r\n/** This interface is implemented by a class that would provide formats for use in formatting quantities.\r\n * @beta\r\n */\r\nexport interface FormatsProvider {\r\n /**\r\n * @param name The full name of the Format or KindOfQuantity.\r\n */\r\n getFormat(name: string): Promise<FormatDefinition | undefined>;\r\n\r\n /**\r\n * Fired when formats are added, removed, or changed.\r\n * If all formats are changed, a single string \"all\" is emitted. Else, an array of changed format names is emitted.\r\n */\r\n onFormatsChanged: BeEvent<(args: FormatsChangedArgs) => void>;\r\n}\r\n\r\n/** This interface is implemented by a class that would provide and allow creating formats for use in formatting quantities.\r\n * @beta\r\n */\r\nexport interface MutableFormatsProvider extends FormatsProvider {\r\n /**\r\n * Adds a new format or updates an existing format associated with the specified name.\r\n */\r\n addFormat(name: string, format: FormatDefinition): Promise<void>;\r\n /**\r\n * Removes the format associated with the specified name.\r\n * @param name The name of the format to remove.\r\n */\r\n removeFormat(name: string): Promise<void>;\r\n}\r\n"]}
1
+ {"version":3,"file":"Interfaces.js","sourceRoot":"","sources":["../../../src/Formatter/Interfaces.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AA+FH;;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 { BeEvent } from \"@itwin/core-bentley\";\r\nimport { UnitProps } from \"../Interfaces\";\r\nimport { DecimalPrecision, FormatTraits, FormatType, FractionalPrecision } from \"./FormatEnums\";\r\n\r\n/** Defines a unit specification with a name and optional label override.\r\n * Used in composite formats and ratio unit specifications.\r\n * @beta\r\n */\r\nexport interface FormatUnitSpec {\r\n /** The name of the unit (e.g., \"Units.FT\", \"Units.IN\") */\r\n readonly name: string;\r\n /** Optional custom label that overrides the unit's default label (e.g., '\"' for inches, \"'\" for feet) */\r\n readonly label?: string;\r\n}\r\n\r\n/** A resolved [[FormatUnitSpec]] with the unit name replaced with the resolved UnitProps.\r\n * @beta\r\n */\r\nexport interface ResolvedFormatUnitSpec {\r\n /** The resolved unit */\r\n readonly unit: UnitProps;\r\n /** Optional custom label that overrides the unit's default label */\r\n readonly label?: string;\r\n}\r\n\r\n/** Defines the units that make up a composite format and their display properties.\r\n * A composite format allows displaying a single quantity value across multiple units,\r\n * such as displaying length as \"5 feet 6 inches\" or angle as \"45° 30' 15\"\".\r\n * @beta\r\n */\r\nexport interface FormatCompositeProps {\r\n /** separates values when formatting composite strings */\r\n readonly spacer?: string;\r\n readonly includeZero?: boolean;\r\n /** Array of units this format is comprised of. Each unit specifies the unit name and\r\n * an optional custom label that will override the unit's default label when displaying values. */\r\n readonly units: FormatUnitSpec[];\r\n}\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 ratioType?: string;\r\n /** The separator character for ratio formatting. Defaults to ':' if not specified. */\r\n readonly ratioSeparator?: string;\r\n /** The format type for the numbers within a ratio. Defaults to \"Decimal\". */\r\n readonly ratioFormatType?: string;\r\n\r\n /** conditionally required. */\r\n readonly stationOffsetSize?: number;\r\n readonly stationSeparator?: string;\r\n\r\n /** Optional base factor for station formatting. A positive integer, defaults to 1. */\r\n readonly stationBaseFactor?: number;\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?: FormatCompositeProps;\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/** A [[FormatCompositeProps]] with unit names replaced with JSON representations of those units.\r\n * @beta\r\n */\r\nexport type ResolvedFormatCompositeProps = Omit<FormatCompositeProps, \"units\"> & {\r\n readonly units: ResolvedFormatUnitSpec[];\r\n};\r\n\r\n/** A [[FormatProps]] with all the references to units replaced with JSON representations of those units.\r\n * @beta\r\n */\r\nexport type ResolvedFormatProps = Omit<FormatDefinition, \"azimuthBaseUnit\" | \"revolutionUnit\" | \"composite\"> & {\r\n readonly azimuthBaseUnit?: UnitProps;\r\n readonly revolutionUnit?: UnitProps;\r\n readonly composite?: ResolvedFormatCompositeProps;\r\n readonly custom?: any;\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\r\n/** An extension of FormatProps to help identify formats.\r\n * @beta\r\n */\r\nexport interface FormatDefinition extends FormatProps {\r\n readonly name?: string;\r\n readonly label?: string;\r\n readonly description?: string;\r\n}\r\n\r\n/** Argument for [[FormatsProvider.onFormatsChanged]]\r\n * @beta\r\n */\r\nexport interface FormatsChangedArgs {\r\n /**\r\n * If `all` - all formats within the `FormatsProvider` have changed.\r\n * If array, the array items list the names of formats that were changed or removed.\r\n */\r\n formatsChanged: \"all\" | string[];\r\n}\r\n\r\n/** This interface is implemented by a class that would provide formats for use in formatting quantities.\r\n * @beta\r\n */\r\nexport interface FormatsProvider {\r\n /**\r\n * @param name The full name of the Format or KindOfQuantity.\r\n */\r\n getFormat(name: string): Promise<FormatDefinition | undefined>;\r\n\r\n /**\r\n * Fired when formats are added, removed, or changed.\r\n * If all formats are changed, a single string \"all\" is emitted. Else, an array of changed format names is emitted.\r\n */\r\n onFormatsChanged: BeEvent<(args: FormatsChangedArgs) => void>;\r\n}\r\n\r\n/** This interface is implemented by a class that would provide and allow creating formats for use in formatting quantities.\r\n * @beta\r\n */\r\nexport interface MutableFormatsProvider extends FormatsProvider {\r\n /**\r\n * Adds a new format or updates an existing format associated with the specified name.\r\n */\r\n addFormat(name: string, format: FormatDefinition): Promise<void>;\r\n /**\r\n * Removes the format associated with the specified name.\r\n * @param name The name of the format to remove.\r\n */\r\n removeFormat(name: string): Promise<void>;\r\n}\r\n"]}
@@ -16,7 +16,8 @@ export declare enum ParseError {
16
16
  InvalidParserSpec = 6,
17
17
  BearingPrefixOrSuffixMissing = 7,
18
18
  MathematicOperationFoundButIsNotAllowed = 8,
19
- BearingAngleOutOfRange = 9
19
+ BearingAngleOutOfRange = 9,
20
+ InvalidMathResult = 10
20
21
  }
21
22
  /** Parse error result from [[Parser.parseToQuantityValue]] or [[Parser.parseToQuantityValue]].
22
23
  * @beta
@@ -116,6 +117,20 @@ export declare class Parser {
116
117
  private static processSpecialBearingDirection;
117
118
  private static parseBearingFormat;
118
119
  private static parseAzimuthFormat;
120
+ /**
121
+ * Parse a ratio part string (numerator or denominator) to extract the numeric value and optional unit label.
122
+ * This method processes tokens without applying unit conversions, allowing the ratio format
123
+ * handler to manage conversions at the ratio level.
124
+ *
125
+ *
126
+ * @note Fractions are already handled by parseQuantitySpecification, which converts them to
127
+ * single numeric tokens (e.g., "1/2" becomes 0.5).
128
+ *
129
+ * @param partStr The string to parse, which may contain a number, fraction, or mixed fraction with optional unit label.
130
+ * @param format The format specification used for token parsing.
131
+ * @returns An object containing the parsed numeric value and optional unit label. Returns NaN for value if no number is found.
132
+ */
133
+ private static parseRatioPart;
119
134
  private static parseRatioFormat;
120
135
  private static normalizeAngle;
121
136
  private static getRevolution;
@@ -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;IACvC,sBAAsB,IAAA;CACvB;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;IA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;;;;OAYG;IACH,OAAO,CAAC,MAAM,CAAC,cAAc;IAoC7B,OAAO,CAAC,MAAM,CAAC,gBAAgB;IAqF/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
@@ -23,13 +23,14 @@ export var ParseError;
23
23
  ParseError[ParseError["BearingPrefixOrSuffixMissing"] = 7] = "BearingPrefixOrSuffixMissing";
24
24
  ParseError[ParseError["MathematicOperationFoundButIsNotAllowed"] = 8] = "MathematicOperationFoundButIsNotAllowed";
25
25
  ParseError[ParseError["BearingAngleOutOfRange"] = 9] = "BearingAngleOutOfRange";
26
+ ParseError[ParseError["InvalidMathResult"] = 10] = "InvalidMathResult";
26
27
  })(ParseError || (ParseError = {}));
27
28
  var Operator;
28
29
  (function (Operator) {
29
30
  Operator["addition"] = "+";
30
31
  Operator["subtraction"] = "-";
31
32
  Operator["multiplication"] = "*";
32
- Operator["division"] = "/"; // unsupported but we recognize it during parsing
33
+ Operator["division"] = "/";
33
34
  })(Operator || (Operator = {}));
34
35
  function isOperator(char) {
35
36
  if (typeof char === "number") {
@@ -784,36 +785,107 @@ export class Parser {
784
785
  magnitude = this.normalizeAngle(magnitude, revolution);
785
786
  return { ok: true, value: magnitude };
786
787
  }
788
+ /**
789
+ * Parse a ratio part string (numerator or denominator) to extract the numeric value and optional unit label.
790
+ * This method processes tokens without applying unit conversions, allowing the ratio format
791
+ * handler to manage conversions at the ratio level.
792
+ *
793
+ *
794
+ * @note Fractions are already handled by parseQuantitySpecification, which converts them to
795
+ * single numeric tokens (e.g., "1/2" becomes 0.5).
796
+ *
797
+ * @param partStr The string to parse, which may contain a number, fraction, or mixed fraction with optional unit label.
798
+ * @param format The format specification used for token parsing.
799
+ * @returns An object containing the parsed numeric value and optional unit label. Returns NaN for value if no number is found.
800
+ */
801
+ static parseRatioPart(partStr, format) {
802
+ partStr = partStr.trim();
803
+ // Parse tokens - fractions are automatically converted to decimal values by parseQuantitySpecification
804
+ const tempFormat = format.clone({ type: FormatType.Decimal });
805
+ const tokens = Parser.parseQuantitySpecification(partStr, tempFormat);
806
+ let value = NaN;
807
+ let unitLabel;
808
+ // Pre-process: merge negative operators with following numbers
809
+ const processedTokens = [];
810
+ for (let i = 0; i < tokens.length; i++) {
811
+ const token = tokens[i];
812
+ if (token.isOperator && i === 0 && token.value === "-" &&
813
+ i + 1 < tokens.length && tokens[i + 1].isNumber) {
814
+ // Merge negative sign with number
815
+ processedTokens.push(new ParseToken(-tokens[i + 1].value));
816
+ i++; // Skip the number token since we consumed it
817
+ }
818
+ else {
819
+ processedTokens.push(token);
820
+ }
821
+ }
822
+ // Extract numeric value and unit label from processed tokens
823
+ for (const token of processedTokens) {
824
+ if (token.isNumber && isNaN(value)) {
825
+ value = token.value;
826
+ }
827
+ else if (token.isString && !token.isOperator) {
828
+ // String token that's not an operator - treat as unit label
829
+ unitLabel = token.value;
830
+ }
831
+ }
832
+ return { value, unitLabel };
833
+ }
787
834
  static parseRatioFormat(inString, spec) {
788
835
  if (!inString)
789
836
  return { ok: false, error: ParseError.NoValueOrUnitFoundInString };
790
- const parts = inString.split(":");
837
+ const separator = spec.format.ratioSeparator ?? ":";
838
+ const parts = inString.split(separator);
791
839
  if (parts.length > 2)
792
840
  return { ok: false, error: ParseError.UnableToConvertParseTokensToQuantity };
793
- const numerator = parseFloat(parts[0]);
794
- let denominator;
795
- if (parts.length === 1) {
796
- denominator = 1.0;
797
- }
798
- else {
799
- denominator = parseFloat(parts[1]);
841
+ // If the string doesn't contain the expected separator but contains other ratio-like separators,
842
+ // return an error since the wrong separator was used
843
+ if (parts.length === 1 && !inString.includes(separator)) {
844
+ // Check if the string contains other common ratio separators
845
+ const otherSeparators = [":", "=", "/"];
846
+ for (const otherSep of otherSeparators) {
847
+ if (otherSep !== separator && inString.includes(otherSep)) {
848
+ // The string looks like a ratio but uses the wrong separator
849
+ return { ok: false, error: ParseError.UnableToConvertParseTokensToQuantity };
850
+ }
851
+ }
852
+ // Parse as a regular quantity value (numerator only, denominator = 1)
853
+ const result = this.parseAndProcessTokens(inString, spec.format, spec.unitConversions);
854
+ return result;
800
855
  }
801
- if (isNaN(numerator) || isNaN(denominator))
856
+ // Parse numerator and denominator parts which may include unit labels
857
+ const numeratorPart = this.parseRatioPart(parts[0], spec.format);
858
+ const denominatorPart = parts.length === 1 ? { value: 1.0 } : this.parseRatioPart(parts[1], spec.format);
859
+ if (isNaN(numeratorPart.value) || isNaN(denominatorPart.value))
802
860
  return { ok: false, error: ParseError.NoValueOrUnitFoundInString };
861
+ // Handle 2-unit composite case - simpler conversion using the pre-computed scale factor
862
+ if (spec.format.units && spec.format.units.length === 2 && spec.unitConversions.length >= 3) {
863
+ const ratioConvSpec = spec.unitConversions[0];
864
+ const scaleFactor = ratioConvSpec.conversion.factor;
865
+ if (denominatorPart.value === 0) {
866
+ return { ok: false, error: ParseError.InvalidMathResult };
867
+ }
868
+ // The ratio value is numerator/denominator in the display units (e.g., 12 for 12"=1')
869
+ // Divide by scale factor to get persistence unit value (e.g., 12/12 = 1.0)
870
+ const ratioValue = numeratorPart.value / denominatorPart.value;
871
+ const convertedValue = ratioValue / scaleFactor;
872
+ return { ok: true, value: convertedValue };
873
+ }
874
+ // Original flow for 1-unit composite - use Quantity.convertTo for proper unit conversion
803
875
  const defaultUnit = spec.format.units && spec.format.units.length > 0 ? spec.format.units[0][0] : undefined;
804
876
  const unitConversion = defaultUnit ? Parser.tryFindUnitConversion(defaultUnit.label, spec.unitConversions, defaultUnit) : undefined;
805
877
  if (!unitConversion) {
806
878
  throw new QuantityError(QuantityStatus.MissingRequiredProperty, `Missing input unit or unit conversion for interpreting ${spec.format.name}.`);
807
879
  }
808
- if (denominator === 0) {
809
- if (unitConversion.inversion && numerator === 1)
880
+ if (denominatorPart.value === 0) {
881
+ if (unitConversion.inversion && numeratorPart.value === 1)
810
882
  return { ok: true, value: 0.0 };
811
883
  else
812
- return { ok: false, error: ParseError.MathematicOperationFoundButIsNotAllowed };
884
+ return { ok: false, error: ParseError.InvalidMathResult };
813
885
  }
814
886
  let quantity;
815
887
  if (spec.format.units && spec.outUnit) {
816
- quantity = new Quantity(spec.format.units[0][0], numerator / denominator);
888
+ quantity = new Quantity(spec.format.units[0][0], numeratorPart.value / denominatorPart.value);
817
889
  }
818
890
  else {
819
891
  throw new QuantityError(QuantityStatus.MissingRequiredProperty, "Missing presentation unit or persistence unit for ratio format.");
@@ -825,7 +897,7 @@ export class Parser {
825
897
  catch (err) {
826
898
  // for input of "0:N" with reversed unit
827
899
  if (err instanceof QuantityError && err.errorNumber === QuantityStatus.InvertingZero) {
828
- return { ok: false, error: ParseError.MathematicOperationFoundButIsNotAllowed };
900
+ return { ok: false, error: ParseError.InvalidMathResult };
829
901
  }
830
902
  }
831
903
  if (converted === undefined || !converted.isValid) {