@itwin/core-quantity 5.10.0-dev.9 → 5.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/CHANGELOG.md +31 -1
  2. package/README.md +17 -0
  3. package/lib/cjs/BasicUnitsProvider.d.ts +8 -5
  4. package/lib/cjs/BasicUnitsProvider.d.ts.map +1 -1
  5. package/lib/cjs/BasicUnitsProvider.js +16 -139
  6. package/lib/cjs/BasicUnitsProvider.js.map +1 -1
  7. package/lib/cjs/Exception.d.ts +2 -1
  8. package/lib/cjs/Exception.d.ts.map +1 -1
  9. package/lib/cjs/Exception.js +1 -0
  10. package/lib/cjs/Exception.js.map +1 -1
  11. package/lib/cjs/FormatSpecHandle.d.ts +1 -2
  12. package/lib/cjs/FormatSpecHandle.d.ts.map +1 -1
  13. package/lib/cjs/FormatSpecHandle.js +0 -1
  14. package/lib/cjs/FormatSpecHandle.js.map +1 -1
  15. package/lib/cjs/Quantity.d.ts +4 -0
  16. package/lib/cjs/Quantity.d.ts.map +1 -1
  17. package/lib/cjs/Quantity.js +12 -23
  18. package/lib/cjs/Quantity.js.map +1 -1
  19. package/lib/cjs/UnitConversion/UnitConversion.js +6 -6
  20. package/lib/cjs/UnitConversion/UnitConversion.js.map +1 -1
  21. package/lib/cjs/UnitConversions.d.ts +40 -0
  22. package/lib/cjs/UnitConversions.d.ts.map +1 -0
  23. package/lib/cjs/UnitConversions.js +121 -0
  24. package/lib/cjs/UnitConversions.js.map +1 -0
  25. package/lib/cjs/assets/Units.json +72 -1
  26. package/lib/cjs/core-quantity.d.ts +6 -0
  27. package/lib/cjs/core-quantity.d.ts.map +1 -1
  28. package/lib/cjs/core-quantity.js +6 -0
  29. package/lib/cjs/core-quantity.js.map +1 -1
  30. package/lib/cjs/generated/Units.generated.d.ts +785 -0
  31. package/lib/cjs/generated/Units.generated.d.ts.map +1 -0
  32. package/lib/cjs/generated/Units.generated.js +775 -0
  33. package/lib/cjs/generated/Units.generated.js.map +1 -0
  34. package/lib/cjs/internal/BasicUnitConversionData.d.ts +33 -0
  35. package/lib/cjs/internal/BasicUnitConversionData.d.ts.map +1 -0
  36. package/lib/cjs/internal/BasicUnitConversionData.js +126 -0
  37. package/lib/cjs/internal/BasicUnitConversionData.js.map +1 -0
  38. package/lib/cjs/internal/BasicUnitConversions.generated.d.ts +507 -0
  39. package/lib/cjs/internal/BasicUnitConversions.generated.d.ts.map +1 -0
  40. package/lib/cjs/internal/BasicUnitConversions.generated.js +515 -0
  41. package/lib/cjs/internal/BasicUnitConversions.generated.js.map +1 -0
  42. package/lib/cjs/internal/BasicUnitsResolvedStateCache.d.ts +12 -0
  43. package/lib/cjs/internal/BasicUnitsResolvedStateCache.d.ts.map +1 -0
  44. package/lib/cjs/internal/BasicUnitsResolvedStateCache.js +37 -0
  45. package/lib/cjs/internal/BasicUnitsResolvedStateCache.js.map +1 -0
  46. package/lib/cjs/internal/DefaultPersistenceUnits.generated.d.ts +84 -0
  47. package/lib/cjs/internal/DefaultPersistenceUnits.generated.d.ts.map +1 -0
  48. package/lib/cjs/internal/DefaultPersistenceUnits.generated.js +94 -0
  49. package/lib/cjs/internal/DefaultPersistenceUnits.generated.js.map +1 -0
  50. package/lib/cjs/internal/UnitConversionMath.d.ts +21 -0
  51. package/lib/cjs/internal/UnitConversionMath.d.ts.map +1 -0
  52. package/lib/cjs/internal/UnitConversionMath.js +55 -0
  53. package/lib/cjs/internal/UnitConversionMath.js.map +1 -0
  54. package/lib/esm/BasicUnitsProvider.d.ts +8 -5
  55. package/lib/esm/BasicUnitsProvider.d.ts.map +1 -1
  56. package/lib/esm/BasicUnitsProvider.js +16 -139
  57. package/lib/esm/BasicUnitsProvider.js.map +1 -1
  58. package/lib/esm/Exception.d.ts +2 -1
  59. package/lib/esm/Exception.d.ts.map +1 -1
  60. package/lib/esm/Exception.js +1 -0
  61. package/lib/esm/Exception.js.map +1 -1
  62. package/lib/esm/FormatSpecHandle.d.ts +1 -2
  63. package/lib/esm/FormatSpecHandle.d.ts.map +1 -1
  64. package/lib/esm/FormatSpecHandle.js +0 -1
  65. package/lib/esm/FormatSpecHandle.js.map +1 -1
  66. package/lib/esm/Quantity.d.ts +4 -0
  67. package/lib/esm/Quantity.d.ts.map +1 -1
  68. package/lib/esm/Quantity.js +12 -23
  69. package/lib/esm/Quantity.js.map +1 -1
  70. package/lib/esm/UnitConversion/UnitConversion.js +1 -1
  71. package/lib/esm/UnitConversion/UnitConversion.js.map +1 -1
  72. package/lib/esm/UnitConversions.d.ts +40 -0
  73. package/lib/esm/UnitConversions.d.ts.map +1 -0
  74. package/lib/esm/UnitConversions.js +116 -0
  75. package/lib/esm/UnitConversions.js.map +1 -0
  76. package/lib/esm/assets/Units.json +72 -1
  77. package/lib/esm/core-quantity.d.ts +6 -0
  78. package/lib/esm/core-quantity.d.ts.map +1 -1
  79. package/lib/esm/core-quantity.js +6 -0
  80. package/lib/esm/core-quantity.js.map +1 -1
  81. package/lib/esm/generated/Units.generated.d.ts +785 -0
  82. package/lib/esm/generated/Units.generated.d.ts.map +1 -0
  83. package/lib/esm/generated/Units.generated.js +772 -0
  84. package/lib/esm/generated/Units.generated.js.map +1 -0
  85. package/lib/esm/internal/BasicUnitConversionData.d.ts +33 -0
  86. package/lib/esm/internal/BasicUnitConversionData.d.ts.map +1 -0
  87. package/lib/esm/internal/BasicUnitConversionData.js +122 -0
  88. package/lib/esm/internal/BasicUnitConversionData.js.map +1 -0
  89. package/lib/esm/internal/BasicUnitConversions.generated.d.ts +507 -0
  90. package/lib/esm/internal/BasicUnitConversions.generated.d.ts.map +1 -0
  91. package/lib/esm/internal/BasicUnitConversions.generated.js +512 -0
  92. package/lib/esm/internal/BasicUnitConversions.generated.js.map +1 -0
  93. package/lib/esm/internal/BasicUnitsResolvedStateCache.d.ts +12 -0
  94. package/lib/esm/internal/BasicUnitsResolvedStateCache.d.ts.map +1 -0
  95. package/lib/esm/internal/BasicUnitsResolvedStateCache.js +33 -0
  96. package/lib/esm/internal/BasicUnitsResolvedStateCache.js.map +1 -0
  97. package/lib/esm/internal/DefaultPersistenceUnits.generated.d.ts +84 -0
  98. package/lib/esm/internal/DefaultPersistenceUnits.generated.d.ts.map +1 -0
  99. package/lib/esm/internal/DefaultPersistenceUnits.generated.js +91 -0
  100. package/lib/esm/internal/DefaultPersistenceUnits.generated.js.map +1 -0
  101. package/lib/esm/internal/UnitConversionMath.d.ts +21 -0
  102. package/lib/esm/internal/UnitConversionMath.d.ts.map +1 -0
  103. package/lib/esm/internal/UnitConversionMath.js +49 -0
  104. package/lib/esm/internal/UnitConversionMath.js.map +1 -0
  105. package/package.json +10 -8
