@atomic-ehr/fhirpath 0.0.2 → 0.0.3-canary.2be66fb.20250905161900

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 (147) hide show
  1. package/README.md +716 -238
  2. package/dist/index.d.ts +226 -120
  3. package/dist/index.js +11552 -5580
  4. package/dist/index.js.map +1 -1
  5. package/package.json +12 -5
  6. package/src/analyzer/augmentor.ts +242 -0
  7. package/src/analyzer/cursor-services.ts +75 -0
  8. package/src/analyzer/scope-manager.ts +57 -0
  9. package/src/analyzer/trivia-indexer.ts +58 -0
  10. package/src/analyzer/type-compat.ts +157 -0
  11. package/src/analyzer/utils.ts +132 -0
  12. package/src/analyzer.ts +939 -1204
  13. package/src/completion-provider.ts +209 -191
  14. package/src/complex-types/quantity-value.ts +410 -0
  15. package/src/complex-types/temporal.ts +1776 -0
  16. package/src/errors.ts +25 -3
  17. package/src/index.ts +17 -104
  18. package/src/inspect.ts +4 -4
  19. package/src/{boxing.ts → interpreter/boxing.ts} +1 -1
  20. package/src/interpreter/navigator.ts +94 -0
  21. package/src/interpreter/runtime-context.ts +273 -0
  22. package/src/interpreter.ts +506 -468
  23. package/src/lexer.ts +192 -211
  24. package/src/model-provider.ts +71 -43
  25. package/src/operations/abs-function.ts +1 -1
  26. package/src/operations/aggregate-function.ts +84 -5
  27. package/src/operations/all-function.ts +4 -3
  28. package/src/operations/allFalse-function.ts +2 -1
  29. package/src/operations/allTrue-function.ts +2 -1
  30. package/src/operations/and-operator.ts +2 -1
  31. package/src/operations/anyFalse-function.ts +2 -1
  32. package/src/operations/anyTrue-function.ts +2 -1
  33. package/src/operations/as-function.ts +99 -0
  34. package/src/operations/as-operator.ts +57 -19
  35. package/src/operations/ceiling-function.ts +1 -1
  36. package/src/operations/children-function.ts +14 -5
  37. package/src/operations/combine-function.ts +6 -3
  38. package/src/operations/combine-operator.ts +6 -7
  39. package/src/operations/comparison.ts +744 -0
  40. package/src/operations/contains-function.ts +1 -1
  41. package/src/operations/contains-operator.ts +2 -1
  42. package/src/operations/convertsToBoolean-function.ts +78 -0
  43. package/src/operations/convertsToDecimal-function.ts +82 -0
  44. package/src/operations/convertsToInteger-function.ts +71 -0
  45. package/src/operations/convertsToLong-function.ts +89 -0
  46. package/src/operations/convertsToQuantity-function.ts +132 -0
  47. package/src/operations/convertsToString-function.ts +88 -0
  48. package/src/operations/count-function.ts +2 -1
  49. package/src/operations/dateOf-function.ts +69 -0
  50. package/src/operations/dayOf-function.ts +66 -0
  51. package/src/operations/decimal-boundaries.ts +133 -0
  52. package/src/operations/defineVariable-function.ts +130 -17
  53. package/src/operations/distinct-function.ts +1 -1
  54. package/src/operations/div-operator.ts +1 -1
  55. package/src/operations/divide-operator.ts +12 -7
  56. package/src/operations/dot-operator.ts +1 -1
  57. package/src/operations/empty-function.ts +30 -21
  58. package/src/operations/endsWith-function.ts +6 -1
  59. package/src/operations/equal-operator.ts +23 -32
  60. package/src/operations/equivalent-operator.ts +13 -53
  61. package/src/operations/exclude-function.ts +2 -1
  62. package/src/operations/exists-function.ts +4 -3
  63. package/src/operations/extension-function.ts +84 -0
  64. package/src/operations/first-function.ts +1 -1
  65. package/src/operations/floor-function.ts +1 -1
  66. package/src/operations/greater-operator.ts +7 -9
  67. package/src/operations/greater-or-equal-operator.ts +7 -9
  68. package/src/operations/highBoundary-function.ts +120 -0
  69. package/src/operations/hourOf-function.ts +66 -0
  70. package/src/operations/iif-function.ts +193 -8
  71. package/src/operations/implies-operator.ts +2 -1
  72. package/src/operations/in-operator.ts +2 -1
  73. package/src/operations/index.ts +43 -0
  74. package/src/operations/indexOf-function.ts +1 -1
  75. package/src/operations/intersect-function.ts +1 -1
  76. package/src/operations/is-function.ts +70 -0
  77. package/src/operations/is-operator.ts +176 -13
  78. package/src/operations/isDistinct-function.ts +2 -1
  79. package/src/operations/join-function.ts +1 -1
  80. package/src/operations/last-function.ts +1 -1
  81. package/src/operations/lastIndexOf-function.ts +85 -0
  82. package/src/operations/length-function.ts +1 -1
  83. package/src/operations/less-operator.ts +8 -9
  84. package/src/operations/less-or-equal-operator.ts +7 -9
  85. package/src/operations/less-than.ts +8 -13
  86. package/src/operations/lowBoundary-function.ts +120 -0
  87. package/src/operations/lower-function.ts +1 -1
  88. package/src/operations/matches-function.ts +86 -0
  89. package/src/operations/matchesFull-function.ts +96 -0
  90. package/src/operations/millisecondOf-function.ts +66 -0
  91. package/src/operations/minus-operator.ts +76 -4
  92. package/src/operations/minuteOf-function.ts +66 -0
  93. package/src/operations/mod-operator.ts +8 -2
  94. package/src/operations/monthOf-function.ts +66 -0
  95. package/src/operations/multiply-operator.ts +27 -3
  96. package/src/operations/not-equal-operator.ts +24 -30
  97. package/src/operations/not-equivalent-operator.ts +13 -53
  98. package/src/operations/not-function.ts +10 -3
  99. package/src/operations/ofType-function.ts +43 -12
  100. package/src/operations/or-operator.ts +2 -1
  101. package/src/operations/plus-operator.ts +71 -7
  102. package/src/operations/power-function.ts +35 -10
  103. package/src/operations/precision-function.ts +146 -0
  104. package/src/operations/repeat-function.ts +169 -0
  105. package/src/operations/replace-function.ts +1 -1
  106. package/src/operations/replaceMatches-function.ts +125 -0
  107. package/src/operations/round-function.ts +1 -1
  108. package/src/operations/secondOf-function.ts +66 -0
  109. package/src/operations/select-function.ts +66 -5
  110. package/src/operations/single-function.ts +1 -1
  111. package/src/operations/skip-function.ts +1 -1
  112. package/src/operations/split-function.ts +1 -1
  113. package/src/operations/sqrt-function.ts +15 -8
  114. package/src/operations/startsWith-function.ts +1 -1
  115. package/src/operations/subsetOf-function.ts +6 -2
  116. package/src/operations/substring-function.ts +1 -1
  117. package/src/operations/supersetOf-function.ts +6 -2
  118. package/src/operations/tail-function.ts +1 -1
  119. package/src/operations/take-function.ts +1 -1
  120. package/src/operations/temporal-functions.ts +555 -0
  121. package/src/operations/timeOf-function.ts +67 -0
  122. package/src/operations/timezoneOffsetOf-function.ts +69 -0
  123. package/src/operations/toBoolean-function.ts +27 -8
  124. package/src/operations/toChars-function.ts +56 -0
  125. package/src/operations/toDecimal-function.ts +27 -8
  126. package/src/operations/toInteger-function.ts +15 -3
  127. package/src/operations/toLong-function.ts +98 -0
  128. package/src/operations/toQuantity-function.ts +181 -0
  129. package/src/operations/toString-function.ts +78 -15
  130. package/src/operations/trace-function.ts +1 -1
  131. package/src/operations/trim-function.ts +1 -1
  132. package/src/operations/truncate-function.ts +1 -1
  133. package/src/operations/unary-minus-operator.ts +2 -2
  134. package/src/operations/unary-plus-operator.ts +1 -1
  135. package/src/operations/union-function.ts +1 -1
  136. package/src/operations/union-operator.ts +16 -26
  137. package/src/operations/upper-function.ts +1 -1
  138. package/src/operations/where-function.ts +3 -3
  139. package/src/operations/xor-operator.ts +1 -1
  140. package/src/operations/yearOf-function.ts +66 -0
  141. package/src/{cursor-nodes.ts → parser/cursor-nodes.ts} +10 -7
  142. package/src/parser.ts +262 -503
  143. package/src/registry.ts +53 -42
  144. package/src/types.ts +129 -17
  145. package/src/utils/decimal.ts +76 -0
  146. package/src/utils/pprint.ts +151 -0
  147. package/src/quantity-value.ts +0 -198
