Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: count with the option that params can have no value
- Loading branch information
1 parent
233dc0e
commit f332490
Showing
2 changed files
with
64 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,51 @@ | ||
module.exports = function compileParams(hrefVariablesElement) { | ||
const params = {}; | ||
function getRequired(memberElement) { | ||
const typeAttributes = memberElement.attributes.getValue('typeAttributes') || []; | ||
return typeAttributes.includes('required'); | ||
} | ||
|
||
if (!hrefVariablesElement) return params; | ||
|
||
hrefVariablesElement.forEach((valueElement, keyElement, memberElement) => { | ||
const name = keyElement.toValue(); | ||
const typeAttributes = memberElement.attributes.getValue('typeAttributes') || []; | ||
const values = valueElement.attributes.getValue('enumerations') || []; | ||
function getDefault(valueElement) { | ||
return valueElement ? valueElement.attributes.getValue('default') : undefined; | ||
} | ||
|
||
|
||
function getExample(valueElement) { | ||
if (valueElement) { | ||
const example = valueElement.toValue(); | ||
params[name] = { | ||
required: Array.from(typeAttributes).includes('required'), | ||
default: valueElement.attributes.getValue('default'), | ||
example: typeof example === 'undefined' || example === null ? values[0] : example, | ||
values, | ||
}; | ||
|
||
return params; | ||
}); | ||
return params; | ||
}; | ||
|
||
if (typeof example === 'undefined' || example === null) { | ||
const values = valueElement.attributes.getValue('enumerations') || []; | ||
return values[0]; | ||
} | ||
return example; | ||
} | ||
return undefined; | ||
} | ||
|
||
|
||
function getValues(valueElement) { | ||
return valueElement | ||
? valueElement.attributes.getValue('enumerations') || [] | ||
: []; | ||
} | ||
|
||
|
||
function compileParams(hrefVariablesElement) { | ||
if (!hrefVariablesElement) return {}; | ||
return hrefVariablesElement | ||
.map((valueElement, keyElement, memberElement) => { | ||
const name = keyElement.toValue(); | ||
return { | ||
[name]: { | ||
required: getRequired(memberElement), | ||
default: getDefault(valueElement), | ||
example: getExample(valueElement), | ||
values: getValues(valueElement), | ||
}, | ||
}; | ||
}) | ||
.reduce((params, param) => Object.assign(params, param), {}); | ||
} | ||
|
||
|
||
module.exports = compileParams; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters