@billdog.io/react-native 1.0.0-beta.2

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 (309) hide show
  1. package/BillDogReactNative.podspec +30 -0
  2. package/LICENSE +21 -0
  3. package/README.md +330 -0
  4. package/android/build.gradle +99 -0
  5. package/android/src/main/AndroidManifest.xml +1 -0
  6. package/android/src/main/kotlin/io/billdog/reactnative/BillDogABTestModule.kt +317 -0
  7. package/android/src/main/kotlin/io/billdog/reactnative/BillDogAnalyticsModule.kt +389 -0
  8. package/android/src/main/kotlin/io/billdog/reactnative/BillDogModule.kt +817 -0
  9. package/android/src/main/kotlin/io/billdog/reactnative/BillDogPaywallViewManager.kt +383 -0
  10. package/android/src/main/kotlin/io/billdog/reactnative/BillDogSurveyModule.kt +427 -0
  11. package/android/src/main/kotlin/io/billdog/reactnative/BillDogVirtualCurrencyModule.kt +407 -0
  12. package/android/src/main/kotlin/io/billdog/virtualcurrency/BillDogVirtualCurrencyPackage.kt +44 -0
  13. package/dist/AdvancedPaywallCache.d.ts +70 -0
  14. package/dist/AdvancedPaywallCache.js +344 -0
  15. package/dist/BillDog.d.ts +255 -0
  16. package/dist/BillDog.js +918 -0
  17. package/dist/BillDogConfiguration.d.ts +145 -0
  18. package/dist/BillDogConfiguration.js +229 -0
  19. package/dist/ComponentRenderer.d.ts +17 -0
  20. package/dist/ComponentRenderer.js +886 -0
  21. package/dist/LayoutEngine.d.ts +101 -0
  22. package/dist/LayoutEngine.js +394 -0
  23. package/dist/LocalizationResolver.d.ts +99 -0
  24. package/dist/LocalizationResolver.js +266 -0
  25. package/dist/NativePaywallView.d.ts +36 -0
  26. package/dist/NativePaywallView.js +59 -0
  27. package/dist/PaywallCache.d.ts +74 -0
  28. package/dist/PaywallCache.js +293 -0
  29. package/dist/PaywallStateManager.d.ts +62 -0
  30. package/dist/PaywallStateManager.js +147 -0
  31. package/dist/PaywallView.d.ts +42 -0
  32. package/dist/PaywallView.js +275 -0
  33. package/dist/ProductVariableProvider.d.ts +60 -0
  34. package/dist/ProductVariableProvider.js +239 -0
  35. package/dist/VariableResolver.d.ts +120 -0
  36. package/dist/VariableResolver.js +379 -0
  37. package/dist/WebViewPaywall.d.ts +75 -0
  38. package/dist/WebViewPaywall.js +360 -0
  39. package/dist/animations/presets.d.ts +49 -0
  40. package/dist/animations/presets.js +367 -0
  41. package/dist/components/AccoladeCpsComponent.d.ts +13 -0
  42. package/dist/components/AccoladeCpsComponent.js +248 -0
  43. package/dist/components/AccordionComponent.d.ts +10 -0
  44. package/dist/components/AccordionComponent.js +187 -0
  45. package/dist/components/AnimatedWrapper.d.ts +35 -0
  46. package/dist/components/AnimatedWrapper.js +277 -0
  47. package/dist/components/CarouselComponent.d.ts +23 -0
  48. package/dist/components/CarouselComponent.js +484 -0
  49. package/dist/components/ChartCurveComponent.d.ts +13 -0
  50. package/dist/components/ChartCurveComponent.js +333 -0
  51. package/dist/components/CheckboxComponent.d.ts +9 -0
  52. package/dist/components/CheckboxComponent.js +172 -0
  53. package/dist/components/ConfettiComponent.d.ts +9 -0
  54. package/dist/components/ConfettiComponent.js +172 -0
  55. package/dist/components/CountdownComponent.d.ts +16 -0
  56. package/dist/components/CountdownComponent.js +414 -0
  57. package/dist/components/DelayedXComponent.d.ts +12 -0
  58. package/dist/components/DelayedXComponent.js +240 -0
  59. package/dist/components/DrawerModalComponent.d.ts +20 -0
  60. package/dist/components/DrawerModalComponent.js +286 -0
  61. package/dist/components/GradientView.d.ts +38 -0
  62. package/dist/components/GradientView.js +126 -0
  63. package/dist/components/HapticFeedbackComponent.d.ts +16 -0
  64. package/dist/components/HapticFeedbackComponent.js +64 -0
  65. package/dist/components/HorizontalProductsComponent.d.ts +17 -0
  66. package/dist/components/HorizontalProductsComponent.js +284 -0
  67. package/dist/components/MultiChoiceGridComponent.d.ts +13 -0
  68. package/dist/components/MultiChoiceGridComponent.js +234 -0
  69. package/dist/components/NPSScaleComponent.d.ts +16 -0
  70. package/dist/components/NPSScaleComponent.js +260 -0
  71. package/dist/components/NavigationComponent.d.ts +26 -0
  72. package/dist/components/NavigationComponent.js +302 -0
  73. package/dist/components/OAuthButtonComponent.d.ts +20 -0
  74. package/dist/components/OAuthButtonComponent.js +237 -0
  75. package/dist/components/ProgressLoaderComponent.d.ts +13 -0
  76. package/dist/components/ProgressLoaderComponent.js +308 -0
  77. package/dist/components/ProgressStepperComponent.d.ts +18 -0
  78. package/dist/components/ProgressStepperComponent.js +228 -0
  79. package/dist/components/SegmentedPickerComponent.d.ts +10 -0
  80. package/dist/components/SegmentedPickerComponent.js +221 -0
  81. package/dist/components/ShareButtonsComponent.d.ts +21 -0
  82. package/dist/components/ShareButtonsComponent.js +265 -0
  83. package/dist/components/ShareCodeComponent.d.ts +16 -0
  84. package/dist/components/ShareCodeComponent.js +274 -0
  85. package/dist/components/SingleChoiceGridComponent.d.ts +13 -0
  86. package/dist/components/SingleChoiceGridComponent.js +210 -0
  87. package/dist/components/SliderComponent.d.ts +9 -0
  88. package/dist/components/SliderComponent.js +263 -0
  89. package/dist/components/StarRatingInputComponent.d.ts +20 -0
  90. package/dist/components/StarRatingInputComponent.js +198 -0
  91. package/dist/components/StickerCompositeComponent.d.ts +13 -0
  92. package/dist/components/StickerCompositeComponent.js +136 -0
  93. package/dist/components/TabsComponent.d.ts +19 -0
  94. package/dist/components/TabsComponent.js +431 -0
  95. package/dist/components/TextInputComponent.d.ts +18 -0
  96. package/dist/components/TextInputComponent.js +227 -0
  97. package/dist/components/TimelineComponent.d.ts +19 -0
  98. package/dist/components/TimelineComponent.js +104 -0
  99. package/dist/components/ToggleComponent.d.ts +14 -0
  100. package/dist/components/ToggleComponent.js +132 -0
  101. package/dist/components/VerticalProductsComponent.d.ts +17 -0
  102. package/dist/components/VerticalProductsComponent.js +311 -0
  103. package/dist/dynamicValues.d.ts +36 -0
  104. package/dist/dynamicValues.js +142 -0
  105. package/dist/filters/dateFilters.d.ts +50 -0
  106. package/dist/filters/dateFilters.js +390 -0
  107. package/dist/filters/index.d.ts +18 -0
  108. package/dist/filters/index.js +253 -0
  109. package/dist/hooks/useOfferings.d.ts +76 -0
  110. package/dist/hooks/useOfferings.js +99 -0
  111. package/dist/hooks/usePaywall.d.ts +27 -0
  112. package/dist/hooks/usePaywall.js +114 -0
  113. package/dist/hooks/usePlacement.d.ts +34 -0
  114. package/dist/hooks/usePlacement.js +89 -0
  115. package/dist/hooks/useSubscription.d.ts +40 -0
  116. package/dist/hooks/useSubscription.js +97 -0
  117. package/dist/index.d.ts +98 -0
  118. package/dist/index.js +198 -0
  119. package/dist/models/BillDogError.d.ts +40 -0
  120. package/dist/models/BillDogError.js +11 -0
  121. package/dist/models/CacheFetchPolicy.d.ts +8 -0
  122. package/dist/models/CacheFetchPolicy.js +4 -0
  123. package/dist/models/CustomProduct.d.ts +32 -0
  124. package/dist/models/CustomProduct.js +15 -0
  125. package/dist/models/CustomerInfo.d.ts +17 -0
  126. package/dist/models/CustomerInfo.js +11 -0
  127. package/dist/models/Delegate.d.ts +20 -0
  128. package/dist/models/Delegate.js +11 -0
  129. package/dist/models/EntitlementInfo.d.ts +19 -0
  130. package/dist/models/EntitlementInfo.js +2 -0
  131. package/dist/models/Placement.d.ts +54 -0
  132. package/dist/models/Placement.js +15 -0
  133. package/dist/models/PurchaseParams.d.ts +39 -0
  134. package/dist/models/PurchaseParams.js +60 -0
  135. package/dist/models/PurchaseResult.d.ts +30 -0
  136. package/dist/models/PurchaseResult.js +15 -0
  137. package/dist/models/RestoreResult.d.ts +30 -0
  138. package/dist/models/RestoreResult.js +22 -0
  139. package/dist/models/RestoredItem.d.ts +26 -0
  140. package/dist/models/RestoredItem.js +13 -0
  141. package/dist/modules/ABTest.d.ts +65 -0
  142. package/dist/modules/ABTest.js +113 -0
  143. package/dist/modules/Analytics.d.ts +91 -0
  144. package/dist/modules/Analytics.js +182 -0
  145. package/dist/modules/Survey.d.ts +65 -0
  146. package/dist/modules/Survey.js +82 -0
  147. package/dist/modules/VirtualCurrency.d.ts +63 -0
  148. package/dist/modules/VirtualCurrency.js +147 -0
  149. package/dist/murmur.d.ts +23 -0
  150. package/dist/murmur.js +104 -0
  151. package/dist/notifications/LiveActivitiesManager.d.ts +47 -0
  152. package/dist/notifications/LiveActivitiesManager.js +106 -0
  153. package/dist/notifications/NotificationScheduler.d.ts +55 -0
  154. package/dist/notifications/NotificationScheduler.js +199 -0
  155. package/dist/notifications/PurchaseNotificationHandler.d.ts +24 -0
  156. package/dist/notifications/PurchaseNotificationHandler.js +55 -0
  157. package/dist/notifications/useDeviceToken.d.ts +36 -0
  158. package/dist/notifications/useDeviceToken.js +97 -0
  159. package/dist/notifications/useLiveActivity.d.ts +39 -0
  160. package/dist/notifications/useLiveActivity.js +104 -0
  161. package/dist/review/ReviewAnalytics.d.ts +34 -0
  162. package/dist/review/ReviewAnalytics.js +73 -0
  163. package/dist/review/ReviewManager.d.ts +83 -0
  164. package/dist/review/ReviewManager.js +339 -0
  165. package/dist/review/ReviewTimingManager.d.ts +82 -0
  166. package/dist/review/ReviewTimingManager.js +224 -0
  167. package/dist/survey-core/types.d.ts +215 -0
  168. package/dist/survey-core/types.js +8 -0
  169. package/dist/types.d.ts +438 -0
  170. package/dist/types.js +158 -0
  171. package/dist/utils/ApiClient.d.ts +65 -0
  172. package/dist/utils/ApiClient.js +132 -0
  173. package/dist/utils/CSSConverter.d.ts +49 -0
  174. package/dist/utils/CSSConverter.js +470 -0
  175. package/dist/utils/animations.d.ts +41 -0
  176. package/dist/utils/animations.js +194 -0
  177. package/dist/utils/calendarIntegration.d.ts +36 -0
  178. package/dist/utils/calendarIntegration.js +89 -0
  179. package/dist/utils/configurationResolver.d.ts +23 -0
  180. package/dist/utils/configurationResolver.js +78 -0
  181. package/dist/utils/deviceDetection.d.ts +38 -0
  182. package/dist/utils/deviceDetection.js +59 -0
  183. package/dist/utils/hapticFeedback.d.ts +17 -0
  184. package/dist/utils/hapticFeedback.js +53 -0
  185. package/dist/utils/iconMapper.d.ts +26 -0
  186. package/dist/utils/iconMapper.js +113 -0
  187. package/dist/utils/notificationPermissions.d.ts +13 -0
  188. package/dist/utils/notificationPermissions.js +51 -0
  189. package/dist/utils/priceFormatting.d.ts +19 -0
  190. package/dist/utils/priceFormatting.js +99 -0
  191. package/dist/utils/shareSheet.d.ts +17 -0
  192. package/dist/utils/shareSheet.js +42 -0
  193. package/dist/utils/subscriptionUpgrade.d.ts +84 -0
  194. package/dist/utils/subscriptionUpgrade.js +100 -0
  195. package/dist/valueUtils.d.ts +48 -0
  196. package/dist/valueUtils.js +129 -0
  197. package/dist/xFlowTags.d.ts +18 -0
  198. package/dist/xFlowTags.js +385 -0
  199. package/ios/BillDogABTest.m +50 -0
  200. package/ios/BillDogABTest.swift +188 -0
  201. package/ios/BillDogAnalytics.m +105 -0
  202. package/ios/BillDogAnalytics.swift +324 -0
  203. package/ios/BillDogBridge.m +222 -0
  204. package/ios/BillDogBridge.swift +715 -0
  205. package/ios/BillDogLiveActivities.m +41 -0
  206. package/ios/BillDogLiveActivities.swift +104 -0
  207. package/ios/BillDogPaywallViewManager.m +39 -0
  208. package/ios/BillDogPaywallViewManager.swift +361 -0
  209. package/ios/BillDogSurvey.m +34 -0
  210. package/ios/BillDogSurvey.swift +257 -0
  211. package/ios/BillDogVirtualCurrencyBridge.m +63 -0
  212. package/ios/BillDogVirtualCurrencyBridge.swift +511 -0
  213. package/junit.xml +25 -0
  214. package/package.json +61 -0
  215. package/react-native.config.js +13 -0
  216. package/src/AdvancedPaywallCache.ts +433 -0
  217. package/src/BillDog.ts +983 -0
  218. package/src/BillDogConfiguration.ts +318 -0
  219. package/src/ComponentRenderer.tsx +1325 -0
  220. package/src/LayoutEngine.ts +459 -0
  221. package/src/LocalizationResolver.ts +311 -0
  222. package/src/NativePaywallView.tsx +132 -0
  223. package/src/PaywallCache.ts +358 -0
  224. package/src/PaywallStateManager.ts +154 -0
  225. package/src/PaywallView.tsx +328 -0
  226. package/src/ProductVariableProvider.ts +282 -0
  227. package/src/VariableResolver.ts +516 -0
  228. package/src/WebViewPaywall.tsx +454 -0
  229. package/src/animations/presets.ts +421 -0
  230. package/src/components/AccoladeCpsComponent.tsx +259 -0
  231. package/src/components/AccordionComponent.tsx +213 -0
  232. package/src/components/AnimatedWrapper.tsx +316 -0
  233. package/src/components/CarouselComponent.tsx +615 -0
  234. package/src/components/ChartCurveComponent.tsx +452 -0
  235. package/src/components/CheckboxComponent.tsx +185 -0
  236. package/src/components/ConfettiComponent.tsx +178 -0
  237. package/src/components/CountdownComponent.tsx +454 -0
  238. package/src/components/DelayedXComponent.tsx +266 -0
  239. package/src/components/DrawerModalComponent.tsx +324 -0
  240. package/src/components/GradientView.tsx +129 -0
  241. package/src/components/HapticFeedbackComponent.tsx +39 -0
  242. package/src/components/HorizontalProductsComponent.tsx +329 -0
  243. package/src/components/MultiChoiceGridComponent.tsx +265 -0
  244. package/src/components/NPSScaleComponent.tsx +294 -0
  245. package/src/components/NavigationComponent.tsx +329 -0
  246. package/src/components/OAuthButtonComponent.tsx +266 -0
  247. package/src/components/ProgressLoaderComponent.tsx +366 -0
  248. package/src/components/ProgressStepperComponent.tsx +250 -0
  249. package/src/components/SegmentedPickerComponent.tsx +245 -0
  250. package/src/components/ShareButtonsComponent.tsx +299 -0
  251. package/src/components/ShareCodeComponent.tsx +303 -0
  252. package/src/components/SingleChoiceGridComponent.tsx +229 -0
  253. package/src/components/SliderComponent.tsx +280 -0
  254. package/src/components/StarRatingInputComponent.tsx +223 -0
  255. package/src/components/StickerCompositeComponent.tsx +168 -0
  256. package/src/components/TabsComponent.tsx +496 -0
  257. package/src/components/TextInputComponent.tsx +258 -0
  258. package/src/components/TimelineComponent.tsx +129 -0
  259. package/src/components/ToggleComponent.tsx +142 -0
  260. package/src/components/VerticalProductsComponent.tsx +357 -0
  261. package/src/dynamicValues.ts +180 -0
  262. package/src/filters/dateFilters.ts +411 -0
  263. package/src/filters/index.ts +281 -0
  264. package/src/hooks/useOfferings.ts +192 -0
  265. package/src/hooks/usePaywall.ts +154 -0
  266. package/src/hooks/usePlacement.ts +143 -0
  267. package/src/hooks/useSubscription.ts +154 -0
  268. package/src/index.ts +245 -0
  269. package/src/models/BillDogError.ts +24 -0
  270. package/src/models/CacheFetchPolicy.ts +13 -0
  271. package/src/models/CustomProduct.ts +33 -0
  272. package/src/models/CustomerInfo.ts +26 -0
  273. package/src/models/Delegate.ts +22 -0
  274. package/src/models/EntitlementInfo.ts +27 -0
  275. package/src/models/Placement.ts +50 -0
  276. package/src/models/PurchaseParams.ts +81 -0
  277. package/src/models/PurchaseResult.ts +32 -0
  278. package/src/models/RestoreResult.ts +27 -0
  279. package/src/models/RestoredItem.ts +25 -0
  280. package/src/modules/ABTest.ts +131 -0
  281. package/src/modules/Analytics.ts +227 -0
  282. package/src/modules/Survey.ts +126 -0
  283. package/src/modules/VirtualCurrency.ts +223 -0
  284. package/src/murmur.ts +102 -0
  285. package/src/notifications/LiveActivitiesManager.ts +140 -0
  286. package/src/notifications/NotificationScheduler.ts +292 -0
  287. package/src/notifications/PurchaseNotificationHandler.ts +67 -0
  288. package/src/notifications/useDeviceToken.ts +116 -0
  289. package/src/notifications/useLiveActivity.ts +125 -0
  290. package/src/review/ReviewAnalytics.ts +89 -0
  291. package/src/review/ReviewManager.ts +355 -0
  292. package/src/review/ReviewTimingManager.ts +265 -0
  293. package/src/survey-core/types.ts +244 -0
  294. package/src/types/ambient.d.ts +9 -0
  295. package/src/types.ts +680 -0
  296. package/src/utils/ApiClient.ts +163 -0
  297. package/src/utils/CSSConverter.ts +552 -0
  298. package/src/utils/animations.ts +232 -0
  299. package/src/utils/calendarIntegration.ts +113 -0
  300. package/src/utils/configurationResolver.ts +106 -0
  301. package/src/utils/deviceDetection.ts +83 -0
  302. package/src/utils/hapticFeedback.ts +55 -0
  303. package/src/utils/iconMapper.ts +102 -0
  304. package/src/utils/notificationPermissions.ts +48 -0
  305. package/src/utils/priceFormatting.ts +119 -0
  306. package/src/utils/shareSheet.ts +55 -0
  307. package/src/utils/subscriptionUpgrade.ts +183 -0
  308. package/src/valueUtils.ts +108 -0
  309. package/src/xFlowTags.ts +463 -0