@@ -1,198 +0,0 @@
1
- import { ucum } from '@atomic-ehr/ucum';
2
- import type { Quantity } from '@atomic-ehr/ucum';
3
-
4
- /**
5
- * Wrapper for FHIRPath quantity values with UCUM integration
6
- */
7
- export interface QuantityValue {
8
- value: number;
9
- unit: string;
10
- _ucumQuantity?: Quantity; // Lazy-initialized
11
- }
12
-
13
- /**
14
- * Calendar duration unit to UCUM unit mapping
15
- */
16
- export const CALENDAR_TO_UCUM: Record<string, string> = {
17
- 'year': 'a', // annum
18
- 'years': 'a',
19
- 'month': 'mo', // month
20
- 'months': 'mo',
21
- 'week': 'wk', // week
22
- 'weeks': 'wk',
23
- 'day': 'd', // day
24
- 'days': 'd',
25
- 'hour': 'h', // hour
26
- 'hours': 'h',
27
- 'minute': 'min', // minute
28
- 'minutes': 'min',
29
- 'second': 's', // second
30
- 'seconds': 's',
31
- 'millisecond': 'ms', // millisecond
32
- 'milliseconds': 'ms'
33
- };
34
-
35
- /**
36
- * Create a quantity value
37
- */
38
- export function createQuantity(value: number, unit: string, isCalendarUnit: boolean = false): QuantityValue {
39
- const actualUnit = isCalendarUnit && CALENDAR_TO_UCUM[unit] ? CALENDAR_TO_UCUM[unit] : unit;
40
- return {
41
- value,
42
- unit: actualUnit
43
- };
44
- }
45
-
46
- /**
47
- * Get or create the UCUM quantity for a QuantityValue
48
- */
49
- export function getUcumQuantity(quantity: QuantityValue): Quantity | null {
50
- if (!quantity._ucumQuantity) {
51
- try {
52
- quantity._ucumQuantity = ucum.quantity(quantity.value, quantity.unit);
53
- } catch (e) {
54
- // Invalid unit - return null
55
- return null;
56
- }
57
- }
58
- return quantity._ucumQuantity || null;
59
- }
60
-
61
- /**
62
- * Check if a quantity has a valid unit
63
- */
64
- export function isValidQuantity(quantity: QuantityValue): boolean {
65
- return getUcumQuantity(quantity) !== null;
66
- }
67
-
68
- /**
69
- * Add two quantities
70
- */
71
- export function addQuantities(left: QuantityValue, right: QuantityValue): QuantityValue | null {
72
- const leftUcum = getUcumQuantity(left);
73
- const rightUcum = getUcumQuantity(right);
74
-
75
- if (!leftUcum || !rightUcum) {
76
- return null;
77
- }
78
-
79
- try {
80
- const result = ucum.add(leftUcum, rightUcum);
81
- return {
82
- value: result.value,
83
- unit: result.unit
84
- };
85
- } catch (e) {
86
- // Incompatible dimensions
87
- return null;
88
- }
89
- }
90
-
91
- /**
92
- * Subtract two quantities
93
- */
94
- export function subtractQuantities(left: QuantityValue, right: QuantityValue): QuantityValue | null {
95
- const leftUcum = getUcumQuantity(left);
96
- const rightUcum = getUcumQuantity(right);
97
-
98
- if (!leftUcum || !rightUcum) {
99
- return null;
100
- }
101
-
102
- try {
103
- const result = ucum.subtract(leftUcum, rightUcum);
104
- return {
105
- value: result.value,
106
- unit: result.unit
107
- };
108
- } catch (e) {
109
- // Incompatible dimensions
110
- return null;
111
- }
112
- }
113
-
114
- /**
115
- * Multiply two quantities
116
- */
117
- export function multiplyQuantities(left: QuantityValue, right: QuantityValue): QuantityValue | null {
118
- const leftUcum = getUcumQuantity(left);
119
- const rightUcum = getUcumQuantity(right);
120
-
121
- if (!leftUcum || !rightUcum) {
122
- return null;
123
- }
124
-
125
- try {
126
- const result = ucum.multiply(leftUcum, rightUcum);
127
- return {
128
- value: result.value,
129
- unit: result.unit
130
- };
131
- } catch (e) {
132
- return null;
133
- }
134
- }
135
-
136
- /**
137
- * Divide two quantities
138
- */
139
- export function divideQuantities(left: QuantityValue, right: QuantityValue): QuantityValue | null {
140
- const leftUcum = getUcumQuantity(left);
141
- const rightUcum = getUcumQuantity(right);
142
-
143
- if (!leftUcum || !rightUcum) {
144
- return null;
145
- }
146
-
147
- try {
148
- const result = ucum.divide(leftUcum, rightUcum);
149
- return {
150
- value: result.value,
151
- unit: result.unit
152
- };
153
- } catch (e) {
154
- return null;
155
- }
156
- }
157
-
158
- /**
159
- * Compare two quantities
160
- * Returns -1 if left < right, 0 if equal, 1 if left > right, null if incomparable
161
- */
162
- export function compareQuantities(left: QuantityValue, right: QuantityValue): number | null {
163
- const leftUcum = getUcumQuantity(left);
164
- const rightUcum = getUcumQuantity(right);
165
-
166
- if (!leftUcum || !rightUcum) {
167
- return null;
168
- }
169
-
170
- try {
171
- // Try to convert right to left's unit for comparison
172
- const rightValue = ucum.convert(rightUcum.value, rightUcum.unit, leftUcum.unit);
173
- if (leftUcum.value < rightValue) {
174
- return -1;
175
- } else if (leftUcum.value > rightValue) {
176
- return 1;
177
- } else {
178
- return 0;
179
- }
180
- } catch (e) {
181
- // Incompatible dimensions
182
- return null;
183
- }
184
- }
185
-
186
- /**
187
- * Check if two quantities are equal
188
- */
189
- export function equalQuantities(left: QuantityValue, right: QuantityValue): boolean {
190
- return compareQuantities(left, right) === 0;
191
- }
192
-
193
- /**
194
- * Convert quantity to string representation
195
- */
196
- export function quantityToString(quantity: QuantityValue): string {
197
- return `${quantity.value} '${quantity.unit}'`;
198
- }