7
7
} = require ( 'minim' ) ;
8
8
const {
9
9
isFixed,
10
+ isFixedType,
10
11
isRequired,
11
12
isNullable,
12
13
isOptional,
@@ -27,17 +28,20 @@ const {
27
28
/**
28
29
* Map the element values
29
30
* @param {element } e - element
31
+ * @param {boolean } inheritFixed - inherited fixed attribute
30
32
* @param {function } f - map function
31
33
* @param {object= } elements - object map of elements to look for inherited types
32
34
* @return {any }
33
35
*/
34
- function mapValue ( e , f , elements ) {
36
+ function mapValue ( e , f , inheritFixed , elements ) {
35
37
if ( e === undefined ) {
36
38
return undefined ;
37
39
}
38
40
41
+ const isElementFixed = inheritFixed || isFixed ( e ) ;
42
+
39
43
if ( e . content && ! isEmptyArray ( e , elements ) && ! isObjectWithUndefinedValues ( e , elements ) ) {
40
- const result = f ( e , elements , 'content' ) ;
44
+ const result = f ( e , isElementFixed , elements , 'content' ) ;
41
45
42
46
if ( result !== undefined ) {
43
47
return result ;
@@ -46,31 +50,31 @@ function mapValue(e, f, elements) {
46
50
47
51
const sample = getFirstSample ( e ) ;
48
52
if ( sample ) {
49
- const result = f ( sample , elements , 'sample' ) ;
53
+ const result = f ( sample , isElementFixed , elements , 'sample' ) ;
50
54
if ( result !== undefined ) {
51
55
return result ;
52
56
}
53
57
}
54
58
55
59
const dflt = getDefault ( e ) ;
56
60
if ( dflt ) {
57
- const result = f ( dflt , elements , 'default' ) ;
61
+ const result = f ( dflt , isElementFixed , elements , 'default' ) ;
58
62
if ( result !== undefined ) {
59
63
return result ;
60
64
}
61
65
}
62
66
63
67
// reconsider content for array and object element (prefer sample/default first)
64
68
if ( isNonEmptyArray ( e , elements ) && isObject ( e , elements ) ) {
65
- const result = f ( e , elements , 'content' ) ;
69
+ const result = f ( e , isElementFixed , elements , 'content' ) ;
66
70
67
71
if ( result !== undefined ) {
68
72
return result ;
69
73
}
70
74
}
71
75
72
76
if ( isNullable ( e ) ) {
73
- const result = f ( new NullElement ( ) , elements , 'nullable' ) ;
77
+ const result = f ( new NullElement ( ) , isElementFixed , elements , 'nullable' ) ;
74
78
if ( result !== undefined ) {
75
79
return result ;
76
80
}
@@ -82,24 +86,24 @@ function mapValue(e, f, elements) {
82
86
const inheritedElements = R . filter ( el => ! el . id . equals ( e . content ) , elements ) ;
83
87
84
88
if ( e . path && e . path . toValue ( ) === 'content' ) {
85
- return mapValue ( result . content , f , inheritedElements ) ;
89
+ return mapValue ( result . content , f , isElementFixed , inheritedElements ) ;
86
90
}
87
91
88
- return mapValue ( result , f , inheritedElements ) ;
92
+ return mapValue ( result , f , isElementFixed , inheritedElements ) ;
89
93
}
90
94
91
95
const result = elements [ e . element ] ;
92
96
if ( result !== undefined ) {
93
97
const inheritedElements = R . filter ( el => ! el . id . equals ( e . element ) , elements ) ;
94
- return mapValue ( result , f , inheritedElements ) ;
98
+ return mapValue ( result , f , isElementFixed , inheritedElements ) ;
95
99
}
96
100
}
97
101
98
102
if ( isEnum ( e , elements ) ) {
99
103
const content = getStructureMembers ( e , elements ) ;
100
104
101
105
if ( content && content [ 0 ] ) {
102
- const result = f ( content [ 0 ] , elements , 'generated' ) ;
106
+ const result = f ( content [ 0 ] , isElementFixed , elements , 'generated' ) ;
103
107
if ( result !== undefined ) {
104
108
return result ;
105
109
}
@@ -108,14 +112,14 @@ function mapValue(e, f, elements) {
108
112
109
113
const trivial = trivialValue ( e ) ;
110
114
if ( trivial ) {
111
- const result = f ( trivial , elements , 'generated' ) ;
115
+ const result = f ( trivial , isElementFixed , elements , 'generated' ) ;
112
116
if ( result !== undefined ) {
113
117
return result ;
114
118
}
115
119
}
116
120
117
121
if ( ( isArray ( e , elements ) && e . isEmpty ) || isObject ( e , elements ) ) {
118
- return f ( e , elements , 'generated' ) ;
122
+ return f ( e , isElementFixed , elements , 'generated' ) ;
119
123
}
120
124
121
125
return undefined ;
@@ -124,12 +128,13 @@ function mapValue(e, f, elements) {
124
128
/**
125
129
* Reduce the element value
126
130
* @param {element } e - element
131
+ * @param {boolean } inheritFixed - inherited fixed attribute
127
132
* @param {object= } elements - object map of elements to look for inherited types
128
133
* @return {any }
129
134
*/
130
- function reduceValue ( e , elements ) {
135
+ function reduceValue ( e , inheritFixed , elements ) {
131
136
if ( e . content === undefined ) {
132
- return mapValue ( e , e => e . content , elements ) ;
137
+ return mapValue ( e , e => e . content , inheritFixed , elements ) ;
133
138
}
134
139
135
140
if ( isPrimitive ( e , elements ) ) {
@@ -141,20 +146,19 @@ function reduceValue(e, elements) {
141
146
}
142
147
143
148
if ( isEnum ( e , elements ) ) {
144
- return mapValue ( e . content , reduceValue , elements ) ;
149
+ return mapValue ( e . content , reduceValue , inheritFixed , elements ) ;
145
150
}
146
151
147
152
if ( isObject ( e , elements ) ) {
148
153
let result = { } ;
149
154
150
- const isFixedElement = isFixed ( e ) ;
151
-
152
155
const content = getStructureMembers ( e , elements ) ;
156
+ const isElementFixedType = isFixedType ( e ) ;
153
157
154
158
content . some ( ( item ) => {
155
- const isSkippable = isOptional ( item ) || ( ! isFixedElement && ! isRequired ( item ) ) ;
159
+ const isSkippable = isOptional ( item ) || ( ! inheritFixed && ! isElementFixedType && ! isRequired ( item ) ) ;
156
160
157
- const key = mapValue ( item . key , reduceValue , elements ) ;
161
+ const key = mapValue ( item . key , reduceValue , inheritFixed , elements ) ;
158
162
if ( key === undefined ) {
159
163
if ( isSkippable ) {
160
164
return false ;
@@ -164,7 +168,7 @@ function reduceValue(e, elements) {
164
168
return true ;
165
169
}
166
170
167
- const value = mapValue ( item . value , reduceValue , elements ) ;
171
+ const value = mapValue ( item . value , reduceValue , inheritFixed , elements ) ;
168
172
if ( value === undefined ) {
169
173
if ( isSkippable ) {
170
174
return false ;
@@ -183,9 +187,9 @@ function reduceValue(e, elements) {
183
187
184
188
if ( isArray ( e , elements ) ) {
185
189
const content = getStructureMembers ( e , elements ) ;
186
- const result = content . map ( item => mapValue ( item , reduceValue , elements ) ) ;
190
+ const result = content . map ( item => mapValue ( item , reduceValue , inheritFixed , elements ) ) ;
187
191
188
- if ( ! isFixed ( e ) ) {
192
+ if ( ! inheritFixed && ! isFixedType ( e ) ) {
189
193
return result . filter ( item => item !== undefined ) ;
190
194
}
191
195
@@ -207,15 +211,18 @@ module.exports = () => {
207
211
Object . defineProperty ( Element . prototype , 'valueOf' , {
208
212
value ( flags , elements ) {
209
213
if ( flags && flags . source ) {
210
- return mapValue ( this , ( value , elements , source ) => {
211
- const result = reduceValue ( value , elements ) ;
214
+ return mapValue ( this , ( value , attrs , elements , source ) => {
215
+ const result = reduceValue ( value , attrs , elements ) ;
216
+
212
217
if ( result === undefined ) {
213
218
return undefined ;
214
219
}
215
- return [ reduceValue ( value , elements ) , source ] ;
216
- } , elements ) ;
220
+
221
+ return [ result , source ] ;
222
+ } , false , elements ) ;
217
223
}
218
- return mapValue ( this , value => reduceValue ( value , elements ) , elements ) ;
224
+
225
+ return mapValue ( this , reduceValue , false , elements ) ;
219
226
} ,
220
227
} ) ;
221
228
} ;
0 commit comments