@itwin/presentation-common 5.0.0-dev.99 → 5.1.0-dev.1

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 CHANGED
@@ -1,6 +1,11 @@
1
1
  # Change Log - @itwin/presentation-common
2
2
 
3
- This log was last generated on Wed, 02 Apr 2025 19:38:36 GMT and should not be manually modified.
3
+ This log was last generated on Thu, 10 Apr 2025 17:50:15 GMT and should not be manually modified.
4
+
5
+ ## 4.10.13
6
+ Thu, 10 Apr 2025 17:47:21 GMT
7
+
8
+ _Version update only_
4
9
 
5
10
  ## 4.10.12
6
11
  Wed, 02 Apr 2025 19:35:47 GMT
@@ -109,19 +109,21 @@ async function getKoqFormatProps(koq, persistenceUnit, defaultFormats, unitSyste
109
109
  }
110
110
  // use default presentation format if persistence unit does not match requested unit system
111
111
  if (koq.defaultPresentationFormat) {
112
- return (0, ecschema_metadata_1.getFormatProps)(koq.defaultPresentationFormat);
112
+ return (0, ecschema_metadata_1.getFormatProps)(await koq.defaultPresentationFormat);
113
113
  }
114
114
  return undefined;
115
115
  }
116
116
  async function getKoqPresentationFormat(koq, unitSystemMatchers) {
117
117
  const presentationFormats = koq.presentationFormats;
118
118
  for (const matcher of unitSystemMatchers) {
119
- for (const format of presentationFormats) {
120
- const unit = format.units && format.units[0][0];
119
+ for (const lazyFormat of presentationFormats) {
120
+ const format = await lazyFormat;
121
+ const lazyUnit = format.units && format.units[0][0];
121
122
  /* c8 ignore next 3 */
122
- if (!unit) {
123
+ if (!lazyUnit) {
123
124
  continue;
124
125
  }
126
+ const unit = await lazyUnit;
125
127
  const currentUnitSystem = await unit.unitSystem;
126
128
  if (currentUnitSystem && matcher(currentUnitSystem)) {
127
129
  return format;
@@ -1 +1 @@
1
- {"version":3,"file":"KoqPropertyValueFormatter.js","sourceRoot":"","sources":["../../../src/presentation-common/KoqPropertyValueFormatter.ts"],"names":[],"mappings":";AAAA;;;gGAGgG;AAChG;;GA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CAAC;QACjF,KAAK,QAAQ;YACX,OAAO,CAAC,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAC3E,KAAK,aAAa;YAChB,OAAO,CAAC,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QACrE,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IACnF,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,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 Core\n */\n\nimport { Format, FormatProps, FormatterSpec, ParserSpec, UnitsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\nimport {\n getFormatProps,\n InvertedUnit,\n KindOfQuantity,\n SchemaContext,\n SchemaKey,\n SchemaMatchType,\n SchemaUnitProvider,\n Unit,\n UnitSystem,\n} from \"@itwin/ecschema-metadata\";\n\n/**\n * A data structure that associates unit systems with property value formatting props. The associations are used for\n * assigning formatting props for specific phenomenon and unit system combinations (see [[FormatsMap]]).\n *\n * @public\n */\nexport interface UnitSystemFormat {\n unitSystems: UnitSystemKey[];\n format: FormatProps;\n}\n\n/**\n * A data structure that associates specific phenomenon with one or more formatting props for specific unit system.\n *\n * Example:\n * ```json\n * {\n * length: [{\n * unitSystems: [\"metric\"],\n * format: formatForCentimeters,\n * }, {\n * unitSystems: [\"imperial\", \"usCustomary\"],\n * format: formatForInches,\n * }, {\n * unitSystems: [\"usSurvey\"],\n * format: formatForUsSurveyInches,\n * }]\n * }\n * ```\n *\n * @public\n */\nexport interface FormatsMap {\n [phenomenon: string]: UnitSystemFormat | UnitSystemFormat[];\n}\n\n/**\n * Options for finding a formatter spec to use in [[KoqPropertyValueFormatter]].\n * @public\n */\nexport interface FormatOptions {\n /** Full name of the `KindOfQuantity`, e.g. `SchemaName:KoqName`. */\n koqName: string;\n /** Unit system to use for formatting. */\n unitSystem?: UnitSystemKey;\n}\n\n/**\n * An utility for formatting property values based on `KindOfQuantity` and unit system.\n * @public\n */\nexport class KoqPropertyValueFormatter {\n private _unitsProvider: UnitsProvider;\n private _defaultFormats?: FormatsMap;\n\n constructor(\n private _schemaContext: SchemaContext,\n defaultFormats?: FormatsMap,\n ) {\n this._unitsProvider = new SchemaUnitProvider(_schemaContext);\n this._defaultFormats = defaultFormats\n ? Object.entries(defaultFormats).reduce((acc, [phenomenon, unitSystemFormats]) => ({ ...acc, [phenomenon.toUpperCase()]: unitSystemFormats }), {})\n : /* c8 ignore next */ undefined;\n }\n\n public async format(value: number, options: FormatOptions) {\n const formatterSpec = await this.getFormatterSpec(options);\n if (!formatterSpec) {\n return undefined;\n }\n return formatterSpec.applyFormatting(value);\n }\n\n public async getFormatterSpec(options: FormatOptions) {\n const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);\n if (!formattingProps) {\n return undefined;\n }\n const { formatProps, persistenceUnitName } = formattingProps;\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\n return FormatterSpec.create(\"\", format, this._unitsProvider, persistenceUnit);\n }\n\n public async getParserSpec(options: FormatOptions) {\n const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);\n if (!formattingProps) {\n return undefined;\n }\n const { formatProps, persistenceUnitName } = formattingProps;\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\n return ParserSpec.create(format, this._unitsProvider, persistenceUnit);\n }\n}\n\ninterface FormattingProps {\n formatProps: FormatProps;\n persistenceUnitName: string;\n}\n\nasync function getFormattingProps(\n schemaLocater: SchemaContext,\n defaultFormats: FormatsMap | undefined,\n options: FormatOptions,\n): Promise<FormattingProps | undefined> {\n const { koqName, unitSystem } = options;\n\n const koq = await getKoq(schemaLocater, koqName);\n if (!koq) {\n return undefined;\n }\n\n const persistenceUnit = await koq.persistenceUnit;\n /* c8 ignore next 3 */\n if (!persistenceUnit) {\n return undefined;\n }\n\n const formatProps = await getKoqFormatProps(koq, persistenceUnit, defaultFormats, unitSystem);\n if (!formatProps) {\n return undefined;\n }\n\n return { formatProps, persistenceUnitName: persistenceUnit.fullName };\n}\n\nasync function getKoq(schemaLocater: SchemaContext, fullName: string) {\n const [schemaName, propKoqName] = fullName.split(\":\");\n const schema = await schemaLocater.getSchema(new SchemaKey(schemaName), SchemaMatchType.Latest);\n if (!schema) {\n return undefined;\n }\n return schema.getItem(propKoqName, KindOfQuantity);\n}\n\nasync function getKoqFormatProps(\n koq: KindOfQuantity,\n persistenceUnit: Unit | InvertedUnit,\n defaultFormats: FormatsMap | undefined,\n unitSystem?: UnitSystemKey,\n) {\n const unitSystemMatchers = getUnitSystemGroupMatchers(unitSystem);\n // use one of KOQ presentation format that matches requested unit system\n const presentationFormat = await getKoqPresentationFormat(koq, unitSystemMatchers);\n if (presentationFormat) {\n return getFormatProps(presentationFormat);\n }\n\n // use one of the formats in default formats map if there is one for matching phenomena and requested unit\n // system combination\n if (defaultFormats && unitSystem) {\n const actualPersistenceUnit = persistenceUnit instanceof InvertedUnit ? /* c8 ignore next */ await persistenceUnit.invertsUnit : persistenceUnit;\n const phenomenon = await actualPersistenceUnit?.phenomenon;\n if (phenomenon && defaultFormats[phenomenon.name.toUpperCase()]) {\n const defaultPhenomenonFormats = defaultFormats[phenomenon.name.toUpperCase()];\n for (const defaultUnitSystemFormat of Array.isArray(defaultPhenomenonFormats)\n ? /* c8 ignore next */ defaultPhenomenonFormats\n : [defaultPhenomenonFormats]) {\n if (defaultUnitSystemFormat.unitSystems.includes(unitSystem)) {\n return defaultUnitSystemFormat.format;\n }\n }\n }\n }\n\n // use persistence unit format if it matches requested unit system and matching presentation format was not found\n const persistenceUnitSystem = await persistenceUnit.unitSystem;\n if (persistenceUnitSystem && unitSystemMatchers.some((matcher) => matcher(persistenceUnitSystem))) {\n return getPersistenceUnitFormatProps(persistenceUnit);\n }\n\n // use default presentation format if persistence unit does not match requested unit system\n if (koq.defaultPresentationFormat) {\n return getFormatProps(koq.defaultPresentationFormat);\n }\n\n return undefined;\n}\n\nasync function getKoqPresentationFormat(koq: KindOfQuantity, unitSystemMatchers: Array<(unitSystem: UnitSystem) => boolean>) {\n const presentationFormats = koq.presentationFormats;\n for (const matcher of unitSystemMatchers) {\n for (const format of presentationFormats) {\n const unit = format.units && format.units[0][0];\n /* c8 ignore next 3 */\n if (!unit) {\n continue;\n }\n const currentUnitSystem = await unit.unitSystem;\n if (currentUnitSystem && matcher(currentUnitSystem)) {\n return format;\n }\n }\n }\n return undefined;\n}\n\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\n // Same as Format \"DefaultRealU\" in Formats ecschema\n return {\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\n precision: 6,\n type: \"Decimal\",\n uomSeparator: \" \",\n decimalSeparator: \".\",\n composite: {\n units: [\n {\n name: persistenceUnit.fullName,\n label: persistenceUnit.label,\n },\n ],\n },\n };\n}\n\nfunction getUnitSystemGroupMatchers(groupKey?: UnitSystemKey) {\n function createMatcher(name: string | string[]) {\n const names = Array.isArray(name) ? name : [name];\n return (unitSystem: UnitSystem) => names.some((n) => n === unitSystem.name.toUpperCase());\n }\n switch (groupKey) {\n case \"imperial\":\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n case \"metric\":\n return [[\"SI\", \"METRIC\"], \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n case \"usCustomary\":\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n case \"usSurvey\":\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n }\n return [];\n}\n"]}
1
+ {"version":3,"file":"KoqPropertyValueFormatter.js","sourceRoot":"","sources":["../../../src/presentation-common/KoqPropertyValueFormatter.ts"],"names":[],"mappings":";AAAA;;;gGAGgG;AAChG;;GAEG;;;AAEH,wDAAoH;AACpH,gEAUkC;AAiDlC;;;GAGG;AACH,MAAa,yBAAyB;IAK1B;IAJF,cAAc,CAAgB;IAC9B,eAAe,CAAc;IAErC,YACU,cAA6B,EACrC,cAA2B;QADnB,mBAAc,GAAd,cAAc,CAAe;QAGrC,IAAI,CAAC,cAAc,GAAG,IAAI,sCAAkB,CAAC,cAAc,CAAC,CAAC;QAC7D,IAAI,CAAC,eAAe,GAAG,cAAc;YACnC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,UAAU,EAAE,iBAAiB,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,GAAG,EAAE,CAAC,UAAU,CAAC,WAAW,EAAE,CAAC,EAAE,iBAAiB,EAAE,CAAC,EAAE,EAAE,CAAC;YAClJ,CAAC,CAAC,oBAAoB,CAAC,SAAS,CAAC;IACrC,CAAC;IAEM,KAAK,CAAC,MAAM,CAAC,KAAa,EAAE,OAAsB;QACvD,MAAM,aAAa,GAAG,MAAM,IAAI,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;QAC3D,IAAI,CAAC,aAAa,EAAE,CAAC;YACnB,OAAO,SAAS,CAAC;QACnB,CAAC;QACD,OAAO,aAAa,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;IAC9C,CAAC;IAEM,KAAK,CAAC,gBAAgB,CAAC,OAAsB;QAClD,MAAM,eAAe,GAAG,MAAM,kBAAkB,CAAC,IAAI,CAAC,cAAc,EAAE,IAAI,CAAC,eAAe,EAAE,O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AC,IAAI,CAAC,CAAC;QAClD,OAAO,CAAC,UAAsB,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,UAAU,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;IAC5F,CAAC;IACD,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QACjF,KAAK,QAAQ;YACX,OAAO,CAAC,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAC3E,KAAK,aAAa;YAChB,OAAO,CAAC,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QACrE,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IACnF,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,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 Core\n */\n\nimport { Format, FormatProps, FormatterSpec, ParserSpec, UnitsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\nimport {\n getFormatProps,\n InvertedUnit,\n KindOfQuantity,\n SchemaContext,\n SchemaKey,\n SchemaMatchType,\n SchemaUnitProvider,\n Unit,\n UnitSystem,\n} from \"@itwin/ecschema-metadata\";\n\n/**\n * A data structure that associates unit systems with property value formatting props. The associations are used for\n * assigning formatting props for specific phenomenon and unit system combinations (see [[FormatsMap]]).\n *\n * @public\n */\nexport interface UnitSystemFormat {\n unitSystems: UnitSystemKey[];\n format: FormatProps;\n}\n\n/**\n * A data structure that associates specific phenomenon with one or more formatting props for specific unit system.\n *\n * Example:\n * ```json\n * {\n * length: [{\n * unitSystems: [\"metric\"],\n * format: formatForCentimeters,\n * }, {\n * unitSystems: [\"imperial\", \"usCustomary\"],\n * format: formatForInches,\n * }, {\n * unitSystems: [\"usSurvey\"],\n * format: formatForUsSurveyInches,\n * }]\n * }\n * ```\n *\n * @public\n */\nexport interface FormatsMap {\n [phenomenon: string]: UnitSystemFormat | UnitSystemFormat[];\n}\n\n/**\n * Options for finding a formatter spec to use in [[KoqPropertyValueFormatter]].\n * @public\n */\nexport interface FormatOptions {\n /** Full name of the `KindOfQuantity`, e.g. `SchemaName:KoqName`. */\n koqName: string;\n /** Unit system to use for formatting. */\n unitSystem?: UnitSystemKey;\n}\n\n/**\n * An utility for formatting property values based on `KindOfQuantity` and unit system.\n * @public\n */\nexport class KoqPropertyValueFormatter {\n private _unitsProvider: UnitsProvider;\n private _defaultFormats?: FormatsMap;\n\n constructor(\n private _schemaContext: SchemaContext,\n defaultFormats?: FormatsMap,\n ) {\n this._unitsProvider = new SchemaUnitProvider(_schemaContext);\n this._defaultFormats = defaultFormats\n ? Object.entries(defaultFormats).reduce((acc, [phenomenon, unitSystemFormats]) => ({ ...acc, [phenomenon.toUpperCase()]: unitSystemFormats }), {})\n : /* c8 ignore next */ undefined;\n }\n\n public async format(value: number, options: FormatOptions) {\n const formatterSpec = await this.getFormatterSpec(options);\n if (!formatterSpec) {\n return undefined;\n }\n return formatterSpec.applyFormatting(value);\n }\n\n public async getFormatterSpec(options: FormatOptions) {\n const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);\n if (!formattingProps) {\n return undefined;\n }\n const { formatProps, persistenceUnitName } = formattingProps;\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\n return FormatterSpec.create(\"\", format, this._unitsProvider, persistenceUnit);\n }\n\n public async getParserSpec(options: FormatOptions) {\n const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);\n if (!formattingProps) {\n return undefined;\n }\n const { formatProps, persistenceUnitName } = formattingProps;\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\n return ParserSpec.create(format, this._unitsProvider, persistenceUnit);\n }\n}\n\ninterface FormattingProps {\n formatProps: FormatProps;\n persistenceUnitName: string;\n}\n\nasync function getFormattingProps(\n schemaLocater: SchemaContext,\n defaultFormats: FormatsMap | undefined,\n options: FormatOptions,\n): Promise<FormattingProps | undefined> {\n const { koqName, unitSystem } = options;\n\n const koq = await getKoq(schemaLocater, koqName);\n if (!koq) {\n return undefined;\n }\n\n const persistenceUnit = await koq.persistenceUnit;\n /* c8 ignore next 3 */\n if (!persistenceUnit) {\n return undefined;\n }\n\n const formatProps = await getKoqFormatProps(koq, persistenceUnit, defaultFormats, unitSystem);\n if (!formatProps) {\n return undefined;\n }\n\n return { formatProps, persistenceUnitName: persistenceUnit.fullName };\n}\n\nasync function getKoq(schemaLocater: SchemaContext, fullName: string) {\n const [schemaName, propKoqName] = fullName.split(\":\");\n const schema = await schemaLocater.getSchema(new SchemaKey(schemaName), SchemaMatchType.Latest);\n if (!schema) {\n return undefined;\n }\n return schema.getItem(propKoqName, KindOfQuantity);\n}\n\nasync function getKoqFormatProps(\n koq: KindOfQuantity,\n persistenceUnit: Unit | InvertedUnit,\n defaultFormats: FormatsMap | undefined,\n unitSystem?: UnitSystemKey,\n) {\n const unitSystemMatchers = getUnitSystemGroupMatchers(unitSystem);\n // use one of KOQ presentation format that matches requested unit system\n const presentationFormat = await getKoqPresentationFormat(koq, unitSystemMatchers);\n if (presentationFormat) {\n return getFormatProps(presentationFormat);\n }\n\n // use one of the formats in default formats map if there is one for matching phenomena and requested unit\n // system combination\n if (defaultFormats && unitSystem) {\n const actualPersistenceUnit = persistenceUnit instanceof InvertedUnit ? /* c8 ignore next */ await persistenceUnit.invertsUnit : persistenceUnit;\n const phenomenon = await actualPersistenceUnit?.phenomenon;\n if (phenomenon && defaultFormats[phenomenon.name.toUpperCase()]) {\n const defaultPhenomenonFormats = defaultFormats[phenomenon.name.toUpperCase()];\n for (const defaultUnitSystemFormat of Array.isArray(defaultPhenomenonFormats)\n ? /* c8 ignore next */ defaultPhenomenonFormats\n : [defaultPhenomenonFormats]) {\n if (defaultUnitSystemFormat.unitSystems.includes(unitSystem)) {\n return defaultUnitSystemFormat.format;\n }\n }\n }\n }\n\n // use persistence unit format if it matches requested unit system and matching presentation format was not found\n const persistenceUnitSystem = await persistenceUnit.unitSystem;\n if (persistenceUnitSystem && unitSystemMatchers.some((matcher) => matcher(persistenceUnitSystem))) {\n return getPersistenceUnitFormatProps(persistenceUnit);\n }\n\n // use default presentation format if persistence unit does not match requested unit system\n if (koq.defaultPresentationFormat) {\n return getFormatProps(await koq.defaultPresentationFormat);\n }\n\n return undefined;\n}\n\nasync function getKoqPresentationFormat(koq: KindOfQuantity, unitSystemMatchers: Array<(unitSystem: UnitSystem) => boolean>) {\n const presentationFormats = koq.presentationFormats;\n for (const matcher of unitSystemMatchers) {\n for (const lazyFormat of presentationFormats) {\n const format = await lazyFormat;\n const lazyUnit = format.units && format.units[0][0];\n /* c8 ignore next 3 */\n if (!lazyUnit) {\n continue;\n }\n const unit = await lazyUnit;\n const currentUnitSystem = await unit.unitSystem;\n if (currentUnitSystem && matcher(currentUnitSystem)) {\n return format;\n }\n }\n }\n return undefined;\n}\n\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\n // Same as Format \"DefaultRealU\" in Formats ecschema\n return {\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\n precision: 6,\n type: \"Decimal\",\n uomSeparator: \" \",\n decimalSeparator: \".\",\n composite: {\n units: [\n {\n name: persistenceUnit.fullName,\n label: persistenceUnit.label,\n },\n ],\n },\n };\n}\n\nfunction getUnitSystemGroupMatchers(groupKey?: UnitSystemKey) {\n function createMatcher(name: string | string[]) {\n const names = Array.isArray(name) ? name : [name];\n return (unitSystem: UnitSystem) => names.some((n) => n === unitSystem.name.toUpperCase());\n }\n switch (groupKey) {\n case \"imperial\":\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n case \"metric\":\n return [[\"SI\", \"METRIC\"], \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n case \"usCustomary\":\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n case \"usSurvey\":\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n }\n return [];\n}\n"]}
@@ -105,19 +105,21 @@ async function getKoqFormatProps(koq, persistenceUnit, defaultFormats, unitSyste
105
105
  }
106
106
  // use default presentation format if persistence unit does not match requested unit system
107
107
  if (koq.defaultPresentationFormat) {
108
- return getFormatProps(koq.defaultPresentationFormat);
108
+ return getFormatProps(await koq.defaultPresentationFormat);
109
109
  }
110
110
  return undefined;
111
111
  }
112
112
  async function getKoqPresentationFormat(koq, unitSystemMatchers) {
113
113
  const presentationFormats = koq.presentationFormats;
114
114
  for (const matcher of unitSystemMatchers) {
115
- for (const format of presentationFormats) {
116
- const unit = format.units && format.units[0][0];
115
+ for (const lazyFormat of presentationFormats) {
116
+ const format = await lazyFormat;
117
+ const lazyUnit = format.units && format.units[0][0];
117
118
  /* c8 ignore next 3 */
118
- if (!unit) {
119
+ if (!lazyUnit) {
119
120
  continue;
120
121
  }
122
+ const unit = await lazyUnit;
121
123
  const currentUnitSystem = await unit.unitSystem;
122
124
  if (currentUnitSystem && matcher(currentUnitSystem)) {
123
125
  return format;
@@ -1 +1 @@
1
- {"version":3,"file":"KoqPropertyValueFormatter.js","sourceRoot":"","sources":["../../../src/presentation-common/KoqPropertyValueFormatter.ts"],"names":[],"mappings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AAC;IACD,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QACjF,KAAK,QAAQ;YACX,OAAO,CAAC,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAC3E,KAAK,aAAa;YAChB,OAAO,CAAC,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QACrE,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IACnF,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,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 Core\n */\n\nimport { Format, FormatProps, FormatterSpec, ParserSpec, UnitsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\nimport {\n getFormatProps,\n InvertedUnit,\n KindOfQuantity,\n SchemaContext,\n SchemaKey,\n SchemaMatchType,\n SchemaUnitProvider,\n Unit,\n UnitSystem,\n} from \"@itwin/ecschema-metadata\";\n\n/**\n * A data structure that associates unit systems with property value formatting props. The associations are used for\n * assigning formatting props for specific phenomenon and unit system combinations (see [[FormatsMap]]).\n *\n * @public\n */\nexport interface UnitSystemFormat {\n unitSystems: UnitSystemKey[];\n format: FormatProps;\n}\n\n/**\n * A data structure that associates specific phenomenon with one or more formatting props for specific unit system.\n *\n * Example:\n * ```json\n * {\n * length: [{\n * unitSystems: [\"metric\"],\n * format: formatForCentimeters,\n * }, {\n * unitSystems: [\"imperial\", \"usCustomary\"],\n * format: formatForInches,\n * }, {\n * unitSystems: [\"usSurvey\"],\n * format: formatForUsSurveyInches,\n * }]\n * }\n * ```\n *\n * @public\n */\nexport interface FormatsMap {\n [phenomenon: string]: UnitSystemFormat | UnitSystemFormat[];\n}\n\n/**\n * Options for finding a formatter spec to use in [[KoqPropertyValueFormatter]].\n * @public\n */\nexport interface FormatOptions {\n /** Full name of the `KindOfQuantity`, e.g. `SchemaName:KoqName`. */\n koqName: string;\n /** Unit system to use for formatting. */\n unitSystem?: UnitSystemKey;\n}\n\n/**\n * An utility for formatting property values based on `KindOfQuantity` and unit system.\n * @public\n */\nexport class KoqPropertyValueFormatter {\n private _unitsProvider: UnitsProvider;\n private _defaultFormats?: FormatsMap;\n\n constructor(\n private _schemaContext: SchemaContext,\n defaultFormats?: FormatsMap,\n ) {\n this._unitsProvider = new SchemaUnitProvider(_schemaContext);\n this._defaultFormats = defaultFormats\n ? Object.entries(defaultFormats).reduce((acc, [phenomenon, unitSystemFormats]) => ({ ...acc, [phenomenon.toUpperCase()]: unitSystemFormats }), {})\n : /* c8 ignore next */ undefined;\n }\n\n public async format(value: number, options: FormatOptions) {\n const formatterSpec = await this.getFormatterSpec(options);\n if (!formatterSpec) {\n return undefined;\n }\n return formatterSpec.applyFormatting(value);\n }\n\n public async getFormatterSpec(options: FormatOptions) {\n const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);\n if (!formattingProps) {\n return undefined;\n }\n const { formatProps, persistenceUnitName } = formattingProps;\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\n return FormatterSpec.create(\"\", format, this._unitsProvider, persistenceUnit);\n }\n\n public async getParserSpec(options: FormatOptions) {\n const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);\n if (!formattingProps) {\n return undefined;\n }\n const { formatProps, persistenceUnitName } = formattingProps;\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\n return ParserSpec.create(format, this._unitsProvider, persistenceUnit);\n }\n}\n\ninterface FormattingProps {\n formatProps: FormatProps;\n persistenceUnitName: string;\n}\n\nasync function getFormattingProps(\n schemaLocater: SchemaContext,\n defaultFormats: FormatsMap | undefined,\n options: FormatOptions,\n): Promise<FormattingProps | undefined> {\n const { koqName, unitSystem } = options;\n\n const koq = await getKoq(schemaLocater, koqName);\n if (!koq) {\n return undefined;\n }\n\n const persistenceUnit = await koq.persistenceUnit;\n /* c8 ignore next 3 */\n if (!persistenceUnit) {\n return undefined;\n }\n\n const formatProps = await getKoqFormatProps(koq, persistenceUnit, defaultFormats, unitSystem);\n if (!formatProps) {\n return undefined;\n }\n\n return { formatProps, persistenceUnitName: persistenceUnit.fullName };\n}\n\nasync function getKoq(schemaLocater: SchemaContext, fullName: string) {\n const [schemaName, propKoqName] = fullName.split(\":\");\n const schema = await schemaLocater.getSchema(new SchemaKey(schemaName), SchemaMatchType.Latest);\n if (!schema) {\n return undefined;\n }\n return schema.getItem(propKoqName, KindOfQuantity);\n}\n\nasync function getKoqFormatProps(\n koq: KindOfQuantity,\n persistenceUnit: Unit | InvertedUnit,\n defaultFormats: FormatsMap | undefined,\n unitSystem?: UnitSystemKey,\n) {\n const unitSystemMatchers = getUnitSystemGroupMatchers(unitSystem);\n // use one of KOQ presentation format that matches requested unit system\n const presentationFormat = await getKoqPresentationFormat(koq, unitSystemMatchers);\n if (presentationFormat) {\n return getFormatProps(presentationFormat);\n }\n\n // use one of the formats in default formats map if there is one for matching phenomena and requested unit\n // system combination\n if (defaultFormats && unitSystem) {\n const actualPersistenceUnit = persistenceUnit instanceof InvertedUnit ? /* c8 ignore next */ await persistenceUnit.invertsUnit : persistenceUnit;\n const phenomenon = await actualPersistenceUnit?.phenomenon;\n if (phenomenon && defaultFormats[phenomenon.name.toUpperCase()]) {\n const defaultPhenomenonFormats = defaultFormats[phenomenon.name.toUpperCase()];\n for (const defaultUnitSystemFormat of Array.isArray(defaultPhenomenonFormats)\n ? /* c8 ignore next */ defaultPhenomenonFormats\n : [defaultPhenomenonFormats]) {\n if (defaultUnitSystemFormat.unitSystems.includes(unitSystem)) {\n return defaultUnitSystemFormat.format;\n }\n }\n }\n }\n\n // use persistence unit format if it matches requested unit system and matching presentation format was not found\n const persistenceUnitSystem = await persistenceUnit.unitSystem;\n if (persistenceUnitSystem && unitSystemMatchers.some((matcher) => matcher(persistenceUnitSystem))) {\n return getPersistenceUnitFormatProps(persistenceUnit);\n }\n\n // use default presentation format if persistence unit does not match requested unit system\n if (koq.defaultPresentationFormat) {\n return getFormatProps(koq.defaultPresentationFormat);\n }\n\n return undefined;\n}\n\nasync function getKoqPresentationFormat(koq: KindOfQuantity, unitSystemMatchers: Array<(unitSystem: UnitSystem) => boolean>) {\n const presentationFormats = koq.presentationFormats;\n for (const matcher of unitSystemMatchers) {\n for (const format of presentationFormats) {\n const unit = format.units && format.units[0][0];\n /* c8 ignore next 3 */\n if (!unit) {\n continue;\n }\n const currentUnitSystem = await unit.unitSystem;\n if (currentUnitSystem && matcher(currentUnitSystem)) {\n return format;\n }\n }\n }\n return undefined;\n}\n\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\n // Same as Format \"DefaultRealU\" in Formats ecschema\n return {\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\n precision: 6,\n type: \"Decimal\",\n uomSeparator: \" \",\n decimalSeparator: \".\",\n composite: {\n units: [\n {\n name: persistenceUnit.fullName,\n label: persistenceUnit.label,\n },\n ],\n },\n };\n}\n\nfunction getUnitSystemGroupMatchers(groupKey?: UnitSystemKey) {\n function createMatcher(name: string | string[]) {\n const names = Array.isArray(name) ? name : [name];\n return (unitSystem: UnitSystem) => names.some((n) => n === unitSystem.name.toUpperCase());\n }\n switch (groupKey) {\n case \"imperial\":\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n case \"metric\":\n return [[\"SI\", \"METRIC\"], \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n case \"usCustomary\":\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n case \"usSurvey\":\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n }\n return [];\n}\n"]}
1
+ {"version":3,"file":"KoqPropertyValueFormatter.js","sourceRoot":"","sources":["../../../src/presentation-common/KoqPropertyValueFormatter.ts"],"names":[],"mappings":"AAAA;;;gGAGgG;AAChG;;GAEG;AAEH,OAAO,EAAE,MAAM,EAAe,aAAa,EAAE,UAAU,EAAgC,MAAM,sBAAsB,CAAC;AACpH,OAAO,EACL,cAAc,EACd,YAAY,EACZ,cAAc,EAEd,SAAS,EACT,eAAe,EACf,kBAAkB,GAGnB,MAAM,0BAA0B,CAAC;AAiDlC;;;GAGG;AACH,MAAM,OAAO,yBAAyB;IAK1B;IAJF,cAAc,CAAgB;IAC9B,eAAe,CAAc;IAErC,YACU,cAA6B,EACrC,cAA2B;QADnB,mBAAc,GAAd,cAAc,CAAe;QAGrC,IAAI,CAAC,cAAc,GAAG,IAAI,kBAAkB,CAAC,cAAc,CAAC,CAAC;QAC7D,IAAI,CAAC,eAAe,GAAG,cAAc;YACnC,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,cAAc,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,UAAU,EAAE,iBAAiB,CAAC,EAAE,EAAE,CAAC,CAAC,EAAE,GAAG,GAAG,EAAE,CAAC,UAAU,CAAC,WAAW,EAAE,CAAC,EAAE,iBAAiB,EAAE,CAAC,EAAE,EAAE,CAAC;YAClJ,CAAC,CAAC,oBAAoB,CAAC,SAAS,CAAC;IACrC,CAAC;IAEM,KAAK,CAAC,MAAM,CAAC,KAAa,EAAE,OAAsB;QACvD,MAAM,aAAa,GAAG,MAAM,IAAI,CA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eAAe,CAAC,QAAQ;oBAC9B,KAAK,EAAE,eAAe,CAAC,KAAK;iBAC7B;aACF;SACF;KACF,CAAC;AACJ,CAAC;AAED,SAAS,0BAA0B,CAAC,QAAwB;IAC1D,SAAS,aAAa,CAAC,IAAuB;QAC5C,MAAM,KAAK,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC;QAClD,OAAO,CAAC,UAAsB,EAAE,EAAE,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,KAAK,UAAU,CAAC,IAAI,CAAC,WAAW,EAAE,CAAC,CAAC;IAC5F,CAAC;IACD,QAAQ,QAAQ,EAAE,CAAC;QACjB,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QACjF,KAAK,QAAQ;YACX,OAAO,CAAC,CAAC,IAAI,EAAE,QAAQ,CAAC,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QAC3E,KAAK,aAAa;YAChB,OAAO,CAAC,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;QACrE,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC,GAAG,CAAC,aAAa,CAAC,CAAC;IACnF,CAAC;IACD,OAAO,EAAE,CAAC;AACZ,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 Core\n */\n\nimport { Format, FormatProps, FormatterSpec, ParserSpec, UnitsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\nimport {\n getFormatProps,\n InvertedUnit,\n KindOfQuantity,\n SchemaContext,\n SchemaKey,\n SchemaMatchType,\n SchemaUnitProvider,\n Unit,\n UnitSystem,\n} from \"@itwin/ecschema-metadata\";\n\n/**\n * A data structure that associates unit systems with property value formatting props. The associations are used for\n * assigning formatting props for specific phenomenon and unit system combinations (see [[FormatsMap]]).\n *\n * @public\n */\nexport interface UnitSystemFormat {\n unitSystems: UnitSystemKey[];\n format: FormatProps;\n}\n\n/**\n * A data structure that associates specific phenomenon with one or more formatting props for specific unit system.\n *\n * Example:\n * ```json\n * {\n * length: [{\n * unitSystems: [\"metric\"],\n * format: formatForCentimeters,\n * }, {\n * unitSystems: [\"imperial\", \"usCustomary\"],\n * format: formatForInches,\n * }, {\n * unitSystems: [\"usSurvey\"],\n * format: formatForUsSurveyInches,\n * }]\n * }\n * ```\n *\n * @public\n */\nexport interface FormatsMap {\n [phenomenon: string]: UnitSystemFormat | UnitSystemFormat[];\n}\n\n/**\n * Options for finding a formatter spec to use in [[KoqPropertyValueFormatter]].\n * @public\n */\nexport interface FormatOptions {\n /** Full name of the `KindOfQuantity`, e.g. `SchemaName:KoqName`. */\n koqName: string;\n /** Unit system to use for formatting. */\n unitSystem?: UnitSystemKey;\n}\n\n/**\n * An utility for formatting property values based on `KindOfQuantity` and unit system.\n * @public\n */\nexport class KoqPropertyValueFormatter {\n private _unitsProvider: UnitsProvider;\n private _defaultFormats?: FormatsMap;\n\n constructor(\n private _schemaContext: SchemaContext,\n defaultFormats?: FormatsMap,\n ) {\n this._unitsProvider = new SchemaUnitProvider(_schemaContext);\n this._defaultFormats = defaultFormats\n ? Object.entries(defaultFormats).reduce((acc, [phenomenon, unitSystemFormats]) => ({ ...acc, [phenomenon.toUpperCase()]: unitSystemFormats }), {})\n : /* c8 ignore next */ undefined;\n }\n\n public async format(value: number, options: FormatOptions) {\n const formatterSpec = await this.getFormatterSpec(options);\n if (!formatterSpec) {\n return undefined;\n }\n return formatterSpec.applyFormatting(value);\n }\n\n public async getFormatterSpec(options: FormatOptions) {\n const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);\n if (!formattingProps) {\n return undefined;\n }\n const { formatProps, persistenceUnitName } = formattingProps;\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\n return FormatterSpec.create(\"\", format, this._unitsProvider, persistenceUnit);\n }\n\n public async getParserSpec(options: FormatOptions) {\n const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);\n if (!formattingProps) {\n return undefined;\n }\n const { formatProps, persistenceUnitName } = formattingProps;\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\n return ParserSpec.create(format, this._unitsProvider, persistenceUnit);\n }\n}\n\ninterface FormattingProps {\n formatProps: FormatProps;\n persistenceUnitName: string;\n}\n\nasync function getFormattingProps(\n schemaLocater: SchemaContext,\n defaultFormats: FormatsMap | undefined,\n options: FormatOptions,\n): Promise<FormattingProps | undefined> {\n const { koqName, unitSystem } = options;\n\n const koq = await getKoq(schemaLocater, koqName);\n if (!koq) {\n return undefined;\n }\n\n const persistenceUnit = await koq.persistenceUnit;\n /* c8 ignore next 3 */\n if (!persistenceUnit) {\n return undefined;\n }\n\n const formatProps = await getKoqFormatProps(koq, persistenceUnit, defaultFormats, unitSystem);\n if (!formatProps) {\n return undefined;\n }\n\n return { formatProps, persistenceUnitName: persistenceUnit.fullName };\n}\n\nasync function getKoq(schemaLocater: SchemaContext, fullName: string) {\n const [schemaName, propKoqName] = fullName.split(\":\");\n const schema = await schemaLocater.getSchema(new SchemaKey(schemaName), SchemaMatchType.Latest);\n if (!schema) {\n return undefined;\n }\n return schema.getItem(propKoqName, KindOfQuantity);\n}\n\nasync function getKoqFormatProps(\n koq: KindOfQuantity,\n persistenceUnit: Unit | InvertedUnit,\n defaultFormats: FormatsMap | undefined,\n unitSystem?: UnitSystemKey,\n) {\n const unitSystemMatchers = getUnitSystemGroupMatchers(unitSystem);\n // use one of KOQ presentation format that matches requested unit system\n const presentationFormat = await getKoqPresentationFormat(koq, unitSystemMatchers);\n if (presentationFormat) {\n return getFormatProps(presentationFormat);\n }\n\n // use one of the formats in default formats map if there is one for matching phenomena and requested unit\n // system combination\n if (defaultFormats && unitSystem) {\n const actualPersistenceUnit = persistenceUnit instanceof InvertedUnit ? /* c8 ignore next */ await persistenceUnit.invertsUnit : persistenceUnit;\n const phenomenon = await actualPersistenceUnit?.phenomenon;\n if (phenomenon && defaultFormats[phenomenon.name.toUpperCase()]) {\n const defaultPhenomenonFormats = defaultFormats[phenomenon.name.toUpperCase()];\n for (const defaultUnitSystemFormat of Array.isArray(defaultPhenomenonFormats)\n ? /* c8 ignore next */ defaultPhenomenonFormats\n : [defaultPhenomenonFormats]) {\n if (defaultUnitSystemFormat.unitSystems.includes(unitSystem)) {\n return defaultUnitSystemFormat.format;\n }\n }\n }\n }\n\n // use persistence unit format if it matches requested unit system and matching presentation format was not found\n const persistenceUnitSystem = await persistenceUnit.unitSystem;\n if (persistenceUnitSystem && unitSystemMatchers.some((matcher) => matcher(persistenceUnitSystem))) {\n return getPersistenceUnitFormatProps(persistenceUnit);\n }\n\n // use default presentation format if persistence unit does not match requested unit system\n if (koq.defaultPresentationFormat) {\n return getFormatProps(await koq.defaultPresentationFormat);\n }\n\n return undefined;\n}\n\nasync function getKoqPresentationFormat(koq: KindOfQuantity, unitSystemMatchers: Array<(unitSystem: UnitSystem) => boolean>) {\n const presentationFormats = koq.presentationFormats;\n for (const matcher of unitSystemMatchers) {\n for (const lazyFormat of presentationFormats) {\n const format = await lazyFormat;\n const lazyUnit = format.units && format.units[0][0];\n /* c8 ignore next 3 */\n if (!lazyUnit) {\n continue;\n }\n const unit = await lazyUnit;\n const currentUnitSystem = await unit.unitSystem;\n if (currentUnitSystem && matcher(currentUnitSystem)) {\n return format;\n }\n }\n }\n return undefined;\n}\n\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\n // Same as Format \"DefaultRealU\" in Formats ecschema\n return {\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\n precision: 6,\n type: \"Decimal\",\n uomSeparator: \" \",\n decimalSeparator: \".\",\n composite: {\n units: [\n {\n name: persistenceUnit.fullName,\n label: persistenceUnit.label,\n },\n ],\n },\n };\n}\n\nfunction getUnitSystemGroupMatchers(groupKey?: UnitSystemKey) {\n function createMatcher(name: string | string[]) {\n const names = Array.isArray(name) ? name : [name];\n return (unitSystem: UnitSystem) => names.some((n) => n === unitSystem.name.toUpperCase());\n }\n switch (groupKey) {\n case \"imperial\":\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n case \"metric\":\n return [[\"SI\", \"METRIC\"], \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n case \"usCustomary\":\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n case \"usSurvey\":\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"].map(createMatcher);\n }\n return [];\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itwin/presentation-common",
3
- "version": "5.0.0-dev.99",
3
+ "version": "5.1.0-dev.1",
4
4
  "description": "Common pieces for iModel.js presentation packages",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -42,10 +42,10 @@
42
42
  "./package.json": "./package.json"
43
43
  },
44
44
  "peerDependencies": {
45
- "@itwin/core-bentley": "5.0.0-dev.99",
46
- "@itwin/core-quantity": "5.0.0-dev.99",
47
- "@itwin/ecschema-metadata": "5.0.0-dev.99",
48
- "@itwin/core-common": "5.0.0-dev.99"
45
+ "@itwin/ecschema-metadata": "5.1.0-dev.1",
46
+ "@itwin/core-quantity": "5.1.0-dev.1",
47
+ "@itwin/core-bentley": "5.1.0-dev.1",
48
+ "@itwin/core-common": "5.1.0-dev.1"
49
49
  },
50
50
  "devDependencies": {
51
51
  "@itwin/eslint-plugin": "5.0.0-dev.1",
@@ -77,12 +77,12 @@
77
77
  "typescript": "~5.6.2",
78
78
  "typescript-json-schema": "^0.55.0",
79
79
  "yargs": "^17.4.0",
80
- "@itwin/build-tools": "5.0.0-dev.99",
81
- "@itwin/ecschema-metadata": "5.0.0-dev.99",
82
- "@itwin/core-bentley": "5.0.0-dev.99",
83
- "@itwin/core-common": "5.0.0-dev.99",
80
+ "@itwin/build-tools": "5.1.0-dev.1",
81
+ "@itwin/core-quantity": "5.1.0-dev.1",
82
+ "@itwin/core-common": "5.1.0-dev.1",
84
83
  "internal-tools": "3.0.0-dev.69",
85
- "@itwin/core-quantity": "5.0.0-dev.99"
84
+ "@itwin/ecschema-metadata": "5.1.0-dev.1",
85
+ "@itwin/core-bentley": "5.1.0-dev.1"
86
86
  },
87
87
  "scripts": {
88
88
  "build": "npm run -s ruleset-json-schema && npm run -s build:assets && npm run -s build:public && npm run -s build:cjs && npm run -s build:esm",