I am on a quest to find an optimal solution for a dilemma I am facing. Essentially, I have a series of questions that need to be answered across different views. My goal is to be able to easily share the exact set of questions and answers with another individual by simply copying and pasting a URL. I wanted to avoid cluttering the URL with numerous query parameters, so I made the decision to base64 encode my questions object and then decode it in real-time, which seemed like a sensible approach.
To accomplish this, I utilized bower to install angular-base64 and developed a straightforward encode method:
function encode(groups) {
var simpleQuestions = groups.map(function (group) {
var questions = group.questions.map(function (question) {
return { id: question.id, answer: question.answer };
})
return { id: group.id, questions: questions };
});
return $base64.encode(angular.toJson(simpleQuestions));
};
This function generates the encoded string flawlessly. Subsequently, I created a decode method:
function decode(data) {
if (!data) return data;
return angular.fromJson($base64.decode(data));
};
Additionally, I implemented an applyAnswers method:
function applyAnswers(groups, simpleGroups) {
if (simpleGroups) {
groups.forEach(function (group) {
var simpleGroup = simpleGroups.find(function (item) {
return item.id === group.id;
});
if (simpleGroup) {
group.questions.forEach(function (question) {
var simpleQuestion = simpleGroup.questions.find(function (item) {
return item.id === question.id;
});
if (simpleQuestion) {
question.answer = simpleQuestion.answer;
}
})
}
});
}
return true;
};
Unfortunately, I encountered an issue where the generated string was too long, causing a bad request error upon page refresh. To resolve this, I considered encrypting the base64 string. Thus, I downloaded crypto-js. However, when attempting to use it, I received an error message stating:
invalid array length
This occurred within the WordArray.clamp method.
At this juncture, my inquiry is: what would be the most effective approach to create a compact string that retains all the necessary information?