/
decode-form.ts
39 lines (38 loc) · 1.13 KB
/
decode-form.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import { getProp } from "../../objects/get-prop";
import { setProp } from "../../objects/set-prop";
import { cloneObject } from "../../util";
import { IS123FormJSON, IS123Question } from "../types";
import { isPageQuestion } from "./is-page-question";
/**
* Decodes certain properties of the Survey Form json into html
* @param {IS123FormJSON} form
*/
export const decodeForm = (form: IS123FormJSON) => {
const target = cloneObject(form);
const props = [
"header.content",
"subHeader.content",
"footer.content",
"settings.thankYouScreenContent",
];
props.forEach((prop) => {
if (getProp(target, prop)) {
setProp(prop, decodeURIComponent(getProp(target, prop)), target);
}
});
const toDecoded = (question: IS123Question) => {
const decode = (q: IS123Question) => {
if (q.description) {
q.description = decodeURIComponent(q.description);
}
return q;
};
return !isPageQuestion(question)
? decode(question)
: { ...question, questions: question.questions.map(decode) };
};
return {
...target,
questions: (target.questions || []).map(toDecoded),
};
};