@defra/forms-model 3.0.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 (336) hide show
  1. package/LICENCE +8 -0
  2. package/README.md +22 -0
  3. package/dist/browser/components/component-types.d.ts +3 -0
  4. package/dist/browser/components/component-types.d.ts.map +1 -0
  5. package/dist/browser/components/component-types.js +220 -0
  6. package/dist/browser/components/component-types.js.map +1 -0
  7. package/dist/browser/components/conditional-component-types.d.ts +3 -0
  8. package/dist/browser/components/conditional-component-types.d.ts.map +1 -0
  9. package/dist/browser/components/conditional-component-types.js +28 -0
  10. package/dist/browser/components/conditional-component-types.js.map +1 -0
  11. package/dist/browser/components/index.d.ts +4 -0
  12. package/dist/browser/components/index.d.ts.map +1 -0
  13. package/dist/browser/components/index.js +45 -0
  14. package/dist/browser/components/index.js.map +1 -0
  15. package/dist/browser/components/types.d.ts +244 -0
  16. package/dist/browser/components/types.d.ts.map +1 -0
  17. package/dist/browser/components/types.js +56 -0
  18. package/dist/browser/components/types.js.map +1 -0
  19. package/dist/browser/conditions/condition-abstract.d.ts +16 -0
  20. package/dist/browser/conditions/condition-abstract.d.ts.map +1 -0
  21. package/dist/browser/conditions/condition-abstract.js +62 -0
  22. package/dist/browser/conditions/condition-abstract.js.map +1 -0
  23. package/dist/browser/conditions/condition-field.d.ts +13 -0
  24. package/dist/browser/conditions/condition-field.d.ts.map +1 -0
  25. package/dist/browser/conditions/condition-field.js +45 -0
  26. package/dist/browser/conditions/condition-field.js.map +1 -0
  27. package/dist/browser/conditions/condition-group-def.d.ts +10 -0
  28. package/dist/browser/conditions/condition-group-def.d.ts.map +1 -0
  29. package/dist/browser/conditions/condition-group-def.js +45 -0
  30. package/dist/browser/conditions/condition-group-def.js.map +1 -0
  31. package/dist/browser/conditions/condition-group.d.ts +15 -0
  32. package/dist/browser/conditions/condition-group.d.ts.map +1 -0
  33. package/dist/browser/conditions/condition-group.js +64 -0
  34. package/dist/browser/conditions/condition-group.js.map +1 -0
  35. package/dist/browser/conditions/condition-model.d.ts +41 -0
  36. package/dist/browser/conditions/condition-model.d.ts.map +1 -0
  37. package/dist/browser/conditions/condition-model.js +214 -0
  38. package/dist/browser/conditions/condition-model.js.map +1 -0
  39. package/dist/browser/conditions/condition-operators.d.ts +248 -0
  40. package/dist/browser/conditions/condition-operators.d.ts.map +1 -0
  41. package/dist/browser/conditions/condition-operators.js +132 -0
  42. package/dist/browser/conditions/condition-operators.js.map +1 -0
  43. package/dist/browser/conditions/condition-ref.d.ts +12 -0
  44. package/dist/browser/conditions/condition-ref.d.ts.map +1 -0
  45. package/dist/browser/conditions/condition-ref.js +51 -0
  46. package/dist/browser/conditions/condition-ref.js.map +1 -0
  47. package/dist/browser/conditions/condition-value-abstract.d.ts +8 -0
  48. package/dist/browser/conditions/condition-value-abstract.d.ts.map +1 -0
  49. package/dist/browser/conditions/condition-value-abstract.js +41 -0
  50. package/dist/browser/conditions/condition-value-abstract.js.map +1 -0
  51. package/dist/browser/conditions/condition-value-registration.d.ts +10 -0
  52. package/dist/browser/conditions/condition-value-registration.d.ts.map +1 -0
  53. package/dist/browser/conditions/condition-value-registration.js +37 -0
  54. package/dist/browser/conditions/condition-value-registration.js.map +1 -0
  55. package/dist/browser/conditions/condition-values.d.ts +38 -0
  56. package/dist/browser/conditions/condition-values.d.ts.map +1 -0
  57. package/dist/browser/conditions/condition-values.js +127 -0
  58. package/dist/browser/conditions/condition-values.js.map +1 -0
  59. package/dist/browser/conditions/condition.d.ts +15 -0
  60. package/dist/browser/conditions/condition.d.ts.map +1 -0
  61. package/dist/browser/conditions/condition.js +56 -0
  62. package/dist/browser/conditions/condition.js.map +1 -0
  63. package/dist/browser/conditions/helpers.d.ts +3 -0
  64. package/dist/browser/conditions/helpers.d.ts.map +1 -0
  65. package/dist/browser/conditions/helpers.js +26 -0
  66. package/dist/browser/conditions/helpers.js.map +1 -0
  67. package/dist/browser/conditions/index.d.ts +11 -0
  68. package/dist/browser/conditions/index.d.ts.map +1 -0
  69. package/dist/browser/conditions/index.js +144 -0
  70. package/dist/browser/conditions/index.js.map +1 -0
  71. package/dist/browser/conditions/inline-condition-model.d.ts +86 -0
  72. package/dist/browser/conditions/inline-condition-model.d.ts.map +1 -0
  73. package/dist/browser/conditions/inline-condition-model.js +418 -0
  74. package/dist/browser/conditions/inline-condition-model.js.map +1 -0
  75. package/dist/browser/conditions/inline-condition-operators.d.ts +175 -0
  76. package/dist/browser/conditions/inline-condition-operators.d.ts.map +1 -0
  77. package/dist/browser/conditions/inline-condition-operators.js +132 -0
  78. package/dist/browser/conditions/inline-condition-operators.js.map +1 -0
  79. package/dist/browser/conditions/inline-condition-values.d.ts +93 -0
  80. package/dist/browser/conditions/inline-condition-values.d.ts.map +1 -0
  81. package/dist/browser/conditions/inline-condition-values.js +159 -0
  82. package/dist/browser/conditions/inline-condition-values.js.map +1 -0
  83. package/dist/browser/conditions/types.d.ts +38 -0
  84. package/dist/browser/conditions/types.d.ts.map +1 -0
  85. package/dist/browser/conditions/types.js +24 -0
  86. package/dist/browser/conditions/types.js.map +1 -0
  87. package/dist/browser/data-model/conditions-wrapper.d.ts +19 -0
  88. package/dist/browser/data-model/conditions-wrapper.d.ts.map +1 -0
  89. package/dist/browser/data-model/conditions-wrapper.js +49 -0
  90. package/dist/browser/data-model/conditions-wrapper.js.map +1 -0
  91. package/dist/browser/data-model/index.d.ts +4 -0
  92. package/dist/browser/data-model/index.d.ts.map +1 -0
  93. package/dist/browser/data-model/index.js +66 -0
  94. package/dist/browser/data-model/index.js.map +1 -0
  95. package/dist/browser/data-model/input-wrapper.d.ts +16 -0
  96. package/dist/browser/data-model/input-wrapper.d.ts.map +1 -0
  97. package/dist/browser/data-model/input-wrapper.js +49 -0
  98. package/dist/browser/data-model/input-wrapper.js.map +1 -0
  99. package/dist/browser/data-model/types.d.ts +153 -0
  100. package/dist/browser/data-model/types.d.ts.map +1 -0
  101. package/dist/browser/data-model/types.js +34 -0
  102. package/dist/browser/data-model/types.js.map +1 -0
  103. package/dist/browser/form/form-configuration.d.ts +8 -0
  104. package/dist/browser/form/form-configuration.d.ts.map +1 -0
  105. package/dist/browser/form/form-configuration.js +38 -0
  106. package/dist/browser/form/form-configuration.js.map +1 -0
  107. package/dist/browser/form/index.d.ts +2 -0
  108. package/dist/browser/form/index.d.ts.map +1 -0
  109. package/dist/browser/form/index.js +24 -0
  110. package/dist/browser/form/index.js.map +1 -0
  111. package/dist/browser/index.d.ts +12 -0
  112. package/dist/browser/index.d.ts.map +1 -0
  113. package/dist/browser/index.js +138 -0
  114. package/dist/browser/index.js.map +1 -0
  115. package/dist/browser/migration/index.d.ts +2 -0
  116. package/dist/browser/migration/index.d.ts.map +1 -0
  117. package/dist/browser/migration/index.js +24 -0
  118. package/dist/browser/migration/index.js.map +1 -0
  119. package/dist/browser/migration/migration.0-2.d.ts +3 -0
  120. package/dist/browser/migration/migration.0-2.d.ts.map +1 -0
  121. package/dist/browser/migration/migration.0-2.js +65 -0
  122. package/dist/browser/migration/migration.0-2.js.map +1 -0
  123. package/dist/browser/migration/migration.1-2.d.ts +3 -0
  124. package/dist/browser/migration/migration.1-2.d.ts.map +1 -0
  125. package/dist/browser/migration/migration.1-2.js +108 -0
  126. package/dist/browser/migration/migration.1-2.js.map +1 -0
  127. package/dist/browser/migration/types.d.ts +2 -0
  128. package/dist/browser/migration/types.d.ts.map +1 -0
  129. package/dist/browser/migration/types.js +16 -0
  130. package/dist/browser/migration/types.js.map +1 -0
  131. package/dist/browser/migration/whichMigrations.d.ts +7 -0
  132. package/dist/browser/migration/whichMigrations.d.ts.map +1 -0
  133. package/dist/browser/migration/whichMigrations.js +38 -0
  134. package/dist/browser/migration/whichMigrations.js.map +1 -0
  135. package/dist/browser/schema/index.d.ts +2 -0
  136. package/dist/browser/schema/index.d.ts.map +1 -0
  137. package/dist/browser/schema/index.js +30 -0
  138. package/dist/browser/schema/index.js.map +1 -0
  139. package/dist/browser/schema/schema.d.ts +20 -0
  140. package/dist/browser/schema/schema.d.ts.map +1 -0
  141. package/dist/browser/schema/schema.js +255 -0
  142. package/dist/browser/schema/schema.js.map +1 -0
  143. package/dist/browser/utils/helpers.d.ts +9 -0
  144. package/dist/browser/utils/helpers.d.ts.map +1 -0
  145. package/dist/browser/utils/helpers.js +48 -0
  146. package/dist/browser/utils/helpers.js.map +1 -0
  147. package/dist/browser/utils/logger.d.ts +11 -0
  148. package/dist/browser/utils/logger.d.ts.map +1 -0
  149. package/dist/browser/utils/logger.js +46 -0
  150. package/dist/browser/utils/logger.js.map +1 -0
  151. package/dist/module/components/component-types.d.ts +3 -0
  152. package/dist/module/components/component-types.d.ts.map +1 -0
  153. package/dist/module/components/component-types.js +206 -0
  154. package/dist/module/components/component-types.js.map +1 -0
  155. package/dist/module/components/conditional-component-types.d.ts +3 -0
  156. package/dist/module/components/conditional-component-types.d.ts.map +1 -0
  157. package/dist/module/components/conditional-component-types.js +14 -0
  158. package/dist/module/components/conditional-component-types.js.map +1 -0
  159. package/dist/module/components/index.d.ts +4 -0
  160. package/dist/module/components/index.d.ts.map +1 -0
  161. package/dist/module/components/index.js +34 -0
  162. package/dist/module/components/index.js.map +1 -0
  163. package/dist/module/components/types.d.ts +244 -0
  164. package/dist/module/components/types.d.ts.map +1 -0
  165. package/dist/module/components/types.js +42 -0
  166. package/dist/module/components/types.js.map +1 -0
  167. package/dist/module/conditions/condition-abstract.d.ts +16 -0
  168. package/dist/module/conditions/condition-abstract.d.ts.map +1 -0
  169. package/dist/module/conditions/condition-abstract.js +45 -0
  170. package/dist/module/conditions/condition-abstract.js.map +1 -0
  171. package/dist/module/conditions/condition-field.d.ts +13 -0
  172. package/dist/module/conditions/condition-field.d.ts.map +1 -0
  173. package/dist/module/conditions/condition-field.js +26 -0
  174. package/dist/module/conditions/condition-field.js.map +1 -0
  175. package/dist/module/conditions/condition-group-def.d.ts +10 -0
  176. package/dist/module/conditions/condition-group-def.d.ts.map +1 -0
  177. package/dist/module/conditions/condition-group-def.js +26 -0
  178. package/dist/module/conditions/condition-group-def.js.map +1 -0
  179. package/dist/module/conditions/condition-group.d.ts +15 -0
  180. package/dist/module/conditions/condition-group.d.ts.map +1 -0
  181. package/dist/module/conditions/condition-group.js +47 -0
  182. package/dist/module/conditions/condition-group.js.map +1 -0
  183. package/dist/module/conditions/condition-model.d.ts +41 -0
  184. package/dist/module/conditions/condition-model.d.ts.map +1 -0
  185. package/dist/module/conditions/condition-model.js +199 -0
  186. package/dist/module/conditions/condition-model.js.map +1 -0
  187. package/dist/module/conditions/condition-operators.d.ts +248 -0
  188. package/dist/module/conditions/condition-operators.d.ts.map +1 -0
  189. package/dist/module/conditions/condition-operators.js +119 -0
  190. package/dist/module/conditions/condition-operators.js.map +1 -0
  191. package/dist/module/conditions/condition-ref.d.ts +12 -0
  192. package/dist/module/conditions/condition-ref.d.ts.map +1 -0
  193. package/dist/module/conditions/condition-ref.js +33 -0
  194. package/dist/module/conditions/condition-ref.js.map +1 -0
  195. package/dist/module/conditions/condition-value-abstract.d.ts +8 -0
  196. package/dist/module/conditions/condition-value-abstract.d.ts.map +1 -0
  197. package/dist/module/conditions/condition-value-abstract.js +24 -0
  198. package/dist/module/conditions/condition-value-abstract.js.map +1 -0
  199. package/dist/module/conditions/condition-value-registration.d.ts +10 -0
  200. package/dist/module/conditions/condition-value-registration.d.ts.map +1 -0
  201. package/dist/module/conditions/condition-value-registration.js +19 -0
  202. package/dist/module/conditions/condition-value-registration.js.map +1 -0
  203. package/dist/module/conditions/condition-values.d.ts +38 -0
  204. package/dist/module/conditions/condition-values.d.ts.map +1 -0
  205. package/dist/module/conditions/condition-values.js +106 -0
  206. package/dist/module/conditions/condition-values.js.map +1 -0
  207. package/dist/module/conditions/condition.d.ts +15 -0
  208. package/dist/module/conditions/condition.d.ts.map +1 -0
  209. package/dist/module/conditions/condition.js +40 -0
  210. package/dist/module/conditions/condition.js.map +1 -0
  211. package/dist/module/conditions/helpers.d.ts +3 -0
  212. package/dist/module/conditions/helpers.d.ts.map +1 -0
  213. package/dist/module/conditions/helpers.js +12 -0
  214. package/dist/module/conditions/helpers.js.map +1 -0
  215. package/dist/module/conditions/index.d.ts +11 -0
  216. package/dist/module/conditions/index.d.ts.map +1 -0
  217. package/dist/module/conditions/index.js +140 -0
  218. package/dist/module/conditions/index.js.map +1 -0
  219. package/dist/module/conditions/inline-condition-model.d.ts +86 -0
  220. package/dist/module/conditions/inline-condition-model.d.ts.map +1 -0
  221. package/dist/module/conditions/inline-condition-model.js +384 -0
  222. package/dist/module/conditions/inline-condition-model.js.map +1 -0
  223. package/dist/module/conditions/inline-condition-operators.d.ts +175 -0
  224. package/dist/module/conditions/inline-condition-operators.d.ts.map +1 -0
  225. package/dist/module/conditions/inline-condition-operators.js +119 -0
  226. package/dist/module/conditions/inline-condition-operators.js.map +1 -0
  227. package/dist/module/conditions/inline-condition-values.d.ts +93 -0
  228. package/dist/module/conditions/inline-condition-values.d.ts.map +1 -0
  229. package/dist/module/conditions/inline-condition-values.js +134 -0
  230. package/dist/module/conditions/inline-condition-values.js.map +1 -0
  231. package/dist/module/conditions/types.d.ts +38 -0
  232. package/dist/module/conditions/types.d.ts.map +1 -0
  233. package/dist/module/conditions/types.js +10 -0
  234. package/dist/module/conditions/types.js.map +1 -0
  235. package/dist/module/data-model/conditions-wrapper.d.ts +19 -0
  236. package/dist/module/data-model/conditions-wrapper.d.ts.map +1 -0
  237. package/dist/module/data-model/conditions-wrapper.js +30 -0
  238. package/dist/module/data-model/conditions-wrapper.js.map +1 -0
  239. package/dist/module/data-model/index.d.ts +4 -0
  240. package/dist/module/data-model/index.d.ts.map +1 -0
  241. package/dist/module/data-model/index.js +55 -0
  242. package/dist/module/data-model/index.js.map +1 -0
  243. package/dist/module/data-model/input-wrapper.d.ts +16 -0
  244. package/dist/module/data-model/input-wrapper.d.ts.map +1 -0
  245. package/dist/module/data-model/input-wrapper.js +25 -0
  246. package/dist/module/data-model/input-wrapper.js.map +1 -0
  247. package/dist/module/data-model/types.d.ts +153 -0
  248. package/dist/module/data-model/types.d.ts.map +1 -0
  249. package/dist/module/data-model/types.js +20 -0
  250. package/dist/module/data-model/types.js.map +1 -0
  251. package/dist/module/form/form-configuration.d.ts +8 -0
  252. package/dist/module/form/form-configuration.d.ts.map +1 -0
  253. package/dist/module/form/form-configuration.js +17 -0
  254. package/dist/module/form/form-configuration.js.map +1 -0
  255. package/dist/module/form/index.d.ts +2 -0
  256. package/dist/module/form/index.d.ts.map +1 -0
  257. package/dist/module/form/index.js +11 -0
  258. package/dist/module/form/index.js.map +1 -0
  259. package/dist/module/index.d.ts +12 -0
  260. package/dist/module/index.d.ts.map +1 -0
  261. package/dist/module/index.js +135 -0
  262. package/dist/module/index.js.map +1 -0
  263. package/dist/module/migration/index.d.ts +2 -0
  264. package/dist/module/migration/index.d.ts.map +1 -0
  265. package/dist/module/migration/index.js +11 -0
  266. package/dist/module/migration/index.js.map +1 -0
  267. package/dist/module/migration/migration.0-2.d.ts +3 -0
  268. package/dist/module/migration/migration.0-2.d.ts.map +1 -0
  269. package/dist/module/migration/migration.0-2.js +42 -0
  270. package/dist/module/migration/migration.0-2.js.map +1 -0
  271. package/dist/module/migration/migration.1-2.d.ts +3 -0
  272. package/dist/module/migration/migration.1-2.d.ts.map +1 -0
  273. package/dist/module/migration/migration.1-2.js +90 -0
  274. package/dist/module/migration/migration.1-2.js.map +1 -0
  275. package/dist/module/migration/types.d.ts +2 -0
  276. package/dist/module/migration/types.d.ts.map +1 -0
  277. package/dist/module/migration/types.js +1 -0
  278. package/dist/module/migration/types.js.map +1 -0
  279. package/dist/module/migration/whichMigrations.d.ts +7 -0
  280. package/dist/module/migration/whichMigrations.d.ts.map +1 -0
  281. package/dist/module/migration/whichMigrations.js +26 -0
  282. package/dist/module/migration/whichMigrations.js.map +1 -0
  283. package/dist/module/schema/index.d.ts +2 -0
  284. package/dist/module/schema/index.d.ts.map +1 -0
  285. package/dist/module/schema/index.js +17 -0
  286. package/dist/module/schema/index.js.map +1 -0
  287. package/dist/module/schema/schema.d.ts +20 -0
  288. package/dist/module/schema/schema.d.ts.map +1 -0
  289. package/dist/module/schema/schema.js +239 -0
  290. package/dist/module/schema/schema.js.map +1 -0
  291. package/dist/module/utils/helpers.d.ts +9 -0
  292. package/dist/module/utils/helpers.d.ts.map +1 -0
  293. package/dist/module/utils/helpers.js +35 -0
  294. package/dist/module/utils/helpers.js.map +1 -0
  295. package/dist/module/utils/logger.d.ts +11 -0
  296. package/dist/module/utils/logger.d.ts.map +1 -0
  297. package/dist/module/utils/logger.js +27 -0
  298. package/dist/module/utils/logger.js.map +1 -0
  299. package/package.json +35 -0
  300. package/src/components/component-types.ts +229 -0
  301. package/src/components/conditional-component-types.ts +14 -0
  302. package/src/components/index.ts +3 -0
  303. package/src/components/types.ts +362 -0
  304. package/src/conditions/condition-abstract.ts +59 -0
  305. package/src/conditions/condition-field.ts +29 -0
  306. package/src/conditions/condition-group-def.ts +29 -0
  307. package/src/conditions/condition-group.ts +61 -0
  308. package/src/conditions/condition-model.ts +314 -0
  309. package/src/conditions/condition-operators.ts +176 -0
  310. package/src/conditions/condition-ref.ts +47 -0
  311. package/src/conditions/condition-value-abstract.ts +31 -0
  312. package/src/conditions/condition-value-registration.ts +18 -0
  313. package/src/conditions/condition-values.ts +146 -0
  314. package/src/conditions/condition.ts +64 -0
  315. package/src/conditions/helpers.ts +7 -0
  316. package/src/conditions/index.ts +27 -0
  317. package/src/conditions/inline-condition-model.ts +550 -0
  318. package/src/conditions/inline-condition-operators.ts +166 -0
  319. package/src/conditions/inline-condition-values.ts +153 -0
  320. package/src/conditions/types.ts +30 -0
  321. package/src/data-model/conditions-wrapper.ts +42 -0
  322. package/src/data-model/index.ts +3 -0
  323. package/src/data-model/input-wrapper.ts +40 -0
  324. package/src/data-model/types.ts +179 -0
  325. package/src/form/form-configuration.ts +21 -0
  326. package/src/form/index.ts +1 -0
  327. package/src/index.ts +11 -0
  328. package/src/migration/index.ts +1 -0
  329. package/src/migration/migration.0-2.ts +29 -0
  330. package/src/migration/migration.1-2.ts +105 -0
  331. package/src/migration/types.ts +1 -0
  332. package/src/migration/whichMigrations.ts +23 -0
  333. package/src/schema/index.ts +1 -0
  334. package/src/schema/schema.ts +300 -0
  335. package/src/utils/helpers.ts +37 -0
  336. package/src/utils/logger.ts +29 -0