@@ -0,0 +1,116 @@
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
+ /** @packageDocumentation
6
+ * @module Quantity
7
+ */
8
+ import { QuantityError, QuantityStatus } from "./Exception";
9
+ import { UnitConversionInvert } from "./Interfaces";
10
+ import { basicUnitConversionData } from "./internal/BasicUnitConversions.generated";
11
+ import { defaultPersistenceUnits } from "./internal/DefaultPersistenceUnits.generated";
12
+ import { convertValueOrThrow } from "./internal/UnitConversionMath";
13
+ const basicUnitConversionLookup = basicUnitConversionData;
14
+ function getUnitEntryFields([phenomenon, factor, offset, invertsUnitName]) {
15
+ return { phenomenon, factor, offset, invertsUnitName };
16
+ }
17
+ function throwUnknownUnit(unitName) {
18
+ throw new QuantityError(QuantityStatus.UnknownUnit, `Unknown unit "${unitName}".`);
19
+ }
20
+ function getUnitEntry(unitName) {
21
+ if (!Object.prototype.hasOwnProperty.call(basicUnitConversionLookup, unitName))
22
+ return throwUnknownUnit(unitName);
23
+ return basicUnitConversionLookup[unitName];
24
+ }
25
+ /** Type guard verifying a string is a known canonical built-in unit name.
26
+ *
27
+ * Returns true only for unit names shipped in the bundled built-in canonical unit set.
28
+ * Use this at system boundaries (e.g. when ingesting unit names from JSON config,
29
+ * iModel metadata, user input, or other dynamic sources) before calling
30
+ * `UnitConversions.getConversion`, `convert`, or `isCompatible`. Narrows the input
31
+ * type to `UnitName` so subsequent calls type-check without casts.
32
+ *
33
+ * @beta
34
+ */
35
+ export function isUnitName(value) {
36
+ return Object.prototype.hasOwnProperty.call(basicUnitConversionLookup, value);
37
+ }
38
+ function getComparableEntry(unit) {
39
+ const { invertsUnitName } = getUnitEntryFields(unit);
40
+ return invertsUnitName ? getUnitEntry(invertsUnitName) : unit;
41
+ }
42
+ function composeConversion(fromUnit, toUnit) {
43
+ const { factor: fromFactor, offset: fromOffset } = getUnitEntryFields(fromUnit);
44
+ const { factor: toFactor, offset: toOffset } = getUnitEntryFields(toUnit);
45
+ return {
46
+ factor: toFactor / fromFactor,
47
+ offset: toOffset - ((toFactor * fromOffset) / fromFactor),
48
+ };
49
+ }
50
+ function getConversion(fromUnit, toUnit) {
51
+ const from = getUnitEntry(fromUnit);
52
+ const to = getUnitEntry(toUnit);
53
+ const comparableFrom = getComparableEntry(from);
54
+ const comparableTo = getComparableEntry(to);
55
+ const fromFields = getUnitEntryFields(from);
56
+ const toFields = getUnitEntryFields(to);
57
+ const comparableFromFields = getUnitEntryFields(comparableFrom);
58
+ const comparableToFields = getUnitEntryFields(comparableTo);
59
+ if (comparableFromFields.phenomenon !== comparableToFields.phenomenon)
60
+ return { factor: 1.0, offset: 0.0, error: true };
61
+ if (fromFields.invertsUnitName && toFields.invertsUnitName)
62
+ return composeConversion(comparableFrom, comparableTo);
63
+ if (fromFields.invertsUnitName) {
64
+ return {
65
+ ...composeConversion(comparableFrom, to),
66
+ inversion: UnitConversionInvert.InvertPreConversion,
67
+ };
68
+ }
69
+ if (toFields.invertsUnitName) {
70
+ return {
71
+ ...composeConversion(from, comparableTo),
72
+ inversion: UnitConversionInvert.InvertPostConversion,
73
+ };
74
+ }
75
+ return composeConversion(from, to);
76
+ }
77
+ function convert(fromUnit, toUnit, value) {
78
+ const conversion = getConversion(fromUnit, toUnit);
79
+ if (conversion.error)
80
+ throw new QuantityError(QuantityStatus.InvalidUnitConversion, `Cannot convert value from "${fromUnit}" to "${toUnit}" using invalid conversion metadata.`);
81
+ return convertValueOrThrow(value, conversion);
82
+ }
83
+ function isCompatible(fromUnit, toUnit) {
84
+ const from = getComparableEntry(getUnitEntry(fromUnit));
85
+ const to = getComparableEntry(getUnitEntry(toUnit));
86
+ const { phenomenon: fromPhenomenon } = getUnitEntryFields(from);
87
+ const { phenomenon: toPhenomenon } = getUnitEntryFields(to);
88
+ return fromPhenomenon === toPhenomenon;
89
+ }
90
+ /** Returns the package's default built-in persistence unit for a supported bundled built-in phenomenon.
91
+ *
92
+ * This helper is intentionally limited to the built-in canonical unit set shipped with `@itwin/core-quantity`.
93
+ * `Phenomena.LENGTH_RATIO` is intentionally excluded because the bundled built-in unit set does not yet provide an agreed default for that phenomenon.
94
+ * For schema-defined, custom, or iModel-specific persistence units, use a `UnitsProvider`-based workflow instead.
95
+ *
96
+ * @beta
97
+ */
98
+ export function getDefaultPersistenceUnit(phenomenon) {
99
+ return defaultPersistenceUnits[phenomenon];
100
+ }
101
+ /** One-stop unit conversion helpers for the built-in canonical unit set generated from `@bentley/units-schema`.
102
+ * This surface is synchronous and only supports built-in canonical unit names shipped with `core-quantity`.
103
+ * `getConversion(...)` returns `UnitConversionProps` and uses `error: true` as the compatibility sentinel for known-but-incompatible built-in units.
104
+ * `convert(...)` and `convertValue(...)` are the throwing application helpers to use when invalid conversion metadata should fail fast.
105
+ * `isCompatible(...)` is the explicit built-in compatibility check to use before applying a conversion when a non-throwing branch is preferred.
106
+ * For schema-defined, custom, or provider-resolved units outside that built-in set, use a `UnitsProvider`-based workflow instead.
107
+ * @beta
108
+ */
109
+ // eslint-disable-next-line @typescript-eslint/naming-convention
110
+ export const UnitConversions = {
111
+ getConversion,
112
+ convert,
113
+ convertValue: convertValueOrThrow,
114
+ isCompatible,
115
+ };
116
+ //# sourceMappingURL=Unit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,MAAM,UAAU,yBAAyB,CACvC,UAAkE;IAElE,OAAO,uBAAuB,CAAC,UAAU,CAAC,CAAC;AAC7C,CAAC;AAED;;;;;;;GAOG;AACH,gEAAgE;AAChE,MAAM,CAAC,MAAM,eAAe,GAKxB;IACF,aAAa;IACb,OAAO;IACP,YAAY,EAAE,mBAAmB;IACjC,YAAY;CACb,CAAC","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\n/** @packageDocumentation\n * @module Quantity\n */\n\nimport { QuantityError, QuantityStatus } from \"./Exception\";\nimport { UnitConversionInvert, type UnitConversionProps } from \"./Interfaces\";\nimport { Phenomena, type PhenomenonName, type UnitName } from \"./generated/Units.generated\";\nimport { basicUnitConversionData } from \"./internal/BasicUnitConversions.generated\";\nimport { defaultPersistenceUnits } from \"./internal/DefaultPersistenceUnits.generated\";\nimport { convertValueOrThrow } from \"./internal/UnitConversionMath\";\n\ntype BasicUnitConversionEntry = readonly [\n phenomenon: string,\n factor: number,\n offset: number,\n invertsUnitName?: string,\n];\n\ninterface BasicUnitConversionFields {\n readonly phenomenon: string;\n readonly factor: number;\n readonly offset: number;\n readonly invertsUnitName?: string;\n}\n\nconst basicUnitConversionLookup: Record<string, BasicUnitConversionEntry> =\n basicUnitConversionData satisfies Record<string, BasicUnitConversionEntry>;\n\nfunction getUnitEntryFields([phenomenon, factor, offset, invertsUnitName]: BasicUnitConversionEntry): BasicUnitConversionFields {\n return { phenomenon, factor, offset, invertsUnitName };\n}\n\nfunction throwUnknownUnit(unitName: string): never {\n throw new QuantityError(QuantityStatus.UnknownUnit, `Unknown unit \"${unitName}\".`);\n}\n\nfunction getUnitEntry(unitName: string): BasicUnitConversionEntry {\n if (!Object.prototype.hasOwnProperty.call(basicUnitConversionLookup, unitName))\n return throwUnknownUnit(unitName);\n\n return basicUnitConversionLookup[unitName];\n}\n\n/** Type guard verifying a string is a known canonical built-in unit name.\n *\n * Returns true only for unit names shipped in the bundled built-in canonical unit set.\n * Use this at system boundaries (e.g. when ingesting unit names from JSON config,\n * iModel metadata, user input, or other dynamic sources) before calling\n * `UnitConversions.getConversion`, `convert`, or `isCompatible`. Narrows the input\n * type to `UnitName` so subsequent calls type-check without casts.\n *\n * @beta\n */\nexport function isUnitName(value: string): value is UnitName {\n return Object.prototype.hasOwnProperty.call(basicUnitConversionLookup, value);\n}\n\nfunction getComparableEntry(unit: BasicUnitConversionEntry): BasicUnitConversionEntry {\n const { invertsUnitName } = getUnitEntryFields(unit);\n return invertsUnitName ? getUnitEntry(invertsUnitName) : unit;\n}\n\nfunction composeConversion(fromUnit: BasicUnitConversionEntry, toUnit: BasicUnitConversionEntry): UnitConversionProps {\n const { factor: fromFactor, offset: fromOffset } = getUnitEntryFields(fromUnit);\n const { factor: toFactor, offset: toOffset } = getUnitEntryFields(toUnit);\n\n return {\n factor: toFactor / fromFactor,\n offset: toOffset - ((toFactor * fromOffset) / fromFactor),\n };\n}\n\nfunction getConversion(fromUnit: UnitName, toUnit: UnitName): UnitConversionProps {\n const from = getUnitEntry(fromUnit);\n const to = getUnitEntry(toUnit);\n const comparableFrom = getComparableEntry(from);\n const comparableTo = getComparableEntry(to);\n const fromFields = getUnitEntryFields(from);\n const toFields = getUnitEntryFields(to);\n const comparableFromFields = getUnitEntryFields(comparableFrom);\n const comparableToFields = getUnitEntryFields(comparableTo);\n\n if (comparableFromFields.phenomenon !== comparableToFields.phenomenon)\n return { factor: 1.0, offset: 0.0, error: true };\n\n if (fromFields.invertsUnitName && toFields.invertsUnitName)\n return composeConversion(comparableFrom, comparableTo);\n\n if (fromFields.invertsUnitName) {\n return {\n ...composeConversion(comparableFrom, to),\n inversion: UnitConversionInvert.InvertPreConversion,\n };\n }\n\n if (toFields.invertsUnitName) {\n return {\n ...composeConversion(from, comparableTo),\n inversion: UnitConversionInvert.InvertPostConversion,\n };\n }\n\n return composeConversion(from, to);\n}\n\nfunction convert(fromUnit: UnitName, toUnit: UnitName, value: number): number {\n const conversion = getConversion(fromUnit, toUnit);\n if (conversion.error)\n throw new QuantityError(QuantityStatus.InvalidUnitConversion, `Cannot convert value from \"${fromUnit}\" to \"${toUnit}\" using invalid conversion metadata.`);\n\n return convertValueOrThrow(value, conversion);\n}\n\nfunction isCompatible(fromUnit: UnitName, toUnit: UnitName): boolean {\n const from = getComparableEntry(getUnitEntry(fromUnit));\n const to = getComparableEntry(getUnitEntry(toUnit));\n const { phenomenon: fromPhenomenon } = getUnitEntryFields(from);\n const { phenomenon: toPhenomenon } = getUnitEntryFields(to);\n return fromPhenomenon === toPhenomenon;\n}\n\n/** Returns the package's default built-in persistence unit for a supported bundled built-in phenomenon.\n *\n * This helper is intentionally limited to the built-in canonical unit set shipped with `@itwin/core-quantity`.\n * `Phenomena.LENGTH_RATIO` is intentionally excluded because the bundled built-in unit set does not yet provide an agreed default for that phenomenon.\n * For schema-defined, custom, or iModel-specific persistence units, use a `UnitsProvider`-based workflow instead.\n *\n * @beta\n */\nexport function getDefaultPersistenceUnit(\n phenomenon: Exclude<PhenomenonName, typeof Phenomena.LENGTH_RATIO>,\n): UnitName {\n return defaultPersistenceUnits[phenomenon];\n}\n\n/** One-stop unit conversion helpers for the built-in canonical unit set generated from `@bentley/units-schema`.\n * This surface is synchronous and only supports built-in canonical unit names shipped with `core-quantity`.\n * `getConversion(...)` returns `UnitConversionProps` and uses `error: true` as the compatibility sentinel for known-but-incompatible built-in units.\n * `convert(...)` and `convertValue(...)` are the throwing application helpers to use when invalid conversion metadata should fail fast.\n * `isCompatible(...)` is the explicit built-in compatibility check to use before applying a conversion when a non-throwing branch is preferred.\n * For schema-defined, custom, or provider-resolved units outside that built-in set, use a `UnitsProvider`-based workflow instead.\n * @beta\n */\n// eslint-disable-next-line @typescript-eslint/naming-convention\nexport const UnitConversions: {\n readonly getConversion: (fromUnit: UnitName, toUnit: UnitName) => UnitConversionProps;\n readonly convert: (fromUnit: UnitName, toUnit: UnitName, value: number) => number;\n readonly convertValue: (value: number, conversion: UnitConversionProps) => number;\n readonly isCompatible: (fromUnit: UnitName, toUnit: UnitName) => boolean;\n} = {\n getConversion,\n convert,\n convertValue: convertValueOrThrow,\n isCompatible,\n};\n"]}
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "version": "01.00.00",
3
- "sourceEcSchemaVersion": "01.00.09",
3
+ "sourceEcSchemaVersion": "01.00.10",
4
4
  "name": "Units",
