@@ -13,6 +13,12 @@ const {
1313
1414const EnumElement = require ( './elements/Enum' ) ;
1515
16+ /**
17+ * Check if element has a typeAttribute
18+ * @param {element } e - element
19+ * @param {string } attribute
20+ * @return {boolean }
21+ */
1622function hasTypeAttribute ( e , attribute ) {
1723 const typeAttributes = e . _attributes && e . attributes . get ( 'typeAttributes' ) ;
1824 if ( typeAttributes ) {
@@ -22,15 +28,67 @@ function hasTypeAttribute(e, attribute) {
2228 return false ;
2329}
2430
31+ /**
32+ * Check if element has 'fixed' or 'fixedType' typeAttribute set
33+ * @param {element } e - element
34+ * @return {boolean }
35+ */
2536const 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+ */
2643const isRequired = e => hasTypeAttribute ( e , 'required' ) ;
44+
45+ /**
46+ * Check if element has 'nullable' typeAttribute set
47+ * @param {element } e - element
48+ * @return {boolean }
49+ */
2750const isNullable = e => hasTypeAttribute ( e , 'nullable' ) ;
51+
52+ /**
53+ * Check if element has 'optional' typeAttribute set
54+ * @param {element } e - element
55+ * @return {boolean }
56+ */
2857const 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+ */
2964const 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+ */
3071const isEnum = e => e instanceof EnumElement ;
72+
73+ /**
74+ * Check if the element type is Array
75+ * @param {element } e - element
76+ * @return {boolean }
77+ */
3178const isArray = e => e instanceof ArrayElement ;
79+
80+ /**
81+ * Check if the element type is Object
82+ * @param {element } e - element
83+ * @return {boolean }
84+ */
3285const isObject = e => e instanceof ObjectElement ;
3386
87+ /**
88+ * Get the element default
89+ * @param {element } e - element
90+ * @return {?element }
91+ */
3492function getDefault ( e ) {
3593 const result = e . _attributes && e . attributes . get ( 'default' ) ;
3694 if ( result !== undefined ) {
@@ -40,6 +98,11 @@ function getDefault(e) {
4098 return null ;
4199}
42100
101+ /**
102+ * Get the element first sample
103+ * @param {element } e - element
104+ * @return {?element }
105+ */
43106function getFirstSample ( e ) {
44107 const samples = e . _attributes && e . attributes . get ( 'samples' ) ;
45108 if ( isArray ( samples ) && samples . content && ! samples . isEmpty ) {
@@ -49,18 +112,82 @@ function getFirstSample(e) {
49112 return null ;
50113}
51114
115+ /**
116+ * Check if the element has a sample
117+ * @param {element } e - element
118+ * @return {boolean }
119+ */
52120const hasSample = e => getFirstSample ( e ) !== null ;
121+
122+ /**
123+ * Check if the element has a default
124+ * @param {element } e - element
125+ * @return {boolean }
126+ */
53127const hasDefault = e => getDefault ( e ) !== null ;
128+
129+ /**
130+ * Check if the element has content
131+ * @param {element } e - element
132+ * @return {boolean }
133+ */
54134const 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+ */
55141const 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+ */
56148const 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+ */
57155const 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+ */
58162const 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+ */
59169const 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+ */
60176const 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+ */
61183const isObjectWithUndefinedValues = e => isObject ( e )
62184 && e . content . every ( prop => prop . value === undefined || prop . value . content === undefined ) ;
63185
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+ */
64191function trivialValue ( e ) {
65192 if ( e instanceof BooleanElement ) {
66193 return new BooleanElement ( false ) ;
@@ -81,6 +208,13 @@ function trivialValue(e) {
81208 return undefined ;
82209}
83210
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+ */
84218function mapValue ( e , f , elements ) {
85219 if ( e === undefined ) {
86220 return undefined ;
@@ -170,6 +304,12 @@ function mapValue(e, f, elements) {
170304 return undefined ;
171305}
172306
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+ */
173313function reduceValue ( e , elements ) {
174314 if ( e . content === undefined ) {
175315 return mapValue ( e , e => e . content , elements ) ;
0 commit comments