@itwin/presentation-common 4.3.0-dev.29 → 4.3.0-dev.31

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.
@@ -1,8 +1,42 @@
1
1
  /** @packageDocumentation
2
2
  * @module Core
3
3
  */
4
- import { FormatterSpec, ParserSpec, UnitSystemKey } from "@itwin/core-quantity";
4
+ import { FormatProps, FormatterSpec, ParserSpec, UnitSystemKey } from "@itwin/core-quantity";
5
5
  import { SchemaContext } from "@itwin/ecschema-metadata";
6
+ /**
7
+ * A data structure that associates unit systems with property value formatting props. The associations are used for
8
+ * assigning formatting props for specific phenomenon and unit system combinations (see [[FormatsMap]]).
9
+ *
10
+ * @public
11
+ */
12
+ export interface UnitSystemFormat {
13
+ unitSystems: UnitSystemKey[];
14
+ format: FormatProps;
15
+ }
16
+ /**
17
+ * A data structure that associates specific phenomenon with one or more formatting props for specific unit system.
18
+ *
19
+ * Example:
20
+ * ```json
21
+ * {
22
+ * length: [{
23
+ * unitSystems: ["metric"],
24
+ * format: formatForCentimeters,
25
+ * }, {
26
+ * unitSystems: ["imperial", "usCustomary"],
27
+ * format: formatForInches,
28
+ * }, {
29
+ * unitSystems: ["usSurvey"],
30
+ * format: formatForUsSurveyInches,
31
+ * }]
32
+ * }
33
+ * ```
34
+ *
35
+ * @public
36
+ */
37
+ export interface FormatsMap {
38
+ [phenomenon: string]: UnitSystemFormat | UnitSystemFormat[];
39
+ }
6
40
  /** @alpha */
