@itwin/core-quantity 5.10.0-dev.1 → 5.10.0-dev.3
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/lib/cjs/BasicUnitsProvider.d.ts +44 -0
- package/lib/cjs/BasicUnitsProvider.d.ts.map +1 -0
- package/lib/cjs/BasicUnitsProvider.js +241 -0
- package/lib/cjs/BasicUnitsProvider.js.map +1 -0
- package/lib/cjs/CompositeUnitsProvider.d.ts +45 -0
- package/lib/cjs/CompositeUnitsProvider.d.ts.map +1 -0
- package/lib/cjs/CompositeUnitsProvider.js +111 -0
- package/lib/cjs/CompositeUnitsProvider.js.map +1 -0
- package/lib/cjs/Formatter/FormatterSpec.d.ts.map +1 -1
- package/lib/cjs/Formatter/FormatterSpec.js +14 -0
- package/lib/cjs/Formatter/FormatterSpec.js.map +1 -1
- package/lib/cjs/Interfaces.d.ts +14 -0
- package/lib/cjs/Interfaces.d.ts.map +1 -1
- package/lib/cjs/Interfaces.js.map +1 -1
- package/lib/cjs/Parser.d.ts.map +1 -1
- package/lib/cjs/Parser.js +16 -1
- package/lib/cjs/Parser.js.map +1 -1
- package/lib/cjs/ParserSpec.d.ts.map +1 -1
- package/lib/cjs/ParserSpec.js +15 -2
- package/lib/cjs/ParserSpec.js.map +1 -1
- package/lib/cjs/QuantityLoggerCategory.d.ts +3 -1
- package/lib/cjs/QuantityLoggerCategory.d.ts.map +1 -1
- package/lib/cjs/QuantityLoggerCategory.js +2 -0
- package/lib/cjs/QuantityLoggerCategory.js.map +1 -1
- package/lib/cjs/SerializedUnitSchema.d.ts +98 -0
- package/lib/cjs/SerializedUnitSchema.d.ts.map +1 -0
- package/lib/cjs/SerializedUnitSchema.js +25 -0
- package/lib/cjs/SerializedUnitSchema.js.map +1 -0
- package/lib/cjs/UnitConversion/Graph.d.ts +35 -0
- package/lib/cjs/UnitConversion/Graph.d.ts.map +1 -0
- package/lib/cjs/UnitConversion/Graph.js +87 -0
- package/lib/cjs/UnitConversion/Graph.js.map +1 -0
- package/lib/cjs/UnitConversion/Parser.d.ts +9 -0
- package/lib/cjs/UnitConversion/Parser.d.ts.map +1 -0
- package/lib/cjs/UnitConversion/Parser.js +42 -0
- package/lib/cjs/UnitConversion/Parser.js.map +1 -0
- package/lib/cjs/UnitConversion/UnitConversion.d.ts +56 -0
- package/lib/cjs/UnitConversion/UnitConversion.d.ts.map +1 -0
- package/lib/cjs/UnitConversion/UnitConversion.js +80 -0
- package/lib/cjs/UnitConversion/UnitConversion.js.map +1 -0
- package/lib/cjs/UnitConversion/UnitDefinitionResolver.d.ts +29 -0
- package/lib/cjs/UnitConversion/UnitDefinitionResolver.d.ts.map +1 -0
- package/lib/cjs/UnitConversion/UnitDefinitionResolver.js +102 -0
- package/lib/cjs/UnitConversion/UnitDefinitionResolver.js.map +1 -0
- package/lib/cjs/UnitConversion/nameUtils.d.ts +11 -0
- package/lib/cjs/UnitConversion/nameUtils.d.ts.map +1 -0
- package/lib/cjs/UnitConversion/nameUtils.js +25 -0
- package/lib/cjs/UnitConversion/nameUtils.js.map +1 -0
- package/lib/cjs/assets/Units.json +4215 -0
- package/lib/cjs/core-quantity.d.ts +15 -1
- package/lib/cjs/core-quantity.d.ts.map +1 -1
- package/lib/cjs/core-quantity.js +17 -1
- package/lib/cjs/core-quantity.js.map +1 -1
- package/lib/cjs/internal/cross-package.d.ts +4 -0
- package/lib/cjs/internal/cross-package.d.ts.map +1 -0
- package/lib/cjs/internal/cross-package.js +13 -0
- package/lib/cjs/internal/cross-package.js.map +1 -0
- package/lib/esm/BasicUnitsProvider.d.ts +44 -0
- package/lib/esm/BasicUnitsProvider.d.ts.map +1 -0
- package/lib/esm/BasicUnitsProvider.js +213 -0
- package/lib/esm/BasicUnitsProvider.js.map +1 -0
- package/lib/esm/CompositeUnitsProvider.d.ts +45 -0
- package/lib/esm/CompositeUnitsProvider.d.ts.map +1 -0
- package/lib/esm/CompositeUnitsProvider.js +108 -0
- package/lib/esm/CompositeUnitsProvider.js.map +1 -0
- package/lib/esm/Formatter/FormatterSpec.d.ts.map +1 -1
- package/lib/esm/Formatter/FormatterSpec.js +14 -0
- package/lib/esm/Formatter/FormatterSpec.js.map +1 -1
- package/lib/esm/Interfaces.d.ts +14 -0
- package/lib/esm/Interfaces.d.ts.map +1 -1
- package/lib/esm/Interfaces.js.map +1 -1
- package/lib/esm/Parser.d.ts.map +1 -1
- package/lib/esm/Parser.js +16 -1
- package/lib/esm/Parser.js.map +1 -1
- package/lib/esm/ParserSpec.d.ts.map +1 -1
- package/lib/esm/ParserSpec.js +15 -2
- package/lib/esm/ParserSpec.js.map +1 -1
- package/lib/esm/QuantityLoggerCategory.d.ts +3 -1
- package/lib/esm/QuantityLoggerCategory.d.ts.map +1 -1
- package/lib/esm/QuantityLoggerCategory.js +2 -0
- package/lib/esm/QuantityLoggerCategory.js.map +1 -1
- package/lib/esm/SerializedUnitSchema.d.ts +98 -0
- package/lib/esm/SerializedUnitSchema.d.ts.map +1 -0
- package/lib/esm/SerializedUnitSchema.js +22 -0
- package/lib/esm/SerializedUnitSchema.js.map +1 -0
- package/lib/esm/UnitConversion/Graph.d.ts +35 -0
- package/lib/esm/UnitConversion/Graph.d.ts.map +1 -0
- package/lib/esm/UnitConversion/Graph.js +83 -0
- package/lib/esm/UnitConversion/Graph.js.map +1 -0
- package/lib/esm/UnitConversion/Parser.d.ts +9 -0
- package/lib/esm/UnitConversion/Parser.d.ts.map +1 -0
- package/lib/esm/UnitConversion/Parser.js +39 -0
- package/lib/esm/UnitConversion/Parser.js.map +1 -0
- package/lib/esm/UnitConversion/UnitConversion.d.ts +56 -0
- package/lib/esm/UnitConversion/UnitConversion.d.ts.map +1 -0
- package/lib/esm/UnitConversion/UnitConversion.js +76 -0
- package/lib/esm/UnitConversion/UnitConversion.js.map +1 -0
- package/lib/esm/UnitConversion/UnitDefinitionResolver.d.ts +29 -0
- package/lib/esm/UnitConversion/UnitDefinitionResolver.d.ts.map +1 -0
- package/lib/esm/UnitConversion/UnitDefinitionResolver.js +98 -0
- package/lib/esm/UnitConversion/UnitDefinitionResolver.js.map +1 -0
- package/lib/esm/UnitConversion/nameUtils.d.ts +11 -0
- package/lib/esm/UnitConversion/nameUtils.d.ts.map +1 -0
- package/lib/esm/UnitConversion/nameUtils.js +21 -0
- package/lib/esm/UnitConversion/nameUtils.js.map +1 -0
- package/lib/esm/assets/Units.json +4215 -0
- package/lib/esm/core-quantity.d.ts +15 -1
- package/lib/esm/core-quantity.d.ts.map +1 -1
- package/lib/esm/core-quantity.js +15 -1
- package/lib/esm/core-quantity.js.map +1 -1
- package/lib/esm/internal/cross-package.d.ts +4 -0
- package/lib/esm/internal/cross-package.d.ts.map +1 -0
- package/lib/esm/internal/cross-package.js +8 -0
- package/lib/esm/internal/cross-package.js.map +1 -0
- package/package.json +6 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"QuantityLoggerCategory.js","sourceRoot":"","sources":["../../src/QuantityLoggerCategory.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAEH;;;GAGG;AACH,MAAM,CAAN,IAAY,
|
|
1
|
+
{"version":3,"file":"QuantityLoggerCategory.js","sourceRoot":"","sources":["../../src/QuantityLoggerCategory.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F;;GAEG;AAEH;;;GAGG;AACH,MAAM,CAAN,IAAY,sBAOX;AAPD,WAAY,sBAAsB;IAChC,uDAAuD;IACvD,mDAAyB,CAAA;IACzB,0DAA0D;IAC1D,iEAAuC,CAAA;IACvC,uDAAuD;IACvD,2DAAiC,CAAA;AACnC,CAAC,EAPW,sBAAsB,KAAtB,sBAAsB,QAOjC","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 Logging\r\n */\r\n\r\n/** Logger categories used by the `@itwin/core-quantity` package.\r\n * @see [Logger]($bentley)\r\n * @beta\r\n */\r\nexport enum QuantityLoggerCategory {\r\n /** The root category for the core-quantity package. */\r\n Package = \"core-quantity\",\r\n /** Logger category for quantity formatting operations. */\r\n Formatting = \"core-quantity.Formatting\",\r\n /** Logger category for quantity parsing operations. */\r\n Parsing = \"core-quantity.Parsing\",\r\n}\r\n"]}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/** Current version of the serialization format for `SerializedUnitSchema`.
|
|
2
|
+
*
|
|
3
|
+
* This value is written into `Units.json` as the `version` field and checked at
|
|
4
|
+
* parse time. Two version axes exist:
|
|
5
|
+
*
|
|
6
|
+
* - **Format version** (`SERIALIZED_UNIT_SCHEMA_VERSION` / `Units.json.version`): bump
|
|
7
|
+
* the major version when the shape of the `SerializedUnitSchema` interfaces changes
|
|
8
|
+
* incompatibly (e.g. renaming fields, removing required properties). Minor bumps for
|
|
9
|
+
* backward-compatible additions.
|
|
10
|
+
*
|
|
11
|
+
* - **Source provenance** (`sourceEcSchemaVersion` inside `Units.json`): records which
|
|
12
|
+
* version of the BIS Units EC schema the data was derived from (e.g. `"01.00.09"`).
|
|
13
|
+
* This is a traceability marker, not a runtime contract.
|
|
14
|
+
*
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
export declare const SERIALIZED_UNIT_SCHEMA_VERSION = "01.00.00";
|
|
18
|
+
/** A serialized constant definition (e.g. `PI`, `MILLI`).
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export interface SerializedConstant {
|
|
22
|
+
readonly schemaItemType: "Constant";
|
|
23
|
+
readonly label?: string;
|
|
24
|
+
readonly description?: string;
|
|
25
|
+
/** The phenomenon this constant belongs to. */
|
|
26
|
+
readonly phenomenon: string;
|
|
27
|
+
/** The unit definition expression (e.g. `"M*M"`). */
|
|
28
|
+
readonly definition: string;
|
|
29
|
+
/** Numerator of the conversion factor to the SI base. */
|
|
30
|
+
readonly numerator?: number;
|
|
31
|
+
/** Denominator of the conversion factor to the SI base. */
|
|
32
|
+
readonly denominator?: number;
|
|
33
|
+
}
|
|
34
|
+
/** A serialized unit definition (e.g. `M`, `FT`, `CELSIUS`).
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
export interface SerializedUnit {
|
|
38
|
+
readonly schemaItemType: "Unit";
|
|
39
|
+
readonly label?: string;
|
|
40
|
+
readonly description?: string;
|
|
41
|
+
readonly phenomenon: string;
|
|
42
|
+
readonly unitSystem: string;
|
|
43
|
+
/** The unit definition expression (e.g. `"M*M"`). */
|
|
44
|
+
readonly definition: string;
|
|
45
|
+
/** Numerator of the conversion factor to the SI base. */
|
|
46
|
+
readonly numerator?: number;
|
|
47
|
+
/** Denominator of the conversion factor to the SI base. */
|
|
48
|
+
readonly denominator?: number;
|
|
49
|
+
/** Additive offset for the conversion (e.g. temperature scales). */
|
|
50
|
+
readonly offset?: number;
|
|
51
|
+
}
|
|
52
|
+
/** A serialized inverted unit definition (e.g. `HORIZONTAL_PER_VERTICAL`).
|
|
53
|
+
* @internal
|
|
54
|
+
*/
|
|
55
|
+
export interface SerializedInvertedUnit {
|
|
56
|
+
readonly schemaItemType: "InvertedUnit";
|
|
57
|
+
readonly label?: string;
|
|
58
|
+
readonly description?: string;
|
|
59
|
+
readonly invertsUnit: string;
|
|
60
|
+
readonly unitSystem: string;
|
|
61
|
+
}
|
|
62
|
+
/** A serialized unit system definition (e.g. `SI`, `METRIC`).
|
|
63
|
+
* @internal
|
|
64
|
+
*/
|
|
65
|
+
export interface SerializedUnitSystem {
|
|
66
|
+
readonly schemaItemType: "UnitSystem";
|
|
67
|
+
readonly label?: string;
|
|
68
|
+
readonly description?: string;
|
|
69
|
+
}
|
|
70
|
+
/** A serialized phenomenon definition (e.g. `LENGTH`, `TEMPERATURE`).
|
|
71
|
+
* @internal
|
|
72
|
+
*/
|
|
73
|
+
export interface SerializedPhenomenon {
|
|
74
|
+
readonly schemaItemType: "Phenomenon";
|
|
75
|
+
readonly label?: string;
|
|
76
|
+
readonly description?: string;
|
|
77
|
+
/** The phenomenon definition expression. */
|
|
78
|
+
readonly definition: string;
|
|
79
|
+
}
|
|
80
|
+
/** Discriminated union of all serialized schema item types.
|
|
81
|
+
* @internal
|
|
82
|
+
*/
|
|
83
|
+
export type SerializedUnitItem = SerializedConstant | SerializedUnit | SerializedInvertedUnit | SerializedUnitSystem | SerializedPhenomenon;
|
|
84
|
+
/** Versioned container for a serialized BIS unit schema. The `items` map is keyed by schema-item name (unqualified).
|
|
85
|
+
* @internal
|
|
86
|
+
*/
|
|
87
|
+
export interface SerializedUnitSchema {
|
|
88
|
+
/** Serialization format version, matching `SERIALIZED_UNIT_SCHEMA_VERSION` (e.g. `"01.00.00"`). */
|
|
89
|
+
readonly version: string;
|
|
90
|
+
readonly name: string;
|
|
91
|
+
readonly alias: string;
|
|
92
|
+
/** EC schema version of the source BIS Units schema this data was derived from (e.g. `"01.00.09"`). */
|
|
93
|
+
readonly sourceEcSchemaVersion: string;
|
|
94
|
+
readonly items: {
|
|
95
|
+
readonly [name: string]: SerializedUnitItem;
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=SerializedUnitSchema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SerializedUnitSchema.d.ts","sourceRoot":"","sources":["../../src/SerializedUnitSchema.ts"],"names":[],"mappings":"AAKA;;;;;;;;;;;;;;;GAeG;AACH,eAAO,MAAM,8BAA8B,aAAa,CAAC;AAEzD;;GAEG;AACH,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,cAAc,EAAE,UAAU,CAAC;IACpC,QAAQ,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,+CAA+C;IAC/C,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,qDAAqD;IACrD,QAAQ,CAAC,UAAU,EAAE,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Q,CAAC,KAAK,CAAC,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,WAAW,CAAC,EAAE,MAAM,CAAC;IAC9B,4CAA4C;IAC5C,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;CAC7B;AAED;;GAEG;AACH,MAAM,MAAM,kBAAkB,GAAG,kBAAkB,GAAG,cAAc,GAAG,sBAAsB,GAAG,oBAAoB,GAAG,oBAAoB,CAAC;AAE5I;;GAEG;AACH,MAAM,WAAW,oBAAoB;IACnC,mGAAmG;IACnG,QAAQ,CAAC,OAAO,EAAE,MAAM,CAAC;IACzB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,uGAAuG;IACvG,QAAQ,CAAC,qBAAqB,EAAE,MAAM,CAAC;IACvC,QAAQ,CAAC,KAAK,EAAE;QAAE,QAAQ,EAAE,IAAI,EAAE,MAAM,GAAG,kBAAkB,CAAA;KAAE,CAAC;CACjE"}
|
|
@@ -0,0 +1,22 @@
|
|
|
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
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
/** Current version of the serialization format for `SerializedUnitSchema`.
|
|
6
|
+
*
|
|
7
|
+
* This value is written into `Units.json` as the `version` field and checked at
|
|
8
|
+
* parse time. Two version axes exist:
|
|
9
|
+
*
|
|
10
|
+
* - **Format version** (`SERIALIZED_UNIT_SCHEMA_VERSION` / `Units.json.version`): bump
|
|
11
|
+
* the major version when the shape of the `SerializedUnitSchema` interfaces changes
|
|
12
|
+
* incompatibly (e.g. renaming fields, removing required properties). Minor bumps for
|
|
13
|
+
* backward-compatible additions.
|
|
14
|
+
*
|
|
15
|
+
* - **Source provenance** (`sourceEcSchemaVersion` inside `Units.json`): records which
|
|
16
|
+
* version of the BIS Units EC schema the data was derived from (e.g. `"01.00.09"`).
|
|
17
|
+
* This is a traceability marker, not a runtime contract.
|
|
18
|
+
*
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
export const SERIALIZED_UNIT_SCHEMA_VERSION = "01.00.00";
|
|
22
|
+
//# sourceMappingURL=SerializedUnitSchema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"SerializedUnitSchema.js","sourceRoot":"","sources":["../../src/SerializedUnitSchema.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAE/F;;;;;;;;;;;;;;;GAeG;AACH,MAAM,CAAC,MAAM,8BAA8B,GAAG,UAAU,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\r\n/** Current version of the serialization format for `SerializedUnitSchema`.\r\n *\r\n * This value is written into `Units.json` as the `version` field and checked at\r\n * parse time. Two version axes exist:\r\n *\r\n * - **Format version** (`SERIALIZED_UNIT_SCHEMA_VERSION` / `Units.json.version`): bump\r\n * the major version when the shape of the `SerializedUnitSchema` interfaces changes\r\n * incompatibly (e.g. renaming fields, removing required properties). Minor bumps for\r\n * backward-compatible additions.\r\n *\r\n * - **Source provenance** (`sourceEcSchemaVersion` inside `Units.json`): records which\r\n * version of the BIS Units EC schema the data was derived from (e.g. `\"01.00.09\"`).\r\n * This is a traceability marker, not a runtime contract.\r\n *\r\n * @internal\r\n */\r\nexport const SERIALIZED_UNIT_SCHEMA_VERSION = \"01.00.00\";\r\n\r\n/** A serialized constant definition (e.g. `PI`, `MILLI`).\r\n * @internal\r\n */\r\nexport interface SerializedConstant {\r\n readonly schemaItemType: \"Constant\";\r\n readonly label?: string;\r\n readonly description?: string;\r\n /** The phenomenon this constant belongs to. */\r\n readonly phenomenon: string;\r\n /** The unit definition expression (e.g. `\"M*M\"`). */\r\n readonly definition: string;\r\n /** Numerator of the conversion factor to the SI base. */\r\n readonly numerator?: number;\r\n /** Denominator of the conversion factor to the SI base. */\r\n readonly denominator?: number;\r\n}\r\n\r\n/** A serialized unit definition (e.g. `M`, `FT`, `CELSIUS`).\r\n * @internal\r\n */\r\nexport interface SerializedUnit {\r\n readonly schemaItemType: \"Unit\";\r\n readonly label?: string;\r\n readonly description?: string;\r\n readonly phenomenon: string;\r\n readonly unitSystem: string;\r\n /** The unit definition expression (e.g. `\"M*M\"`). */\r\n readonly definition: string;\r\n /** Numerator of the conversion factor to the SI base. */\r\n readonly numerator?: number;\r\n /** Denominator of the conversion factor to the SI base. */\r\n readonly denominator?: number;\r\n /** Additive offset for the conversion (e.g. temperature scales). */\r\n readonly offset?: number;\r\n}\r\n\r\n/** A serialized inverted unit definition (e.g. `HORIZONTAL_PER_VERTICAL`).\r\n * @internal\r\n */\r\nexport interface SerializedInvertedUnit {\r\n readonly schemaItemType: \"InvertedUnit\";\r\n readonly label?: string;\r\n readonly description?: string;\r\n readonly invertsUnit: string;\r\n readonly unitSystem: string;\r\n}\r\n\r\n/** A serialized unit system definition (e.g. `SI`, `METRIC`).\r\n * @internal\r\n */\r\nexport interface SerializedUnitSystem {\r\n readonly schemaItemType: \"UnitSystem\";\r\n readonly label?: string;\r\n readonly description?: string;\r\n}\r\n\r\n/** A serialized phenomenon definition (e.g. `LENGTH`, `TEMPERATURE`).\r\n * @internal\r\n */\r\nexport interface SerializedPhenomenon {\r\n readonly schemaItemType: \"Phenomenon\";\r\n readonly label?: string;\r\n readonly description?: string;\r\n /** The phenomenon definition expression. */\r\n readonly definition: string;\r\n}\r\n\r\n/** Discriminated union of all serialized schema item types.\r\n * @internal\r\n */\r\nexport type SerializedUnitItem = SerializedConstant | SerializedUnit | SerializedInvertedUnit | SerializedUnitSystem | SerializedPhenomenon;\r\n\r\n/** Versioned container for a serialized BIS unit schema. The `items` map is keyed by schema-item name (unqualified).\r\n * @internal\r\n */\r\nexport interface SerializedUnitSchema {\r\n /** Serialization format version, matching `SERIALIZED_UNIT_SCHEMA_VERSION` (e.g. `\"01.00.00\"`). */\r\n readonly version: string;\r\n readonly name: string;\r\n readonly alias: string;\r\n /** EC schema version of the source BIS Units schema this data was derived from (e.g. `\"01.00.09\"`). */\r\n readonly sourceEcSchemaVersion: string;\r\n readonly items: { readonly [name: string]: SerializedUnitItem };\r\n}\r\n"]}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/** @internal */
|
|
2
|
+
export declare class UnitConversionGraph<T> {
|
|
3
|
+
private _edgeKeyDelim;
|
|
4
|
+
private _label;
|
|
5
|
+
private _nodeCount;
|
|
6
|
+
private _edgeCount;
|
|
7
|
+
private _nodes;
|
|
8
|
+
private _edgeObjs;
|
|
9
|
+
private _edgeLabels;
|
|
10
|
+
private _outEdges;
|
|
11
|
+
constructor();
|
|
12
|
+
setGraph: (label: string) => UnitConversionGraph<T>;
|
|
13
|
+
graph: () => string;
|
|
14
|
+
nodeCount: () => number;
|
|
15
|
+
nodes: () => string[];
|
|
16
|
+
setNode: (nodeKey: string, nodeValue: T) => void;
|
|
17
|
+
node: (nodeKey: string) => T;
|
|
18
|
+
hasNode: (nodeKey: string) => boolean;
|
|
19
|
+
edgeCount: () => number;
|
|
20
|
+
edges: () => {
|
|
21
|
+
v: string;
|
|
22
|
+
w: string;
|
|
23
|
+
}[];
|
|
24
|
+
setEdge: (v: string, w: string, value: {
|
|
25
|
+
exponent: number;
|
|
26
|
+
}) => void;
|
|
27
|
+
edge: (v: string, w: string) => {
|
|
28
|
+
exponent: number;
|
|
29
|
+
};
|
|
30
|
+
outEdges: (v: string) => {
|
|
31
|
+
v: string;
|
|
32
|
+
w: string;
|
|
33
|
+
}[];
|
|
34
|
+
}
|
|
35
|
+
//# sourceMappingURL=Graph.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Graph.d.ts","sourceRoot":"","sources":["../../../src/UnitConversion/Graph.ts"],"names":[],"mappings":"AA8CA,gBAAgB;AAChB,qBAAa,mBAAmB,CAAC,CAAC;IAChC,OAAO,CAAC,aAAa,CAAU;IAC/B,OAAO,CAAC,MAAM,CAAM;IACpB,OAAO,CAAC,UAAU,CAAK;IACvB,OAAO,CAAC,UAAU,CAAK;IACvB,OAAO,CAAC,MAAM,CAAc;IAC5B,OAAO,CAAC,SAAS,CAAc;IAC/B,OAAO,CAAC,WAAW,CAAgB;IACnC,OAAO,CAAC,SAAS,CAAc;;IASxB,QAAQ,UAAW,MAAM,KAAG,mBAAmB,CAAC,CAAC,CAAC,CAGvD;IAEK,KAAK,eAEV;IAEK,SAAS,eAEd;IAEK,KAAK,iBAEV;IAEK,OAAO,YAAa,MAAM,aAAa,CAAC,UAQ7C;IAEK,IAAI,YAAa,MAAM,OAE5B;IAEK,OAAO,YAAa,MAAM,aAE/B;IAEK,SAAS,eAEd;IAEK,KAAK;WAnFP,MAAM;WACN,MAAM;QAoFT;IAEK,OAAO,MAAO,MAAM,KAAK,MAAM,SAAS;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,UAiBjE;IAEK,IAAI,MAAO,MAAM,KAAK,MAAM;kBAhGP,MAAM;MAmGhC;IAEK,QAAQ,MAAO,MAAM;WA3FrB,MAAM;WACN,MAAM;QA8FX;CACH"}
|
|
@@ -0,0 +1,83 @@
|
|
|
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
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
// Following https://github.com/dagrejs/graphlib/blob/master/lib/graph.js
|
|
6
|
+
/** @internal */
|
|
7
|
+
export class UnitConversionGraph {
|
|
8
|
+
_edgeKeyDelim = "\x01";
|
|
9
|
+
_label = "";
|
|
10
|
+
_nodeCount = 0;
|
|
11
|
+
_edgeCount = 0;
|
|
12
|
+
_nodes;
|
|
13
|
+
_edgeObjs;
|
|
14
|
+
_edgeLabels;
|
|
15
|
+
_outEdges;
|
|
16
|
+
constructor() {
|
|
17
|
+
this._nodes = {};
|
|
18
|
+
this._edgeObjs = {};
|
|
19
|
+
this._edgeLabels = {};
|
|
20
|
+
this._outEdges = {};
|
|
21
|
+
}
|
|
22
|
+
setGraph = (label) => {
|
|
23
|
+
this._label = label;
|
|
24
|
+
return this;
|
|
25
|
+
};
|
|
26
|
+
graph = () => {
|
|
27
|
+
return this._label;
|
|
28
|
+
};
|
|
29
|
+
nodeCount = () => {
|
|
30
|
+
return this._nodeCount;
|
|
31
|
+
};
|
|
32
|
+
nodes = () => {
|
|
33
|
+
return Object.keys(this._nodes);
|
|
34
|
+
};
|
|
35
|
+
setNode = (nodeKey, nodeValue) => {
|
|
36
|
+
if (nodeKey in this._nodes) {
|
|
37
|
+
this._nodes[nodeKey] = nodeValue;
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
this._nodes[nodeKey] = nodeValue;
|
|
41
|
+
this._outEdges[nodeKey] = {};
|
|
42
|
+
++this._nodeCount;
|
|
43
|
+
};
|
|
44
|
+
node = (nodeKey) => {
|
|
45
|
+
return this._nodes[nodeKey];
|
|
46
|
+
};
|
|
47
|
+
hasNode = (nodeKey) => {
|
|
48
|
+
return nodeKey in this._nodes;
|
|
49
|
+
};
|
|
50
|
+
edgeCount = () => {
|
|
51
|
+
return this._edgeCount;
|
|
52
|
+
};
|
|
53
|
+
edges = () => {
|
|
54
|
+
return Object.values(this._edgeObjs);
|
|
55
|
+
};
|
|
56
|
+
setEdge = (v, w, value) => {
|
|
57
|
+
const edgeId = v + this._edgeKeyDelim + w + this._edgeKeyDelim;
|
|
58
|
+
if (edgeId in this._edgeLabels) {
|
|
59
|
+
// Update exponent, specific to this graph's use case
|
|
60
|
+
this._edgeLabels[edgeId].exponent += value.exponent;
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
this._edgeLabels[edgeId] = value;
|
|
64
|
+
const edgeObj = {
|
|
65
|
+
v,
|
|
66
|
+
w,
|
|
67
|
+
};
|
|
68
|
+
this._edgeObjs[edgeId] = edgeObj;
|
|
69
|
+
// setNode should have ran first, so this.outEdges[v] shouldn't be undefined
|
|
70
|
+
this._outEdges[v][edgeId] = edgeObj;
|
|
71
|
+
this._edgeCount++;
|
|
72
|
+
};
|
|
73
|
+
edge = (v, w) => {
|
|
74
|
+
const edgeId = v + this._edgeKeyDelim + w + this._edgeKeyDelim;
|
|
75
|
+
return this._edgeLabels[edgeId];
|
|
76
|
+
};
|
|
77
|
+
outEdges = (v) => {
|
|
78
|
+
const outV = this._outEdges[v];
|
|
79
|
+
const edges = Object.values(outV);
|
|
80
|
+
return edges;
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
//# sourceMappingURL=Graph.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Graph.js","sourceRoot":"","sources":["../../../src/UnitConversion/Graph.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F,yEAAyE;AA0CzE,gBAAgB;AAChB,MAAM,OAAO,mBAAmB;IACtB,aAAa,GAAG,MAAM,CAAC;IACvB,MAAM,GAAG,EAAE,CAAC;IACZ,UAAU,GAAG,CAAC,CAAC;IACf,UAAU,GAAG,CAAC,CAAC;IACf,MAAM,CAAc;IACpB,SAAS,CAAc;IACvB,WAAW,CAAgB;IAC3B,SAAS,CAAc;IAE/B;QACE,IAAI,CAAC,MAAM,GAAG,EAAE,CAAC;QACjB,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;QACpB,IAAI,CAAC,WAAW,GAAG,EAAE,CAAC;QACtB,IAAI,CAAC,SAAS,GAAG,EAAE,CAAC;IACtB,CAAC;IAEM,QAAQ,GAAG,CAAC,KAAa,EAA0B,EAAE;QAC1D,IAAI,CAAC,MAAM,GAAG,KAAK,CAAC;QACpB,OAAO,IAAI,CAAC;IACd,CAAC,CAAC;IAEK,KAAK,GAAG,GAAG,EAAE;QAClB,OAAO,IAA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,EAAE,CAAC;YAC/B,qDAAqD;YACrD,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC,QAAQ,IAAI,KAAK,CAAC,QAAQ,CAAC;YACpD,OAAO;QACT,CAAC;QAED,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,GAAG,KAAK,CAAC;QACjC,MAAM,OAAO,GAAG;YACd,CAAC;YACD,CAAC;SACF,CAAC;QACF,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC;QACjC,4EAA4E;QAC5E,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC;QACpC,IAAI,CAAC,UAAU,EAAE,CAAC;IACpB,CAAC,CAAC;IAEK,IAAI,GAAG,CAAC,CAAS,EAAE,CAAS,EAAE,EAAE;QACrC,MAAM,MAAM,GAAG,CAAC,GAAG,IAAI,CAAC,aAAa,GAAG,CAAC,GAAG,IAAI,CAAC,aAAa,CAAC;QAC/D,OAAO,IAAI,CAAC,WAAW,CAAC,MAAM,CAAC,CAAC;IAClC,CAAC,CAAC;IAEK,QAAQ,GAAG,CAAC,CAAS,EAAE,EAAE;QAC9B,MAAM,IAAI,GAAG,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QAC/B,MAAM,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;QAClC,OAAO,KAAK,CAAC;IACf,CAAC,CAAC;CACH","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// Following https://github.com/dagrejs/graphlib/blob/master/lib/graph.js\r\n\r\n/**\r\n * Using generics for nodes - T represents Unit | Constant in our use case\r\n * @internal\r\n */\r\ninterface NodesMap<T> {\r\n [node: string]: T;\r\n}\r\n\r\n/**\r\n * Describe edges based on its node endpoints (v, w)\r\n * @internal\r\n */\r\ninterface EdgeObjsMap {\r\n [edge: string]: {\r\n v: string;\r\n w: string;\r\n };\r\n}\r\n\r\n/**\r\n * Describe edges based on the exponent they carry\r\n * @internal\r\n */\r\ninterface EdgeLabelsMap {\r\n [edge: string]: { exponent: number };\r\n}\r\n\r\n/**\r\n * List each node with outgoing edges in outer map; describe each node's outgoing edges in inner map\r\n * @internal\r\n */\r\ninterface OutEdgesMap {\r\n [node: string]: {\r\n [edges: string]: {\r\n v: string;\r\n w: string;\r\n };\r\n };\r\n}\r\n\r\n/** @internal */\r\nexport class UnitConversionGraph<T> {\r\n private _edgeKeyDelim = \"\\x01\";\r\n private _label = \"\";\r\n private _nodeCount = 0;\r\n private _edgeCount = 0;\r\n private _nodes: NodesMap<T>;\r\n private _edgeObjs: EdgeObjsMap;\r\n private _edgeLabels: EdgeLabelsMap;\r\n private _outEdges: OutEdgesMap;\r\n\r\n constructor() {\r\n this._nodes = {};\r\n this._edgeObjs = {};\r\n this._edgeLabels = {};\r\n this._outEdges = {};\r\n }\r\n\r\n public setGraph = (label: string): UnitConversionGraph<T> => {\r\n this._label = label;\r\n return this;\r\n };\r\n\r\n public graph = () => {\r\n return this._label;\r\n };\r\n\r\n public nodeCount = () => {\r\n return this._nodeCount;\r\n };\r\n\r\n public nodes = () => {\r\n return Object.keys(this._nodes);\r\n };\r\n\r\n public setNode = (nodeKey: string, nodeValue: T) => {\r\n if (nodeKey in this._nodes) {\r\n this._nodes[nodeKey] = nodeValue;\r\n return;\r\n }\r\n this._nodes[nodeKey] = nodeValue;\r\n this._outEdges[nodeKey] = {};\r\n ++this._nodeCount;\r\n };\r\n\r\n public node = (nodeKey: string) => {\r\n return this._nodes[nodeKey];\r\n };\r\n\r\n public hasNode = (nodeKey: string) => {\r\n return nodeKey in this._nodes;\r\n };\r\n\r\n public edgeCount = () => {\r\n return this._edgeCount;\r\n };\r\n\r\n public edges = () => {\r\n return Object.values(this._edgeObjs);\r\n };\r\n\r\n public setEdge = (v: string, w: string, value: { exponent: number }) => {\r\n const edgeId = v + this._edgeKeyDelim + w + this._edgeKeyDelim;\r\n if (edgeId in this._edgeLabels) {\r\n // Update exponent, specific to this graph's use case\r\n this._edgeLabels[edgeId].exponent += value.exponent;\r\n return;\r\n }\r\n\r\n this._edgeLabels[edgeId] = value;\r\n const edgeObj = {\r\n v,\r\n w,\r\n };\r\n this._edgeObjs[edgeId] = edgeObj;\r\n // setNode should have ran first, so this.outEdges[v] shouldn't be undefined\r\n this._outEdges[v][edgeId] = edgeObj;\r\n this._edgeCount++;\r\n };\r\n\r\n public edge = (v: string, w: string) => {\r\n const edgeId = v + this._edgeKeyDelim + w + this._edgeKeyDelim;\r\n return this._edgeLabels[edgeId];\r\n };\r\n\r\n public outEdges = (v: string) => {\r\n const outV = this._outEdges[v];\r\n const edges = Object.values(outV);\r\n return edges;\r\n };\r\n}\r\n"]}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/** @internal */
|
|
2
|
+
export interface DefinitionFragment {
|
|
3
|
+
name: string;
|
|
4
|
+
exponent: number;
|
|
5
|
+
constant: boolean;
|
|
6
|
+
}
|
|
7
|
+
/** @internal */
|
|
8
|
+
export declare function parseDefinition(definition: string): Map<string, DefinitionFragment>;
|
|
9
|
+
//# sourceMappingURL=Parser.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Parser.d.ts","sourceRoot":"","sources":["../../../src/UnitConversion/Parser.ts"],"names":[],"mappings":"AAeA,gBAAgB;AAChB,MAAM,WAAW,kBAAkB;IACjC,IAAI,EAAE,MAAM,CAAC;IACb,QAAQ,EAAE,MAAM,CAAC;IACjB,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED,gBAAgB;AAChB,wBAAgB,eAAe,CAAC,UAAU,EAAE,MAAM,GAAG,GAAG,CAAC,MAAM,EAAE,kBAAkB,CAAC,CAsBnF"}
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
const expressionRgx = /^(([A-Z]\w*:)?([A-Z]\w*|\[([A-Z]\w*:)?[A-Z]\w*\])(\(-?\d+\))?(\*(?!$)|$))+$/i;
|
|
6
|
+
const tokenRgx = /(?:(\[)?((?:[A-Z]\w*:)?[A-Z]\w*)\]?)(?:\((-?\d+)\))?/i;
|
|
7
|
+
const sp = "*";
|
|
8
|
+
/** @internal */
|
|
9
|
+
var Tokens;
|
|
10
|
+
(function (Tokens) {
|
|
11
|
+
Tokens[Tokens["Bracket"] = 1] = "Bracket";
|
|
12
|
+
Tokens[Tokens["Word"] = 2] = "Word";
|
|
13
|
+
Tokens[Tokens["Exponent"] = 3] = "Exponent";
|
|
14
|
+
})(Tokens || (Tokens = {}));
|
|
15
|
+
/** @internal */
|
|
16
|
+
export function parseDefinition(definition) {
|
|
17
|
+
const unitMap = new Map();
|
|
18
|
+
if (expressionRgx.test(definition)) {
|
|
19
|
+
for (const unit of definition.split(sp)) {
|
|
20
|
+
const tokens = unit.split(tokenRgx);
|
|
21
|
+
const name = tokens[Tokens.Word];
|
|
22
|
+
const exponent = tokens[Tokens.Exponent] ? Number(tokens[Tokens.Exponent]) : 1;
|
|
23
|
+
const constant = tokens[Tokens.Bracket] !== undefined;
|
|
24
|
+
if (unitMap.has(name)) {
|
|
25
|
+
const currentDefinition = unitMap.get(name);
|
|
26
|
+
if (currentDefinition)
|
|
27
|
+
unitMap.set(name, { ...currentDefinition, exponent: currentDefinition.exponent + exponent });
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
unitMap.set(name, { name, exponent, constant });
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return unitMap;
|
|
34
|
+
}
|
|
35
|
+
else {
|
|
36
|
+
throw new Error("Invalid definition expression.");
|
|
37
|
+
}
|
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,CAAC,IAAI,EAAE,EAAE,GAAG,iBAAiB,EAAE,QAAQ,EAAE,iBAAiB,CAAC,QAAQ,GAAG,QAAQ,EAAE,CAAC,CAAC;YACjG,CAAC;iBAAM,CAAC;gBACN,OAAO,CAAC,GAAG,CAAC,IAAI,EAAE,EAAE,IAAI,EAAE,QAAQ,EAAE,QAAQ,EAAE,CAAC,CAAC;YAClD,CAAC;QACH,CAAC;QAED,OAAO,OAAO,CAAC;IACjB,CAAC;SAAM,CAAC;QACN,MAAM,IAAI,KAAK,CAAC,gCAAgC,CAAC,CAAC;IACpD,CAAC;AACH,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\nconst expressionRgx = /^(([A-Z]\\w*:)?([A-Z]\\w*|\\[([A-Z]\\w*:)?[A-Z]\\w*\\])(\\(-?\\d+\\))?(\\*(?!$)|$))+$/i;\r\nconst tokenRgx = /(?:(\\[)?((?:[A-Z]\\w*:)?[A-Z]\\w*)\\]?)(?:\\((-?\\d+)\\))?/i;\r\nconst sp = \"*\";\r\n\r\n/** @internal */\r\nenum Tokens {\r\n Bracket = 1,\r\n Word = 2,\r\n Exponent = 3,\r\n}\r\n\r\n/** @internal */\r\nexport interface DefinitionFragment {\r\n name: string;\r\n exponent: number;\r\n constant: boolean;\r\n}\r\n\r\n/** @internal */\r\nexport function parseDefinition(definition: string): Map<string, DefinitionFragment> {\r\n const unitMap: Map<string, DefinitionFragment> = new Map();\r\n\r\n if (expressionRgx.test(definition)) {\r\n for (const unit of definition.split(sp)) {\r\n const tokens = unit.split(tokenRgx);\r\n const name = tokens[Tokens.Word];\r\n const exponent = tokens[Tokens.Exponent] ? Number(tokens[Tokens.Exponent]) : 1;\r\n const constant = tokens[Tokens.Bracket] !== undefined;\r\n if (unitMap.has(name)) {\r\n const currentDefinition = unitMap.get(name);\r\n if (currentDefinition)\r\n unitMap.set(name, { ...currentDefinition, exponent: currentDefinition.exponent + exponent });\r\n } else {\r\n unitMap.set(name, { name, exponent, constant });\r\n }\r\n }\r\n\r\n return unitMap;\r\n } else {\r\n throw new Error(\"Invalid definition expression.\");\r\n }\r\n}\r\n"]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Structural interface satisfied by any object that carries a numerator/denominator
|
|
3
|
+
* factor and an optional offset — for example, the EC `Unit` and `Constant` classes in
|
|
4
|
+
* `@itwin/ecschema-metadata`.
|
|
5
|
+
* @internal
|
|
6
|
+
*/
|
|
7
|
+
export interface UnitConversionSource {
|
|
8
|
+
readonly numerator: number;
|
|
9
|
+
readonly denominator: number;
|
|
10
|
+
readonly offset?: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Class used for storing calculated conversion between two Units and converting values from one Unit to another.
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare class UnitConversion {
|
|
17
|
+
readonly factor: number;
|
|
18
|
+
readonly offset: number;
|
|
19
|
+
constructor(factor?: number, offset?: number);
|
|
20
|
+
/**
|
|
21
|
+
* Converts x using UnitConversion
|
|
22
|
+
* @param x Input magnitude to be converted
|
|
23
|
+
* @returns Output magnitude after conversion
|
|
24
|
+
*/
|
|
25
|
+
evaluate(x: number): number;
|
|
26
|
+
/**
|
|
27
|
+
* Used to invert source's UnitConversion so that it can be composed with target's UnitConversion cleanly
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
inverse(): UnitConversion;
|
|
31
|
+
/**
|
|
32
|
+
* Combines two UnitConversions
|
|
33
|
+
* Used to combine source's UnitConversion and target's UnitConversion for a final UnitConversion that can be evaluated
|
|
34
|
+
* @internal
|
|
35
|
+
*/
|
|
36
|
+
compose(conversion: UnitConversion): UnitConversion;
|
|
37
|
+
/**
|
|
38
|
+
* Multiples two UnitConversions together to calculate factor during reducing
|
|
39
|
+
* @internal
|
|
40
|
+
*/
|
|
41
|
+
multiply(conversion: UnitConversion): UnitConversion;
|
|
42
|
+
/**
|
|
43
|
+
* Raise UnitConversion's factor with power exponent to calculate factor during reducing
|
|
44
|
+
* @internal
|
|
45
|
+
*/
|
|
46
|
+
raise(power: number): UnitConversion;
|
|
47
|
+
/** @internal */
|
|
48
|
+
static identity: UnitConversion;
|
|
49
|
+
/**
|
|
50
|
+
* Returns UnitConversion with source's numerator and denominator in factor and source's offset in offset for reducing.
|
|
51
|
+
* Accepts any object that structurally satisfies `UnitConversionSource` (e.g. EC `Unit` or `Constant`).
|
|
52
|
+
* @internal
|
|
53
|
+
*/
|
|
54
|
+
static from(source: UnitConversionSource): UnitConversion;
|
|
55
|
+
}
|
|
56
|
+
//# sourceMappingURL=UnitConversion.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UnitConversion.d.ts","sourceRoot":"","sources":["../../../src/UnitConversion/UnitConversion.ts"],"names":[],"mappings":"AAMA;;;;;GAKG;AACH,MAAM,WAAW,oBAAoB;IACnC,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,WAAW,EAAE,MAAM,CAAC;IAC7B,QAAQ,CAAC,MAAM,CAAC,EAAE,MAAM,CAAC;CAC1B;AAED;;;GAGG;AACH,qBAAa,cAAc;aACG,MAAM,EAAE,MAAM;aAAwB,MAAM,EAAE,MAAM;gBAApD,MAAM,GAAE,MAAY,EAAkB,MAAM,GAAE,MAAY;IAEtF;;;;OAIG;IACI,QAAQ,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM;IAIlC;;;OAGG;IACI,OAAO,IAAI,cAAc;IAKhC;;;;OAIG;IACI,OAAO,CAAC,UAAU,EAAE,cAAc,GAAG,cAAc;IAO1D;;;OAGG;IACI,QAAQ,CAAC,UAAU,EAAE,cAAc,GAAG,cAAc;IAO3D;;;OAGG;IACI,KAAK,CAAC,KAAK,EAAE,MAAM,GAAG,cAAc;IAY3C,gBAAgB;IAChB,OAAc,QAAQ,iBAAwB;IAE9C;;;;OAIG;WACW,IAAI,CAAC,MAAM,EAAE,oBAAoB,GAAG,cAAc;CAKjE"}
|
|
@@ -0,0 +1,76 @@
|
|
|
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
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
import { almostEqual } from "../Quantity";
|
|
6
|
+
/**
|
|
7
|
+
* Class used for storing calculated conversion between two Units and converting values from one Unit to another.
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export class UnitConversion {
|
|
11
|
+
factor;
|
|
12
|
+
offset;
|
|
13
|
+
constructor(factor = 1.0, offset = 0.0) {
|
|
14
|
+
this.factor = factor;
|
|
15
|
+
this.offset = offset;
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Converts x using UnitConversion
|
|
19
|
+
* @param x Input magnitude to be converted
|
|
20
|
+
* @returns Output magnitude after conversion
|
|
21
|
+
*/
|
|
22
|
+
evaluate(x) {
|
|
23
|
+
return this.factor * x + this.offset;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Used to invert source's UnitConversion so that it can be composed with target's UnitConversion cleanly
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
inverse() {
|
|
30
|
+
const inverseFactor = 1.0 / this.factor;
|
|
31
|
+
return new UnitConversion(inverseFactor, -this.offset * inverseFactor);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Combines two UnitConversions
|
|
35
|
+
* Used to combine source's UnitConversion and target's UnitConversion for a final UnitConversion that can be evaluated
|
|
36
|
+
* @internal
|
|
37
|
+
*/
|
|
38
|
+
compose(conversion) {
|
|
39
|
+
return new UnitConversion(this.factor * conversion.factor, conversion.factor * this.offset + conversion.offset);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Multiples two UnitConversions together to calculate factor during reducing
|
|
43
|
+
* @internal
|
|
44
|
+
*/
|
|
45
|
+
multiply(conversion) {
|
|
46
|
+
if (almostEqual(conversion.offset, 0.0) && almostEqual(this.offset, 0.0))
|
|
47
|
+
return new UnitConversion(this.factor * conversion.factor, 0.0);
|
|
48
|
+
throw new Error("Cannot multiply two maps with non-zero offsets");
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Raise UnitConversion's factor with power exponent to calculate factor during reducing
|
|
52
|
+
* @internal
|
|
53
|
+
*/
|
|
54
|
+
raise(power) {
|
|
55
|
+
if (almostEqual(power, 1.0))
|
|
56
|
+
return new UnitConversion(this.factor, this.offset);
|
|
57
|
+
else if (almostEqual(power, 0.0))
|
|
58
|
+
return new UnitConversion(1.0, 0.0);
|
|
59
|
+
if (almostEqual(this.offset, 0.0))
|
|
60
|
+
return new UnitConversion(this.factor ** power, 0.0);
|
|
61
|
+
throw new Error("Cannot raise map with non-zero offset");
|
|
62
|
+
}
|
|
63
|
+
/** @internal */
|
|
64
|
+
static identity = new UnitConversion();
|
|
65
|
+
/**
|
|
66
|
+
* Returns UnitConversion with source's numerator and denominator in factor and source's offset in offset for reducing.
|
|
67
|
+
* Accepts any object that structurally satisfies `UnitConversionSource` (e.g. EC `Unit` or `Constant`).
|
|
68
|
+
* @internal
|
|
69
|
+
*/
|
|
70
|
+
static from(source) {
|
|
71
|
+
const offset = source.offset ?? 0;
|
|
72
|
+
const hasOffset = !almostEqual(offset, 0.0);
|
|
73
|
+
return new UnitConversion(source.denominator / source.numerator, hasOffset ? -offset : 0.0);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=UnitConversion.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UnitConversion.js","sourceRoot":"","sources":["../../../src/UnitConversion/UnitConversion.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F,OAAO,EAAE,WA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AAE,GAAG,CAAC;YAC9B,OAAO,IAAI,cAAc,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;QAEtC,IAAI,WAAW,CAAC,IAAI,CAAC,MAAM,EAAE,GAAG,CAAC;YAC/B,OAAO,IAAI,cAAc,CAAC,IAAI,CAAC,MAAM,IAAI,KAAK,EAAE,GAAG,CAAC,CAAC;QAEvD,MAAM,IAAI,KAAK,CAAC,uCAAuC,CAAC,CAAC;IAC3D,CAAC;IAED,gBAAgB;IACT,MAAM,CAAC,QAAQ,GAAG,IAAI,cAAc,EAAE,CAAC;IAE9C;;;;OAIG;IACI,MAAM,CAAC,IAAI,CAAC,MAA4B;QAC7C,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC;QAClC,MAAM,SAAS,GAAG,CAAC,WAAW,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;QAC5C,OAAO,IAAI,cAAc,CAAC,MAAM,CAAC,WAAW,GAAG,MAAM,CAAC,SAAS,EAAE,SAAS,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;IAC9F,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\nimport { almostEqual } from \"../Quantity\";\r\n\r\n/**\r\n * Structural interface satisfied by any object that carries a numerator/denominator\r\n * factor and an optional offset — for example, the EC `Unit` and `Constant` classes in\r\n * `@itwin/ecschema-metadata`.\r\n * @internal\r\n */\r\nexport interface UnitConversionSource {\r\n readonly numerator: number;\r\n readonly denominator: number;\r\n readonly offset?: number;\r\n}\r\n\r\n/**\r\n * Class used for storing calculated conversion between two Units and converting values from one Unit to another.\r\n * @internal\r\n */\r\nexport class UnitConversion {\r\n constructor(public readonly factor: number = 1.0, public readonly offset: number = 0.0) {}\r\n\r\n /**\r\n * Converts x using UnitConversion\r\n * @param x Input magnitude to be converted\r\n * @returns Output magnitude after conversion\r\n */\r\n public evaluate(x: number): number {\r\n return this.factor * x + this.offset;\r\n }\r\n\r\n /**\r\n * Used to invert source's UnitConversion so that it can be composed with target's UnitConversion cleanly\r\n * @internal\r\n */\r\n public inverse(): UnitConversion {\r\n const inverseFactor = 1.0 / this.factor;\r\n return new UnitConversion(inverseFactor, -this.offset * inverseFactor);\r\n }\r\n\r\n /**\r\n * Combines two UnitConversions\r\n * Used to combine source's UnitConversion and target's UnitConversion for a final UnitConversion that can be evaluated\r\n * @internal\r\n */\r\n public compose(conversion: UnitConversion): UnitConversion {\r\n return new UnitConversion(\r\n this.factor * conversion.factor,\r\n conversion.factor * this.offset + conversion.offset,\r\n );\r\n }\r\n\r\n /**\r\n * Multiples two UnitConversions together to calculate factor during reducing\r\n * @internal\r\n */\r\n public multiply(conversion: UnitConversion): UnitConversion {\r\n if (almostEqual(conversion.offset, 0.0) && almostEqual(this.offset, 0.0))\r\n return new UnitConversion(this.factor * conversion.factor, 0.0);\r\n\r\n throw new Error(\"Cannot multiply two maps with non-zero offsets\");\r\n }\r\n\r\n /**\r\n * Raise UnitConversion's factor with power exponent to calculate factor during reducing\r\n * @internal\r\n */\r\n public raise(power: number): UnitConversion {\r\n if (almostEqual(power, 1.0))\r\n return new UnitConversion(this.factor, this.offset);\r\n else if (almostEqual(power, 0.0))\r\n return new UnitConversion(1.0, 0.0);\r\n\r\n if (almostEqual(this.offset, 0.0))\r\n return new UnitConversion(this.factor ** power, 0.0);\r\n\r\n throw new Error(\"Cannot raise map with non-zero offset\");\r\n }\r\n\r\n /** @internal */\r\n public static identity = new UnitConversion();\r\n\r\n /**\r\n * Returns UnitConversion with source's numerator and denominator in factor and source's offset in offset for reducing.\r\n * Accepts any object that structurally satisfies `UnitConversionSource` (e.g. EC `Unit` or `Constant`).\r\n * @internal\r\n */\r\n public static from(source: UnitConversionSource): UnitConversion {\r\n const offset = source.offset ?? 0;\r\n const hasOffset = !almostEqual(offset, 0.0);\r\n return new UnitConversion(source.denominator / source.numerator, hasOffset ? -offset : 0.0);\r\n }\r\n}\r\n"]}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import { type SerializedUnitSchema } from "../SerializedUnitSchema";
|
|
2
|
+
import { UnitConversion } from "./UnitConversion";
|
|
3
|
+
/** Resolved unit entry containing the unit's name and its conversion to the base of its phenomenon.
|
|
4
|
+
* @internal
|
|
5
|
+
*/
|
|
6
|
+
export interface ResolvedUnit {
|
|
7
|
+
readonly name: string;
|
|
8
|
+
readonly label: string;
|
|
9
|
+
readonly phenomenon: string;
|
|
10
|
+
readonly unitSystem: string;
|
|
11
|
+
readonly conversion: UnitConversion;
|
|
12
|
+
}
|
|
13
|
+
/** Resolves every unit in a `SerializedUnitSchema` to a `UnitConversion` relative to its phenomenon's base unit.
|
|
14
|
+
* @internal
|
|
15
|
+
*/
|
|
16
|
+
export declare class UnitDefinitionResolver {
|
|
17
|
+
private readonly _schema;
|
|
18
|
+
private readonly _cache;
|
|
19
|
+
constructor(schema: SerializedUnitSchema);
|
|
20
|
+
/** Resolve all Unit items in the schema and return a map from item name to `ResolvedUnit`. */
|
|
21
|
+
resolveAll(): Map<string, ResolvedUnit>;
|
|
22
|
+
/** Resolve a single unit by unqualified name, returning its conversion to base. */
|
|
23
|
+
private _resolveUnit;
|
|
24
|
+
private _resolveConstant;
|
|
25
|
+
private _resolveUnitItem;
|
|
26
|
+
/** Parse and resolve a compound definition string like `[MILLI]*M` or `IN`. */
|
|
27
|
+
private _resolveDefinition;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=UnitDefinitionResolver.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"UnitDefinitionResolver.d.ts","sourceRoot":"","sources":["../../../src/UnitConversion/UnitDefinitionResolver.ts"],"names":[],"mappings":"AAIA,OAAO,EAAgF,KAAK,oBAAoB,EAAE,MAAM,yBAAyB,CAAC;AAClJ,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAMlD;;GAEG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC5B,QAAQ,CAAC,UAAU,EAAE,cAAc,CAAC;CACrC;AAED;;GAEG;AACH,qBAAa,sBAAsB;IACjC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAuB;IAC/C,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAqC;gBAEhD,MAAM,EAAE,oBAAoB;IAMxC,8FAA8F;IACvF,UAAU,IAAI,GAAG,CAAC,MAAM,EAAE,YAAY,CAAC;IAmB9C,mFAAmF;IACnF,OAAO,CAAC,YAAY;IA0BpB,OAAO,CAAC,gBAAgB;IAcxB,OAAO,CAAC,gBAAgB;IAexB,+EAA+E;IAC/E,OAAO,CAAC,kBAAkB;CAc3B"}
|
|
@@ -0,0 +1,98 @@
|
|
|
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
|
+
*--------------------------------------------------------------------------------------------*/
|
|
5
|
+
import { SERIALIZED_UNIT_SCHEMA_VERSION } from "../SerializedUnitSchema";
|
|
6
|
+
import { UnitConversion } from "./UnitConversion";
|
|
7
|
+
import { parseDefinition } from "./Parser";
|
|
8
|
+
import { stripAliasPrefix } from "./nameUtils";
|
|
9
|
+
const MAX_RESOLUTION_DEPTH = 30;
|
|
10
|
+
/** Resolves every unit in a `SerializedUnitSchema` to a `UnitConversion` relative to its phenomenon's base unit.
|
|
11
|
+
* @internal
|
|
12
|
+
*/
|
|
13
|
+
export class UnitDefinitionResolver {
|
|
14
|
+
_schema;
|
|
15
|
+
_cache = new Map();
|
|
16
|
+
constructor(schema) {
|
|
17
|
+
if (schema.version !== SERIALIZED_UNIT_SCHEMA_VERSION)
|
|
18
|
+
throw new Error(`Unsupported Units.json version "${schema.version}". Expected "${SERIALIZED_UNIT_SCHEMA_VERSION}".`);
|
|
19
|
+
this._schema = schema;
|
|
20
|
+
}
|
|
21
|
+
/** Resolve all Unit items in the schema and return a map from item name to `ResolvedUnit`. */
|
|
22
|
+
resolveAll() {
|
|
23
|
+
const result = new Map();
|
|
24
|
+
for (const [name, item] of Object.entries(this._schema.items)) {
|
|
25
|
+
if (item.schemaItemType === "Unit") {
|
|
26
|
+
const conversion = this._resolveUnit(name, 0);
|
|
27
|
+
result.set(name, {
|
|
28
|
+
name,
|
|
29
|
+
label: item.label ?? name,
|
|
30
|
+
phenomenon: item.phenomenon,
|
|
31
|
+
unitSystem: item.unitSystem,
|
|
32
|
+
conversion,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
/** Resolve a single unit by unqualified name, returning its conversion to base. */
|
|
39
|
+
_resolveUnit(name, depth) {
|
|
40
|
+
if (depth > MAX_RESOLUTION_DEPTH)
|
|
41
|
+
throw new Error(`Unit resolution depth exceeded ${MAX_RESOLUTION_DEPTH} for "${name}"`);
|
|
42
|
+
const cached = this._cache.get(name);
|
|
43
|
+
if (cached)
|
|
44
|
+
return cached;
|
|
45
|
+
const item = this._schema.items[name];
|
|
46
|
+
if (!item)
|
|
47
|
+
throw new Error(`Unknown schema item: "${name}"`);
|
|
48
|
+
let conversion;
|
|
49
|
+
if (item.schemaItemType === "Constant") {
|
|
50
|
+
conversion = this._resolveConstant(name, item, depth);
|
|
51
|
+
}
|
|
52
|
+
else if (item.schemaItemType === "Unit") {
|
|
53
|
+
conversion = this._resolveUnitItem(name, item, depth);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
throw new Error(`Cannot resolve item of type "${item.schemaItemType}": "${name}"`);
|
|
57
|
+
}
|
|
58
|
+
this._cache.set(name, conversion);
|
|
59
|
+
return conversion;
|
|
60
|
+
}
|
|
61
|
+
_resolveConstant(name, item, depth) {
|
|
62
|
+
// A constant is identity if its definition is its own name
|
|
63
|
+
if (item.definition === name)
|
|
64
|
+
return UnitConversion.identity;
|
|
65
|
+
const selfConv = UnitConversion.from({
|
|
66
|
+
numerator: item.numerator ?? 1,
|
|
67
|
+
denominator: item.denominator ?? 1,
|
|
68
|
+
});
|
|
69
|
+
const defConv = this._resolveDefinition(item.definition, depth + 1);
|
|
70
|
+
return defConv.compose(selfConv);
|
|
71
|
+
}
|
|
72
|
+
_resolveUnitItem(name, item, depth) {
|
|
73
|
+
// A unit is a base unit if its definition is its own name
|
|
74
|
+
if (item.definition === name)
|
|
75
|
+
return UnitConversion.identity;
|
|
76
|
+
const selfConv = UnitConversion.from({
|
|
77
|
+
numerator: item.numerator ?? 1,
|
|
78
|
+
denominator: item.denominator ?? 1,
|
|
79
|
+
offset: item.offset,
|
|
80
|
+
});
|
|
81
|
+
const defConv = this._resolveDefinition(item.definition, depth + 1);
|
|
82
|
+
return defConv.compose(selfConv);
|
|
83
|
+
}
|
|
84
|
+
/** Parse and resolve a compound definition string like `[MILLI]*M` or `IN`. */
|
|
85
|
+
_resolveDefinition(definition, depth) {
|
|
86
|
+
const fragments = parseDefinition(definition);
|
|
87
|
+
let result;
|
|
88
|
+
for (const [, fragment] of fragments) {
|
|
89
|
+
// Strip alias prefix if present (definitions in the schema use unqualified names)
|
|
90
|
+
const fragName = stripAliasPrefix(fragment.name);
|
|
91
|
+
const fragConv = this._resolveUnit(fragName, depth + 1);
|
|
92
|
+
const raised = fragConv.raise(fragment.exponent);
|
|
93
|
+
result = result ? result.multiply(raised) : raised;
|
|
94
|
+
}
|
|
95
|
+
return result ?? UnitConversion.identity;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
//# sourceMappingURL=UnitDefinitionResolver.js.map
|