|
3 | 3 | const R = require('ramda'); |
4 | 4 | const { |
5 | 5 | Element, |
6 | | - ArrayElement, |
7 | | - ObjectElement, |
8 | | - StringElement, |
9 | | - BooleanElement, |
10 | | - NumberElement, |
11 | 6 | NullElement, |
12 | 7 | } = require('minim'); |
13 | | - |
14 | | -const EnumElement = require('./elements/Enum'); |
15 | | - |
16 | | -/** |
17 | | - * Check if element has a typeAttribute |
18 | | - * @param {element} e - element |
19 | | - * @param {string} attribute |
20 | | - * @return {boolean} |
21 | | - */ |
22 | | -function hasTypeAttribute(e, attribute) { |
23 | | - const typeAttributes = e._attributes && e.attributes.get('typeAttributes'); |
24 | | - if (typeAttributes) { |
25 | | - return typeAttributes.includes(attribute); |
26 | | - } |
27 | | - |
28 | | - return false; |
29 | | -} |
30 | | - |
31 | | -/** |
32 | | - * Check if element has 'fixed' or 'fixedType' typeAttribute set |
33 | | - * @param {element} e - element |
34 | | - * @return {boolean} |
35 | | - */ |
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 | | - */ |
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 | | - */ |
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 | | - */ |
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 | | - */ |
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 | | - */ |
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 | | - */ |
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 | | - */ |
85 | | -const isObject = e => e instanceof ObjectElement; |
86 | | - |
87 | | -/** |
88 | | - * Get the element default |
89 | | - * @param {element} e - element |
90 | | - * @return {?element} |
91 | | - */ |
92 | | -function getDefault(e) { |
93 | | - const result = e._attributes && e.attributes.get('default'); |
94 | | - if (result !== undefined) { |
95 | | - return result; |
96 | | - } |
97 | | - |
98 | | - return null; |
99 | | -} |
100 | | - |
101 | | -/** |
102 | | - * Get the element first sample |
103 | | - * @param {element} e - element |
104 | | - * @return {?element} |
105 | | - */ |
106 | | -function getFirstSample(e) { |
107 | | - const samples = e._attributes && e.attributes.get('samples'); |
108 | | - if (isArray(samples) && samples.content && !samples.isEmpty) { |
109 | | - return samples.content[0]; |
110 | | - } |
111 | | - |
112 | | - return null; |
113 | | -} |
114 | | - |
115 | | -/** |
116 | | - * Check if the element has a sample |
117 | | - * @param {element} e - element |
118 | | - * @return {boolean} |
119 | | - */ |
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 | | - */ |
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 | | - */ |
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 | | - */ |
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 | | - */ |
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 | | - */ |
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 | | - */ |
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 | | - */ |
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 | | - */ |
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 | | - */ |
183 | | -const isObjectWithUndefinedValues = e => isObject(e) |
184 | | - && e.content.every(prop => prop.value === undefined || prop.value.content === undefined); |
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 | | - */ |
191 | | -function trivialValue(e) { |
192 | | - if (e instanceof BooleanElement) { |
193 | | - return new BooleanElement(false); |
194 | | - } |
195 | | - |
196 | | - if (e instanceof NumberElement) { |
197 | | - return new NumberElement(0); |
198 | | - } |
199 | | - |
200 | | - if (e instanceof StringElement) { |
201 | | - return new StringElement(''); |
202 | | - } |
203 | | - |
204 | | - if (e instanceof NullElement) { |
205 | | - return new NullElement(); |
206 | | - } |
207 | | - |
208 | | - return undefined; |
209 | | -} |
| 8 | +const { |
| 9 | + isFixed, |
| 10 | + isRequired, |
| 11 | + isNullable, |
| 12 | + isOptional, |
| 13 | + isPrimitive, |
| 14 | + isEnum, |
| 15 | + isArray, |
| 16 | + isObject, |
| 17 | + getDefault, |
| 18 | + getFirstSample, |
| 19 | + isNonEmptyArray, |
| 20 | + isEmptyArray, |
| 21 | + isRef, |
| 22 | + isObjectWithUndefinedValues, |
| 23 | + trivialValue, |
| 24 | +} = require('./utils'); |
210 | 25 |
|
211 | 26 | /** |
212 | 27 | * Map the element values |
|
0 commit comments