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

Commit f618f48

Browse files
committed
fix(core): prefer sample/default over empty primitive array
1 parent 4e1e226 commit f618f48

File tree

3 files changed

+49
-3
lines changed

3 files changed

+49
-3
lines changed

packages/api-elements/CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
# API Elements (JavaScript) CHANGELOG
22

3+
## Master
4+
5+
### Bug Fixes
6+
7+
- Generating a value from an element will now prefer sample/default values for
8+
arrays which contain empty primitive values.
9+
310
## 0.2.6 (2020-07-01)
411

512
### Bug Fixes

packages/api-elements/lib/define-value-of.js

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,16 @@ function findFirstSample(e) {
8585

8686
const isPrimitive = e => (e instanceof StringElement) || (e instanceof NumberElement) || (e instanceof BooleanElement);
8787
const isEnumElement = e => e instanceof EnumElement;
88-
const isPlural = e => e instanceof ArrayElement;
88+
const isArrayElement = e => e instanceof ArrayElement;
89+
90+
const hasSample = e => findFirstSample(e) !== null;
91+
const hasDefault = e => findDefault(e) !== null;
92+
const hasContent = e => e.content !== undefined;
93+
const hasValue = R.anyPass([hasContent, hasSample, hasDefault]);
94+
const hasNoValue = R.complement(hasValue);
95+
const isNoValuePrimitive = R.both(isPrimitive, hasNoValue);
96+
const isNonEmptyArrayElement = e => isArrayElement(e) && e.content && !e.isEmpty;
97+
8998

9099
function trivialValue(e) {
91100
if (e instanceof BooleanElement) {
@@ -113,8 +122,10 @@ function mapValue(e, options, f, elements) {
113122
}
114123

115124
const opts = updateTypeAttributes(e, options);
116-
if (e.content && (!isPlural(e) || !e.isEmpty)) {
125+
126+
if (e.content && !(isArrayElement(e) && e.content.every(isNoValuePrimitive))) {
117127
const result = f(e, opts, elements, 'content');
128+
118129
if (undefined !== result) {
119130
return result;
120131
}
@@ -136,6 +147,15 @@ function mapValue(e, options, f, elements) {
136147
}
137148
}
138149

150+
// reconsider content for array element (prefer sample/default first)
151+
if (isNonEmptyArrayElement(e)) {
152+
const result = f(e, opts, elements, 'content');
153+
154+
if (undefined !== result) {
155+
return result;
156+
}
157+
}
158+
139159
if (isFlag(NULLABLE_FLAG, opts)) {
140160
const result = f(new NullElement(), opts, elements, 'nullable');
141161
if (undefined !== result) {
@@ -180,7 +200,7 @@ function mapValue(e, options, f, elements) {
180200
}
181201
}
182202

183-
if (isPlural(e) && e.isEmpty) {
203+
if (isArrayElement(e) && e.isEmpty) {
184204
return f(e, opts, elements, 'generated');
185205
}
186206

packages/api-elements/test/value-of-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,25 @@ describe('valueOf ArrayElement', () => {
700700
expect(value).to.deep.equal([4, 2]);
701701
});
702702

703+
it('prefers samples over empty primitive value', () => {
704+
const element = new ArrayElement([new NumberElement()]);
705+
element.attributes.set('default', new ArrayElement([4, 2]));
706+
element.attributes.set('samples', new ArrayElement([
707+
new ArrayElement([2, 'hello']),
708+
]));
709+
const value = element.valueOf();
710+
711+
expect(value).to.deep.equal([2, 'hello']);
712+
});
713+
714+
it('prefers default over empty primitive value', () => {
715+
const element = new ArrayElement([new NumberElement()]);
716+
element.attributes.set('default', new ArrayElement([4, 2]));
717+
const value = element.valueOf();
718+
719+
expect(value).to.deep.equal([4, 2]);
720+
});
721+
703722
it('generates [] if no content, default, samples and not nullable', () => {
704723
const element = new ArrayElement();
705724
const value = element.valueOf();

0 commit comments

Comments
 (0)