@@ -13,6 +13,12 @@ const {
13
13
14
14
const EnumElement = require ( './elements/Enum' ) ;
15
15
16
+ /**
17
+ * Check if element has a typeAttribute
18
+ * @param {element } e - element
19
+ * @param {string } attribute
20
+ * @return {boolean }
21
+ */
16
22
function hasTypeAttribute ( e , attribute ) {
17
23
const typeAttributes = e . _attributes && e . attributes . get ( 'typeAttributes' ) ;
18
24
if ( typeAttributes ) {
@@ -22,15 +28,67 @@ function hasTypeAttribute(e, attribute) {
22
28
return false ;
23
29
}
24
30
31
+ /**
32
+ * Check if element has 'fixed' or 'fixedType' typeAttribute set
33
+ * @param {element } e - element
34
+ * @return {boolean }
35
+ */
25
36
const isFixed = e => hasTypeAttribute ( e , 'fixed' ) || hasTypeAttribute ( e , 'fixedType' ) ;
37
+
38
+ /**
39
+ * Check if element has 'required' typeAttribute set
40
+ * @param {element } e - element
41
+ * @return {boolean }
42
+ */
26
43
const isRequired = e => hasTypeAttribute ( e , 'required' ) ;
44
+
45
+ /**
46
+ * Check if element has 'nullable' typeAttribute set
47
+ * @param {element } e - element
48
+ * @return {boolean }
49
+ */
27
50
const isNullable = e => hasTypeAttribute ( e , 'nullable' ) ;
51
+
52
+ /**
53
+ * Check if element has 'optional' typeAttribute set
54
+ * @param {element } e - element
55
+ * @return {boolean }
56
+ */
28
57
const isOptional = e => hasTypeAttribute ( e , 'optional' ) ;
58
+
59
+ /**
60
+ * Check if the element is of a primitive type
61
+ * @param {element } e - element
62
+ * @return {boolean }
63
+ */
29
64
const isPrimitive = e => ( e instanceof StringElement ) || ( e instanceof NumberElement ) || ( e instanceof BooleanElement ) ;
65
+
66
+ /**
67
+ * Check if the element type is Enum
68
+ * @param {element } e - element
69
+ * @return {boolean }
70
+ */
30
71
const isEnum = e => e instanceof EnumElement ;
72
+
73
+ /**
74
+ * Check if the element type is Array
75
+ * @param {element } e - element
76
+ * @return {boolean }
77
+ */
31
78
const isArray = e => e instanceof ArrayElement ;
79
+
80
+ /**
81
+ * Check if the element type is Object
82
+ * @param {element } e - element
83
+ * @return {boolean }
84
+ */
32
85
const isObject = e => e instanceof ObjectElement ;
33
86
87
+ /**
88
+ * Get the element default
89
+ * @param {element } e - element
90
+ * @return {?element }
91
+ */
34
92
function getDefault ( e ) {
35
93
const result = e . _attributes && e . attributes . get ( 'default' ) ;
36
94
if ( result !== undefined ) {
@@ -40,6 +98,11 @@ function getDefault(e) {
40
98
return null ;
41
99
}
42
100
101
+ /**
102
+ * Get the element first sample
103
+ * @param {element } e - element
104
+ * @return {?element }
105
+ */
43
106
function getFirstSample ( e ) {
44
107
const samples = e . _attributes && e . attributes . get ( 'samples' ) ;
45
108
if ( isArray ( samples ) && samples . content && ! samples . isEmpty ) {
@@ -49,18 +112,82 @@ function getFirstSample(e) {
49
112
return null ;
50
113
}
51
114
115
+ /**
116
+ * Check if the element has a sample
117
+ * @param {element } e - element
118
+ * @return {boolean }
119
+ */
52
120
const hasSample = e => getFirstSample ( e ) !== null ;
121
+
122
+ /**
123
+ * Check if the element has a default
124
+ * @param {element } e - element
125
+ * @return {boolean }
126
+ */
53
127
const hasDefault = e => getDefault ( e ) !== null ;
128
+
129
+ /**
130
+ * Check if the element has content
131
+ * @param {element } e - element
132
+ * @return {boolean }
133
+ */
54
134
const hasContent = e => e . content !== undefined ;
135
+
136
+ /**
137
+ * Check if the element has value (content/sample/default)
138
+ * @param {element } e - element
139
+ * @return {boolean }
140
+ */
55
141
const hasValue = R . anyPass ( [ hasContent , hasSample , hasDefault ] ) ;
142
+
143
+ /**
144
+ * Check if the element has no value (content/sample/default)
145
+ * @param {element } e - element
146
+ * @return {boolean }
147
+ */
56
148
const hasNoValue = R . complement ( hasValue ) ;
149
+
150
+ /**
151
+ * Check if the element is of a primitive type and has no value (content/sample/default)
152
+ * @param {element } e - element
153
+ * @return {boolean }
154
+ */
57
155
const isNoValuePrimitive = R . both ( isPrimitive , hasNoValue ) ;
156
+
157
+ /**
158
+ * Check if the element type is array and is not empty
159
+ * @param {element } e - element
160
+ * @return {boolean }
161
+ */
58
162
const isNonEmptyArray = e => isArray ( e ) && e . content && ! e . isEmpty ;
163
+
164
+ /**
165
+ * Check if the element type is array and has only primitive elements with no value
166
+ * @param {element } e - element
167
+ * @return {boolean }
168
+ */
59
169
const isEmptyArray = e => isArray ( e ) && e . content . every ( isNoValuePrimitive ) ;
170
+
171
+ /**
172
+ * Check if the element type is 'ref'
173
+ * @param {element } e - element
174
+ * @return {boolean }
175
+ */
60
176
const isRef = e => e && e . element === 'ref' ;
177
+
178
+ /**
179
+ * Check if the element type is object and has all property values undefined
180
+ * @param {element } e - element
181
+ * @return {boolean }
182
+ */
61
183
const isObjectWithUndefinedValues = e => isObject ( e )
62
184
&& e . content . every ( prop => prop . value === undefined || prop . value . content === undefined ) ;
63
185
186
+ /**
187
+ * Get a trivial value, to fill the unset, according to the element type
188
+ * @param {element } e - element
189
+ * @return {element|undefined }
190
+ */
64
191
function trivialValue ( e ) {
65
192
if ( e instanceof BooleanElement ) {
66
193
return new BooleanElement ( false ) ;
@@ -81,6 +208,13 @@ function trivialValue(e) {
81
208
return undefined ;
82
209
}
83
210
211
+ /**
212
+ * Map the element values
213
+ * @param {element } e - element
214
+ * @param {function } f - map function
215
+ * @param {object= } elements - object map of elements to look for inherited types
216
+ * @return {any }
217
+ */
84
218
function mapValue ( e , f , elements ) {
85
219
if ( e === undefined ) {
86
220
return undefined ;
@@ -170,6 +304,12 @@ function mapValue(e, f, elements) {
170
304
return undefined ;
171
305
}
172
306
307
+ /**
308
+ * Reduce the element value
309
+ * @param {element } e - element
310
+ * @param {object= } elements - object map of elements to look for inherited types
311
+ * @return {any }
312
+ */
173
313
function reduceValue ( e , elements ) {
174
314
if ( e . content === undefined ) {
175
315
return mapValue ( e , e => e . content , elements ) ;
0 commit comments