-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: handle undefined values in answer lists #2204
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
e918207
fix: handle undefined values in answer lists
jfmcquade c7732ba
fix: only convert undefined to undefined in answer list parsing
jfmcquade 082196b
refactor: filteredAnswerListItems logic
jfmcquade 942fbbb
Merge branch 'master' into fix/combo-box-radio-group-empty-fields
jfmcquade 265c30c
refactor: incorporate parseAnswerList into new getAnserListParamFromT…
jfmcquade File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -209,6 +209,71 @@ export function getBooleanParamFromTemplateRow( | |
return params.hasOwnProperty(name) ? params[name] === "true" : _default; | ||
} | ||
|
||
export function getAnswerListParamFromTemplateRow( | ||
row: FlowTypes.TemplateRow, | ||
name: string, | ||
_default: IAnswerListItem[] | ||
): IAnswerListItem[] { | ||
const params = row.parameter_list || {}; | ||
console.log(params[name]); | ||
return params.hasOwnProperty(name) ? parseAnswerList(params[name]) : _default; | ||
} | ||
|
||
export interface IAnswerListItem { | ||
name: string; | ||
image?: string; | ||
text?: string; | ||
image_checked?: string | null; | ||
} | ||
|
||
/** | ||
* Parse an answer_list parameter and return an array of AnswerListItems | ||
* @param answerList an answer_list parameter, either an array of IAnswerListItems | ||
* (possibly still in string representation) or a data list (hashmap of IAnswerListItems) | ||
*/ | ||
function parseAnswerList(answerList: any) { | ||
if (!answerList) return []; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nice addition (although shame the git diff isn't showing very cleanly) |
||
// If a data_list (hashmap) is provided as input, convert to an array | ||
if (answerList.constructor === {}.constructor) { | ||
answerList = objectToArray(answerList); | ||
} | ||
const answerListItems: IAnswerListItem[] = answerList.map( | ||
(item: string | Record<string, string>) => { | ||
return parseAnswerListItem(item); | ||
} | ||
); | ||
// Remove any items from the list which only have a value for "name", | ||
// e.g. "image" and "text" are undefined because the list has been generated within a template | ||
const filteredAnswerListItems = answerListItems.filter((item: IAnswerListItem) => { | ||
const hadItemData = Object.entries(item).find( | ||
([key, value]) => key !== "name" && value !== undefined | ||
); | ||
return hadItemData ? true : false; | ||
}); | ||
return filteredAnswerListItems; | ||
} | ||
|
||
/** | ||
* Convert answer list item (string or object) to relevant mappings | ||
* TODO - CC 2023-03-16 - should ideally convert in parsers instead of at runtime | ||
*/ | ||
function parseAnswerListItem(item: any) { | ||
const itemObj: IAnswerListItem = {} as any; | ||
if (typeof item === "string") { | ||
const stringProperties = item.split("|"); | ||
stringProperties.forEach((s) => { | ||
let [field, value] = s.split(":").map((v) => v.trim()); | ||
if (field && value) { | ||
if (value === "undefined") value = undefined; | ||
itemObj[field] = value; | ||
} | ||
}); | ||
// NOTE CC 2021-08-07 - allow passing of object, not just string for conversion | ||
return itemObj; | ||
} | ||
return item; | ||
} | ||
|
||
/** | ||
* Evaluate a javascript expression in a safe context | ||
* @param expression string expression, e.g. "!true", "5 - 4" | ||
|
@@ -415,51 +480,6 @@ function supportsOptionalChaining() { | |
return true; | ||
} | ||
|
||
export interface IAnswerListItem { | ||
name: string; | ||
image?: string; | ||
text?: string; | ||
image_checked?: string | null; | ||
} | ||
|
||
/** | ||
* Parse an answer_list parameter and return an array of AnswerListItems | ||
* @param answerList an answer_list parameter, either an array of IAnswerListItems | ||
* (possibly still in string representation) or a data list (hashmap of IAnswerListItems) | ||
*/ | ||
export function parseAnswerList(answerList: any) { | ||
// If a data_list (hashmap) is provided as input, convert to an array | ||
if (answerList.constructor === {}.constructor) { | ||
answerList = objectToArray(answerList); | ||
} | ||
const answerListItems: IAnswerListItem[] = answerList.map( | ||
(item: string | Record<string, string>) => { | ||
return parseAnswerListItem(item); | ||
} | ||
); | ||
return answerListItems; | ||
} | ||
|
||
/** | ||
* Convert answer list item (string or object) to relevant mappings | ||
* TODO - CC 2023-03-16 - should ideally convert in parsers instead of at runtime | ||
*/ | ||
function parseAnswerListItem(item: any) { | ||
const itemObj: IAnswerListItem = {} as any; | ||
if (typeof item === "string") { | ||
const stringProperties = item.split("|"); | ||
stringProperties.forEach((s) => { | ||
const [field, value] = s.split(":").map((v) => v.trim()); | ||
if (field && value) { | ||
itemObj[field] = value; | ||
} | ||
}); | ||
// NOTE CC 2021-08-07 - allow passing of object, not just string for conversion | ||
return itemObj; | ||
} | ||
return item; | ||
} | ||
|
||
/** | ||
* Compiles markdown to HTML synchronously. | ||
* Extends the renderer of "marked" plugin to ensure that links open in new tags. | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit(non-blocking) can remove temp logs