5
5
  "alias": "u",
6
6
  "label": "Units",
@@ -400,6 +400,11 @@
400
400
  "label": "Angle",
401
401
  "definition": "ANGLE"
402
402
  },
403
+ "HORIZONTAL_DIRECTION": {
404
+ "schemaItemType": "Phenomenon",
405
+ "label": "Horizontal Direction",
406
+ "definition": "HORIZONTAL_DIRECTION"
407
+ },
403
408
  "SOLIDANGLE": {
404
409
  "schemaItemType": "Phenomenon",
405
410
  "label": "Solid Angle",
@@ -1284,6 +1289,58 @@
1284
1289
  "unitSystem": "Units.METRIC",
1285
1290
  "definition": "[TWO_PI]*RAD"
1286
1291
  },
1292
+ "HORIZONTAL_DIR_ARC_DEG": {
1293
+ "schemaItemType": "Unit",
1294
+ "label": "°",
1295
+ "description": "1/180",
1296
+ "phenomenon": "Units.HORIZONTAL_DIRECTION",
1297
+ "unitSystem": "Units.METRIC",
1298
+ "definition": "[PI]*HORIZONTAL_DIR_RAD",
1299
+ "denominator": 180
1300
+ },
1301
+ "HORIZONTAL_DIR_ARC_MINUTE": {
1302
+ "schemaItemType": "Unit",
1303
+ "label": "'",
1304
+ "description": "1/60",
1305
+ "phenomenon": "Units.HORIZONTAL_DIRECTION",
1306
+ "unitSystem": "Units.METRIC",
1307
+ "definition": "HORIZONTAL_DIR_ARC_DEG",
1308
+ "denominator": 60
1309
+ },
1310
+ "HORIZONTAL_DIR_ARC_SECOND": {
1311
+ "schemaItemType": "Unit",
1312
+ "label": "''",
1313
+ "description": "1/3600",
1314
+ "phenomenon": "Units.HORIZONTAL_DIRECTION",
1315
+ "unitSystem": "Units.METRIC",
1316
+ "definition": "HORIZONTAL_DIR_ARC_DEG",
1317
+ "denominator": 3600
1318
+ },
1319
+ "HORIZONTAL_DIR_GRAD": {
1320
+ "schemaItemType": "Unit",
1321
+ "label": "grad",
1322
+ "description": "1/200",
1323
+ "phenomenon": "Units.HORIZONTAL_DIRECTION",
1324
+ "unitSystem": "Units.METRIC",
1325
+ "definition": "[PI]*HORIZONTAL_DIR_RAD",
1326
+ "denominator": 200
1327
+ },
1328
+ "HORIZONTAL_DIR_MILLIRAD": {
1329
+ "schemaItemType": "Unit",
1330
+ "label": "mrad",
1331
+ "description": "1/1000",
1332
+ "phenomenon": "Units.HORIZONTAL_DIRECTION",
1333
+ "unitSystem": "Units.METRIC",
1334
+ "definition": "[PI]*HORIZONTAL_DIR_RAD",
1335
+ "denominator": 1000
1336
+ },
1337
+ "HORIZONTAL_DIR_REVOLUTION": {
1338
+ "schemaItemType": "Unit",
1339
+ "label": "r",
1340
+ "phenomenon": "Units.HORIZONTAL_DIRECTION",
1341
+ "unitSystem": "Units.METRIC",
1342
+ "definition": "[TWO_PI]*HORIZONTAL_DIR_RAD"
1343
+ },
1287
1344
  "SQ_M": {
1288
1345
  "schemaItemType": "Unit",
1289
1346
  "label": "m²",
@@ -3795,6 +3852,13 @@
3795
3852
  "unitSystem": "Units.METRIC",
3796
3853
  "definition": "[MEGA]*LITRE"
3797
3854
  },
