@itwin/ecschema-metadata 4.10.0-dev.9 → 5.0.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 +26 -1
- package/lib/cjs/DelayedPromise.d.ts.map +1 -1
- package/lib/cjs/Deserialization/AbstractParser.d.ts +3 -0
- package/lib/cjs/Deserialization/AbstractParser.d.ts.map +1 -1
- package/lib/cjs/Deserialization/AbstractParser.js.map +1 -1
- package/lib/cjs/Deserialization/Helper.d.ts +9 -0
- package/lib/cjs/Deserialization/Helper.d.ts.map +1 -1
- package/lib/cjs/Deserialization/Helper.js +24 -6
- package/lib/cjs/Deserialization/Helper.js.map +1 -1
- package/lib/cjs/Deserialization/JsonParser.d.ts +3 -0
- package/lib/cjs/Deserialization/JsonParser.d.ts.map +1 -1
- package/lib/cjs/Deserialization/JsonParser.js +55 -9
- package/lib/cjs/Deserialization/JsonParser.js.map +1 -1
- package/lib/cjs/Deserialization/JsonProps.d.ts +4 -0
- package/lib/cjs/Deserialization/JsonProps.d.ts.map +1 -1
- package/lib/cjs/Deserialization/JsonProps.js.map +1 -1
- package/lib/cjs/Deserialization/XmlParser.d.ts +3 -2
- package/lib/cjs/Deserialization/XmlParser.d.ts.map +1 -1
- package/lib/cjs/Deserialization/XmlParser.js +32 -13
- package/lib/cjs/Deserialization/XmlParser.js.map +1 -1
- package/lib/cjs/ECObjects.d.ts.map +1 -1
- package/lib/cjs/ECObjects.js +24 -21
- package/lib/cjs/ECObjects.js.map +1 -1
- package/lib/cjs/Exception.d.ts +2 -1
- package/lib/cjs/Exception.d.ts.map +1 -1
- package/lib/cjs/Exception.js +1 -0
- package/lib/cjs/Exception.js.map +1 -1
- package/lib/cjs/Metadata/Class.d.ts.map +1 -1
- package/lib/cjs/Metadata/Class.js +10 -3
- package/lib/cjs/Metadata/Class.js.map +1 -1
- package/lib/cjs/Metadata/Constant.js.map +1 -1
- package/lib/cjs/Metadata/CustomAttribute.js +1 -2
- package/lib/cjs/Metadata/CustomAttribute.js.map +1 -1
- package/lib/cjs/Metadata/CustomAttributeClass.js.map +1 -1
- package/lib/cjs/Metadata/EntityClass.js +3 -3
- package/lib/cjs/Metadata/EntityClass.js.map +1 -1
- package/lib/cjs/Metadata/Enumeration.d.ts.map +1 -1
- package/lib/cjs/Metadata/Enumeration.js +11 -4
- package/lib/cjs/Metadata/Enumeration.js.map +1 -1
- package/lib/cjs/Metadata/Format.d.ts.map +1 -1
- package/lib/cjs/Metadata/Format.js +6 -6
- package/lib/cjs/Metadata/Format.js.map +1 -1
- package/lib/cjs/Metadata/InvertedUnit.d.ts +4 -0
- package/lib/cjs/Metadata/InvertedUnit.d.ts.map +1 -1
- package/lib/cjs/Metadata/InvertedUnit.js +6 -0
- package/lib/cjs/Metadata/InvertedUnit.js.map +1 -1
- package/lib/cjs/Metadata/KindOfQuantity.d.ts +0 -4
- package/lib/cjs/Metadata/KindOfQuantity.d.ts.map +1 -1
- package/lib/cjs/Metadata/KindOfQuantity.js +2 -54
- package/lib/cjs/Metadata/KindOfQuantity.js.map +1 -1
- package/lib/cjs/Metadata/Mixin.js.map +1 -1
- package/lib/cjs/Metadata/OverrideFormat.d.ts +12 -0
- package/lib/cjs/Metadata/OverrideFormat.d.ts.map +1 -1
- package/lib/cjs/Metadata/OverrideFormat.js +55 -2
- package/lib/cjs/Metadata/OverrideFormat.js.map +1 -1
- package/lib/cjs/Metadata/Phenomenon.js.map +1 -1
- package/lib/cjs/Metadata/PropertyCategory.js.map +1 -1
- package/lib/cjs/Metadata/RelationshipClass.d.ts.map +1 -1
- package/lib/cjs/Metadata/RelationshipClass.js +8 -3
- package/lib/cjs/Metadata/RelationshipClass.js.map +1 -1
- package/lib/cjs/Metadata/Schema.d.ts +9 -0
- package/lib/cjs/Metadata/Schema.d.ts.map +1 -1
- package/lib/cjs/Metadata/Schema.js +74 -7
- package/lib/cjs/Metadata/Schema.js.map +1 -1
- package/lib/cjs/Metadata/SchemaItem.d.ts.map +1 -1
- package/lib/cjs/Metadata/Unit.js.map +1 -1
- package/lib/cjs/Metadata/UnitSystem.js.map +1 -1
- package/lib/cjs/PropertyTypes.js +2 -2
- package/lib/cjs/PropertyTypes.js.map +1 -1
- package/lib/cjs/SchemaKey.d.ts.map +1 -1
- package/lib/cjs/SchemaKey.js +1 -5
- package/lib/cjs/SchemaKey.js.map +1 -1
- package/lib/cjs/UnitConversion/Graph.d.ts.map +1 -1
- package/lib/cjs/UnitConversion/Parser.js +1 -2
- package/lib/cjs/UnitConversion/Parser.js.map +1 -1
- package/lib/cjs/UnitProvider/SchemaUnitProvider.d.ts +1 -6
- package/lib/cjs/UnitProvider/SchemaUnitProvider.d.ts.map +1 -1
- package/lib/cjs/UnitProvider/SchemaUnitProvider.js +100 -58
- package/lib/cjs/UnitProvider/SchemaUnitProvider.js.map +1 -1
- package/lib/esm/DelayedPromise.d.ts.map +1 -1
- package/lib/esm/Deserialization/AbstractParser.d.ts +3 -0
- package/lib/esm/Deserialization/AbstractParser.d.ts.map +1 -1
- package/lib/esm/Deserialization/AbstractParser.js.map +1 -1
- package/lib/esm/Deserialization/Helper.d.ts +9 -0
- package/lib/esm/Deserialization/Helper.d.ts.map +1 -1
- package/lib/esm/Deserialization/Helper.js +24 -6
- package/lib/esm/Deserialization/Helper.js.map +1 -1
- package/lib/esm/Deserialization/JsonParser.d.ts +3 -0
- package/lib/esm/Deserialization/JsonParser.d.ts.map +1 -1
- package/lib/esm/Deserialization/JsonParser.js +55 -9
- package/lib/esm/Deserialization/JsonParser.js.map +1 -1
- package/lib/esm/Deserialization/JsonProps.d.ts +4 -0
- package/lib/esm/Deserialization/JsonProps.d.ts.map +1 -1
- package/lib/esm/Deserialization/JsonProps.js.map +1 -1
- package/lib/esm/Deserialization/XmlParser.d.ts +3 -2
- package/lib/esm/Deserialization/XmlParser.d.ts.map +1 -1
- package/lib/esm/Deserialization/XmlParser.js +32 -13
- package/lib/esm/Deserialization/XmlParser.js.map +1 -1
- package/lib/esm/ECObjects.d.ts.map +1 -1
- package/lib/esm/ECObjects.js +8 -5
- package/lib/esm/ECObjects.js.map +1 -1
- package/lib/esm/Exception.d.ts +2 -1
- package/lib/esm/Exception.d.ts.map +1 -1
- package/lib/esm/Exception.js +1 -0
- package/lib/esm/Exception.js.map +1 -1
- package/lib/esm/Metadata/Class.d.ts.map +1 -1
- package/lib/esm/Metadata/Class.js +10 -3
- package/lib/esm/Metadata/Class.js.map +1 -1
- package/lib/esm/Metadata/Constant.js.map +1 -1
- package/lib/esm/Metadata/CustomAttributeClass.js.map +1 -1
- package/lib/esm/Metadata/EntityClass.js.map +1 -1
- package/lib/esm/Metadata/Enumeration.d.ts.map +1 -1
- package/lib/esm/Metadata/Enumeration.js +11 -4
- package/lib/esm/Metadata/Enumeration.js.map +1 -1
- package/lib/esm/Metadata/Format.d.ts.map +1 -1
- package/lib/esm/Metadata/Format.js +7 -7
- package/lib/esm/Metadata/Format.js.map +1 -1
- package/lib/esm/Metadata/InvertedUnit.d.ts +4 -0
- package/lib/esm/Metadata/InvertedUnit.d.ts.map +1 -1
- package/lib/esm/Metadata/InvertedUnit.js +6 -0
- package/lib/esm/Metadata/InvertedUnit.js.map +1 -1
- package/lib/esm/Metadata/KindOfQuantity.d.ts +0 -4
- package/lib/esm/Metadata/KindOfQuantity.d.ts.map +1 -1
- package/lib/esm/Metadata/KindOfQuantity.js +2 -54
- package/lib/esm/Metadata/KindOfQuantity.js.map +1 -1
- package/lib/esm/Metadata/Mixin.js.map +1 -1
- package/lib/esm/Metadata/OverrideFormat.d.ts +12 -0
- package/lib/esm/Metadata/OverrideFormat.d.ts.map +1 -1
- package/lib/esm/Metadata/OverrideFormat.js +53 -0
- package/lib/esm/Metadata/OverrideFormat.js.map +1 -1
- package/lib/esm/Metadata/Phenomenon.js.map +1 -1
- package/lib/esm/Metadata/PropertyCategory.js.map +1 -1
- package/lib/esm/Metadata/RelationshipClass.d.ts.map +1 -1
- package/lib/esm/Metadata/RelationshipClass.js +8 -3
- package/lib/esm/Metadata/RelationshipClass.js.map +1 -1
- package/lib/esm/Metadata/Schema.d.ts +9 -0
- package/lib/esm/Metadata/Schema.d.ts.map +1 -1
- package/lib/esm/Metadata/Schema.js +74 -7
- package/lib/esm/Metadata/Schema.js.map +1 -1
- package/lib/esm/Metadata/SchemaItem.d.ts.map +1 -1
- package/lib/esm/Metadata/Unit.js.map +1 -1
- package/lib/esm/Metadata/UnitSystem.js.map +1 -1
- package/lib/esm/SchemaKey.d.ts.map +1 -1
- package/lib/esm/SchemaKey.js +1 -5
- package/lib/esm/SchemaKey.js.map +1 -1
- package/lib/esm/UnitConversion/Graph.d.ts.map +1 -1
- package/lib/esm/UnitProvider/SchemaUnitProvider.d.ts +1 -6
- package/lib/esm/UnitProvider/SchemaUnitProvider.d.ts.map +1 -1
- package/lib/esm/UnitProvider/SchemaUnitProvider.js +100 -58
- package/lib/esm/UnitProvider/SchemaUnitProvider.js.map +1 -1
- package/package.json +10 -10
|
@@ -3,12 +3,14 @@
|
|
|
3
3
|
* See LICENSE.md in the project root for license terms and full copyright notice.
|
|
4
4
|
*--------------------------------------------------------------------------------------------*/
|
|
5
5
|
import { BentleyError, BentleyStatus } from "@itwin/core-bentley";
|
|
6
|
+
import { BadUnit, UnitConversionInvert } from "@itwin/core-quantity";
|
|
6
7
|
import { SchemaContext } from "../Context";
|
|
7
8
|
import { SchemaItem } from "../Metadata/SchemaItem";
|
|
8
9
|
import { SchemaItemKey, SchemaKey } from "../SchemaKey";
|
|
9
10
|
import { Unit } from "../Metadata/Unit";
|
|
10
11
|
import { SchemaItemType } from "../ECObjects";
|
|
11
12
|
import { UnitConverter } from "../UnitConversion/UnitConverter";
|
|
13
|
+
import { InvertedUnit } from "../Metadata/InvertedUnit";
|
|
12
14
|
/**
|
|
13
15
|
* Class used to find Units in SchemaContext by attributes such as Phenomenon and DisplayLabel.
|
|
14
16
|
* @beta
|
|
@@ -38,8 +40,22 @@ export class SchemaUnitProvider {
|
|
|
38
40
|
* @returns UnitProps interface from @itwin/core-quantity whose name matches unitName.
|
|
39
41
|
*/
|
|
40
42
|
async findUnitByName(unitName) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
+
// Check if schema exists and unit exists in schema
|
|
44
|
+
const [schemaName, schemaItemName] = SchemaItem.parseFullName(unitName);
|
|
45
|
+
const schemaKey = new SchemaKey(schemaName);
|
|
46
|
+
const schema = await this._context.getSchema(schemaKey);
|
|
47
|
+
if (!schema) {
|
|
48
|
+
return new BadUnit(); // return BadUnit if schema does not exist
|
|
49
|
+
}
|
|
50
|
+
const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);
|
|
51
|
+
const unit = await this._context.getSchemaItem(itemKey);
|
|
52
|
+
if (unit && unit.schemaItemType === SchemaItemType.Unit)
|
|
53
|
+
return this.getUnitsProps(unit);
|
|
54
|
+
const invertedUnit = await this._context.getSchemaItem(itemKey);
|
|
55
|
+
if (invertedUnit && invertedUnit.schemaItemType === SchemaItemType.InvertedUnit) {
|
|
56
|
+
return this.getUnitsProps(invertedUnit);
|
|
57
|
+
}
|
|
58
|
+
return new BadUnit();
|
|
43
59
|
}
|
|
44
60
|
/**
|
|
45
61
|
* Find all units in context that belongs to given phenomenon.
|
|
@@ -74,10 +90,20 @@ export class SchemaUnitProvider {
|
|
|
74
90
|
if (Unit.isUnit(value)) {
|
|
75
91
|
const foundPhenomenon = await value.phenomenon;
|
|
76
92
|
if (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)) {
|
|
77
|
-
const unitProps = this.getUnitsProps(value);
|
|
93
|
+
const unitProps = await this.getUnitsProps(value);
|
|
78
94
|
filteredUnits.push(unitProps);
|
|
79
95
|
}
|
|
80
96
|
}
|
|
97
|
+
else if (InvertedUnit.isInvertedUnit(value) && value.invertsUnit) {
|
|
98
|
+
const invertsUnit = await value.invertsUnit;
|
|
99
|
+
if (invertsUnit) {
|
|
100
|
+
const foundPhenomenon = await invertsUnit.phenomenon;
|
|
101
|
+
if (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)) {
|
|
102
|
+
const unitProps = await this.getUnitsProps(value);
|
|
103
|
+
filteredUnits.push(unitProps);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
81
107
|
({ value, done } = schemaItems.next());
|
|
82
108
|
}
|
|
83
109
|
return filteredUnits;
|
|
@@ -111,22 +137,11 @@ export class SchemaUnitProvider {
|
|
|
111
137
|
const findSchema = schemaName ? schemaName.toLowerCase() : undefined;
|
|
112
138
|
const findPhenomenon = phenomenon ? phenomenon.toLowerCase() : undefined;
|
|
113
139
|
const findUnitSystem = unitSystem ? unitSystem.toLowerCase() : undefined;
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
catch (err) {
|
|
120
|
-
// If there is no Unit with display label that matches label, then check for alternate display labels that may match
|
|
121
|
-
foundUnit = await this.findUnitByAltDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
catch (err) {
|
|
125
|
-
throw new BentleyError(BentleyStatus.ERROR, "Cannot find unit with label", () => {
|
|
126
|
-
return { unitLabel };
|
|
127
|
-
});
|
|
128
|
-
}
|
|
129
|
-
return this.getUnitsProps(foundUnit);
|
|
140
|
+
const foundUnit = await this.findUnitByDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);
|
|
141
|
+
if (foundUnit.isValid)
|
|
142
|
+
return foundUnit;
|
|
143
|
+
// If there is no Unit with display label that matches label, then check for alternate display labels that may match
|
|
144
|
+
return this.findUnitByAltDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);
|
|
130
145
|
}
|
|
131
146
|
/**
|
|
132
147
|
* Gets the @itwin/core-quantity UnitConversionProps for the given fromUnit and toUnit.
|
|
@@ -135,49 +150,63 @@ export class SchemaUnitProvider {
|
|
|
135
150
|
* @returns The UnitConversionProps interface from the @itwin/core-quantity package.
|
|
136
151
|
*/
|
|
137
152
|
async getConversion(fromUnit, toUnit) {
|
|
138
|
-
|
|
139
|
-
|
|
153
|
+
// need to check if either side is an inverted unit. The UnitConverter can only handle Units
|
|
154
|
+
if (!fromUnit.isValid || !toUnit.isValid)
|
|
155
|
+
throw new BentleyError(BentleyStatus.ERROR, "Both provided units must be valid.", () => {
|
|
156
|
+
return { fromUnit, toUnit };
|
|
157
|
+
});
|
|
158
|
+
const { unitName: fromUnitName, isInverted: fromIsInverted } = await this.checkUnitPropsForConversion(fromUnit, this._context);
|
|
159
|
+
const { unitName: toUnitName, isInverted: toIsInverted } = await this.checkUnitPropsForConversion(toUnit, this._context);
|
|
160
|
+
const conversion = await this._unitConverter.calculateConversion(fromUnitName, toUnitName);
|
|
161
|
+
const result = {
|
|
140
162
|
factor: conversion.factor,
|
|
141
163
|
offset: conversion.offset,
|
|
142
164
|
};
|
|
165
|
+
if (fromIsInverted && !toIsInverted)
|
|
166
|
+
result.inversion = UnitConversionInvert.InvertPreConversion;
|
|
167
|
+
else if (!fromIsInverted && toIsInverted)
|
|
168
|
+
result.inversion = UnitConversionInvert.InvertPostConversion;
|
|
169
|
+
return result;
|
|
143
170
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
* @param unitName Full name of unit.
|
|
147
|
-
* @returns Unit whose full name matches unitName.
|
|
148
|
-
*/
|
|
149
|
-
async findECUnitByName(unitName) {
|
|
150
|
-
// Check if schema exists and unit exists in schema
|
|
151
|
-
const [schemaName, schemaItemName] = SchemaItem.parseFullName(unitName);
|
|
171
|
+
async checkUnitPropsForConversion(input, context) {
|
|
172
|
+
const [schemaName, schemaItemName] = SchemaItem.parseFullName(input.name);
|
|
152
173
|
const schemaKey = new SchemaKey(schemaName);
|
|
153
|
-
const schema = await
|
|
174
|
+
const schema = await context.getSchema(schemaKey);
|
|
154
175
|
if (!schema) {
|
|
155
|
-
throw new BentleyError(BentleyStatus.ERROR, "
|
|
156
|
-
return {
|
|
176
|
+
throw new BentleyError(BentleyStatus.ERROR, "Could not obtain schema for unit.", () => {
|
|
177
|
+
return { name: input.name };
|
|
157
178
|
});
|
|
158
179
|
}
|
|
159
180
|
const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);
|
|
160
|
-
const
|
|
161
|
-
if
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return item;
|
|
167
|
-
throw new BentleyError(BentleyStatus.ERROR, "Item is not a unit", () => {
|
|
168
|
-
return { itemType: item.key.fullName };
|
|
169
|
-
});
|
|
181
|
+
const invertedUnit = await context.getSchemaItem(itemKey);
|
|
182
|
+
// Check if we found an item, the item is an inverted unit, and it has its invertsUnit property set
|
|
183
|
+
if (invertedUnit && InvertedUnit.isInvertedUnit(invertedUnit) && invertedUnit.invertsUnit) {
|
|
184
|
+
return { unitName: invertedUnit.invertsUnit.fullName, isInverted: true };
|
|
185
|
+
}
|
|
186
|
+
return { unitName: input.name, isInverted: false };
|
|
170
187
|
}
|
|
171
188
|
/**
|
|
172
189
|
* Gets the @itwin/core UnitProps for the given Unit.
|
|
173
190
|
* @param unit The Unit to convert.
|
|
174
191
|
* @returns UnitProps interface from @itwin/core.
|
|
175
192
|
*/
|
|
176
|
-
getUnitsProps(unit) {
|
|
193
|
+
async getUnitsProps(unit) {
|
|
194
|
+
if (Unit.isUnit(unit)) {
|
|
195
|
+
return {
|
|
196
|
+
name: unit.fullName,
|
|
197
|
+
label: unit.label ?? "",
|
|
198
|
+
phenomenon: unit.phenomenon ? unit.phenomenon.fullName : "",
|
|
199
|
+
isValid: true,
|
|
200
|
+
system: unit.unitSystem === undefined ? "" : unit.unitSystem.fullName,
|
|
201
|
+
};
|
|
202
|
+
}
|
|
203
|
+
const invertsUnit = await unit.invertsUnit;
|
|
204
|
+
if (!invertsUnit)
|
|
205
|
+
return new BadUnit();
|
|
177
206
|
return {
|
|
178
207
|
name: unit.fullName,
|
|
179
208
|
label: unit.label ?? "",
|
|
180
|
-
phenomenon:
|
|
209
|
+
phenomenon: invertsUnit.phenomenon ? invertsUnit.phenomenon.fullName : "",
|
|
181
210
|
isValid: true,
|
|
182
211
|
system: unit.unitSystem === undefined ? "" : unit.unitSystem.fullName,
|
|
183
212
|
};
|
|
@@ -187,22 +216,33 @@ export class SchemaUnitProvider {
|
|
|
187
216
|
* @internal
|
|
188
217
|
*/
|
|
189
218
|
async findUnitByDisplayLabel(displayLabel, schemaName, phenomenon, unitSystem) {
|
|
219
|
+
// TODO: Known bug: This only looks through loaded schemas. If schema name is provided, we can attempt to load that schema
|
|
190
220
|
const schemaItems = this._context.getSchemaItems();
|
|
191
221
|
let { value, done } = schemaItems.next();
|
|
192
222
|
while (!done) {
|
|
193
223
|
if (Unit.isUnit(value) && value.label?.toLowerCase() === displayLabel) {
|
|
224
|
+
// TODO: this can be optimized. We don't have to await these if we don't want to check for them
|
|
194
225
|
const currPhenomenon = await value.phenomenon;
|
|
195
226
|
const currUnitSystem = await value.unitSystem;
|
|
196
227
|
if (!schemaName || value.schema.name.toLowerCase() === schemaName)
|
|
197
228
|
if (!phenomenon || (currPhenomenon && currPhenomenon.key.matchesFullName(phenomenon)))
|
|
198
229
|
if (!unitSystem || (currUnitSystem && currUnitSystem.key.matchesFullName(unitSystem)))
|
|
199
|
-
return value;
|
|
230
|
+
return this.getUnitsProps(value);
|
|
231
|
+
}
|
|
232
|
+
else if (InvertedUnit.isInvertedUnit(value) && value.label?.toLowerCase() === displayLabel && value.invertsUnit) {
|
|
233
|
+
const invertsUnit = await value.invertsUnit;
|
|
234
|
+
if (invertsUnit) {
|
|
235
|
+
const currPhenomenon = await invertsUnit.phenomenon;
|
|
236
|
+
const currUnitSystem = await invertsUnit.unitSystem;
|
|
237
|
+
if (!schemaName || value.schema.name.toLowerCase() === schemaName)
|
|
238
|
+
if (!phenomenon || (currPhenomenon && currPhenomenon.key.matchesFullName(phenomenon)))
|
|
239
|
+
if (!unitSystem || (currUnitSystem && currUnitSystem.key.matchesFullName(unitSystem)))
|
|
240
|
+
return this.getUnitsProps(value);
|
|
241
|
+
}
|
|
200
242
|
}
|
|
201
243
|
({ value, done } = schemaItems.next());
|
|
202
244
|
}
|
|
203
|
-
|
|
204
|
-
return { displayLabel };
|
|
205
|
-
});
|
|
245
|
+
return new BadUnit();
|
|
206
246
|
}
|
|
207
247
|
/**
|
|
208
248
|
* Finds Unit by altDisplayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.
|
|
@@ -213,19 +253,21 @@ export class SchemaUnitProvider {
|
|
|
213
253
|
if (entry.altDisplayLabels && entry.altDisplayLabels.length > 0) {
|
|
214
254
|
if (entry.altDisplayLabels.findIndex((ref) => ref.toLowerCase() === altDisplayLabel) !== -1) {
|
|
215
255
|
// Found altDisplayLabel that matches label to find
|
|
216
|
-
const
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
256
|
+
const unitProps = await this.findUnitByName(entry.name);
|
|
257
|
+
// If no schemaName, phenomenon, or unitSystem are provided, return unitProps
|
|
258
|
+
if (!schemaName && !phenomenon && !unitSystem)
|
|
259
|
+
return unitProps;
|
|
260
|
+
// Check if the provided values match unitProps
|
|
261
|
+
const schemaNameMatches = !schemaName || unitProps.name.toLowerCase().startsWith(schemaName);
|
|
262
|
+
const phenomenonMatches = !phenomenon || unitProps.phenomenon.toLowerCase() === phenomenon;
|
|
263
|
+
const unitSystemMatches = !unitSystem || unitProps.system.toLowerCase() === unitSystem;
|
|
264
|
+
// If all provided values match, return unitProps
|
|
265
|
+
if (schemaNameMatches && phenomenonMatches && unitSystemMatches)
|
|
266
|
+
return unitProps;
|
|
223
267
|
}
|
|
224
268
|
}
|
|
225
269
|
}
|
|
226
|
-
|
|
227
|
-
return { altDisplayLabel };
|
|
228
|
-
});
|
|
270
|
+
return new BadUnit();
|
|
229
271
|
}
|
|
230
272
|
}
|
|
231
273
|
//# sourceMappingURL=SchemaUnitProvider.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"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EAAE,UAAmB,EAAE,UAAmB,EAAE,UAAmB;QAC5H,KAAK,MAAM,KAAK,IAAI,IAAI,CAAC,cAAc,EAAE,CAAC;YACxC,IAAI,KAAK,CAAC,gBAAgB,IAAI,KAAK,CAAC,gBAAgB,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;gBAChE,IAAI,KAAK,CAAC,gBAAgB,CAAC,SAAS,CAAC,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,CAAC,WAAW,EAAE,KAAK,eAAe,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC;oBACpG,mDAAmD;oBACnD,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,gBAAgB,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;oBACrD,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC;oBAC9C,MAAM,eAAe,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC;oBAC9C,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,WAAW,EAAE,KAAK,UAAU;wBAC9D,IAAI,CAAC,UAAU,IAAI,CAAC,eAAe,IAAI,eAAe,CAAC,GAAG,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;4BACrF,IAAI,CAAC,UAAU,IAAI,CAAC,eAAe,IAAI,eAAe,CAAC,GAAG,CAAC,eAAe,CAAC,UAAU,CAAC,CAAC;gCACrF,OAAO,IAAI,CAAC;gBACpB,CAAC;YACH,CAAC;QACH,CAAC;QAED,MAAM,IAAI,YAAY,CAAC,aAAa,CAAC,KAAK,EAAE,+CAA+C,EAAE,GAAG,EAAE;YAChG,OAAO,EAAE,eAAe,EAAE,CAAC;QAC7B,CAAC,CAAC,CAAC;IACL,CAAC;CACF","sourcesContent":["/*---------------------------------------------------------------------------------------------\r\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\r\n* See LICENSE.md in the project root for license terms and full copyright notice.\r\n*--------------------------------------------------------------------------------------------*/\r\nimport { BentleyError, BentleyStatus } from \"@itwin/core-bentley\";\r\nimport { UnitConversionProps, UnitExtraData, UnitProps, UnitsProvider } from \"@itwin/core-quantity\";\r\nimport { ISchemaLocater, SchemaContext } from \"../Context\";\r\nimport { SchemaItem } from \"../Metadata/SchemaItem\";\r\nimport { SchemaItemKey, SchemaKey } from \"../SchemaKey\";\r\nimport { Unit } from \"../Metadata/Unit\";\r\nimport { SchemaItemType } from \"../ECObjects\";\r\nimport { UnitConverter } from \"../UnitConversion/UnitConverter\";\r\n\r\n/**\r\n * Class used to find Units in SchemaContext by attributes such as Phenomenon and DisplayLabel.\r\n * @beta\r\n */\r\nexport class SchemaUnitProvider implements UnitsProvider {\r\n private _unitConverter: UnitConverter;\r\n private _context: SchemaContext;\r\n\r\n /**\r\n *\r\n * @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext\r\n * class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be\r\n * created and the locater will be added.\r\n * @param _unitExtraData Additional data like alternate display label not found in Units Schema to match with Units; Defaults to empty array.\r\n */\r\n constructor(contextOrLocater: ISchemaLocater, private _unitExtraData: UnitExtraData[] = []){\r\n if (contextOrLocater instanceof SchemaContext) {\r\n this._context = contextOrLocater;\r\n } else {\r\n this._context = new SchemaContext();\r\n this._context.addLocater(contextOrLocater);\r\n }\r\n this._unitConverter = new UnitConverter(this._context);\r\n }\r\n\r\n /**\r\n * Find unit in a schema that has unitName.\r\n * @param unitName Full name of unit.\r\n * @returns UnitProps interface from @itwin/core-quantity whose name matches unitName.\r\n */\r\n public async findUnitByName(unitName: string): Promise<UnitProps> {\r\n const unit = await this.findECUnitByName(unitName);\r\n return this.getUnitsProps(unit);\r\n }\r\n\r\n /**\r\n * Find all units in context that belongs to given phenomenon.\r\n * @param phenomenon Full name of phenomenon.\r\n * @returns Array of UnitProps (from @itwin/core-quantity) interface objects whose name matches phenomenon param.\r\n */\r\n public async getUnitsByFamily(phenomenon: string): Promise<Array<UnitProps>> {\r\n // Check if schema exists and phenomenon exists in schema\r\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(phenomenon);\r\n const schemaKey = new SchemaKey(schemaName);\r\n const schema = await this._context.getSchema(schemaKey);\r\n\r\n if (!schema) {\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema for phenomenon\", () => {\r\n return { phenomenon, schema: schemaName };\r\n });\r\n }\r\n\r\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\r\n const phenom = await this._context.getSchemaItem(itemKey);\r\n if (!phenom)\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema item/phenomenon\", () => {\r\n return { item: schemaItemName, schema: schemaName };\r\n });\r\n\r\n if (phenom.schemaItemType !== SchemaItemType.Phenomenon)\r\n throw new BentleyError(BentleyStatus.ERROR, \"Item is not a phenomenon\", () => {\r\n return { itemType: phenom.key.fullName };\r\n });\r\n\r\n // Find units' full name that match given phenomenon param.\r\n const filteredUnits: Array<UnitProps> = [];\r\n const schemaItems = this._context.getSchemaItems();\r\n let { value, done } = schemaItems.next();\r\n while (!done) {\r\n if (Unit.isUnit(value)) {\r\n const foundPhenomenon = await value.phenomenon;\r\n if (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)) {\r\n const unitProps = this.getUnitsProps(value);\r\n filteredUnits.push(unitProps);\r\n }\r\n }\r\n ({ value, done } = schemaItems.next());\r\n }\r\n\r\n return filteredUnits;\r\n }\r\n\r\n /**\r\n * Find alternate display labels associated with unitName, if any.\r\n * @param unitName Full name of Unit.\r\n */\r\n public getAlternateDisplayLabels(unitName: string): Array<string> {\r\n let alternateLabels: Array<string> = [];\r\n for (const entry of this._unitExtraData) {\r\n if (entry.name.toLowerCase() === unitName.toLowerCase()) {\r\n alternateLabels = entry.altDisplayLabels;\r\n }\r\n }\r\n\r\n return alternateLabels;\r\n }\r\n\r\n /**\r\n * Finds Unit by unitLabel, which could be a display label in the schema or alternate an display label defined in\r\n * this._unitExtraData. If there are duplicates of the same display label in the context or teh same alternate display\r\n * labels, specify schemaName, phenomenon, or unitSystem to get a specific unit.\r\n *\r\n * @param unitLabel Display label or alternate display label to query unit by.\r\n * @param schemaName Ensure Unit with unitLabel belongs to Schema with schemaName.\r\n * @param phenomenon Full name of phenomenon that Unit belongs to.\r\n * @param unitSystem Full name of unitSystem that Unit belongs to.\r\n * @returns The UnitProps interface from the @itwin/core-quantity package.\r\n */\r\n public async findUnit(unitLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<UnitProps> {\r\n const findLabel = unitLabel.toLowerCase();\r\n const findSchema = schemaName ? schemaName.toLowerCase() : undefined;\r\n const findPhenomenon = phenomenon ? phenomenon.toLowerCase() : undefined;\r\n const findUnitSystem = unitSystem ? unitSystem.toLowerCase() : undefined;\r\n let foundUnit: Unit | undefined;\r\n\r\n try {\r\n try {\r\n foundUnit = await this.findUnitByDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);\r\n } catch (err) {\r\n // If there is no Unit with display label that matches label, then check for alternate display labels that may match\r\n foundUnit = await this.findUnitByAltDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);\r\n }\r\n } catch (err) {\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find unit with label\", () => {\r\n return { unitLabel };\r\n });\r\n }\r\n\r\n return this.getUnitsProps(foundUnit);\r\n }\r\n\r\n /**\r\n * Gets the @itwin/core-quantity UnitConversionProps for the given fromUnit and toUnit.\r\n * @param fromUnit The UnitProps of the 'from' unit.\r\n * @param toUnit The UnitProps of the 'to' unit.\r\n * @returns The UnitConversionProps interface from the @itwin/core-quantity package.\r\n */\r\n public async getConversion(fromUnit: UnitProps, toUnit: UnitProps): Promise<UnitConversionProps> {\r\n const conversion = await this._unitConverter.calculateConversion(fromUnit.name, toUnit.name);\r\n return {\r\n factor: conversion.factor,\r\n offset: conversion.offset,\r\n };\r\n }\r\n\r\n /**\r\n * Find unit in a schema that has unitName.\r\n * @param unitName Full name of unit.\r\n * @returns Unit whose full name matches unitName.\r\n */\r\n private async findECUnitByName(unitName: string): Promise<Unit> {\r\n // Check if schema exists and unit exists in schema\r\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(unitName);\r\n const schemaKey = new SchemaKey(schemaName);\r\n const schema = await this._context.getSchema(schemaKey);\r\n\r\n if (!schema) {\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema for unit\", () => {\r\n return { schema: schemaName, unit: unitName };\r\n });\r\n }\r\n\r\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\r\n const item = await this._context.getSchemaItem<Unit>(itemKey);\r\n if (!item)\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema item/unit\", () => {\r\n return { item: schemaItemName, schema: schemaName };\r\n });\r\n\r\n if (item.schemaItemType === SchemaItemType.Unit)\r\n return item;\r\n\r\n throw new BentleyError(BentleyStatus.ERROR, \"Item is not a unit\", () => {\r\n return { itemType: item.key.fullName };\r\n });\r\n }\r\n\r\n /**\r\n * Gets the @itwin/core UnitProps for the given Unit.\r\n * @param unit The Unit to convert.\r\n * @returns UnitProps interface from @itwin/core.\r\n */\r\n private getUnitsProps(unit: Unit): UnitProps {\r\n return {\r\n name: unit.fullName,\r\n label: unit.label ?? \"\",\r\n phenomenon: unit.phenomenon ? unit.phenomenon.fullName : \"\",\r\n isValid: true,\r\n system: unit.unitSystem === undefined ? \"\" : unit.unitSystem.fullName,\r\n };\r\n }\r\n\r\n /**\r\n * Finds Unit by displayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.\r\n * @internal\r\n */\r\n private async findUnitByDisplayLabel(displayLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<Unit> {\r\n const schemaItems = this._context.getSchemaItems();\r\n let { value, done } = schemaItems.next();\r\n while (!done) {\r\n if (Unit.isUnit(value) && value.label?.toLowerCase() === displayLabel) {\r\n const currPhenomenon = await value.phenomenon;\r\n const currUnitSystem = await value.unitSystem;\r\n if (!schemaName || value.schema.name.toLowerCase() === schemaName)\r\n if (!phenomenon || (currPhenomenon && currPhenomenon.key.matchesFullName(phenomenon)))\r\n if (!unitSystem || (currUnitSystem && currUnitSystem.key.matchesFullName(unitSystem)))\r\n return value;\r\n }\r\n ({ value, done } = schemaItems.next());\r\n }\r\n\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find unit with display label\", () => {\r\n return { displayLabel };\r\n });\r\n }\r\n\r\n /**\r\n * Finds Unit by altDisplayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.\r\n * @internal\r\n */\r\n private async findUnitByAltDisplayLabel(altDisplayLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<Unit> {\r\n for (const entry of this._unitExtraData) {\r\n if (entry.altDisplayLabels && entry.altDisplayLabels.length > 0) {\r\n if (entry.altDisplayLabels.findIndex((ref: string) => ref.toLowerCase() === altDisplayLabel) !== -1) {\r\n // Found altDisplayLabel that matches label to find\r\n const unit = await this.findECUnitByName(entry.name);\r\n const foundPhenomenon = await unit.phenomenon;\r\n const foundUnitSystem = await unit.unitSystem;\r\n if (!schemaName || unit.schema.name.toLowerCase() === schemaName)\r\n if (!phenomenon || (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)))\r\n if (!unitSystem || (foundUnitSystem && foundUnitSystem.key.matchesFullName(unitSystem)))\r\n return unit;\r\n }\r\n }\r\n }\r\n\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find unit with alternate display label\", () => {\r\n return { altDisplayLabel };\r\n });\r\n }\r\n}\r\n"]}
|
|
1
|
+
{"version":3,"file":"SchemaUnitProvider.js","sourceRoot":"","sources":["../../../src/UnitProvider/SchemaUnitProvider.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAClE,OAAO,EAAE,OAAO,EAAE,oBAAoB,EAAgE,MAAM,sBAAsB,CAAC;AACnI,OAAO,EAAkB,aAAa,E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------------------\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\nimport { BentleyError, BentleyStatus } from \"@itwin/core-bentley\";\r\nimport { BadUnit, UnitConversionInvert, UnitConversionProps, UnitExtraData, UnitProps, UnitsProvider } from \"@itwin/core-quantity\";\r\nimport { ISchemaLocater, SchemaContext } from \"../Context\";\r\nimport { SchemaItem } from \"../Metadata/SchemaItem\";\r\nimport { SchemaItemKey, SchemaKey } from \"../SchemaKey\";\r\nimport { Unit } from \"../Metadata/Unit\";\r\nimport { SchemaItemType } from \"../ECObjects\";\r\nimport { UnitConverter } from \"../UnitConversion/UnitConverter\";\r\nimport { InvertedUnit } from \"../Metadata/InvertedUnit\";\r\n\r\n/**\r\n * Class used to find Units in SchemaContext by attributes such as Phenomenon and DisplayLabel.\r\n * @beta\r\n */\r\nexport class SchemaUnitProvider implements UnitsProvider {\r\n private _unitConverter: UnitConverter;\r\n private _context: SchemaContext;\r\n\r\n /**\r\n *\r\n * @param contextOrLocater The SchemaContext or a different ISchemaLocater implementation used to retrieve the schema. The SchemaContext\r\n * class implements the ISchemaLocater interface. If the provided locater is not a SchemaContext instance a new SchemaContext will be\r\n * created and the locater will be added.\r\n * @param _unitExtraData Additional data like alternate display label not found in Units Schema to match with Units; Defaults to empty array.\r\n */\r\n constructor(contextOrLocater: ISchemaLocater, private _unitExtraData: UnitExtraData[] = []) {\r\n if (contextOrLocater instanceof SchemaContext) {\r\n this._context = contextOrLocater;\r\n } else {\r\n this._context = new SchemaContext();\r\n this._context.addLocater(contextOrLocater);\r\n }\r\n this._unitConverter = new UnitConverter(this._context);\r\n }\r\n\r\n /**\r\n * Find unit in a schema that has unitName.\r\n * @param unitName Full name of unit.\r\n * @returns UnitProps interface from @itwin/core-quantity whose name matches unitName.\r\n */\r\n public async findUnitByName(unitName: string): Promise<UnitProps> {\r\n // Check if schema exists and unit exists in schema\r\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(unitName);\r\n const schemaKey = new SchemaKey(schemaName);\r\n const schema = await this._context.getSchema(schemaKey);\r\n\r\n if (!schema) {\r\n return new BadUnit(); // return BadUnit if schema does not exist\r\n }\r\n\r\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\r\n const unit = await this._context.getSchemaItem<Unit>(itemKey);\r\n if (unit && unit.schemaItemType === SchemaItemType.Unit)\r\n return this.getUnitsProps(unit);\r\n\r\n const invertedUnit = await this._context.getSchemaItem<InvertedUnit>(itemKey);\r\n if (invertedUnit && invertedUnit.schemaItemType === SchemaItemType.InvertedUnit) {\r\n return this.getUnitsProps(invertedUnit);\r\n }\r\n\r\n return new BadUnit();\r\n }\r\n\r\n /**\r\n * Find all units in context that belongs to given phenomenon.\r\n * @param phenomenon Full name of phenomenon.\r\n * @returns Array of UnitProps (from @itwin/core-quantity) interface objects whose name matches phenomenon param.\r\n */\r\n public async getUnitsByFamily(phenomenon: string): Promise<Array<UnitProps>> {\r\n // Check if schema exists and phenomenon exists in schema\r\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(phenomenon);\r\n const schemaKey = new SchemaKey(schemaName);\r\n const schema = await this._context.getSchema(schemaKey);\r\n\r\n if (!schema) {\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema for phenomenon\", () => {\r\n return { phenomenon, schema: schemaName };\r\n });\r\n }\r\n\r\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\r\n const phenom = await this._context.getSchemaItem(itemKey);\r\n if (!phenom)\r\n throw new BentleyError(BentleyStatus.ERROR, \"Cannot find schema item/phenomenon\", () => {\r\n return { item: schemaItemName, schema: schemaName };\r\n });\r\n\r\n if (phenom.schemaItemType !== SchemaItemType.Phenomenon)\r\n throw new BentleyError(BentleyStatus.ERROR, \"Item is not a phenomenon\", () => {\r\n return { itemType: phenom.key.fullName };\r\n });\r\n\r\n // Find units' full name that match given phenomenon param.\r\n const filteredUnits: Array<UnitProps> = [];\r\n const schemaItems = this._context.getSchemaItems();\r\n let { value, done } = schemaItems.next();\r\n while (!done) {\r\n if (Unit.isUnit(value)) {\r\n const foundPhenomenon = await value.phenomenon;\r\n if (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)) {\r\n const unitProps = await this.getUnitsProps(value);\r\n filteredUnits.push(unitProps);\r\n }\r\n } else if (InvertedUnit.isInvertedUnit(value) && value.invertsUnit) {\r\n const invertsUnit = await value.invertsUnit;\r\n if (invertsUnit) {\r\n const foundPhenomenon = await invertsUnit.phenomenon;\r\n if (foundPhenomenon && foundPhenomenon.key.matchesFullName(phenomenon)) {\r\n const unitProps = await this.getUnitsProps(value);\r\n filteredUnits.push(unitProps);\r\n }\r\n }\r\n }\r\n ({ value, done } = schemaItems.next());\r\n }\r\n\r\n return filteredUnits;\r\n }\r\n\r\n /**\r\n * Find alternate display labels associated with unitName, if any.\r\n * @param unitName Full name of Unit.\r\n */\r\n public getAlternateDisplayLabels(unitName: string): Array<string> {\r\n let alternateLabels: Array<string> = [];\r\n for (const entry of this._unitExtraData) {\r\n if (entry.name.toLowerCase() === unitName.toLowerCase()) {\r\n alternateLabels = entry.altDisplayLabels;\r\n }\r\n }\r\n\r\n return alternateLabels;\r\n }\r\n\r\n /**\r\n * Finds Unit by unitLabel, which could be a display label in the schema or alternate an display label defined in\r\n * this._unitExtraData. If there are duplicates of the same display label in the context or teh same alternate display\r\n * labels, specify schemaName, phenomenon, or unitSystem to get a specific unit.\r\n *\r\n * @param unitLabel Display label or alternate display label to query unit by.\r\n * @param schemaName Ensure Unit with unitLabel belongs to Schema with schemaName.\r\n * @param phenomenon Full name of phenomenon that Unit belongs to.\r\n * @param unitSystem Full name of unitSystem that Unit belongs to.\r\n * @returns The UnitProps interface from the @itwin/core-quantity package.\r\n */\r\n public async findUnit(unitLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<UnitProps> {\r\n const findLabel = unitLabel.toLowerCase();\r\n const findSchema = schemaName ? schemaName.toLowerCase() : undefined;\r\n const findPhenomenon = phenomenon ? phenomenon.toLowerCase() : undefined;\r\n const findUnitSystem = unitSystem ? unitSystem.toLowerCase() : undefined;\r\n\r\n const foundUnit: UnitProps = await this.findUnitByDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);\r\n if (foundUnit.isValid)\r\n return foundUnit;\r\n\r\n // If there is no Unit with display label that matches label, then check for alternate display labels that may match\r\n return this.findUnitByAltDisplayLabel(findLabel, findSchema, findPhenomenon, findUnitSystem);\r\n }\r\n\r\n /**\r\n * Gets the @itwin/core-quantity UnitConversionProps for the given fromUnit and toUnit.\r\n * @param fromUnit The UnitProps of the 'from' unit.\r\n * @param toUnit The UnitProps of the 'to' unit.\r\n * @returns The UnitConversionProps interface from the @itwin/core-quantity package.\r\n */\r\n public async getConversion(fromUnit: UnitProps, toUnit: UnitProps): Promise<UnitConversionProps> {\r\n // need to check if either side is an inverted unit. The UnitConverter can only handle Units\r\n if (!fromUnit.isValid || !toUnit.isValid)\r\n throw new BentleyError(BentleyStatus.ERROR, \"Both provided units must be valid.\", () => {\r\n return { fromUnit, toUnit };\r\n });\r\n\r\n const { unitName: fromUnitName, isInverted: fromIsInverted } = await this.checkUnitPropsForConversion(fromUnit, this._context);\r\n const { unitName: toUnitName, isInverted: toIsInverted } = await this.checkUnitPropsForConversion(toUnit, this._context);\r\n\r\n const conversion = await this._unitConverter.calculateConversion(fromUnitName, toUnitName);\r\n const result: UnitConversionProps = {\r\n factor: conversion.factor,\r\n offset: conversion.offset,\r\n };\r\n if (fromIsInverted && !toIsInverted)\r\n result.inversion = UnitConversionInvert.InvertPreConversion;\r\n else if (!fromIsInverted && toIsInverted)\r\n result.inversion = UnitConversionInvert.InvertPostConversion;\r\n\r\n return result;\r\n }\r\n\r\n private async checkUnitPropsForConversion(input: UnitProps, context: SchemaContext): Promise<{ unitName: string, isInverted: boolean }> {\r\n const [schemaName, schemaItemName] = SchemaItem.parseFullName(input.name);\r\n const schemaKey = new SchemaKey(schemaName);\r\n const schema = await context.getSchema(schemaKey);\r\n\r\n if (!schema) {\r\n throw new BentleyError(BentleyStatus.ERROR, \"Could not obtain schema for unit.\", () => {\r\n return { name: input.name };\r\n });\r\n }\r\n\r\n const itemKey = new SchemaItemKey(schemaItemName, schema.schemaKey);\r\n const invertedUnit = await context.getSchemaItem<InvertedUnit>(itemKey);\r\n // Check if we found an item, the item is an inverted unit, and it has its invertsUnit property set\r\n if (invertedUnit && InvertedUnit.isInvertedUnit(invertedUnit) && invertedUnit.invertsUnit) {\r\n return { unitName: invertedUnit.invertsUnit.fullName, isInverted: true };\r\n }\r\n\r\n return { unitName: input.name, isInverted: false };\r\n }\r\n\r\n /**\r\n * Gets the @itwin/core UnitProps for the given Unit.\r\n * @param unit The Unit to convert.\r\n * @returns UnitProps interface from @itwin/core.\r\n */\r\n private async getUnitsProps(unit: Unit | InvertedUnit): Promise<UnitProps> {\r\n if (Unit.isUnit(unit)) {\r\n return {\r\n name: unit.fullName,\r\n label: unit.label ?? \"\",\r\n phenomenon: unit.phenomenon ? unit.phenomenon.fullName : \"\",\r\n isValid: true,\r\n system: unit.unitSystem === undefined ? \"\" : unit.unitSystem.fullName,\r\n };\r\n }\r\n\r\n const invertsUnit = await unit.invertsUnit;\r\n if (!invertsUnit)\r\n return new BadUnit();\r\n\r\n return {\r\n name: unit.fullName,\r\n label: unit.label ?? \"\",\r\n phenomenon: invertsUnit.phenomenon ? invertsUnit.phenomenon.fullName : \"\",\r\n isValid: true,\r\n system: unit.unitSystem === undefined ? \"\" : unit.unitSystem.fullName,\r\n };\r\n }\r\n\r\n /**\r\n * Finds Unit by displayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.\r\n * @internal\r\n */\r\n private async findUnitByDisplayLabel(displayLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<UnitProps> {\r\n // TODO: Known bug: This only looks through loaded schemas. If schema name is provided, we can attempt to load that schema\r\n const schemaItems = this._context.getSchemaItems();\r\n let { value, done } = schemaItems.next();\r\n while (!done) {\r\n if (Unit.isUnit(value) && value.label?.toLowerCase() === displayLabel) {\r\n // TODO: this can be optimized. We don't have to await these if we don't want to check for them\r\n const currPhenomenon = await value.phenomenon;\r\n const currUnitSystem = await value.unitSystem;\r\n if (!schemaName || value.schema.name.toLowerCase() === schemaName)\r\n if (!phenomenon || (currPhenomenon && currPhenomenon.key.matchesFullName(phenomenon)))\r\n if (!unitSystem || (currUnitSystem && currUnitSystem.key.matchesFullName(unitSystem)))\r\n return this.getUnitsProps(value);\r\n } else if (InvertedUnit.isInvertedUnit(value) && value.label?.toLowerCase() === displayLabel && value.invertsUnit) {\r\n const invertsUnit = await value.invertsUnit;\r\n if (invertsUnit) {\r\n const currPhenomenon = await invertsUnit.phenomenon;\r\n const currUnitSystem = await invertsUnit.unitSystem;\r\n if (!schemaName || value.schema.name.toLowerCase() === schemaName)\r\n if (!phenomenon || (currPhenomenon && currPhenomenon.key.matchesFullName(phenomenon)))\r\n if (!unitSystem || (currUnitSystem && currUnitSystem.key.matchesFullName(unitSystem)))\r\n return this.getUnitsProps(value);\r\n }\r\n }\r\n ({ value, done } = schemaItems.next());\r\n }\r\n\r\n return new BadUnit();\r\n }\r\n\r\n /**\r\n * Finds Unit by altDisplayLabel and that it belongs to schemaName, phenomenon, and unitSystem if defined.\r\n * @internal\r\n */\r\n private async findUnitByAltDisplayLabel(altDisplayLabel: string, schemaName?: string, phenomenon?: string, unitSystem?: string): Promise<UnitProps> {\r\n for (const entry of this._unitExtraData) {\r\n if (entry.altDisplayLabels && entry.altDisplayLabels.length > 0) {\r\n if (entry.altDisplayLabels.findIndex((ref: string) => ref.toLowerCase() === altDisplayLabel) !== -1) {\r\n // Found altDisplayLabel that matches label to find\r\n const unitProps = await this.findUnitByName(entry.name);\r\n // If no schemaName, phenomenon, or unitSystem are provided, return unitProps\r\n if (!schemaName && !phenomenon && !unitSystem)\r\n return unitProps;\r\n\r\n // Check if the provided values match unitProps\r\n const schemaNameMatches = !schemaName || unitProps.name.toLowerCase().startsWith(schemaName);\r\n const phenomenonMatches = !phenomenon || unitProps.phenomenon.toLowerCase() === phenomenon;\r\n const unitSystemMatches = !unitSystem || unitProps.system.toLowerCase() === unitSystem;\r\n // If all provided values match, return unitProps\r\n if (schemaNameMatches && phenomenonMatches && unitSystemMatches)\r\n return unitProps;\r\n }\r\n }\r\n }\r\n return new BadUnit();\r\n }\r\n}\r\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@itwin/ecschema-metadata",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "5.0.0-dev.1",
|
|
4
4
|
"description": "ECObjects core concepts in typescript",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"main": "lib/cjs/ecschema-metadata.js",
|
|
@@ -24,7 +24,7 @@
|
|
|
24
24
|
},
|
|
25
25
|
"devDependencies": {
|
|
26
26
|
"@bentley/units-schema": "^1.0.8",
|
|
27
|
-
"@itwin/eslint-plugin": "
|
|
27
|
+
"@itwin/eslint-plugin": "5.0.0-dev.1",
|
|
28
28
|
"@types/benchmark": "^2.1.0",
|
|
29
29
|
"@types/chai": "4.3.1",
|
|
30
30
|
"@types/chai-as-promised": "^7",
|
|
@@ -36,19 +36,19 @@
|
|
|
36
36
|
"chai": "^4.3.10",
|
|
37
37
|
"chai-as-promised": "^7.1.1",
|
|
38
38
|
"cpx2": "^3.0.0",
|
|
39
|
-
"eslint": "^
|
|
39
|
+
"eslint": "^9.13.0",
|
|
40
40
|
"mocha": "^10.2.0",
|
|
41
41
|
"nyc": "^15.1.0",
|
|
42
42
|
"rimraf": "^3.0.2",
|
|
43
43
|
"sinon": "^17.0.2",
|
|
44
|
-
"typescript": "~5.
|
|
45
|
-
"@itwin/build-tools": "
|
|
46
|
-
"@itwin/core-bentley": "
|
|
47
|
-
"@itwin/core-quantity": "
|
|
44
|
+
"typescript": "~5.6.2",
|
|
45
|
+
"@itwin/build-tools": "5.0.0-dev.1",
|
|
46
|
+
"@itwin/core-bentley": "5.0.0-dev.1",
|
|
47
|
+
"@itwin/core-quantity": "5.0.0-dev.1"
|
|
48
48
|
},
|
|
49
49
|
"peerDependencies": {
|
|
50
|
-
"@itwin/core-bentley": "^
|
|
51
|
-
"@itwin/core-quantity": "^
|
|
50
|
+
"@itwin/core-bentley": "^5.0.0-dev.1",
|
|
51
|
+
"@itwin/core-quantity": "^5.0.0-dev.1"
|
|
52
52
|
},
|
|
53
53
|
"nyc": {
|
|
54
54
|
"extends": "./node_modules/@itwin/build-tools/.nycrc"
|
|
@@ -60,7 +60,7 @@
|
|
|
60
60
|
"clean": "rimraf lib .rush/temp/package-deps*.json",
|
|
61
61
|
"copy:test-assets": "cpx \"./src/test/assets/**/*\" ./lib/cjs/test/assets",
|
|
62
62
|
"extract-api": "betools extract-api --entry=ecschema-metadata",
|
|
63
|
-
"lint": "eslint
|
|
63
|
+
"lint": "eslint \"./src/**/*.ts\" 1>&2",
|
|
64
64
|
"test": "mocha",
|
|
65
65
|
"docs": "betools docs --includes=../../generated-docs/extract --json=../../generated-docs/core/ecschema-metadata/file.json --tsIndexFile=./ecschema-metadata.ts --onlyJson",
|
|
66
66
|
"cover": "nyc npm -s test",
|