@@ -0,0 +1,463 @@
1
+ /**
2
+ * xFlow Tag Preprocessor — React Native SDK.
3
+ *
4
+ * Faithful port of src/lib/filters/xFlowTags.ts (the web reference). Implements
5
+ * the canonical grammar at src/schemas/xflow-grammar.md. Runs as a first pass
6
+ * before {{ }} interpolation. The conformance corpus
7
+ * (tests/parity/xflow-corpus/) is the cross-platform contract — web, iOS,
8
+ * Android, and React Native parsers must produce identical output.
9
+ *
10
+ * Tags: {% assign %} (with filter chains), {% if/elsif/else/endif %},
11
+ * {% unless/endunless %}, {% case/when/else/endcase %}.
12
+ * Conditions: ==, !=, >, <, >=, <=, contains, and, or, unary !, postfix exists.
13
+ */
14
+
15
+ import { getNestedValue, isTruthy, stringify, toNumber } from './valueUtils';
16
+ import { applyFilterChain } from './filters';
17
+
18
+ // ============= Public API =============
19
+
20
+ export interface XFlowTagResult {
21
+ text: string;
22
+ variables: Record<string, any>;
23
+ }
24
+
25
+ export function processXFlowTags(
26
+ text: string,
27
+ variables: Record<string, any>,
28
+ ): XFlowTagResult {
29
+ if (!text || typeof text !== 'string' || !text.includes('{%')) {
30
+ return { text: text ?? '', variables };
31
+ }
32
+ const vars = { ...variables };
33
+ const tokens = tokenize(text);
34
+ const output = processTokens(tokens, vars);
35
+ return { text: output, variables: vars };
36
+ }
37
+
38
+ // ============= Tokenizer =============
39
+
40
+ interface Token {
41
+ type: 'text' | 'tag';
42
+ content: string;
43
+ raw: string;
44
+ }
45
+
46
+ function tokenize(text: string): Token[] {
47
+ const tokens: Token[] = [];
48
+ let pos = 0;
49
+
50
+ while (pos < text.length) {
51
+ const tagStart = text.indexOf('{%', pos);
52
+ if (tagStart === -1) {
53
+ tokens.push({ type: 'text', content: text.slice(pos), raw: text.slice(pos) });
54
+ break;
55
+ }
56
+ if (tagStart > pos) {
57
+ const segment = text.slice(pos, tagStart);
58
+ tokens.push({ type: 'text', content: segment, raw: segment });
59
+ }
60
+ const tagEnd = text.indexOf('%}', tagStart);
61
+ if (tagEnd === -1) {
62
+ const segment = text.slice(tagStart);
63
+ tokens.push({ type: 'text', content: segment, raw: segment });
64
+ break;
65
+ }
66
+ const raw = text.slice(tagStart, tagEnd + 2);
67
+ const content = text.slice(tagStart + 2, tagEnd).trim();
68
+ tokens.push({ type: 'tag', content, raw });
69
+ pos = tagEnd + 2;
70
+ }
71
+ return tokens;
72
+ }
73
+
74
+ // ============= Token Processor =============
75
+
76
+ function processTokens(tokens: Token[], vars: Record<string, any>): string {
77
+ let output = '';
78
+ let i = 0;
79
+
80
+ while (i < tokens.length) {
81
+ const token = tokens[i];
82
+
83
+ if (token.type === 'text') {
84
+ output += token.content;
85
+ i++;
86
+ continue;
87
+ }
88
+
89
+ const tagContent = token.content;
90
+
91
+ if (tagContent.startsWith('assign ')) {
92
+ processAssign(tagContent, vars);
93
+ i++;
94
+ continue;
95
+ }
96
+
97
+ if (tagContent.startsWith('if ')) {
98
+ const { result, endIndex } = processIfBlock(tokens, i, vars);
99
+ output += result;
100
+ i = endIndex + 1;
101
+ continue;
102
+ }
103
+
104
+ // {% unless X %}…{% endunless %} — Liquid's negated-if. Rewrite the open
105
+ // token to `if !(X)` and reuse processIfBlock (which also closes on
106
+ // `endunless`). elsif/else keep their if-semantics.
107
+ if (tagContent.startsWith('unless ')) {
108
+ const condition = tagContent.slice('unless '.length).trim();
109
+ const rewritten = tokens.slice();
110
+ rewritten[i] = { type: 'tag', content: `if !(${condition})`, raw: token.raw };
111
+ const { result, endIndex } = processIfBlock(rewritten, i, vars);
112
+ output += result;
113
+ i = endIndex + 1;
114
+ continue;
115
+ }
116
+
117
+ // {% case X %}{% when Y %}…{% else %}…{% endcase %}
118
+ if (tagContent.startsWith('case ')) {
119
+ const { result, endIndex } = processCaseBlock(tokens, i, vars);
120
+ output += result;
121
+ i = endIndex + 1;
122
+ continue;
123
+ }
124
+
125
+ // Unknown tag — keep raw.
126
+ output += token.raw;
127
+ i++;
128
+ }
129
+
130
+ return output;
131
+ }
132
+
133
+ // ============= Assign Processing =============
134
+
135
+ function processAssign(tagContent: string, vars: Record<string, any>): void {
136
+ const assignMatch = tagContent.match(/^assign\s+(\w+)\s*=\s*(.+)$/);
137
+ if (!assignMatch) return;
138
+
139
+ const varName = assignMatch[1];
140
+ const expression = assignMatch[2].trim();
141
+ const { baseExpr, filterChain } = splitExpressionAndFilters(expression);
142
+
143
+ let value = resolveValue(baseExpr.trim(), vars);
144
+ if (filterChain) {
145
+ try {
146
+ value = applyFilterChain(value, filterChain);
147
+ } catch (error) {
148
+ console.warn(`[xFlowTags] Filter chain failed for assign "${varName}":`, error);
149
+ }
150
+ }
151
+ vars[varName] = value;
152
+ }
153
+
154
+ /** Split "value | filter1 | filter2" at the first unquoted pipe. */
155
+ function splitExpressionAndFilters(
156
+ expression: string,
157
+ ): { baseExpr: string; filterChain: string | null } {
158
+ let inQuote = false;
159
+ let quoteChar = '';
160
+
161
+ for (let i = 0; i < expression.length; i++) {
162
+ const char = expression[i];
163
+ if ((char === '"' || char === "'") && expression[i - 1] !== '\\') {
164
+ if (!inQuote) { inQuote = true; quoteChar = char; }
165
+ else if (char === quoteChar) { inQuote = false; }
166
+ } else if (char === '|' && !inQuote) {
167
+ return {
168
+ baseExpr: expression.slice(0, i),
169
+ filterChain: expression.slice(i + 1).trim(),
170
+ };
171
+ }
172
+ }
173
+ return { baseExpr: expression, filterChain: null };
174
+ }
175
+
176
+ // ============= If / Unless Block Processing =============
177
+
178
+ interface BlockResult {
179
+ result: string;
180
+ endIndex: number;
181
+ }
182
+
183
+ /**
184
+ * Process an {% if %} / {% unless %} block. `endif` and `endunless` are
185
+ * interchangeable terminators (grammar §2.3).
186
+ */
187
+ function processIfBlock(
188
+ tokens: Token[],
189
+ startIndex: number,
190
+ vars: Record<string, any>,
191
+ ): BlockResult {
192
+ const openingCondition = tokens[startIndex].content.slice(3).trim(); // remove "if "
193
+
194
+ const branches: { condition: string | null; bodyTokens: Token[] }[] = [];
195
+ let currentCondition: string | null = openingCondition;
196
+ let currentBody: Token[] = [];
197
+ let depth = 0;
198
+ let i = startIndex + 1;
199
+
200
+ while (i < tokens.length) {
201
+ const token = tokens[i];
202
+
203
+ if (token.type === 'tag') {
204
+ const content = token.content;
205
+
206
+ if (content.startsWith('if ') || content.startsWith('unless ')) {
207
+ depth++;
208
+ currentBody.push(token);
209
+ i++;
210
+ continue;
211
+ }
212
+
213
+ if (content === 'endif' || content === 'endunless') {
214
+ if (depth > 0) {
215
+ depth--;
216
+ currentBody.push(token);
217
+ i++;
218
+ continue;
219
+ }
220
+ branches.push({ condition: currentCondition, bodyTokens: currentBody });
221
+ break;
222
+ }
223
+
224
+ if (depth === 0 && content.startsWith('elsif ')) {
225
+ branches.push({ condition: currentCondition, bodyTokens: currentBody });
226
+ currentCondition = content.slice(6).trim();
227
+ currentBody = [];
228
+ i++;
229
+ continue;
230
+ }
231
+
232
+ if (depth === 0 && content === 'else') {
233
+ branches.push({ condition: currentCondition, bodyTokens: currentBody });
234
+ currentCondition = null;
235
+ currentBody = [];
236
+ i++;
237
+ continue;
238
+ }
239
+ }
240
+
241
+ currentBody.push(token);
242
+ i++;
243
+ }
244
+
245
+ if (i >= tokens.length) {
246
+ branches.push({ condition: currentCondition, bodyTokens: currentBody });
247
+ }
248
+
249
+ for (const branch of branches) {
250
+ if (branch.condition === null || evaluateCondition(branch.condition, vars)) {
251
+ return { result: processTokens(branch.bodyTokens, vars), endIndex: i };
252
+ }
253
+ }
254
+ return { result: '', endIndex: i };
255
+ }
256
+
257
+ // ============= Case Block Processing =============
258
+
259
+ /**
260
+ * Process {% case X %}{% when Y %}…{% when A, B %}…{% else %}…{% endcase %}.
261
+ * Evaluates X once; emits the first `when` whose value (any of its
262
+ * comma-separated candidates) is string-equal to X; falls back to `else`.
263
+ */
264
+ function processCaseBlock(
265
+ tokens: Token[],
266
+ startIndex: number,
267
+ vars: Record<string, any>,
268
+ ): BlockResult {
269
+ const caseExpr = tokens[startIndex].content.slice('case '.length).trim();
270
+ const caseValue = resolveValue(caseExpr, vars);
271
+
272
+ const branches: { condition: string | null; bodyTokens: Token[] }[] = [];
273
+ let currentCondition: string | null = null;
274
+ let currentBody: Token[] = [];
275
+ let inBranch = false;
276
+ let depth = 0;
277
+ let i = startIndex + 1;
278
+
279
+ while (i < tokens.length) {
280
+ const token = tokens[i];
281
+
282
+ if (token.type === 'tag') {
283
+ const content = token.content;
284
+
285
+ if (content.startsWith('case ')) {
286
+ depth++;
287
+ currentBody.push(token);
288
+ i++;
289
+ continue;
290
+ }
291
+
292
+ if (content === 'endcase') {
293
+ if (depth > 0) {
294
+ depth--;
295
+ currentBody.push(token);
296
+ i++;
297
+ continue;
298
+ }
299
+ if (inBranch) branches.push({ condition: currentCondition, bodyTokens: currentBody });
300
+ break;
301
+ }
302
+
303
+ if (depth === 0 && content.startsWith('when ')) {
304
+ if (inBranch) branches.push({ condition: currentCondition, bodyTokens: currentBody });
305
+ currentCondition = content.slice('when '.length).trim();
306
+ currentBody = [];
307
+ inBranch = true;
308
+ i++;
309
+ continue;
310
+ }
311
+
312
+ if (depth === 0 && content === 'else') {
313
+ if (inBranch) branches.push({ condition: currentCondition, bodyTokens: currentBody });
314
+ currentCondition = null;
315
+ currentBody = [];
316
+ inBranch = true;
317
+ i++;
318
+ continue;
319
+ }
320
+ }
321
+
322
+ if (inBranch) currentBody.push(token);
323
+ i++;
324
+ }
325
+
326
+ if (i >= tokens.length && inBranch) {
327
+ branches.push({ condition: currentCondition, bodyTokens: currentBody });
328
+ }
329
+
330
+ const caseStr = stringify(caseValue);
331
+ for (const branch of branches) {
332
+ if (branch.condition === null) {
333
+ // else branch
334
+ return { result: processTokens(branch.bodyTokens, vars), endIndex: i };
335
+ }
336
+ // when supports comma-separated candidates: `when "a", "b"`.
337
+ const candidates = branch.condition.split(',').map((c) => c.trim());
338
+ for (const candidate of candidates) {
339
+ if (stringify(resolveValue(candidate, vars)) === caseStr) {
340
+ return { result: processTokens(branch.bodyTokens, vars), endIndex: i };
341
+ }
342
+ }
343
+ }
344
+ return { result: '', endIndex: i };
345
+ }
346
+
347
+ // ============= Condition Evaluation =============
348
+
349
+ function evaluateCondition(condition: string, vars: Record<string, any>): boolean {
350
+ const trimmed = condition.trim();
351
+
352
+ // Unary `!` negation. Strips one optional outer paren pair so `!(a and b)`
353
+ // negates the whole inner condition.
354
+ if (trimmed.startsWith('!')) {
355
+ let inner = trimmed.slice(1).trim();
356
+ if (inner.startsWith('(') && inner.endsWith(')')) {
357
+ inner = inner.slice(1, -1).trim();
358
+ }
359
+ return !evaluateCondition(inner, vars);
360
+ }
361
+
362
+ const parts = splitLogical(trimmed);
363
+
364
+ if (parts.length === 1) {
365
+ return evaluateComparison(parts[0].expr.trim(), vars);
366
+ }
367
+
368
+ let result = evaluateComparison(parts[0].expr.trim(), vars);
369
+ for (let i = 1; i < parts.length; i++) {
370
+ const { operator, expr } = parts[i];
371
+ const right = evaluateComparison(expr.trim(), vars);
372
+ result = operator === 'and' ? result && right : result || right;
373
+ }
374
+ return result;
375
+ }
376
+
377
+ interface LogicalPart {
378
+ operator: 'and' | 'or' | null;
379
+ expr: string;
380
+ }
381
+
382
+ function splitLogical(condition: string): LogicalPart[] {
383
+ const segments = condition.split(/\b(and|or)\b/);
384
+ const result: LogicalPart[] = [];
385
+
386
+ for (let i = 0; i < segments.length; i++) {
387
+ if (i === 0) {
388
+ result.push({ operator: null, expr: segments[0] });
389
+ } else if (segments[i] === 'and' || segments[i] === 'or') {
390
+ result.push({ operator: segments[i] as 'and' | 'or', expr: segments[i + 1] || '' });
391
+ i++;
392
+ }
393
+ }
394
+ return result.length > 0 ? result : [{ operator: null, expr: condition }];
395
+ }
396
+
397
+ /** Evaluate "a == b", "a contains b", "a exists", or bare "a" (truthiness). */
398
+ function evaluateComparison(expr: string, vars: Record<string, any>): boolean {
399
+ // Postfix `exists` — checked before binary operators so an identifier
400
+ // containing "exists" isn't mis-parsed.
401
+ if (expr.endsWith(' exists')) {
402
+ const leftStr = expr.slice(0, -' exists'.length).trim();
403
+ if (leftStr) {
404
+ const value = resolveValue(leftStr, vars);
405
+ return value !== null && value !== undefined && stringify(value) !== '';
406
+ }
407
+ }
408
+
409
+ const operators = ['!=', '>=', '<=', '==', '>', '<', ' contains '];
410
+ for (const op of operators) {
411
+ const opIndex = expr.indexOf(op);
412
+ if (opIndex !== -1) {
413
+ const left = resolveValue(expr.slice(0, opIndex).trim(), vars);
414
+ const right = resolveValue(expr.slice(opIndex + op.length).trim(), vars);
415
+ return compareValues(left, right, op.trim());
416
+ }
417
+ }
418
+
419
+ return isTruthy(resolveValue(expr.trim(), vars));
420
+ }
421
+
422
+ function isNullishEqual(a: any, b: any): boolean {
423
+ const an = a === null || a === undefined;
424
+ const bn = b === null || b === undefined;
425
+ if (an && bn) return true;
426
+ if (an || bn) return false;
427
+ return stringify(a) === stringify(b);
428
+ }
429
+
430
+ function compareValues(left: any, right: any, op: string): boolean {
431
+ switch (op) {
432
+ case '==': return isNullishEqual(left, right);
433
+ case '!=': return !isNullishEqual(left, right);
434
+ case '>': return toNumber(left) > toNumber(right);
435
+ case '<': return toNumber(left) < toNumber(right);
436
+ case '>=': return toNumber(left) >= toNumber(right);
437
+ case '<=': return toNumber(left) <= toNumber(right);
438
+ case 'contains': {
439
+ if (Array.isArray(left)) {
440
+ return left.some((el) => stringify(el) === stringify(right));
441
+ }
442
+ return stringify(left).includes(stringify(right));
443
+ }
444
+ default: return false;
445
+ }
446
+ }
447
+
448
+ // ============= Value Resolution =============
449
+
450
+ function resolveValue(token: string, vars: Record<string, any>): any {
451
+ if (!token) return undefined;
452
+
453
+ if ((token.startsWith('"') && token.endsWith('"')) ||
454
+ (token.startsWith("'") && token.endsWith("'"))) {
455
+ return token.slice(1, -1);
456
+ }
457
+ if (token === 'true') return true;
458
+ if (token === 'false') return false;
459
+ if (token === 'nil' || token === 'null') return null;
460
+ if (/^-?\d+(\.\d+)?$/.test(token)) return Number(token);
461
+
462
+ return getNestedValue(vars, token);
463
+ }