@@ -0,0 +1,314 @@
1
+ import { ConditionField } from './condition-field'
2
+ import { ConditionGroupDef } from './condition-group-def'
3
+ import { Condition } from './condition'
4
+ import { ConditionRef } from './condition-ref'
5
+ import { ConditionGroup } from './condition-group'
6
+ import { conditionValueFrom } from './condition-values'
7
+ import { toPresentationString, toExpression } from './helpers'
8
+ import { Coordinator, ConditionsArray } from './types'
9
+
10
+ type ConditionRawObject =
11
+ | ConditionsModel
12
+ | {
13
+ name: string
14
+ conditions: Condition[]
15
+ }
16
+
17
+ export class ConditionsModel {
18
+ #groupedConditions: ConditionsArray = []
19
+ #userGroupedConditions: ConditionsArray = []
20
+ #conditionName: string | undefined = undefined
21
+
22
+ constructor(_conditionsObject?: ConditionRawObject) {}
23
+
24
+ clone() {
25
+ const toReturn = new ConditionsModel()
26
+ toReturn.#groupedConditions = this.#groupedConditions.map((it) =>
27
+ it.clone()
28
+ )
29
+ toReturn.#userGroupedConditions = this.#userGroupedConditions.map((it) =>
30
+ it.clone()
31
+ )
32
+ toReturn.#conditionName = this.#conditionName
33
+ return toReturn
34
+ }
35
+
36
+ clear() {
37
+ this.#userGroupedConditions = []
38
+ this.#groupedConditions = []
39
+ this.#conditionName = undefined
40
+ return this
41
+ }
42
+
43
+ set name(name) {
44
+ this.#conditionName = name
45
+ }
46
+
47
+ get name() {
48
+ return this.#conditionName
49
+ }
50
+
51
+ add(condition: Condition) {
52
+ const coordinatorExpected = this.#userGroupedConditions.length !== 0
53
+
54
+ if (condition.getCoordinator() && !coordinatorExpected) {
55
+ throw Error('No coordinator allowed on the first condition')
56
+ } else if (!condition.getCoordinator() && coordinatorExpected) {
57
+ throw Error('Coordinator must be present on subsequent conditions')
58
+ }
59
+
60
+ this.#userGroupedConditions.push(condition)
61
+ this.#groupedConditions = this._applyGroups(this.#userGroupedConditions)
62
+
63
+ return this
64
+ }
65
+
66
+ replace(index: number, condition: Condition) {
67
+ const coordinatorExpected = index !== 0
68
+
69
+ if (condition.getCoordinator() && !coordinatorExpected) {
70
+ throw Error('No coordinator allowed on the first condition')
71
+ } else if (!condition.getCoordinator() && coordinatorExpected) {
72
+ throw Error('Coordinator must be present on subsequent conditions')
73
+ } else if (index >= this.#userGroupedConditions.length) {
74
+ throw Error(
75
+ `Cannot replace condition ${index} as no such condition exists`
76
+ )
77
+ }
78
+
79
+ this.#userGroupedConditions.splice(index, 1, condition)
80
+ this.#groupedConditions = this._applyGroups(this.#userGroupedConditions)
81
+
82
+ return this
83
+ }
84
+
85
+ remove(indexes: number[]) {
86
+ this.#userGroupedConditions = this.#userGroupedConditions
87
+ .filter((_condition, index) => !indexes.includes(index))
88
+ .map((condition, index) =>
89
+ index === 0 ? condition.asFirstCondition() : condition
90
+ )
91
+
92
+ this.#groupedConditions = this._applyGroups(this.#userGroupedConditions)
93
+ return this
94
+ }
95
+
96
+ addGroups(groupDefs: ConditionGroupDef[]) {
97
+ this.#userGroupedConditions = this._group(
98
+ this.#userGroupedConditions,
99
+ groupDefs
100
+ )
101
+ this.#groupedConditions = this._applyGroups(this.#userGroupedConditions)
102
+ return this
103
+ }
104
+
105
+ splitGroup(index: number) {
106
+ this.#userGroupedConditions = this._ungroup(
107
+ this.#userGroupedConditions,
108
+ index
109
+ )
110
+ this.#groupedConditions = this._applyGroups(this.#userGroupedConditions)
111
+ return this
112
+ }
113
+
114
+ moveEarlier(index: number) {
115
+ if (index > 0 && index < this.#userGroupedConditions.length) {
116
+ this.#userGroupedConditions.splice(
117
+ index - 1,
118
+ 0,
119
+ this.#userGroupedConditions.splice(index, 1)[0]
120
+ )
121
+ if (index === 1) {
122
+ this.switchCoordinators()
123
+ }
124
+ this.#groupedConditions = this._applyGroups(this.#userGroupedConditions)
125
+ }
126
+ return this
127
+ }
128
+
129
+ moveLater(index: number) {
130
+ if (index >= 0 && index < this.#userGroupedConditions.length - 1) {
131
+ this.#userGroupedConditions.splice(
132
+ index + 1,
133
+ 0,
134
+ this.#userGroupedConditions.splice(index, 1)[0]
135
+ )
136
+ if (index === 0) {
137
+ this.switchCoordinators()
138
+ }
139
+ this.#groupedConditions = this._applyGroups(this.#userGroupedConditions)
140
+ }
141
+ return this
142
+ }
143
+
144
+ switchCoordinators() {
145
+ this.#userGroupedConditions[1].setCoordinator(
146
+ this.#userGroupedConditions[0].getCoordinator()
147
+ )
148
+ this.#userGroupedConditions[0].setCoordinator(undefined)
149
+ }
150
+
151
+ get asPerUserGroupings() {
152
+ return [...this.#userGroupedConditions]
153
+ }
154
+
155
+ get hasConditions() {
156
+ return this.#userGroupedConditions.length > 0
157
+ }
158
+
159
+ get lastIndex() {
160
+ return this.#userGroupedConditions.length - 1
161
+ }
162
+
163
+ toPresentationString() {
164
+ return this.#groupedConditions
165
+ .map((condition) => toPresentationString(condition))
166
+ .join(' ')
167
+ }
168
+
169
+ toExpression() {
170
+ return this.#groupedConditions
171
+ .map((condition) => toExpression(condition))
172
+ .join(' ')
173
+ }
174
+
175
+ _applyGroups(
176
+ userGroupedConditions: (Condition | ConditionGroup | ConditionRef)[]
177
+ ) {
178
+ const correctedUserGroups = userGroupedConditions.map((condition) =>
179
+ condition instanceof ConditionGroup && condition.conditions.length > 2
180
+ ? new ConditionGroup(
181
+ this._group(
182
+ condition.conditions,
183
+ this._autoGroupDefs(condition.conditions)
184
+ )
185
+ )
186
+ : condition
187
+ )
188
+
189
+ return this._group(
190
+ correctedUserGroups,
191
+ this._autoGroupDefs(correctedUserGroups)
192
+ )
193
+ }
194
+
195
+ _group(conditions: ConditionsArray, groupDefs: ConditionGroupDef[]) {
196
+ return conditions.reduce((groups, condition, index, conditions) => {
197
+ const groupDef = groupDefs.find((groupDef) => groupDef.contains(index))
198
+
199
+ if (groupDef) {
200
+ if (groupDef.startsWith(index)) {
201
+ const groupConditions = groupDef.applyTo(conditions)
202
+ groups.push(new ConditionGroup(groupConditions))
203
+ }
204
+ } else {
205
+ groups.push(condition)
206
+ }
207
+
208
+ return groups
209
+ }, [] as ConditionsArray)
210
+ }
211
+
212
+ _ungroup(conditions: ConditionsArray, splitIndex: number) {
213
+ if (conditions[splitIndex].isGroup()) {
214
+ const copy = [...conditions]
215
+ copy.splice(
216
+ splitIndex,
217
+ 1,
218
+ ...conditions[splitIndex].getGroupedConditions()
219
+ )
220
+ return copy
221
+ }
222
+ return conditions
223
+ }
224
+
225
+ _autoGroupDefs(conditions: ConditionsArray) {
226
+ const orPositions: number[] = []
227
+
228
+ conditions.forEach((condition, index) => {
229
+ if (condition.getCoordinator() === Coordinator.OR) {
230
+ orPositions.push(index)
231
+ }
232
+ })
233
+
234
+ const hasOr = orPositions.length > 0
235
+ const hasAnd = !!conditions.find(
236
+ (condition) => condition.getCoordinator() === Coordinator.AND
237
+ )
238
+
239
+ if (hasAnd && hasOr) {
240
+ let start = 0
241
+ const groupDefs: ConditionGroupDef[] = []
242
+ orPositions.forEach((position, index) => {
243
+ if (start < position - 1) {
244
+ groupDefs.push(new ConditionGroupDef(start, position - 1))
245
+ }
246
+ const thisIsTheLastOr = orPositions.length === index + 1
247
+ const thereAreMoreConditions = conditions.length - 1 > position
248
+ if (thisIsTheLastOr && thereAreMoreConditions) {
249
+ groupDefs.push(new ConditionGroupDef(position, conditions.length - 1))
250
+ }
251
+ start = position
252
+ })
253
+ return groupDefs
254
+ }
255
+
256
+ return []
257
+ }
258
+
259
+ toJSON() {
260
+ const name = this.#conditionName
261
+ const conditions = this.#userGroupedConditions
262
+ return {
263
+ name,
264
+ conditions: conditions.map((it) => it.clone())
265
+ }
266
+ }
267
+
268
+ // TODO:- why is this not a constructor?
269
+ static from(obj: ConditionRawObject | ConditionsModel) {
270
+ if (obj instanceof ConditionsModel) {
271
+ return obj
272
+ }
273
+ const toReturn = new ConditionsModel()
274
+ toReturn.#conditionName = obj.name
275
+ toReturn.#userGroupedConditions = obj.conditions.map((condition) =>
276
+ conditionFrom(condition)
277
+ )
278
+ toReturn.#groupedConditions = toReturn._applyGroups(
279
+ toReturn.#userGroupedConditions
280
+ )
281
+ return toReturn
282
+ }
283
+ }
284
+
285
+ interface ConditionFrom {
286
+ (
287
+ it: Condition | ConditionRef | ConditionGroup
288
+ ): Condition | ConditionRef | ConditionGroup
289
+ }
290
+
291
+ const conditionFrom: ConditionFrom = function (it) {
292
+ if ('conditions' in it) {
293
+ return new ConditionGroup(
294
+ (it as ConditionGroup).conditions.map((condition) =>
295
+ conditionFrom(condition)
296
+ )
297
+ )
298
+ }
299
+
300
+ if ('conditionName' in it) {
301
+ return new ConditionRef(
302
+ it.conditionName,
303
+ it.conditionDisplayName,
304
+ it.coordinator
305
+ )
306
+ }
307
+
308
+ return new Condition(
309
+ ConditionField.from(it.field),
310
+ it.operator,
311
+ conditionValueFrom(it.value),
312
+ it.coordinator
313
+ )
314
+ }
@@ -0,0 +1,176 @@
1
+ import { ComponentType, ComponentDef } from '../components'
2
+ import { ConditionValueAbstract } from './condition-value-abstract'
3
+
4
+ import {
5
+ timeUnits,
6
+ dateUnits,
7
+ dateTimeUnits,
8
+ ConditionValue,
9
+ DateDirections,
10
+ RelativeTimeValue
11
+ } from './condition-values'
12
+
13
+ type Operator = '==' | '!=' | '>=' | '<=' | '<' | '>'
14
+
15
+ const defaultOperators = {
16
+ is: inline('=='),
17
+ 'is not': inline('!=')
18
+ }
19
+
20
+ function withDefaults<T>(param: T) {
21
+ return Object.assign({}, param, defaultOperators)
22
+ }
23
+
24
+ const textBasedFieldCustomisations = {
25
+ 'is longer than': lengthIs('>'),
26
+ 'is shorter than': lengthIs('<'),
27
+ 'has length': lengthIs('==')
28
+ }
29
+
30
+ const absoluteDateTimeOperators = {
31
+ is: absoluteDateTime('=='),
32
+ 'is not': absoluteDateTime('!='),
33
+ 'is before': absoluteDateTime('<'),
34
+ 'is after': absoluteDateTime('>')
35
+ }
36
+
37
+ const relativeTimeOperators = (units) => ({
38
+ 'is at least': relativeTime('<=', '>=', units),
39
+ 'is at most': relativeTime('>=', '<=', units),
40
+ 'is less than': relativeTime('>', '<', units),
41
+ 'is more than': relativeTime('<', '>', units)
42
+ })
43
+
44
+ export const customOperators = {
45
+ CheckboxesField: {
46
+ contains: reverseInline('in'),
47
+ 'does not contain': not(reverseInline('in'))
48
+ },
49
+ NumberField: withDefaults({
50
+ 'is at least': inline('>='),
51
+ 'is at most': inline('<='),
52
+ 'is less than': inline('<'),
53
+ 'is more than': inline('>')
54
+ }),
55
+ DateField: Object.assign(
56
+ {},
57
+ absoluteDateTimeOperators,
58
+ relativeTimeOperators(dateUnits)
59
+ ),
60
+ TimeField: Object.assign(
61
+ {},
62
+ absoluteDateTimeOperators,
63
+ relativeTimeOperators(timeUnits)
64
+ ),
65
+ DatePartsField: Object.assign(
66
+ {},
67
+ absoluteDateTimeOperators,
68
+ relativeTimeOperators(dateUnits)
69
+ ),
70
+ DateTimeField: Object.assign(
71
+ {},
72
+ absoluteDateTimeOperators,
73
+ relativeTimeOperators(dateTimeUnits)
74
+ ),
75
+ DateTimePartsField: Object.assign(
76
+ {},
77
+ absoluteDateTimeOperators,
78
+ relativeTimeOperators(dateTimeUnits)
79
+ ),
80
+ TextField: withDefaults(textBasedFieldCustomisations),
81
+ MultilineTextField: withDefaults(textBasedFieldCustomisations),
82
+ EmailAddressField: withDefaults(textBasedFieldCustomisations)
83
+ }
84
+
85
+ export function getOperatorNames(fieldType) {
86
+ return Object.keys(getConditionals(fieldType)).sort()
87
+ }
88
+
89
+ export function getExpression(
90
+ fieldType: ComponentType,
91
+ fieldName: string,
92
+ operator: string,
93
+ value: ConditionValueAbstract
94
+ ) {
95
+ return getConditionals(fieldType)[operator].expression(
96
+ { type: fieldType, name: fieldName },
97
+ value
98
+ )
99
+ }
100
+
101
+ export function getOperatorConfig(fieldType: ComponentType, operator) {
102
+ return getConditionals(fieldType)[operator]
103
+ }
104
+
105
+ function getConditionals(fieldType: ComponentType) {
106
+ return customOperators[fieldType] || defaultOperators
107
+ }
108
+
109
+ function inline(operator: Operator) {
110
+ return {
111
+ expression: (field: ComponentDef, value) =>
112
+ `${field.name} ${operator} ${formatValue(field.type, value.value)}`
113
+ }
114
+ }
115
+
116
+ function lengthIs(operator: Operator) {
117
+ return {
118
+ expression: (field: ComponentDef, value) =>
119
+ `length(${field.name}) ${operator} ${value.value}`
120
+ }
121
+ }
122
+
123
+ function reverseInline(operator: 'in') {
124
+ return {
125
+ expression: (field: ComponentDef, value) =>
126
+ `${formatValue(field.type, value.value)} ${operator} ${field.name}`
127
+ }
128
+ }
129
+
130
+ function not(operatorDefinition) {
131
+ return {
132
+ expression: (field: ComponentDef, value) =>
133
+ `not (${operatorDefinition.expression(field, value)})`
134
+ }
135
+ }
136
+
137
+ function formatValue(fieldType: ComponentType, value) {
138
+ if (fieldType === 'NumberField' || fieldType === 'YesNoField') {
139
+ return value
140
+ }
141
+ return `'${value}'`
142
+ }
143
+
144
+ export const absoluteDateOrTimeOperatorNames = Object.keys(
145
+ absoluteDateTimeOperators
146
+ )
147
+ export const relativeDateOrTimeOperatorNames = Object.keys(
148
+ relativeTimeOperators(dateTimeUnits)
149
+ )
150
+
151
+ function absoluteDateTime(operator: Operator) {
152
+ return {
153
+ expression: (field: ComponentDef, value) => {
154
+ if (value instanceof ConditionValue) {
155
+ return `${field.name} ${operator} '${value.toExpression()}'`
156
+ }
157
+ throw Error('only Value types are supported')
158
+ }
159
+ }
160
+ }
161
+
162
+ function relativeTime(pastOperator, futureOperator, units) {
163
+ return {
164
+ units,
165
+ expression: (field: ComponentDef, value) => {
166
+ if (value instanceof RelativeTimeValue) {
167
+ const operator =
168
+ value.direction === DateDirections.PAST
169
+ ? pastOperator
170
+ : futureOperator
171
+ return `${field.name} ${operator} ${value.toExpression()}`
172
+ }
173
+ throw Error('time shift requires a TimeShiftValue')
174
+ }
175
+ }
176
+ }
@@ -0,0 +1,47 @@
1
+ import { ConditionAbstract } from './condition-abstract'
2
+ import { Coordinator } from './types'
3
+
4
+ export class ConditionRef extends ConditionAbstract {
5
+ conditionName: string
6
+ conditionDisplayName: string
7
+
8
+ constructor(
9
+ conditionName: string,
10
+ conditionDisplayName: string,
11
+ coordinator: Coordinator | undefined
12
+ ) {
13
+ super(coordinator)
14
+
15
+ if (typeof conditionName !== 'string') {
16
+ throw Error(`condition name ${conditionName} is not valid`)
17
+ }
18
+
19
+ if (typeof conditionDisplayName !== 'string') {
20
+ throw Error(`condition display name ${conditionDisplayName} is not valid`)
21
+ }
22
+
23
+ this.conditionName = conditionName
24
+ this.conditionDisplayName = conditionDisplayName
25
+ }
26
+
27
+ asFirstCondition() {
28
+ this._asFirstCondition()
29
+ return this
30
+ }
31
+
32
+ conditionString() {
33
+ return `'${this.conditionDisplayName}'`
34
+ }
35
+
36
+ conditionExpression() {
37
+ return this.conditionName
38
+ }
39
+
40
+ clone() {
41
+ return new ConditionRef(
42
+ this.conditionName,
43
+ this.conditionDisplayName,
44
+ this.coordinator
45
+ )
46
+ }
47
+ }
@@ -0,0 +1,31 @@
1
+ import { Registration } from './condition-value-registration'
2
+
3
+ export class ConditionValueAbstract {
4
+ type: string
5
+
6
+ constructor(registration: Registration) {
7
+ if (new.target === ConditionValueAbstract) {
8
+ throw new TypeError('Cannot construct ConditionValue instances directly')
9
+ }
10
+
11
+ if (!(registration instanceof Registration)) {
12
+ throw new TypeError(
13
+ 'You must register your value type! Call registerValueType!'
14
+ )
15
+ }
16
+
17
+ this.type = registration.type
18
+ }
19
+
20
+ toPresentationString() {
21
+ throw new Error(
22
+ 'Unsupported Operation. Method toPresentationString have not been implemented'
23
+ )
24
+ }
25
+
26
+ toExpression() {
27
+ throw new Error(
28
+ 'Unsupported Operation. Method toExpression have not been implemented'
29
+ )
30
+ }
31
+ }
@@ -0,0 +1,18 @@
1
+ const conditionValueFactories = {}
2
+
3
+ export class Registration {
4
+ type: string
5
+
6
+ constructor(type: string, factory: (obj: any) => Registration) {
7
+ conditionValueFactories[type] = factory
8
+ this.type = type
9
+ }
10
+
11
+ static register(type: string, factory: (obj: any) => Registration) {
12
+ return new Registration(type, factory)
13
+ }
14
+
15
+ static conditionValueFrom(obj: { type: string; [prop: string]: any }) {
16
+ return conditionValueFactories[obj.type](obj)
17
+ }
18
+ }