-
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add an ability to parse json encoded options (#192)
- Loading branch information
Showing
6 changed files
with
122 additions
and
10 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
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
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
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
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
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,20 +1,35 @@ | ||
const mapDescription = require('./mapDescription'); | ||
const errorMessage = require('./errorMessage'); | ||
|
||
const ENUM_IDENTIFIER = 'enum:'; | ||
const JSON_IDENTIFIER = 'json:'; | ||
const DESCRIPTION_DIVIDER = ' - '; | ||
|
||
const formatDescription = description => { | ||
const descriptionTypes = mapDescription(description); | ||
const descriptionValue = descriptionTypes.filter(value => ( | ||
!value.includes(ENUM_IDENTIFIER) | ||
!value.includes(ENUM_IDENTIFIER) && !value.includes(JSON_IDENTIFIER) | ||
)).join(DESCRIPTION_DIVIDER); | ||
const enumOption = descriptionTypes.find(value => value.includes(ENUM_IDENTIFIER)); | ||
if (!enumOption) { | ||
return [descriptionValue]; | ||
const jsonOption = descriptionTypes.find(value => value.includes(JSON_IDENTIFIER)); | ||
const res = [descriptionValue]; | ||
if (enumOption) { | ||
const [, enumOptions] = enumOption.split('enum:'); | ||
const enumValues = enumOptions.split(','); | ||
res.push(enumValues); | ||
} else { | ||
res.push(undefined); | ||
} | ||
const [, enumOptions] = enumOption.split('enum:'); | ||
const enumValues = enumOptions.split(','); | ||
return [descriptionValue, enumValues]; | ||
if (jsonOption) { | ||
try { | ||
const jsonOptions = JSON.parse(jsonOption.slice(JSON_IDENTIFIER.length)); | ||
if (typeof jsonOptions !== 'object') { throw new Error('options must be object'); } | ||
res.push(jsonOptions); | ||
} catch (err) { | ||
errorMessage(`json options are malformed: ${err.message}`); | ||
} | ||
} | ||
return res; | ||
}; | ||
|
||
module.exports = formatDescription; |