@defra/forms-model 3.0.161 → 3.0.163

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.
@@ -1,153 +0,0 @@
1
- const conditionValueFactories = {}
2
-
3
- class Registration {
4
- type
5
-
6
- constructor(type, factory) {
7
- conditionValueFactories[type] = factory
8
- this.type = type
9
- }
10
- }
11
-
12
- export class AbstractConditionValue {
13
- type
14
-
15
- constructor(registration) {
16
- if (new.target === AbstractConditionValue) {
17
- throw new TypeError('Cannot construct ConditionValue instances directly')
18
- }
19
- if (!(registration instanceof Registration)) {
20
- throw new TypeError(
21
- 'You must register your value type! Call registerValueType!'
22
- )
23
- }
24
- this.type = registration.type
25
- }
26
-
27
- toPresentationString() {}
28
- toExpression() {}
29
- }
30
-
31
- const valueType = registerValueType('Value', (obj) => ConditionValue.from(obj))
32
- export class ConditionValue extends AbstractConditionValue {
33
- value
34
- display
35
-
36
- constructor(value, display) {
37
- super(valueType)
38
- if (!value || typeof value !== 'string') {
39
- throw Error(`value ${value} is not valid`)
40
- }
41
- if (display && typeof display !== 'string') {
42
- throw Error(`display ${display} is not valid`)
43
- }
44
- this.value = value
45
- this.display = display || value
46
- }
47
-
48
- toPresentationString() {
49
- return this.display
50
- }
51
-
52
- toExpression() {
53
- return this.value
54
- }
55
-
56
- static from(obj) {
57
- return new ConditionValue(obj.value, obj.display)
58
- }
59
-
60
- clone() {
61
- return ConditionValue.from(this)
62
- }
63
- }
64
-
65
- export const dateDirections = {
66
- FUTURE: 'in the future',
67
- PAST: 'in the past'
68
- }
69
-
70
- export const dateUnits = {
71
- YEARS: { display: 'year(s)', value: 'years' },
72
- MONTHS: { display: 'month(s)', value: 'months' },
73
- DAYS: { display: 'day(s)', value: 'days' }
74
- }
75
- export const timeUnits = {
76
- HOURS: { display: 'hour(s)', value: 'hours' },
77
- MINUTES: { display: 'minute(s)', value: 'minutes' },
78
- SECONDS: { display: 'second(s)', value: 'seconds' }
79
- }
80
- export const dateTimeUnits = Object.assign({}, dateUnits, timeUnits)
81
-
82
- export const relativeTimeValueType = registerValueType('RelativeTime', (obj) =>
83
- RelativeTimeValue.from(obj)
84
- )
85
- export class RelativeTimeValue extends AbstractConditionValue {
86
- timePeriod
87
- timeUnit
88
- direction
89
- timeOnly
90
-
91
- constructor(timePeriod, timeUnit, direction, timeOnly = false) {
92
- super(relativeTimeValueType)
93
- if (typeof timePeriod !== 'string') {
94
- throw Error(`time period ${timePeriod} is not valid`)
95
- }
96
- if (
97
- !Object.values(dateTimeUnits)
98
- .map((it) => it.value)
99
- .includes(timeUnit)
100
- ) {
101
- throw Error(`time unit ${timeUnit} is not valid`)
102
- }
103
- if (!Object.values(dateDirections).includes(direction)) {
104
- throw Error(`direction ${direction} is not valid`)
105
- }
106
- this.timePeriod = timePeriod
107
- this.timeUnit = timeUnit
108
- this.direction = direction
109
- this.timeOnly = timeOnly
110
- }
111
-
112
- toPresentationString() {
113
- return `${this.timePeriod} ${this.timeUnit} ${this.direction}`
114
- }
115
-
116
- toExpression() {
117
- const timePeriod =
118
- this.direction === dateDirections.PAST
119
- ? 0 - Number(this.timePeriod)
120
- : this.timePeriod
121
- return this.timeOnly
122
- ? `timeForComparison(${timePeriod}, '${this.timeUnit}')`
123
- : `dateForComparison(${timePeriod}, '${this.timeUnit}')`
124
- }
125
-
126
- static from(obj) {
127
- return new RelativeTimeValue(
128
- obj.timePeriod,
129
- obj.timeUnit,
130
- obj.direction,
131
- obj.timeOnly
132
- )
133
- }
134
-
135
- clone() {
136
- return RelativeTimeValue.from(this)
137
- }
138
- }
139
-
140
- /**
141
- * All value types should call this, and should be located in this file.
142
- * Furthermore the types should be registered without the classes needing to be instantiated.
143
- *
144
- * Otherwise we can't guarantee they've been registered for deserialization before
145
- * valueFrom is called
146
- */
147
- function registerValueType(type, factory) {
148
- return new Registration(type, factory)
149
- }
150
-
151
- export function valueFrom(obj) {
152
- return conditionValueFactories[obj.type](obj)
153
- }