3855
+ "MILLILITRE": {
3856
+ "schemaItemType": "Unit",
3857
+ "label": "mL",
3858
+ "phenomenon": "Units.VOLUME",
3859
+ "unitSystem": "Units.METRIC",
3860
+ "definition": "[MILLI]*LITRE"
3861
+ },
3798
3862
  "MICROLITRE": {
3799
3863
  "schemaItemType": "Unit",
3800
3864
  "label": "µliter",
@@ -4061,6 +4125,13 @@
4061
4125
  "unitSystem": "Units.SI",
4062
4126
  "definition": "RAD"
4063
4127
  },
4128
+ "HORIZONTAL_DIR_RAD": {
4129
+ "schemaItemType": "Unit",
4130
+ "label": "rad",
4131
+ "phenomenon": "Units.HORIZONTAL_DIRECTION",
4132
+ "unitSystem": "Units.SI",
4133
+ "definition": "HORIZONTAL_DIR_RAD"
4134
+ },
4064
4135
  "STERAD": {
4065
4136
  "schemaItemType": "Unit",
4066
4137
  "label": "sterad",
@@ -5,6 +5,7 @@ export * from "./Interfaces";
5
5
  export * from "./Parser";
6
6
  export * from "./ParserSpec";
7
7
  export * from "./Quantity";
8
+ export * from "./UnitConversions";
8
9
  export * from "./Unit";
9
10
  export * from "./Formatter/Format";
10
11
  export * from "./Formatter/FormatterSpec";
@@ -18,6 +19,7 @@ export * from "./UnitConversion/UnitDefinitionResolver";
18
19
  export * from "./SerializedUnitSchema";
19
20
  export { BasicUnitsProvider } from "./BasicUnitsProvider";
20
21
  export * from "./CompositeUnitsProvider";
22
+ export * from "./generated/Units.generated";
21
23
  export * from "./internal/cross-package";
22
24
  /** @docs-package-description
23
25
  * The core-quantity package contains classes, interfaces, and definitions for formatting and parsing quantity values.
@@ -34,6 +36,10 @@ export * from "./internal/cross-package";
34
36
  * @docs-group-description CompositeUnitsProvider
35
37
  * Factory and composition utilities for layering multiple UnitsProviders.
36
38
  */
39
+ /**
40
+ * @docs-group-description Units.generated
41
+ * Generated built-in canonical unit, phenomenon, and unit-system identifiers.
42
+ */
37
43
  /**
38
44
  * @docs-group-description Logging
39
45
  * Logger categories used by this package.
@@ -1 +1 @@
1
- {"version":3,"file":"core-quantity.d.ts","sourceRoot":"","sources":["../../src/core-quantity.ts"],"names":[],"mappings":"AAIA,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,QAAQ,CAAC;AACvB,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sCAAsC,CAAC;AACrD,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AAEzC,cAAc,iCAAiC,CAAC;AAChD,cAAc,yCAAyC,CAAC;AACxD,cAAc,wBAAwB,CAAC;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AAEzC;;GAEG;AAEH;;;GAGG;AACH;;;GAGG;AACH;;;GAGG;AACH;;;GAGG"}
1
+ {"version":3,"file":"core-quantity.d.ts","sourceRoot":"","sources":["../../src/core-quantity.ts"],"names":[],"mappings":"AAIA,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,QAAQ,CAAC;AACvB,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sCAAsC,CAAC;AACrD,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AAEzC,cAAc,iCAAiC,CAAC;AAChD,cAAc,yCAAyC,CAAC;AACxD,cAAc,wBAAwB,CAAC;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC;AAEzC;;GAEG;AAEH;;;GAGG;AACH;;;GAGG;AACH;;;GAGG;AACH;;;GAGG;AACH;;;GAGG"}
@@ -9,6 +9,7 @@ export * from "./Interfaces";
9
9
  export * from "./Parser";
10
10
  export * from "./ParserSpec";
11
11
  export * from "./Quantity";
12
+ export * from "./UnitConversions";
12
13
  export * from "./Unit";
13
14
  export * from "./Formatter/Format";
14
15
  export * from "./Formatter/FormatterSpec";
@@ -22,6 +23,7 @@ export * from "./UnitConversion/UnitDefinitionResolver";
22
23
  export * from "./SerializedUnitSchema";
23
24
  export { BasicUnitsProvider } from "./BasicUnitsProvider";
24
25
  export * from "./CompositeUnitsProvider";
26
+ export * from "./generated/Units.generated";
25
27
  export * from "./internal/cross-package";
26
28
  /** @docs-package-description
27
29
  * The core-quantity package contains classes, interfaces, and definitions for formatting and parsing quantity values.
@@ -38,6 +40,10 @@ export * from "./internal/cross-package";
38
40
  * @docs-group-description CompositeUnitsProvider
39
41
  * Factory and composition utilities for layering multiple UnitsProviders.
40
42
  */
43
+ /**
44
+ * @docs-group-description Units.generated
45
+ * Generated built-in canonical unit, phenomenon, and unit-system identifiers.
46
+ */
41
47
  /**
42
48
  * @docs-group-description Logging
43
49
  * Logger categories used by this package.
@@ -1 +1 @@
1
- {"version":3,"file":"core-quantity.js","sourceRoot":"","sources":["../../src/core-quantity.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,QAAQ,CAAC;AACvB,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sCAAsC,CAAC;AACrD,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AAEzC,cAAc,iCAAiC,CAAC;AAChD,cAAc,yCAAyC,CAAC;AACxD,cAAc,wBAAwB,CAAC;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,cAAc,0BAA0B,CAAC;AACzC,cAAc,0BAA0B,CAAC;AAEzC;;GAEG;AAEH;;;GAGG;AACH;;;GAGG;AACH;;;GAGG;AACH;;;GAGG","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\nexport * from \"./Constants\";\nexport * from \"./Exception\";\nexport * from \"./FormatSpecHandle\";\nexport * from \"./Interfaces\";\nexport * from \"./Parser\";\nexport * from \"./ParserSpec\";\nexport * from \"./Quantity\";\nexport * from \"./Unit\";\nexport * from \"./Formatter/Format\";\nexport * from \"./Formatter/FormatterSpec\";\nexport * from \"./Formatter/FormatEnums\";\nexport * from \"./Formatter/Formatter\";\nexport * from \"./Formatter/FormattingReadyCollector\";\nexport * from \"./Formatter/Interfaces\";\nexport * from \"./QuantityLoggerCategory\";\n\nexport * from \"./UnitConversion/UnitConversion\";\nexport * from \"./UnitConversion/UnitDefinitionResolver\";\nexport * from \"./SerializedUnitSchema\";\nexport { BasicUnitsProvider } from \"./BasicUnitsProvider\";\nexport * from \"./CompositeUnitsProvider\";\nexport * from \"./internal/cross-package\";\n\n/** @docs-package-description\n * The core-quantity package contains classes, interfaces, and definitions for formatting and parsing quantity values.\n */\n\n/**\n * @docs-group-description Quantity\n * Classes, Interfaces, and definitions used to format and parse quantity values.\n */\n/**\n * @docs-group-description BasicUnitsProvider\n * A UnitsProvider backed by the bundled BIS Units schema JSON asset.\n */\n/**\n * @docs-group-description CompositeUnitsProvider\n * Factory and composition utilities for layering multiple UnitsProviders.\n */\n/**\n * @docs-group-description Logging\n * Logger categories used by this package.\n */\n"]}
1
+ {"version":3,"file":"core-quantity.js","sourceRoot":"","sources":["../../src/core-quantity.ts"],"names":[],"mappings":"AAAA;;;+FAG+F;AAC/F,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,YAAY,CAAC;AAC3B,cAAc,mBAAmB,CAAC;AAClC,cAAc,QAAQ,CAAC;AACvB,cAAc,oBAAoB,CAAC;AACnC,cAAc,2BAA2B,CAAC;AAC1C,cAAc,yBAAyB,CAAC;AACxC,cAAc,uBAAuB,CAAC;AACtC,cAAc,sCAAsC,CAAC;AACrD,cAAc,wBAAwB,CAAC;AACvC,cAAc,0BAA0B,CAAC;AAEzC,cAAc,iCAAiC,CAAC;AAChD,cAAc,yCAAyC,CAAC;AACxD,cAAc,wBAAwB,CAAC;AACvC,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAC1D,cAAc,0BAA0B,CAAC;AACzC,cAAc,6BAA6B,CAAC;AAC5C,cAAc,0BAA0B,CAAC;AAEzC;;GAEG;AAEH;;;GAGG;AACH;;;GAGG;AACH;;;GAGG;AACH;;;GAGG;AACH;;;GAGG","sourcesContent":["/*---------------------------------------------------------------------------------------------\n* Copyright (c) Bentley Systems, Incorporated. All rights reserved.\n* See LICENSE.md in the project root for license terms and full copyright notice.\n*--------------------------------------------------------------------------------------------*/\nexport * from \"./Constants\";\nexport * from \"./Exception\";\nexport * from \"./FormatSpecHandle\";\nexport * from \"./Interfaces\";\nexport * from \"./Parser\";\nexport * from \"./ParserSpec\";\nexport * from \"./Quantity\";\nexport * from \"./UnitConversions\";\nexport * from \"./Unit\";\nexport * from \"./Formatter/Format\";\nexport * from \"./Formatter/FormatterSpec\";\nexport * from \"./Formatter/FormatEnums\";\nexport * from \"./Formatter/Formatter\";\nexport * from \"./Formatter/FormattingReadyCollector\";\nexport * from \"./Formatter/Interfaces\";\nexport * from \"./QuantityLoggerCategory\";\n\nexport * from \"./UnitConversion/UnitConversion\";\nexport * from \"./UnitConversion/UnitDefinitionResolver\";\nexport * from \"./SerializedUnitSchema\";\nexport { BasicUnitsProvider } from \"./BasicUnitsProvider\";\nexport * from \"./CompositeUnitsProvider\";\nexport * from \"./generated/Units.generated\";\nexport * from \"./internal/cross-package\";\n\n/** @docs-package-description\n * The core-quantity package contains classes, interfaces, and definitions for formatting and parsing quantity values.\n */\n\n/**\n * @docs-group-description Quantity\n * Classes, Interfaces, and definitions used to format and parse quantity values.\n */\n/**\n * @docs-group-description BasicUnitsProvider\n * A UnitsProvider backed by the bundled BIS Units schema JSON asset.\n */\n/**\n * @docs-group-description CompositeUnitsProvider\n * Factory and composition utilities for layering multiple UnitsProviders.\n */\n/**\n * @docs-group-description Units.generated\n * Generated built-in canonical unit, phenomenon, and unit-system identifiers.\n */\n/**\n * @docs-group-description Logging\n * Logger categories used by this package.\n */\n"]}