Skip to content

Commit

Permalink
Merge pull request #212 from ONSdigital/EAR-2347-Remove-leading-white…
Browse files Browse the repository at this point in the history
…-spaces

EAR-2347-Remove-leading-spaces
  • Loading branch information
Farhanam76 authored Mar 27, 2024
2 parents 18e9ba5 + 287451c commit 8f58b75
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ const buildAuthorConfirmationQuestion = (page, groupId, routing, ctx) => {
}

if (page.confirmation.qCode) {
confirmationAnswerObject.qCode = page.confirmation.qCode.replace(/\s+$/, '');
confirmationAnswerObject.qCode = page.confirmation.qCode.trim();
}

const confirmationQuestionBlock = new Block(
Expand Down
12 changes: 6 additions & 6 deletions src/eq_schema/builders/routing2/newRoutingDestination/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const routingConditionConversion = require("../../../../utils/routingConditionCo
const {
getMetadataKey,
} = require("../../../../utils/contentUtils/getMetadataKey");
const { getValueSource } = require("../../valueSource")
const { getValueSource } = require("../../valueSource");
const { getListFromAll } = require("../../../../utils/functions/listGetters");

const { flatMap, filter } = require("lodash");
Expand All @@ -23,7 +23,9 @@ const getOptionsFromQuestionaire = (questionnaire) => {
const getOptionValues = (optionIds, questionnaire) => {
const options = getOptionsFromQuestionaire(questionnaire);

const optionResults = optionIds.map((id) => filter(options, { id })[0].label.replace(/\s+$/, ''));
const optionResults = optionIds.map((id) =>
filter(options, { id })[0].label.trim()
);

if (optionResults === undefined || optionResults.length < 0) {
return null;
Expand Down Expand Up @@ -56,9 +58,7 @@ const buildAnswerObject = (
{ left, condition, secondaryCondition, right },
ctx
) => {
let returnVal = [
getValueSource(ctx, left.answerId)
];
let returnVal = [getValueSource(ctx, left.answerId)];

if (right.type === "DateValue") {
returnVal = [
Expand Down Expand Up @@ -204,7 +204,7 @@ const checkValidRoutingType = (expression, ctx) => {
return buildAnswerObject(expression, ctx);
} else if (expression.left.type === "Metadata") {
return buildMetadataObject(expression, ctx);
} else if (expression.left.type === "List" ) {
} else if (expression.left.type === "List") {
return buildListObject(expression, ctx);
} else {
throw new Error(
Expand Down
12 changes: 3 additions & 9 deletions src/eq_schema/schema/Answer/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class Answer {

if (!ctx || (answer.qCode && ctx.questionnaireJson.dataVersion !== "3")) {
if (answer.type !== CHECKBOX) {
this.q_code = answer.qCode.replace(/\s+$/, "");
this.q_code = answer.qCode.trim();
}
}

Expand Down Expand Up @@ -272,16 +272,10 @@ class Answer {
id: `answer${additionalAnswer.id}`,
mandatory: properties.required,
};
option.detail_answer.label = option.detail_answer.label.replace(
/\s+$/,
""
);
option.detail_answer.label = option.detail_answer.label.trim();
if (ctx.questionnaireJson.dataVersion !== "3") {
if (additionalAnswer.qCode && type !== "Checkbox") {
option.detail_answer.q_code = additionalAnswer.qCode.replace(
/\s+$/,
""
);
option.detail_answer.q_code = additionalAnswer.qCode.trim();
}
}
}
Expand Down
66 changes: 34 additions & 32 deletions src/eq_schema/schema/Question/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ const {
wrapContents,
reversePipeContent,
} = require("../../../utils/compoundFunctions");
const { getList, getSupplementaryList } = require("../../../utils/functions/listGetters");
const { getPagesByListId } = require("../../../utils/functions/pageGetters")
const {
getList,
getSupplementaryList,
} = require("../../../utils/functions/listGetters");
const { getPagesByListId } = require("../../../utils/functions/pageGetters");

const Answer = require("../Answer");
const { getValueSource } = require("../../builders/valueSource");
Expand Down Expand Up @@ -37,7 +40,7 @@ class Question {
this.id = `question${question.id}`;
this.title = processPipe(ctx)(question.title);
if (question.qCode) {
this.q_code = question.qCode.replace(/\s+$/, '');
this.q_code = question.qCode.trim();
}
if (question.descriptionEnabled && question.description) {
this.description = [convertPipes(ctx)(question.description)];
Expand All @@ -63,14 +66,12 @@ class Question {

if (question.answers.some((answer) => answer.repeatingLabelAndInput)) {
this.type = "General";
const list = getList(
ctx,
question.answers[0].repeatingLabelAndInputListId
) ||
getSupplementaryList(
ctx,
question.answers[0].repeatingLabelAndInputListId
);
const list =
getList(ctx, question.answers[0].repeatingLabelAndInputListId) ||
getSupplementaryList(
ctx,
question.answers[0].repeatingLabelAndInputListId
);
this.dynamic_answers = {
values: {
source: "list",
Expand All @@ -80,30 +81,31 @@ class Question {
answers: this.buildAnswers(question.answers, ctx),
};

const ListCollectorPages = filter(getPagesByListId(ctx, question.answers[0].repeatingLabelAndInputListId), { pageType: "ListCollectorConfirmationPage" })
const ListCollectorPages = filter(
getPagesByListId(ctx, question.answers[0].repeatingLabelAndInputListId),
{ pageType: "ListCollectorConfirmationPage" }
);

if (ListCollectorPages.length) {
const expressions = ListCollectorPages.map((page) => ({
condition: "Unanswered",
left: {
answerId: page.answers[0].id,
type: "Answer"
},
right: {
optionIds: [
]
}
})
)
condition: "Unanswered",
left: {
answerId: page.answers[0].id,
type: "Answer",
},
right: {
optionIds: [],
},
}));
const newSkip = {
expressions: expressions,
operator: "And"
}
operator: "And",
};

if(question.skipConditions) {
question.skipConditions = [newSkip, ...question.skipConditions]
} else {
question.skipConditions = [newSkip]
if (question.skipConditions) {
question.skipConditions = [newSkip, ...question.skipConditions];
} else {
question.skipConditions = [newSkip];
}
}

Expand Down Expand Up @@ -233,7 +235,7 @@ class Question {
};
if (dataVersion !== "3") {
if (answer.qCode) {
dateFrom.q_code = answer.qCode.replace(/\s+$/, '');
dateFrom.q_code = answer.qCode.trim();
}
}
const dateTo = {
Expand All @@ -243,7 +245,7 @@ class Question {
};
if (dataVersion !== "3") {
if (answer.secondaryQCode) {
dateTo.q_code = answer.secondaryQCode.replace(/\s+$/, '');
dateTo.q_code = answer.secondaryQCode.trim();
}
}
return [dateFrom, dateTo];
Expand All @@ -262,7 +264,7 @@ class Question {
...answer,
type: "Checkbox",
};
tempAnswer.options[0].qCode = answer.qCode && answer.qCode.replace(/\s+$/, '');
tempAnswer.options[0].qCode = answer.qCode && answer.qCode.trim();
delete tempAnswer.qCode;
mutuallyExclusiveAnswer = new Answer(tempAnswer, ctx);
} else if (
Expand Down
2 changes: 1 addition & 1 deletion src/utils/HTMLUtils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const unescapePiping = (value, isMultipleChoiceValue) => {
} else {
updatedValue = replace(/&apos;/g, `\u2019`, value);
}
updatedValue = updatedValue.replace(/\s+$/, ''); //remove trailing spaces
updatedValue = updatedValue.trim(); //remove leading and trailing spaces

return updatedValue;
};
Expand Down
14 changes: 8 additions & 6 deletions src/utils/createAnswerCodes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,29 +59,29 @@ const createAnswerCodes = (questionnaireJson) => {
if (answer.type === DATE_RANGE) {
answerCodes.push({
answer_id: `answer${answer.id}from`,
code: answer.qCode && answer.qCode.replace(/\s+$/, ''),
code: answer.qCode && answer.qCode.trim(),
});
answerCodes.push({
answer_id: `answer${answer.id}to`,
code: answer.secondaryQCode && answer.secondaryQCode.replace(/\s+$/, ''),
code: answer.secondaryQCode && answer.secondaryQCode.trim(),
});
}
// Other answer types output answer ID and answer QCode as their answer codes
else {
if (page && page.pageType === "ListCollectorQualifierPage") {
answerCodes.push({
answer_id: `answer${answer.id}`,
code: answer.qCode && answer.qCode.replace(/\s+$/, ''),
code: answer.qCode && answer.qCode.trim(),
});
} else if (page && page.pageType === "ListCollectorConfirmationPage") {
answerCodes.push({
answer_id: `answer${answer.id}`,
code: answer.qCode && answer.qCode.replace(/\s+$/, ''),
code: answer.qCode && answer.qCode.trim(),
});
} else {
answerCodes.push({
answer_id: `answer${answer.id}`,
code: answer.qCode && answer.qCode.replace(/\s+$/, ''),
code: answer.qCode && answer.qCode.trim(),
});
if (
[RADIO, CHECKBOX, SELECT, MUTUALLY_EXCLUSIVE].includes(answer.type)
Expand All @@ -93,7 +93,9 @@ const createAnswerCodes = (questionnaireJson) => {
) {
answerCodes.push({
answer_id: `answer${option.additionalAnswer.id}`,
code: option.additionalAnswer.qCode && option.additionalAnswer.qCode.replace(/\s+$/, ''),
code:
option.additionalAnswer.qCode &&
option.additionalAnswer.qCode.trim(),
});
}
});
Expand Down

0 comments on commit 8f58b75

Please sign in to comment.