Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit a80dfaf

Browse files
committed
refactor(core): clean valueOf code
1 parent 28feb36 commit a80dfaf

File tree

1 file changed

+85
-127
lines changed

1 file changed

+85
-127
lines changed
Lines changed: 85 additions & 127 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
/* eslint-disable no-bitwise, no-underscore-dangle */
1+
/* eslint-disable no-underscore-dangle */
22

33
const R = require('ramda');
4-
54
const {
65
Element,
76
ArrayElement,
@@ -14,92 +13,54 @@ const {
1413

1514
const EnumElement = require('./elements/Enum');
1615

17-
const setFlag = (mask, options) => (options | mask);
18-
const clearFlag = (mask, options) => (options & ~mask);
19-
const isFlag = (mask, options) => (options & mask) !== 0;
20-
21-
const FIXED_FLAG = 1 << 0;
22-
const NULLABLE_FLAG = 1 << 1;
23-
const FIXED_TYPE_FLAG = 1 << 2;
24-
25-
function findDefault(e) {
26-
if (undefined !== e._attributes) {
27-
const result = e.attributes.get('default');
28-
if (undefined !== result) {
29-
return result;
30-
}
31-
}
32-
return null;
33-
}
34-
3516
function hasTypeAttribute(e, attribute) {
36-
if (undefined !== e._attributes) {
37-
const typeAttributes = e.attributes.get('typeAttributes');
38-
if (typeAttributes) {
39-
return typeAttributes.includes(attribute);
40-
}
17+
const typeAttributes = e._attributes && e.attributes.get('typeAttributes');
18+
if (typeAttributes) {
19+
return typeAttributes.includes(attribute);
4120
}
4221

4322
return false;
4423
}
4524

46-
const hasFixedTypeAttribute = e => hasTypeAttribute(e, 'fixed');
47-
const hasFixedTypeTypeAttribute = e => hasTypeAttribute(e, 'fixedType');
48-
const hasNullableTypeAttribute = e => hasTypeAttribute(e, 'nullable');
49-
const hasOptionalTypeAttribute = e => hasTypeAttribute(e, 'optional');
50-
51-
function updateTypeAttributes(e, options) {
52-
let result = options;
53-
54-
if (hasFixedTypeAttribute(e)) {
55-
result = setFlag(FIXED_FLAG, result);
56-
}
57-
58-
if (hasFixedTypeTypeAttribute(e)) {
59-
result = setFlag(FIXED_TYPE_FLAG, result);
60-
}
25+
const isFixed = e => hasTypeAttribute(e, 'fixed') || hasTypeAttribute(e, 'fixedType');
26+
const isRequired = e => hasTypeAttribute(e, 'required');
27+
const isNullable = e => hasTypeAttribute(e, 'nullable');
28+
const isOptional = e => hasTypeAttribute(e, 'optional');
29+
const isPrimitive = e => (e instanceof StringElement) || (e instanceof NumberElement) || (e instanceof BooleanElement);
30+
const isEnum = e => e instanceof EnumElement;
31+
const isArray = e => e instanceof ArrayElement;
32+
const isObject = e => e instanceof ObjectElement;
6133

62-
if (hasNullableTypeAttribute(e)) {
63-
result = setFlag(NULLABLE_FLAG, result);
34+
function getDefault(e) {
35+
const result = e._attributes && e.attributes.get('default');
36+
if (result !== undefined) {
37+
return result;
6438
}
6539

66-
return result;
67-
}
68-
69-
function inheritFlags(options) {
70-
return clearFlag(clearFlag(FIXED_TYPE_FLAG | NULLABLE_FLAG, options));
40+
return null;
7141
}
7242

73-
function findFirstSample(e) {
74-
if (undefined !== e._attributes) {
75-
const samples = e.attributes.get('samples');
76-
if (samples instanceof ArrayElement) {
77-
if (undefined !== samples.content && samples.content.length > 0) {
78-
return samples.content[0];
79-
}
80-
}
43+
function getFirstSample(e) {
44+
const samples = e._attributes && e.attributes.get('samples');
45+
if (isArray(samples) && samples.content && !samples.isEmpty) {
46+
return samples.content[0];
8147
}
8248

8349
return null;
8450
}
8551

86-
const isPrimitive = e => (e instanceof StringElement) || (e instanceof NumberElement) || (e instanceof BooleanElement);
87-
const isEnumElement = e => e instanceof EnumElement;
88-
const isArrayElement = e => e instanceof ArrayElement;
89-
const isObjectElement = e => e instanceof ObjectElement;
90-
91-
const hasSample = e => findFirstSample(e) !== null;
92-
const hasDefault = e => findDefault(e) !== null;
52+
const hasSample = e => getFirstSample(e) !== null;
53+
const hasDefault = e => getDefault(e) !== null;
9354
const hasContent = e => e.content !== undefined;
9455
const hasValue = R.anyPass([hasContent, hasSample, hasDefault]);
9556
const hasNoValue = R.complement(hasValue);
9657
const isNoValuePrimitive = R.both(isPrimitive, hasNoValue);
97-
const isNonEmptyArrayElement = e => isArrayElement(e) && e.content && !e.isEmpty;
98-
const isEmptyArray = e => isArrayElement(e) && e.content.every(isNoValuePrimitive);
99-
const isObjectWithUndefinedValues = e => isObjectElement(e)
58+
const isNonEmptyArray = e => isArray(e) && e.content && !e.isEmpty;
59+
const isEmptyArray = e => isArray(e) && e.content.every(isNoValuePrimitive);
60+
const isRef = e => e && e.element === 'ref';
61+
const isObjectWithUndefinedValues = e => isObject(e)
10062
&& e.content.every(prop => prop.value === undefined || prop.value.content === undefined);
10163

102-
10364
function trivialValue(e) {
10465
if (e instanceof BooleanElement) {
10566
return new BooleanElement(false);
@@ -120,102 +81,98 @@ function trivialValue(e) {
12081
return undefined;
12182
}
12283

123-
function mapValue(e, options, f, elements) {
84+
function mapValue(e, f, elements) {
12485
if (e === undefined) {
125-
return e;
86+
return undefined;
12687
}
12788

128-
const opts = updateTypeAttributes(e, options);
129-
13089
if (e.content && !isEmptyArray(e) && !isObjectWithUndefinedValues(e)) {
131-
const result = f(e, opts, elements, 'content');
90+
const result = f(e, elements, 'content');
13291

133-
if (undefined !== result) {
92+
if (result !== undefined) {
13493
return result;
13594
}
13695
}
13796

138-
const sample = findFirstSample(e);
97+
const sample = getFirstSample(e);
13998
if (sample) {
140-
const result = f(sample, opts, elements, 'sample');
141-
if (undefined !== result) {
99+
const result = f(sample, elements, 'sample');
100+
if (result !== undefined) {
142101
return result;
143102
}
144103
}
145104

146-
const dflt = findDefault(e);
105+
const dflt = getDefault(e);
147106
if (dflt) {
148-
const result = f(dflt, opts, elements, 'default');
149-
if (undefined !== result) {
107+
const result = f(dflt, elements, 'default');
108+
if (result !== undefined) {
150109
return result;
151110
}
152111
}
153112

154113
// reconsider content for array element (prefer sample/default first)
155-
if (isNonEmptyArrayElement(e)) {
156-
const result = f(e, opts, elements, 'content');
114+
if (isNonEmptyArray(e)) {
115+
const result = f(e, elements, 'content');
157116

158-
if (undefined !== result) {
117+
if (result !== undefined) {
159118
return result;
160119
}
161120
}
162121

163-
if (isFlag(NULLABLE_FLAG, opts)) {
164-
const result = f(new NullElement(), opts, elements, 'nullable');
165-
if (undefined !== result) {
122+
if (isNullable(e)) {
123+
const result = f(new NullElement(), elements, 'nullable');
124+
if (result !== undefined) {
166125
return result;
167126
}
168127
}
169128

170129
if (elements) {
171-
if (e.element === 'ref') {
130+
if (isRef(e)) {
172131
const result = elements[e.content];
173132
const inheritedElements = R.filter(el => !el.id.equals(e.content), elements);
174133

175134
if (e.path && e.path.toValue() === 'content') {
176-
return mapValue(result.content, opts, f, inheritedElements);
135+
return mapValue(result.content, f, inheritedElements);
177136
}
178137

179-
return mapValue(result, opts, f, inheritedElements);
138+
return mapValue(result, f, inheritedElements);
180139
}
181140

182141
const result = elements[e.element];
183142
if (result) {
184143
const inheritedElements = R.filter(el => !el.id.equals(e.element), elements);
185-
return mapValue(result, opts, f, inheritedElements);
144+
return mapValue(result, f, inheritedElements);
186145
}
187146
}
188147

189-
if (isEnumElement(e)) {
148+
if (isEnum(e)) {
190149
const enums = e.enumerations;
191150
if (enums && enums.content && enums.content[0]) {
192-
const result = f(enums.content[0], opts, elements, 'generated');
193-
if (undefined !== result) {
151+
const result = f(enums.content[0], elements, 'generated');
152+
if (result !== undefined) {
194153
return result;
195154
}
196155
}
197156
}
198157

199158
const trivial = trivialValue(e);
200159
if (trivial) {
201-
const result = f(trivial, opts, elements, 'generated');
202-
if (undefined !== result) {
160+
const result = f(trivial, elements, 'generated');
161+
if (result !== undefined) {
203162
return result;
204163
}
205164
}
206165

207-
if (isArrayElement(e) && e.isEmpty) {
208-
return f(e, opts, elements, 'generated');
166+
if (isArray(e) && e.isEmpty) {
167+
return f(e, elements, 'generated');
209168
}
210169

211170
return undefined;
212171
}
213172

214-
function reduceValue(e, options, elements) {
215-
const opts = updateTypeAttributes(e, options);
216-
173+
function reduceValue(e, elements) {
217174
if (e.content === undefined) {
218-
return mapValue(e, opts, e => e.content, elements);
175+
return mapValue(e, e => e.content, elements);
219176
}
220177

221178
if (isPrimitive(e)) {
@@ -226,32 +183,31 @@ function reduceValue(e, options, elements) {
226183
return null;
227184
}
228185

229-
if (isEnumElement(e)) {
230-
return mapValue(e.content, inheritFlags(opts), reduceValue, elements);
186+
if (isEnum(e)) {
187+
return mapValue(e.content, reduceValue, elements);
231188
}
232189

233-
if (isObjectElement(e)) {
190+
if (isObject(e)) {
234191
let result = {};
235192

236-
const isFixed = isFlag(FIXED_FLAG, opts);
237-
const isFixedType = isFlag(FIXED_TYPE_FLAG, opts);
193+
const isFixedElement = isFixed(e);
238194

239195
e.content.some((item) => {
240-
const skippable = hasOptionalTypeAttribute(item) || (!isFixed && !isFixedType && !hasTypeAttribute(item, 'required'));
196+
const isSkippable = isOptional(item) || (!isFixedElement && !isRequired(item));
241197

242-
const key = mapValue(item.key, inheritFlags(opts), reduceValue, elements);
198+
const key = mapValue(item.key, reduceValue, elements);
243199
if (key === undefined) {
244-
if (skippable) {
200+
if (isSkippable) {
245201
return false;
246202
}
247203

248204
result = undefined;
249205
return true;
250206
}
251207

252-
const value = mapValue(item.value, inheritFlags(opts), reduceValue, elements);
208+
const value = mapValue(item.value, reduceValue, elements);
253209
if (value === undefined) {
254-
if (skippable) {
210+
if (isSkippable) {
255211
return false;
256212
}
257213

@@ -266,10 +222,10 @@ function reduceValue(e, options, elements) {
266222
return result;
267223
}
268224

269-
if (e instanceof ArrayElement) {
270-
const result = e.map(item => mapValue(item, inheritFlags(opts), reduceValue, elements));
225+
if (isArray(e)) {
226+
const result = e.map(item => mapValue(item, reduceValue, elements));
271227

272-
if (!isFlag(FIXED_FLAG, opts) && !isFlag(FIXED_TYPE_FLAG, opts)) {
228+
if (!isFixed(e)) {
273229
return result.filter(item => item !== undefined);
274230
}
275231

@@ -284,20 +240,22 @@ function reduceValue(e, options, elements) {
284240
}
285241

286242
module.exports = () => {
287-
if (!Object.getOwnPropertyNames(Element.prototype).includes('valueOf')) {
288-
Object.defineProperty(Element.prototype, 'valueOf', {
289-
value(flags, elements) {
290-
if (flags !== undefined && flags.source) {
291-
return mapValue(this, 0, (value, opts, elements, source) => {
292-
const result = reduceValue(value, opts, elements);
293-
if (undefined === result) {
294-
return undefined;
295-
}
296-
return [reduceValue(value, opts, elements), source];
297-
}, elements);
298-
}
299-
return mapValue(this, 0, (value, opts) => reduceValue(value, opts, elements), elements);
300-
},
301-
});
243+
if (Object.getOwnPropertyNames(Element.prototype).includes('valueOf')) {
244+
return;
302245
}
246+
247+
Object.defineProperty(Element.prototype, 'valueOf', {
248+
value(flags, elements) {
249+
if (flags && flags.source) {
250+
return mapValue(this, (value, elements, source) => {
251+
const result = reduceValue(value, elements);
252+
if (result === undefined) {
253+
return undefined;
254+
}
255+
return [reduceValue(value, elements), source];
256+
}, elements);
257+
}
258+
return mapValue(this, value => reduceValue(value, elements), elements);
259+
},
260+
});
303261
};

0 commit comments

Comments
 (0)