7
41
  export interface FormatOptions {
8
42
  koqName: string;
@@ -12,7 +46,8 @@ export interface FormatOptions {
12
46
  export declare class KoqPropertyValueFormatter {
13
47
  private _schemaContext;
14
48
  private _unitsProvider;
15
- constructor(_schemaContext: SchemaContext);
49
+ private _defaultFormats?;
50
+ constructor(_schemaContext: SchemaContext, defaultFormats?: FormatsMap);
16
51
  format(value: number, options: FormatOptions): Promise<string | undefined>;
17
52
  getFormatterSpec(options: FormatOptions): Promise<FormatterSpec | undefined>;
18
53
  getParserSpec(options: FormatOptions): Promise<ParserSpec | undefined>;
@@ -1 +1 @@
1
- {"version":3,"file":"KoqPropertyValueFormatter.d.ts","sourceRoot":"","sources":["../../../src/presentation-common/KoqPropertyValueFormatter.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH,OAAO,EAAuB,aAAa,EAAE,UAAU,EAAiB,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACpH,OAAO,EACyC,aAAa,EAE5D,MAAM,0BAA0B,CAAC;AAElC,aAAa;AACb,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,aAAa,CAAC;CAC5B;AAED,aAAa;AACb,qBAAa,yBAAyB;IAGxB,OAAO,CAAC,cAAc;IAFlC,OAAO,CAAC,cAAc,CAAgB;gBAElB,cAAc,EAAE,aAAa;IAIpC,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa;IAO5C,gBAAgB,CAAC,OAAO,EAAE,aAAa;IAUvC,aAAa,CAAC,OAAO,EAAE,aAAa;CASlD"}
1
+ {"version":3,"file":"KoqPropertyValueFormatter.d.ts","sourceRoot":"","sources":["../../../src/presentation-common/KoqPropertyValueFormatter.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH,OAAO,EAAU,WAAW,EAAE,aAAa,EAAE,UAAU,EAAiB,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACpH,OAAO,EAAgD,aAAa,EAAwD,MAAM,0BAA0B,CAAC;AAE7J;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,aAAa,EAAE,CAAC;IAC7B,MAAM,EAAE,WAAW,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,WAAW,UAAU;IACzB,CAAC,UAAU,EAAE,MAAM,GAAG,gBAAgB,GAAG,gBAAgB,EAAE,CAAC;CAC7D;AAED,aAAa;AACb,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,aAAa,CAAC;CAC5B;AAED,aAAa;AACb,qBAAa,yBAAyB;IAIxB,OAAO,CAAC,cAAc;IAHlC,OAAO,CAAC,cAAc,CAAgB;IACtC,OAAO,CAAC,eAAe,CAAC,CAAa;gBAEjB,cAAc,EAAE,aAAa,EAAE,cAAc,CAAC,EAAE,UAAU;IAOjE,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa;IAQ5C,gBAAgB,CAAC,OAAO,EAAE,aAAa;IAWvC,aAAa,CAAC,OAAO,EAAE,aAAa;CAUlD"}
@@ -1,8 +1,8 @@
1
1
  "use strict";
2
2
  /*---------------------------------------------------------------------------------------------
3
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
- * See LICENSE.md in the project root for license terms and full copyright notice.
5
- *--------------------------------------------------------------------------------------------*/
3
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
4
+ * See LICENSE.md in the project root for license terms and full copyright notice.
5
+ *--------------------------------------------------------------------------------------------*/
6
6
  /** @packageDocumentation
7
7
  * @module Core
8
8
  */
@@ -12,29 +12,35 @@ const core_quantity_1 = require("@itwin/core-quantity");
12
12
  const ecschema_metadata_1 = require("@itwin/ecschema-metadata");
13
13
  /** @alpha */
14
14
  class KoqPropertyValueFormatter {
15
- constructor(_schemaContext) {
15
+ constructor(_schemaContext, defaultFormats) {
16
16
  this._schemaContext = _schemaContext;
17
17
  this._unitsProvider = new ecschema_metadata_1.SchemaUnitProvider(_schemaContext);
18
+ this._defaultFormats = defaultFormats
19
+ ? Object.entries(defaultFormats).reduce((acc, [phenomenon, unitSystemFormats]) => ({ ...acc, [phenomenon.toUpperCase()]: unitSystemFormats }), {})
20
+ : /* istanbul ignore next */ undefined;
18
21
  }
19
22
  async format(value, options) {
20
23
  const formatterSpec = await this.getFormatterSpec(options);
21
- if (!formatterSpec)
24
+ if (!formatterSpec) {
22
25
  return undefined;
26
+ }
23
27
  return formatterSpec.applyFormatting(value);
24
28
  }
25
29
  async getFormatterSpec(options) {
26
- const formattingProps = await getFormattingProps(this._schemaContext, options);
27
- if (!formattingProps)
30
+ const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);
31
+ if (!formattingProps) {
28
32
  return undefined;
33
+ }
29
34
  const { formatProps, persistenceUnitName } = formattingProps;
30
35
  const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);
31
36
  const format = await core_quantity_1.Format.createFromJSON("", this._unitsProvider, formatProps);
32
37
  return core_quantity_1.FormatterSpec.create("", format, this._unitsProvider, persistenceUnit);
33
38
  }
34
39
  async getParserSpec(options) {
35
- const formattingProps = await getFormattingProps(this._schemaContext, options);
36
- if (!formattingProps)
40
+ const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);
41
+ if (!formattingProps) {
37
42
  return undefined;
43
+ }
38
44
  const { formatProps, persistenceUnitName } = formattingProps;
39
45
  const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);
40
46
  const format = await core_quantity_1.Format.createFromJSON("", this._unitsProvider, formatProps);
@@ -42,40 +48,62 @@ class KoqPropertyValueFormatter {
42
48
  }
43
49
  }
44
50
  exports.KoqPropertyValueFormatter = KoqPropertyValueFormatter;
45
- async function getFormattingProps(schemaLocater, options) {
51
+ async function getFormattingProps(schemaLocater, defaultFormats, options) {
46
52
  const { koqName, unitSystem } = options;
47
53
  const koq = await getKoq(schemaLocater, koqName);
48
- if (!koq)
54
+ if (!koq) {
49
55
  return undefined;
56
+ }
50
57
  const persistenceUnit = await koq.persistenceUnit;
51
58
  // istanbul ignore if
52
- if (!persistenceUnit)
59
+ if (!persistenceUnit) {
53
60
  return undefined;
54
- const formatProps = await getKoqFormatProps(koq, persistenceUnit, unitSystem);
55
- if (!formatProps)
61
+ }
62
+ const formatProps = await getKoqFormatProps(koq, persistenceUnit, defaultFormats, unitSystem);
63
+ if (!formatProps) {
56
64
  return undefined;
65
+ }
57
66
  return { formatProps, persistenceUnitName: persistenceUnit.fullName };
58
67
  }
59
68
  async function getKoq(schemaLocater, fullName) {
60
69
  const [schemaName, propKoqName] = fullName.split(":");
61
70
  const schema = await schemaLocater.getSchema(new ecschema_metadata_1.SchemaKey(schemaName), ecschema_metadata_1.SchemaMatchType.Latest);
62
- if (!schema)
71
+ if (!schema) {
63
72
  return undefined;
73
+ }
64
74
  return schema.getItem(propKoqName);
65
75
  }
66
- async function getKoqFormatProps(koq, persistenceUnit, unitSystem) {
76
+ async function getKoqFormatProps(koq, persistenceUnit, defaultFormats, unitSystem) {
67
77
  const unitSystems = getUnitSystemGroupNames(unitSystem);
68
78
  // use one of KOQ presentation format that matches requested unit system
69
79
  const presentationFormat = await getKoqPresentationFormat(koq, unitSystems);
70
- if (presentationFormat)
80
+ if (presentationFormat) {
71
81
  return (0, ecschema_metadata_1.getFormatProps)(presentationFormat);
82
+ }
83
+ // use one of the formats in default formats map if there is one for matching phenomena and requested unit
84
+ // system combination
85
+ if (defaultFormats && unitSystem) {
86
+ const actualPersistenceUnit = persistenceUnit instanceof ecschema_metadata_1.InvertedUnit ? /* istanbul ignore next */ await persistenceUnit.invertsUnit : persistenceUnit;
87
+ const phenomenon = await actualPersistenceUnit?.phenomenon;
88
+ // istanbul ignore else
89
+ if (phenomenon && defaultFormats[phenomenon.name.toUpperCase()]) {
90
+ const defaultPhenomenonFormats = defaultFormats[phenomenon.name.toUpperCase()];
91
+ for (const defaultUnitSystemFormat of Array.isArray(defaultPhenomenonFormats) ? /* istanbul ignore next */ defaultPhenomenonFormats : [defaultPhenomenonFormats]) {
92
+ if (defaultUnitSystemFormat.unitSystems.includes(unitSystem)) {
93
+ return defaultUnitSystemFormat.format;
94
+ }
95
+ }
96
+ }
97
+ }
72
98
  // use persistence unit format if it matches requested unit system and matching presentation format was not found
73
99
  const persistenceUnitSystem = await persistenceUnit.unitSystem;
74
- if (persistenceUnitSystem && unitSystems.includes(persistenceUnitSystem.name.toUpperCase()))
100
+ if (persistenceUnitSystem && unitSystems.includes(persistenceUnitSystem.name.toUpperCase())) {
75
101
  return getPersistenceUnitFormatProps(persistenceUnit);
102
+ }
76
103
  // use default presentation format if persistence unit does not match requested unit system
77
- if (koq.defaultPresentationFormat)
104
+ if (koq.defaultPresentationFormat) {
78
105
  return (0, ecschema_metadata_1.getFormatProps)(koq.defaultPresentationFormat);
106
+ }
79
107
  return undefined;
80
108
  }
81
109
  async function getKoqPresentationFormat(koq, unitSystems) {
@@ -84,11 +112,13 @@ async function getKoqPresentationFormat(koq, unitSystems) {
84
112
  for (const format of presentationFormats) {
85
113
  const unit = format.units && format.units[0][0];
86
114
  // istanbul ignore if
87
- if (!unit)
115
+ if (!unit) {
88
116
  continue;
117
+ }
89
118
  const currentUnitSystem = await unit.unitSystem;
90
- if (currentUnitSystem && currentUnitSystem.name.toUpperCase() === system)
119
+ if (currentUnitSystem && currentUnitSystem.name.toUpperCase() === system) {
91
120
  return format;
121
+ }
92
122
  }
93
123
  }
94
124
  return undefined;
@@ -102,10 +132,12 @@ function getPersistenceUnitFormatProps(persistenceUnit) {
102
132
  uomSeparator: " ",
103
133
  decimalSeparator: ".",
104
134
  composite: {
105
- units: [{
135
+ units: [
136
+ {
106
137
  name: persistenceUnit.fullName,
107
138
  label: persistenceUnit.label,
108
- }],
139
+ },
140
+ ],
109
141
  },
110
142
  };
111
143
  }
@@ -1 +1 @@
1
- {"version":3,"file":"KoqPropertyValueFormatter.js","sourceRoot":"","sources":["../../../src/presentation-common/KoqPropertyValueFormatter.ts"],"names":[],"mappings":";AAAA;;;+FAG+F;AAC/F;;GAEG;;;AAEH,wDAAoH;AACpH,gEAGkC;AAQlC,aAAa;AACb,MAAa,yBAAyB;IAGpC,YAAoB,cAA6B;QAA7B,mBAAc,GAAd,cAAc,CAAe;QAC/C,IAAI,CAAC,cAAc,GAAG,IAAI,sCAAkB,CAAC,cAAc,CAAC,CAAC;IAC/D,CAAC;IAEM,KAAK,CAAC,MAAM,CAAC,KAAa,EAAE,OAAsB;QAC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YACT,KAAK,EAAE,CAAC;oBACN,IAAI,EAAE,eAAe,CAAC,QAAQ;oBAC9B,KAAK,EAAE,eAAe,CAAC,KAAK;iBAC7B,CAAC;SACH;KACF,CAAC;AACJ,CAAC;AAED,SAAS,uBAAuB,CAAC,UAA0B;IACzD,QAAQ,UAAU,EAAE;QAClB,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;QAC9D,KAAK,QAAQ;YACX,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;QACtD,KAAK,aAAa;YAChB,OAAO,CAAC,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;QAClD,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;KAC/D;IACD,OAAO,EAAE,CAAC;AACZ,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 Core\r\n */\r\n\r\nimport { Format, FormatProps, FormatterSpec, ParserSpec, UnitsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\r\nimport {\r\n getFormatProps, InvertedUnit, KindOfQuantity, SchemaContext, SchemaKey, SchemaMatchType, SchemaUnitProvider,\r\n Unit,\r\n} from \"@itwin/ecschema-metadata\";\r\n\r\n/** @alpha */\r\nexport interface FormatOptions {\r\n koqName: string;\r\n unitSystem?: UnitSystemKey;\r\n}\r\n\r\n/** @alpha */\r\nexport class KoqPropertyValueFormatter {\r\n private _unitsProvider: UnitsProvider;\r\n\r\n constructor(private _schemaContext: SchemaContext) {\r\n this._unitsProvider = new SchemaUnitProvider(_schemaContext);\r\n }\r\n\r\n public async format(value: number, options: FormatOptions) {\r\n const formatterSpec = await this.getFormatterSpec(options);\r\n if (!formatterSpec)\r\n return undefined;\r\n return formatterSpec.applyFormatting(value);\r\n }\r\n\r\n public async getFormatterSpec(options: FormatOptions) {\r\n const formattingProps = await getFormattingProps(this._schemaContext, options);\r\n if (!formattingProps)\r\n return undefined;\r\n const { formatProps, persistenceUnitName } = formattingProps;\r\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\r\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\r\n return FormatterSpec.create(\"\", format, this._unitsProvider, persistenceUnit);\r\n }\r\n\r\n public async getParserSpec(options: FormatOptions) {\r\n const formattingProps = await getFormattingProps(this._schemaContext, options);\r\n if (!formattingProps)\r\n return undefined;\r\n const { formatProps, persistenceUnitName } = formattingProps;\r\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\r\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\r\n return ParserSpec.create(format, this._unitsProvider, persistenceUnit);\r\n }\r\n}\r\n\r\ninterface FormattingProps {\r\n formatProps: FormatProps;\r\n persistenceUnitName: string;\r\n}\r\n\r\nasync function getFormattingProps(schemaLocater: SchemaContext, options: FormatOptions): Promise<FormattingProps | undefined> {\r\n const { koqName, unitSystem } = options;\r\n\r\n const koq = await getKoq(schemaLocater, koqName);\r\n if (!koq)\r\n return undefined;\r\n\r\n const persistenceUnit = await koq.persistenceUnit;\r\n // istanbul ignore if\r\n if (!persistenceUnit)\r\n return undefined;\r\n\r\n const formatProps = await getKoqFormatProps(koq, persistenceUnit, unitSystem);\r\n if (!formatProps)\r\n return undefined;\r\n\r\n return { formatProps, persistenceUnitName: persistenceUnit.fullName };\r\n}\r\n\r\nasync function getKoq(schemaLocater: SchemaContext, fullName: string) {\r\n const [schemaName, propKoqName] = fullName.split(\":\");\r\n const schema = await schemaLocater.getSchema(new SchemaKey(schemaName), SchemaMatchType.Latest);\r\n if (!schema)\r\n return undefined;\r\n\r\n return schema.getItem<KindOfQuantity>(propKoqName);\r\n}\r\n\r\nasync function getKoqFormatProps(koq: KindOfQuantity, persistenceUnit: Unit | InvertedUnit, unitSystem?: UnitSystemKey) {\r\n const unitSystems = getUnitSystemGroupNames(unitSystem);\r\n // use one of KOQ presentation format that matches requested unit system\r\n const presentationFormat = await getKoqPresentationFormat(koq, unitSystems);\r\n if (presentationFormat)\r\n return getFormatProps(presentationFormat);\r\n\r\n // use persistence unit format if it matches requested unit system and matching presentation format was not found\r\n const persistenceUnitSystem = await persistenceUnit.unitSystem;\r\n if (persistenceUnitSystem && unitSystems.includes(persistenceUnitSystem.name.toUpperCase()))\r\n return getPersistenceUnitFormatProps(persistenceUnit);\r\n\r\n // use default presentation format if persistence unit does not match requested unit system\r\n if (koq.defaultPresentationFormat)\r\n return getFormatProps(koq.defaultPresentationFormat);\r\n\r\n return undefined;\r\n}\r\n\r\nasync function getKoqPresentationFormat(koq: KindOfQuantity, unitSystems: string[]) {\r\n const presentationFormats = koq.presentationFormats;\r\n for (const system of unitSystems) {\r\n for (const format of presentationFormats) {\r\n const unit = format.units && format.units[0][0];\r\n // istanbul ignore if\r\n if (!unit)\r\n continue;\r\n\r\n const currentUnitSystem = await unit.unitSystem;\r\n if (currentUnitSystem && currentUnitSystem.name.toUpperCase() === system)\r\n return format;\r\n }\r\n }\r\n return undefined;\r\n}\r\n\r\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\r\n // Same as Format \"DefaultRealU\" in Formats ecschema\r\n return {\r\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\r\n precision: 6,\r\n type: \"Decimal\",\r\n uomSeparator: \" \",\r\n decimalSeparator: \".\",\r\n composite: {\r\n units: [{\r\n name: persistenceUnit.fullName,\r\n label: persistenceUnit.label,\r\n }],\r\n },\r\n };\r\n}\r\n\r\nfunction getUnitSystemGroupNames(unitSystem?: UnitSystemKey) {\r\n switch (unitSystem) {\r\n case \"imperial\":\r\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\r\n case \"metric\":\r\n return [\"SI\", \"METRIC\", \"INTERNATIONAL\", \"FINANCE\"];\r\n case \"usCustomary\":\r\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\r\n case \"usSurvey\":\r\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\r\n }\r\n return [];\r\n}\r\n"]}
1
+ {"version":3,"file":"KoqPropertyValueFormatter.js","sourceRoot":"","sources":["../../../src/presentation-common/KoqPropertyValueFormatter.ts"],"names":[],"mappings":";AAAA;;;gGAGgG;AAChG;;GAEG;;;AAEH,wDAAoH;AACpH,gEAA6J;AA4C7J,aAAa;AACb,MAAa,yBAAyB;IAIpC,YAAoB,cAA6B,EAAE,cAA2B;QAA1D,mBAAc,GAAd,cAAc,CAAe;QAC/C,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,0BAA0B,CAAC,SAAS,CAAC;IAC3C,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;YAClB,OAAO,SAAS,CAAC;SAClB;QACD,OAAO,aAAa,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;IAC9C,CAAC;IAEM,KAAK,CAAC,gBAAgB,CAAC,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AAe,EAAE,SAAS,CAAC,CAAC;KAC/D;IACD,OAAO,EAAE,CAAC;AACZ,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 Core\r\n */\r\n\r\nimport { Format, FormatProps, FormatterSpec, ParserSpec, UnitsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\r\nimport { getFormatProps, InvertedUnit, KindOfQuantity, SchemaContext, SchemaKey, SchemaMatchType, SchemaUnitProvider, Unit } from \"@itwin/ecschema-metadata\";\r\n\r\n/**\r\n * A data structure that associates unit systems with property value formatting props. The associations are used for\r\n * assigning formatting props for specific phenomenon and unit system combinations (see [[FormatsMap]]).\r\n *\r\n * @public\r\n */\r\nexport interface UnitSystemFormat {\r\n unitSystems: UnitSystemKey[];\r\n format: FormatProps;\r\n}\r\n\r\n/**\r\n * A data structure that associates specific phenomenon with one or more formatting props for specific unit system.\r\n *\r\n * Example:\r\n * ```json\r\n * {\r\n * length: [{\r\n * unitSystems: [\"metric\"],\r\n * format: formatForCentimeters,\r\n * }, {\r\n * unitSystems: [\"imperial\", \"usCustomary\"],\r\n * format: formatForInches,\r\n * }, {\r\n * unitSystems: [\"usSurvey\"],\r\n * format: formatForUsSurveyInches,\r\n * }]\r\n * }\r\n * ```\r\n *\r\n * @public\r\n */\r\nexport interface FormatsMap {\r\n [phenomenon: string]: UnitSystemFormat | UnitSystemFormat[];\r\n}\r\n\r\n/** @alpha */\r\nexport interface FormatOptions {\r\n koqName: string;\r\n unitSystem?: UnitSystemKey;\r\n}\r\n\r\n/** @alpha */\r\nexport class KoqPropertyValueFormatter {\r\n private _unitsProvider: UnitsProvider;\r\n private _defaultFormats?: FormatsMap;\r\n\r\n constructor(private _schemaContext: SchemaContext, defaultFormats?: FormatsMap) {\r\n this._unitsProvider = new SchemaUnitProvider(_schemaContext);\r\n this._defaultFormats = defaultFormats\r\n ? Object.entries(defaultFormats).reduce((acc, [phenomenon, unitSystemFormats]) => ({ ...acc, [phenomenon.toUpperCase()]: unitSystemFormats }), {})\r\n : /* istanbul ignore next */ undefined;\r\n }\r\n\r\n public async format(value: number, options: FormatOptions) {\r\n const formatterSpec = await this.getFormatterSpec(options);\r\n if (!formatterSpec) {\r\n return undefined;\r\n }\r\n return formatterSpec.applyFormatting(value);\r\n }\r\n\r\n public async getFormatterSpec(options: FormatOptions) {\r\n const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);\r\n if (!formattingProps) {\r\n return undefined;\r\n }\r\n const { formatProps, persistenceUnitName } = formattingProps;\r\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\r\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\r\n return FormatterSpec.create(\"\", format, this._unitsProvider, persistenceUnit);\r\n }\r\n\r\n public async getParserSpec(options: FormatOptions) {\r\n const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);\r\n if (!formattingProps) {\r\n return undefined;\r\n }\r\n const { formatProps, persistenceUnitName } = formattingProps;\r\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\r\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\r\n return ParserSpec.create(format, this._unitsProvider, persistenceUnit);\r\n }\r\n}\r\n\r\ninterface FormattingProps {\r\n formatProps: FormatProps;\r\n persistenceUnitName: string;\r\n}\r\n\r\nasync function getFormattingProps(\r\n schemaLocater: SchemaContext,\r\n defaultFormats: FormatsMap | undefined,\r\n options: FormatOptions,\r\n): Promise<FormattingProps | undefined> {\r\n const { koqName, unitSystem } = options;\r\n\r\n const koq = await getKoq(schemaLocater, koqName);\r\n if (!koq) {\r\n return undefined;\r\n }\r\n\r\n const persistenceUnit = await koq.persistenceUnit;\r\n // istanbul ignore if\r\n if (!persistenceUnit) {\r\n return undefined;\r\n }\r\n\r\n const formatProps = await getKoqFormatProps(koq, persistenceUnit, defaultFormats, unitSystem);\r\n if (!formatProps) {\r\n return undefined;\r\n }\r\n\r\n return { formatProps, persistenceUnitName: persistenceUnit.fullName };\r\n}\r\n\r\nasync function getKoq(schemaLocater: SchemaContext, fullName: string) {\r\n const [schemaName, propKoqName] = fullName.split(\":\");\r\n const schema = await schemaLocater.getSchema(new SchemaKey(schemaName), SchemaMatchType.Latest);\r\n if (!schema) {\r\n return undefined;\r\n }\r\n return schema.getItem<KindOfQuantity>(propKoqName);\r\n}\r\n\r\nasync function getKoqFormatProps(\r\n koq: KindOfQuantity,\r\n persistenceUnit: Unit | InvertedUnit,\r\n defaultFormats: FormatsMap | undefined,\r\n unitSystem?: UnitSystemKey,\r\n) {\r\n const unitSystems = getUnitSystemGroupNames(unitSystem);\r\n // use one of KOQ presentation format that matches requested unit system\r\n const presentationFormat = await getKoqPresentationFormat(koq, unitSystems);\r\n if (presentationFormat) {\r\n return getFormatProps(presentationFormat);\r\n }\r\n\r\n // use one of the formats in default formats map if there is one for matching phenomena and requested unit\r\n // system combination\r\n if (defaultFormats && unitSystem) {\r\n const actualPersistenceUnit = persistenceUnit instanceof InvertedUnit ? /* istanbul ignore next */ await persistenceUnit.invertsUnit : persistenceUnit;\r\n const phenomenon = await actualPersistenceUnit?.phenomenon;\r\n // istanbul ignore else\r\n if (phenomenon && defaultFormats[phenomenon.name.toUpperCase()]) {\r\n const defaultPhenomenonFormats = defaultFormats[phenomenon.name.toUpperCase()];\r\n for (const defaultUnitSystemFormat of Array.isArray(defaultPhenomenonFormats) ? /* istanbul ignore next */ defaultPhenomenonFormats : [defaultPhenomenonFormats]) {\r\n if (defaultUnitSystemFormat.unitSystems.includes(unitSystem)) {\r\n return defaultUnitSystemFormat.format;\r\n }\r\n }\r\n }\r\n }\r\n\r\n // use persistence unit format if it matches requested unit system and matching presentation format was not found\r\n const persistenceUnitSystem = await persistenceUnit.unitSystem;\r\n if (persistenceUnitSystem && unitSystems.includes(persistenceUnitSystem.name.toUpperCase())) {\r\n return getPersistenceUnitFormatProps(persistenceUnit);\r\n }\r\n\r\n // use default presentation format if persistence unit does not match requested unit system\r\n if (koq.defaultPresentationFormat) {\r\n return getFormatProps(koq.defaultPresentationFormat);\r\n }\r\n\r\n return undefined;\r\n}\r\n\r\nasync function getKoqPresentationFormat(koq: KindOfQuantity, unitSystems: string[]) {\r\n const presentationFormats = koq.presentationFormats;\r\n for (const system of unitSystems) {\r\n for (const format of presentationFormats) {\r\n const unit = format.units && format.units[0][0];\r\n // istanbul ignore if\r\n if (!unit) {\r\n continue;\r\n }\r\n const currentUnitSystem = await unit.unitSystem;\r\n if (currentUnitSystem && currentUnitSystem.name.toUpperCase() === system) {\r\n return format;\r\n }\r\n }\r\n }\r\n return undefined;\r\n}\r\n\r\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\r\n // Same as Format \"DefaultRealU\" in Formats ecschema\r\n return {\r\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\r\n precision: 6,\r\n type: \"Decimal\",\r\n uomSeparator: \" \",\r\n decimalSeparator: \".\",\r\n composite: {\r\n units: [\r\n {\r\n name: persistenceUnit.fullName,\r\n label: persistenceUnit.label,\r\n },\r\n ],\r\n },\r\n };\r\n}\r\n\r\nfunction getUnitSystemGroupNames(unitSystem?: UnitSystemKey) {\r\n switch (unitSystem) {\r\n case \"imperial\":\r\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\r\n case \"metric\":\r\n return [\"SI\", \"METRIC\", \"INTERNATIONAL\", \"FINANCE\"];\r\n case \"usCustomary\":\r\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\r\n case \"usSurvey\":\r\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\r\n }\r\n return [];\r\n}\r\n"]}
@@ -1,8 +1,42 @@
1
1
  /** @packageDocumentation
2
2
  * @module Core
3
3
  */
4
- import { FormatterSpec, ParserSpec, UnitSystemKey } from "@itwin/core-quantity";
4
+ import { FormatProps, FormatterSpec, ParserSpec, UnitSystemKey } from "@itwin/core-quantity";
5
5
  import { SchemaContext } from "@itwin/ecschema-metadata";
6
+ /**
7
+ * A data structure that associates unit systems with property value formatting props. The associations are used for
8
+ * assigning formatting props for specific phenomenon and unit system combinations (see [[FormatsMap]]).
9
+ *
10
+ * @public
11
+ */
12
+ export interface UnitSystemFormat {
13
+ unitSystems: UnitSystemKey[];
14
+ format: FormatProps;
15
+ }
16
+ /**
17
+ * A data structure that associates specific phenomenon with one or more formatting props for specific unit system.
18
+ *
19
+ * Example:
20
+ * ```json
21
+ * {
22
+ * length: [{
23
+ * unitSystems: ["metric"],
24
+ * format: formatForCentimeters,
25
+ * }, {
26
+ * unitSystems: ["imperial", "usCustomary"],
27
+ * format: formatForInches,
28
+ * }, {
29
+ * unitSystems: ["usSurvey"],
30
+ * format: formatForUsSurveyInches,
31
+ * }]
32
+ * }
33
+ * ```
34
+ *
35
+ * @public
36
+ */
37
+ export interface FormatsMap {
38
+ [phenomenon: string]: UnitSystemFormat | UnitSystemFormat[];
39
+ }
6
40
  /** @alpha */
7
41
  export interface FormatOptions {
8
42
  koqName: string;
@@ -12,7 +46,8 @@ export interface FormatOptions {
12
46
  export declare class KoqPropertyValueFormatter {
13
47
  private _schemaContext;
14
48
  private _unitsProvider;
15
- constructor(_schemaContext: SchemaContext);
49
+ private _defaultFormats?;
50
+ constructor(_schemaContext: SchemaContext, defaultFormats?: FormatsMap);
16
51
  format(value: number, options: FormatOptions): Promise<string | undefined>;
17
52
  getFormatterSpec(options: FormatOptions): Promise<FormatterSpec | undefined>;
18
53
  getParserSpec(options: FormatOptions): Promise<ParserSpec | undefined>;
@@ -1 +1 @@
1
- {"version":3,"file":"KoqPropertyValueFormatter.d.ts","sourceRoot":"","sources":["../../../src/presentation-common/KoqPropertyValueFormatter.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH,OAAO,EAAuB,aAAa,EAAE,UAAU,EAAiB,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACpH,OAAO,EACyC,aAAa,EAE5D,MAAM,0BAA0B,CAAC;AAElC,aAAa;AACb,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,aAAa,CAAC;CAC5B;AAED,aAAa;AACb,qBAAa,yBAAyB;IAGxB,OAAO,CAAC,cAAc;IAFlC,OAAO,CAAC,cAAc,CAAgB;gBAElB,cAAc,EAAE,aAAa;IAIpC,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa;IAO5C,gBAAgB,CAAC,OAAO,EAAE,aAAa;IAUvC,aAAa,CAAC,OAAO,EAAE,aAAa;CASlD"}
1
+ {"version":3,"file":"KoqPropertyValueFormatter.d.ts","sourceRoot":"","sources":["../../../src/presentation-common/KoqPropertyValueFormatter.ts"],"names":[],"mappings":"AAIA;;GAEG;AAEH,OAAO,EAAU,WAAW,EAAE,aAAa,EAAE,UAAU,EAAiB,aAAa,EAAE,MAAM,sBAAsB,CAAC;AACpH,OAAO,EAAgD,aAAa,EAAwD,MAAM,0BAA0B,CAAC;AAE7J;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAC/B,WAAW,EAAE,aAAa,EAAE,CAAC;IAC7B,MAAM,EAAE,WAAW,CAAC;CACrB;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,MAAM,WAAW,UAAU;IACzB,CAAC,UAAU,EAAE,MAAM,GAAG,gBAAgB,GAAG,gBAAgB,EAAE,CAAC;CAC7D;AAED,aAAa;AACb,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,CAAC,EAAE,aAAa,CAAC;CAC5B;AAED,aAAa;AACb,qBAAa,yBAAyB;IAIxB,OAAO,CAAC,cAAc;IAHlC,OAAO,CAAC,cAAc,CAAgB;IACtC,OAAO,CAAC,eAAe,CAAC,CAAa;gBAEjB,cAAc,EAAE,aAAa,EAAE,cAAc,CAAC,EAAE,UAAU;IAOjE,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,OAAO,EAAE,aAAa;IAQ5C,gBAAgB,CAAC,OAAO,EAAE,aAAa;IAWvC,aAAa,CAAC,OAAO,EAAE,aAAa;CAUlD"}
@@ -1,77 +1,105 @@
1
1
  /*---------------------------------------------------------------------------------------------
2
- * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
- * See LICENSE.md in the project root for license terms and full copyright notice.
4
- *--------------------------------------------------------------------------------------------*/
2
+ * Copyright (c) Bentley Systems, Incorporated. All rights reserved.
3
+ * See LICENSE.md in the project root for license terms and full copyright notice.
4
+ *--------------------------------------------------------------------------------------------*/
5
5
  /** @packageDocumentation
6
6
  * @module Core
7
7
  */
8
8
  import { Format, FormatterSpec, ParserSpec } from "@itwin/core-quantity";
9
- import { getFormatProps, SchemaKey, SchemaMatchType, SchemaUnitProvider, } from "@itwin/ecschema-metadata";
9
+ import { getFormatProps, InvertedUnit, SchemaKey, SchemaMatchType, SchemaUnitProvider } from "@itwin/ecschema-metadata";
10
10
  /** @alpha */
11
11
  export class KoqPropertyValueFormatter {
12
- constructor(_schemaContext) {
12
+ constructor(_schemaContext, defaultFormats) {
13
13
  this._schemaContext = _schemaContext;
14
14
  this._unitsProvider = new SchemaUnitProvider(_schemaContext);
15
+ this._defaultFormats = defaultFormats
16
+ ? Object.entries(defaultFormats).reduce((acc, [phenomenon, unitSystemFormats]) => ({ ...acc, [phenomenon.toUpperCase()]: unitSystemFormats }), {})
17
+ : /* istanbul ignore next */ undefined;
15
18
  }
16
19
  async format(value, options) {
17
20
  const formatterSpec = await this.getFormatterSpec(options);
18
- if (!formatterSpec)
21
+ if (!formatterSpec) {
19
22
  return undefined;
23
+ }
20
24
  return formatterSpec.applyFormatting(value);
21
25
  }
22
26
  async getFormatterSpec(options) {
23
- const formattingProps = await getFormattingProps(this._schemaContext, options);
24
- if (!formattingProps)
27
+ const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);
28
+ if (!formattingProps) {
25
29
  return undefined;
30
+ }
26
31
  const { formatProps, persistenceUnitName } = formattingProps;
27
32
  const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);
28
33
  const format = await Format.createFromJSON("", this._unitsProvider, formatProps);
29
34
  return FormatterSpec.create("", format, this._unitsProvider, persistenceUnit);
30
35
  }
31
36
  async getParserSpec(options) {
32
- const formattingProps = await getFormattingProps(this._schemaContext, options);
33
- if (!formattingProps)
37
+ const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);
38
+ if (!formattingProps) {
34
39
  return undefined;
40
+ }
35
41
  const { formatProps, persistenceUnitName } = formattingProps;
36
42
  const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);
37
43
  const format = await Format.createFromJSON("", this._unitsProvider, formatProps);
38
44
  return ParserSpec.create(format, this._unitsProvider, persistenceUnit);
39
45
  }
40
46
  }
41
- async function getFormattingProps(schemaLocater, options) {
47
+ async function getFormattingProps(schemaLocater, defaultFormats, options) {
42
48
  const { koqName, unitSystem } = options;
43
49
  const koq = await getKoq(schemaLocater, koqName);
44
- if (!koq)
50
+ if (!koq) {
45
51
  return undefined;
52
+ }
46
53
  const persistenceUnit = await koq.persistenceUnit;
47
54
  // istanbul ignore if
48
- if (!persistenceUnit)
55
+ if (!persistenceUnit) {
49
56
  return undefined;
50
- const formatProps = await getKoqFormatProps(koq, persistenceUnit, unitSystem);
51
- if (!formatProps)
57
+ }
58
+ const formatProps = await getKoqFormatProps(koq, persistenceUnit, defaultFormats, unitSystem);
59
+ if (!formatProps) {
52
60
  return undefined;
61
+ }
53
62
  return { formatProps, persistenceUnitName: persistenceUnit.fullName };
54
63
  }
55
64
  async function getKoq(schemaLocater, fullName) {
56
65
  const [schemaName, propKoqName] = fullName.split(":");
57
66
  const schema = await schemaLocater.getSchema(new SchemaKey(schemaName), SchemaMatchType.Latest);
58
- if (!schema)
67
+ if (!schema) {
59
68
  return undefined;
69
+ }
60
70
  return schema.getItem(propKoqName);
61
71
  }
62
- async function getKoqFormatProps(koq, persistenceUnit, unitSystem) {
72
+ async function getKoqFormatProps(koq, persistenceUnit, defaultFormats, unitSystem) {
63
73
  const unitSystems = getUnitSystemGroupNames(unitSystem);
64
74
  // use one of KOQ presentation format that matches requested unit system
65
75
  const presentationFormat = await getKoqPresentationFormat(koq, unitSystems);
66
- if (presentationFormat)
76
+ if (presentationFormat) {
67
77
  return getFormatProps(presentationFormat);
78
+ }
79
+ // use one of the formats in default formats map if there is one for matching phenomena and requested unit
80
+ // system combination
81
+ if (defaultFormats && unitSystem) {
82
+ const actualPersistenceUnit = persistenceUnit instanceof InvertedUnit ? /* istanbul ignore next */ await persistenceUnit.invertsUnit : persistenceUnit;
83
+ const phenomenon = await actualPersistenceUnit?.phenomenon;
84
+ // istanbul ignore else
85
+ if (phenomenon && defaultFormats[phenomenon.name.toUpperCase()]) {
86
+ const defaultPhenomenonFormats = defaultFormats[phenomenon.name.toUpperCase()];
87
+ for (const defaultUnitSystemFormat of Array.isArray(defaultPhenomenonFormats) ? /* istanbul ignore next */ defaultPhenomenonFormats : [defaultPhenomenonFormats]) {
88
+ if (defaultUnitSystemFormat.unitSystems.includes(unitSystem)) {
89
+ return defaultUnitSystemFormat.format;
90
+ }
91
+ }
92
+ }
93
+ }
68
94
  // use persistence unit format if it matches requested unit system and matching presentation format was not found
69
95
  const persistenceUnitSystem = await persistenceUnit.unitSystem;
70
- if (persistenceUnitSystem && unitSystems.includes(persistenceUnitSystem.name.toUpperCase()))
96
+ if (persistenceUnitSystem && unitSystems.includes(persistenceUnitSystem.name.toUpperCase())) {
71
97
  return getPersistenceUnitFormatProps(persistenceUnit);
98
+ }
72
99
  // use default presentation format if persistence unit does not match requested unit system
73
- if (koq.defaultPresentationFormat)
100
+ if (koq.defaultPresentationFormat) {
74
101
  return getFormatProps(koq.defaultPresentationFormat);
102
+ }
75
103
  return undefined;
76
104
  }
77
105
  async function getKoqPresentationFormat(koq, unitSystems) {
@@ -80,11 +108,13 @@ async function getKoqPresentationFormat(koq, unitSystems) {
80
108
  for (const format of presentationFormats) {
81
109
  const unit = format.units && format.units[0][0];
82
110
  // istanbul ignore if
83
- if (!unit)
111
+ if (!unit) {
84
112
  continue;
113
+ }
85
114
  const currentUnitSystem = await unit.unitSystem;
86
- if (currentUnitSystem && currentUnitSystem.name.toUpperCase() === system)
115
+ if (currentUnitSystem && currentUnitSystem.name.toUpperCase() === system) {
87
116
  return format;
117
+ }
88
118
  }
89
119
  }
90
120
  return undefined;
@@ -98,10 +128,12 @@ function getPersistenceUnitFormatProps(persistenceUnit) {
98
128
  uomSeparator: " ",
99
129
  decimalSeparator: ".",
100
130
  composite: {
101
- units: [{
131
+ units: [
132
+ {
102
133
  name: persistenceUnit.fullName,
103
134
  label: persistenceUnit.label,
104
- }],
135
+ },
136
+ ],
105
137
  },
106
138
  };
107
139
  }
@@ -1 +1 @@
1
- {"version":3,"file":"KoqPropertyValueFormatter.js","sourceRoot":"","sources":["../../../src/presentation-common/KoqPropertyValueFormatter.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAEH,OAAO,EAAE,MAAM,EAAe,aAAa,EAAE,UAAU,EAAgC,MAAM,sBAAsB,CAAC;AACpH,OAAO,EACL,cAAc,EAA+C,SAAS,EAAE,eAAe,EAAE,kBAAkB,GAE5G,MAAM,0BAA0B,CAAC;AAQlC,aAAa;AACb,MAAM,OAAO,yBAAyB;IAGpC,YAAoB,cAA6B;QAA7B,mBAAc,GAAd,cAAc,CAAe;QAC/C,IAAI,CAAC,cAAc,GAAG,IAAI,kBAAkB,CAAC,cAAc,CAAC,CAAC;IAC/D,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;YAChB,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AAED,SAAS,uBAAuB,CAAC,UAA0B;IACzD,QAAQ,UAAU,EAAE;QAClB,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;QAC9D,KAAK,QAAQ;YACX,OAAO,CAAC,IAAI,EAAE,QAAQ,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;QACtD,KAAK,aAAa;YAChB,OAAO,CAAC,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;QAClD,KAAK,UAAU;YACb,OAAO,CAAC,UAAU,EAAE,UAAU,EAAE,eAAe,EAAE,SAAS,CAAC,CAAC;KAC/D;IACD,OAAO,EAAE,CAAC;AACZ,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 Core\r\n */\r\n\r\nimport { Format, FormatProps, FormatterSpec, ParserSpec, UnitsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\r\nimport {\r\n getFormatProps, InvertedUnit, KindOfQuantity, SchemaContext, SchemaKey, SchemaMatchType, SchemaUnitProvider,\r\n Unit,\r\n} from \"@itwin/ecschema-metadata\";\r\n\r\n/** @alpha */\r\nexport interface FormatOptions {\r\n koqName: string;\r\n unitSystem?: UnitSystemKey;\r\n}\r\n\r\n/** @alpha */\r\nexport class KoqPropertyValueFormatter {\r\n private _unitsProvider: UnitsProvider;\r\n\r\n constructor(private _schemaContext: SchemaContext) {\r\n this._unitsProvider = new SchemaUnitProvider(_schemaContext);\r\n }\r\n\r\n public async format(value: number, options: FormatOptions) {\r\n const formatterSpec = await this.getFormatterSpec(options);\r\n if (!formatterSpec)\r\n return undefined;\r\n return formatterSpec.applyFormatting(value);\r\n }\r\n\r\n public async getFormatterSpec(options: FormatOptions) {\r\n const formattingProps = await getFormattingProps(this._schemaContext, options);\r\n if (!formattingProps)\r\n return undefined;\r\n const { formatProps, persistenceUnitName } = formattingProps;\r\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\r\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\r\n return FormatterSpec.create(\"\", format, this._unitsProvider, persistenceUnit);\r\n }\r\n\r\n public async getParserSpec(options: FormatOptions) {\r\n const formattingProps = await getFormattingProps(this._schemaContext, options);\r\n if (!formattingProps)\r\n return undefined;\r\n const { formatProps, persistenceUnitName } = formattingProps;\r\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\r\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\r\n return ParserSpec.create(format, this._unitsProvider, persistenceUnit);\r\n }\r\n}\r\n\r\ninterface FormattingProps {\r\n formatProps: FormatProps;\r\n persistenceUnitName: string;\r\n}\r\n\r\nasync function getFormattingProps(schemaLocater: SchemaContext, options: FormatOptions): Promise<FormattingProps | undefined> {\r\n const { koqName, unitSystem } = options;\r\n\r\n const koq = await getKoq(schemaLocater, koqName);\r\n if (!koq)\r\n return undefined;\r\n\r\n const persistenceUnit = await koq.persistenceUnit;\r\n // istanbul ignore if\r\n if (!persistenceUnit)\r\n return undefined;\r\n\r\n const formatProps = await getKoqFormatProps(koq, persistenceUnit, unitSystem);\r\n if (!formatProps)\r\n return undefined;\r\n\r\n return { formatProps, persistenceUnitName: persistenceUnit.fullName };\r\n}\r\n\r\nasync function getKoq(schemaLocater: SchemaContext, fullName: string) {\r\n const [schemaName, propKoqName] = fullName.split(\":\");\r\n const schema = await schemaLocater.getSchema(new SchemaKey(schemaName), SchemaMatchType.Latest);\r\n if (!schema)\r\n return undefined;\r\n\r\n return schema.getItem<KindOfQuantity>(propKoqName);\r\n}\r\n\r\nasync function getKoqFormatProps(koq: KindOfQuantity, persistenceUnit: Unit | InvertedUnit, unitSystem?: UnitSystemKey) {\r\n const unitSystems = getUnitSystemGroupNames(unitSystem);\r\n // use one of KOQ presentation format that matches requested unit system\r\n const presentationFormat = await getKoqPresentationFormat(koq, unitSystems);\r\n if (presentationFormat)\r\n return getFormatProps(presentationFormat);\r\n\r\n // use persistence unit format if it matches requested unit system and matching presentation format was not found\r\n const persistenceUnitSystem = await persistenceUnit.unitSystem;\r\n if (persistenceUnitSystem && unitSystems.includes(persistenceUnitSystem.name.toUpperCase()))\r\n return getPersistenceUnitFormatProps(persistenceUnit);\r\n\r\n // use default presentation format if persistence unit does not match requested unit system\r\n if (koq.defaultPresentationFormat)\r\n return getFormatProps(koq.defaultPresentationFormat);\r\n\r\n return undefined;\r\n}\r\n\r\nasync function getKoqPresentationFormat(koq: KindOfQuantity, unitSystems: string[]) {\r\n const presentationFormats = koq.presentationFormats;\r\n for (const system of unitSystems) {\r\n for (const format of presentationFormats) {\r\n const unit = format.units && format.units[0][0];\r\n // istanbul ignore if\r\n if (!unit)\r\n continue;\r\n\r\n const currentUnitSystem = await unit.unitSystem;\r\n if (currentUnitSystem && currentUnitSystem.name.toUpperCase() === system)\r\n return format;\r\n }\r\n }\r\n return undefined;\r\n}\r\n\r\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\r\n // Same as Format \"DefaultRealU\" in Formats ecschema\r\n return {\r\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\r\n precision: 6,\r\n type: \"Decimal\",\r\n uomSeparator: \" \",\r\n decimalSeparator: \".\",\r\n composite: {\r\n units: [{\r\n name: persistenceUnit.fullName,\r\n label: persistenceUnit.label,\r\n }],\r\n },\r\n };\r\n}\r\n\r\nfunction getUnitSystemGroupNames(unitSystem?: UnitSystemKey) {\r\n switch (unitSystem) {\r\n case \"imperial\":\r\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\r\n case \"metric\":\r\n return [\"SI\", \"METRIC\", \"INTERNATIONAL\", \"FINANCE\"];\r\n case \"usCustomary\":\r\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\r\n case \"usSurvey\":\r\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\r\n }\r\n return [];\r\n}\r\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,EAAE,cAAc,EAAE,YAAY,EAAiC,SAAS,EAAE,eAAe,EAAE,kBAAkB,EAAQ,MAAM,0BAA0B,CAAC;AA4C7J,aAAa;AACb,MAAM,OAAO,yBAAyB;IAIpC,YAAoB,cAA6B,EAAE,cAA2B;QAA1D,mBAAc,GAAd,cAAc,CAAe;QAC/C,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,0BAA0B,CAAC,SAAS,CAAC;IAC3C,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;YAClB,OAAO,SAAS,CAAC;SAClB;QACD,OAAO,aAAa,CAAC,eAAe,CAAC,KAAK,CAAC,CAAC;IAC9C,CAAC;IAEM,KAAK,CAAC,g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AC;KAC/D;IACD,OAAO,EAAE,CAAC;AACZ,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 Core\r\n */\r\n\r\nimport { Format, FormatProps, FormatterSpec, ParserSpec, UnitsProvider, UnitSystemKey } from \"@itwin/core-quantity\";\r\nimport { getFormatProps, InvertedUnit, KindOfQuantity, SchemaContext, SchemaKey, SchemaMatchType, SchemaUnitProvider, Unit } from \"@itwin/ecschema-metadata\";\r\n\r\n/**\r\n * A data structure that associates unit systems with property value formatting props. The associations are used for\r\n * assigning formatting props for specific phenomenon and unit system combinations (see [[FormatsMap]]).\r\n *\r\n * @public\r\n */\r\nexport interface UnitSystemFormat {\r\n unitSystems: UnitSystemKey[];\r\n format: FormatProps;\r\n}\r\n\r\n/**\r\n * A data structure that associates specific phenomenon with one or more formatting props for specific unit system.\r\n *\r\n * Example:\r\n * ```json\r\n * {\r\n * length: [{\r\n * unitSystems: [\"metric\"],\r\n * format: formatForCentimeters,\r\n * }, {\r\n * unitSystems: [\"imperial\", \"usCustomary\"],\r\n * format: formatForInches,\r\n * }, {\r\n * unitSystems: [\"usSurvey\"],\r\n * format: formatForUsSurveyInches,\r\n * }]\r\n * }\r\n * ```\r\n *\r\n * @public\r\n */\r\nexport interface FormatsMap {\r\n [phenomenon: string]: UnitSystemFormat | UnitSystemFormat[];\r\n}\r\n\r\n/** @alpha */\r\nexport interface FormatOptions {\r\n koqName: string;\r\n unitSystem?: UnitSystemKey;\r\n}\r\n\r\n/** @alpha */\r\nexport class KoqPropertyValueFormatter {\r\n private _unitsProvider: UnitsProvider;\r\n private _defaultFormats?: FormatsMap;\r\n\r\n constructor(private _schemaContext: SchemaContext, defaultFormats?: FormatsMap) {\r\n this._unitsProvider = new SchemaUnitProvider(_schemaContext);\r\n this._defaultFormats = defaultFormats\r\n ? Object.entries(defaultFormats).reduce((acc, [phenomenon, unitSystemFormats]) => ({ ...acc, [phenomenon.toUpperCase()]: unitSystemFormats }), {})\r\n : /* istanbul ignore next */ undefined;\r\n }\r\n\r\n public async format(value: number, options: FormatOptions) {\r\n const formatterSpec = await this.getFormatterSpec(options);\r\n if (!formatterSpec) {\r\n return undefined;\r\n }\r\n return formatterSpec.applyFormatting(value);\r\n }\r\n\r\n public async getFormatterSpec(options: FormatOptions) {\r\n const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);\r\n if (!formattingProps) {\r\n return undefined;\r\n }\r\n const { formatProps, persistenceUnitName } = formattingProps;\r\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\r\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\r\n return FormatterSpec.create(\"\", format, this._unitsProvider, persistenceUnit);\r\n }\r\n\r\n public async getParserSpec(options: FormatOptions) {\r\n const formattingProps = await getFormattingProps(this._schemaContext, this._defaultFormats, options);\r\n if (!formattingProps) {\r\n return undefined;\r\n }\r\n const { formatProps, persistenceUnitName } = formattingProps;\r\n const persistenceUnit = await this._unitsProvider.findUnitByName(persistenceUnitName);\r\n const format = await Format.createFromJSON(\"\", this._unitsProvider, formatProps);\r\n return ParserSpec.create(format, this._unitsProvider, persistenceUnit);\r\n }\r\n}\r\n\r\ninterface FormattingProps {\r\n formatProps: FormatProps;\r\n persistenceUnitName: string;\r\n}\r\n\r\nasync function getFormattingProps(\r\n schemaLocater: SchemaContext,\r\n defaultFormats: FormatsMap | undefined,\r\n options: FormatOptions,\r\n): Promise<FormattingProps | undefined> {\r\n const { koqName, unitSystem } = options;\r\n\r\n const koq = await getKoq(schemaLocater, koqName);\r\n if (!koq) {\r\n return undefined;\r\n }\r\n\r\n const persistenceUnit = await koq.persistenceUnit;\r\n // istanbul ignore if\r\n if (!persistenceUnit) {\r\n return undefined;\r\n }\r\n\r\n const formatProps = await getKoqFormatProps(koq, persistenceUnit, defaultFormats, unitSystem);\r\n if (!formatProps) {\r\n return undefined;\r\n }\r\n\r\n return { formatProps, persistenceUnitName: persistenceUnit.fullName };\r\n}\r\n\r\nasync function getKoq(schemaLocater: SchemaContext, fullName: string) {\r\n const [schemaName, propKoqName] = fullName.split(\":\");\r\n const schema = await schemaLocater.getSchema(new SchemaKey(schemaName), SchemaMatchType.Latest);\r\n if (!schema) {\r\n return undefined;\r\n }\r\n return schema.getItem<KindOfQuantity>(propKoqName);\r\n}\r\n\r\nasync function getKoqFormatProps(\r\n koq: KindOfQuantity,\r\n persistenceUnit: Unit | InvertedUnit,\r\n defaultFormats: FormatsMap | undefined,\r\n unitSystem?: UnitSystemKey,\r\n) {\r\n const unitSystems = getUnitSystemGroupNames(unitSystem);\r\n // use one of KOQ presentation format that matches requested unit system\r\n const presentationFormat = await getKoqPresentationFormat(koq, unitSystems);\r\n if (presentationFormat) {\r\n return getFormatProps(presentationFormat);\r\n }\r\n\r\n // use one of the formats in default formats map if there is one for matching phenomena and requested unit\r\n // system combination\r\n if (defaultFormats && unitSystem) {\r\n const actualPersistenceUnit = persistenceUnit instanceof InvertedUnit ? /* istanbul ignore next */ await persistenceUnit.invertsUnit : persistenceUnit;\r\n const phenomenon = await actualPersistenceUnit?.phenomenon;\r\n // istanbul ignore else\r\n if (phenomenon && defaultFormats[phenomenon.name.toUpperCase()]) {\r\n const defaultPhenomenonFormats = defaultFormats[phenomenon.name.toUpperCase()];\r\n for (const defaultUnitSystemFormat of Array.isArray(defaultPhenomenonFormats) ? /* istanbul ignore next */ defaultPhenomenonFormats : [defaultPhenomenonFormats]) {\r\n if (defaultUnitSystemFormat.unitSystems.includes(unitSystem)) {\r\n return defaultUnitSystemFormat.format;\r\n }\r\n }\r\n }\r\n }\r\n\r\n // use persistence unit format if it matches requested unit system and matching presentation format was not found\r\n const persistenceUnitSystem = await persistenceUnit.unitSystem;\r\n if (persistenceUnitSystem && unitSystems.includes(persistenceUnitSystem.name.toUpperCase())) {\r\n return getPersistenceUnitFormatProps(persistenceUnit);\r\n }\r\n\r\n // use default presentation format if persistence unit does not match requested unit system\r\n if (koq.defaultPresentationFormat) {\r\n return getFormatProps(koq.defaultPresentationFormat);\r\n }\r\n\r\n return undefined;\r\n}\r\n\r\nasync function getKoqPresentationFormat(koq: KindOfQuantity, unitSystems: string[]) {\r\n const presentationFormats = koq.presentationFormats;\r\n for (const system of unitSystems) {\r\n for (const format of presentationFormats) {\r\n const unit = format.units && format.units[0][0];\r\n // istanbul ignore if\r\n if (!unit) {\r\n continue;\r\n }\r\n const currentUnitSystem = await unit.unitSystem;\r\n if (currentUnitSystem && currentUnitSystem.name.toUpperCase() === system) {\r\n return format;\r\n }\r\n }\r\n }\r\n return undefined;\r\n}\r\n\r\nfunction getPersistenceUnitFormatProps(persistenceUnit: Unit | InvertedUnit): FormatProps {\r\n // Same as Format \"DefaultRealU\" in Formats ecschema\r\n return {\r\n formatTraits: [\"keepSingleZero\", \"keepDecimalPoint\", \"showUnitLabel\"],\r\n precision: 6,\r\n type: \"Decimal\",\r\n uomSeparator: \" \",\r\n decimalSeparator: \".\",\r\n composite: {\r\n units: [\r\n {\r\n name: persistenceUnit.fullName,\r\n label: persistenceUnit.label,\r\n },\r\n ],\r\n },\r\n };\r\n}\r\n\r\nfunction getUnitSystemGroupNames(unitSystem?: UnitSystemKey) {\r\n switch (unitSystem) {\r\n case \"imperial\":\r\n return [\"IMPERIAL\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\r\n case \"metric\":\r\n return [\"SI\", \"METRIC\", \"INTERNATIONAL\", \"FINANCE\"];\r\n case \"usCustomary\":\r\n return [\"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\r\n case \"usSurvey\":\r\n return [\"USSURVEY\", \"USCUSTOM\", \"INTERNATIONAL\", \"FINANCE\"];\r\n }\r\n return [];\r\n}\r\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@itwin/presentation-common",
3
- "version": "4.3.0-dev.29",
3
+ "version": "4.3.0-dev.31",
4
4
  "description": "Common pieces for iModel.js presentation packages",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -22,10 +22,10 @@
22
22
  "module": "lib/esm/presentation-common.js",
23
23
  "typings": "lib/cjs/presentation-common",
24
24
  "peerDependencies": {
25
- "@itwin/core-bentley": "^4.3.0-dev.29",
26
- "@itwin/core-common": "^4.3.0-dev.29",
27
- "@itwin/core-quantity": "^4.3.0-dev.29",
28
- "@itwin/ecschema-metadata": "^4.3.0-dev.29"
25
+ "@itwin/core-bentley": "^4.3.0-dev.31",
26
+ "@itwin/core-common": "^4.3.0-dev.31",
27
+ "@itwin/core-quantity": "^4.3.0-dev.31",
28
+ "@itwin/ecschema-metadata": "^4.3.0-dev.31"
29
29
  },
30
30
  "devDependencies": {
31
31
  "@itwin/eslint-plugin": "4.0.0-dev.44",
@@ -58,11 +58,11 @@
58
58
  "typescript": "~5.0.2",
59
59
  "typescript-json-schema": "^0.55.0",
60
60
  "yargs": "^17.4.0",
61
- "@itwin/core-bentley": "4.3.0-dev.29",
62
- "@itwin/build-tools": "4.3.0-dev.29",
63
- "@itwin/core-common": "4.3.0-dev.29",
64
- "@itwin/core-quantity": "4.3.0-dev.29",
65
- "@itwin/ecschema-metadata": "4.3.0-dev.29"
61
+ "@itwin/build-tools": "4.3.0-dev.31",
62
+ "@itwin/core-bentley": "4.3.0-dev.31",
63
+ "@itwin/core-common": "4.3.0-dev.31",
64
+ "@itwin/core-quantity": "4.3.0-dev.31",
65
+ "@itwin/ecschema-metadata": "4.3.0-dev.31"
66
66
  },
67
67
  "nyc": {
68
68
  "extends": "./node_modules/@itwin/build-tools/.nycrc"