From d18b1135d65f8fb7186c96bbc5a256d3ccc52251 Mon Sep 17 00:00:00 2001 From: Corey Peterson Date: Thu, 30 Jan 2020 17:06:12 -0500 Subject: [PATCH] Replaces hash function with btoa() for questionToId key encoding --- src/player.js | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/src/player.js b/src/player.js index c9efcfe..edd7223 100644 --- a/src/player.js +++ b/src/player.js @@ -69,21 +69,10 @@ SortItOut.controller("SortItOutEngineCtrl", ["$scope", "$rootScope", "$timeout", $scope.$apply() } - // hash function to be used in conjunction with questionToId - const hash = (str) => { - let hash = 0, i, char - if (str.length == 0) return hash - for (i = 0; i < str.length; i++) { - char = str.charCodeAt(i) - hash = ((hash << 5) - hash) + char - hash |= 0 - } - return Math.abs(hash) - } - const generateQuestionToId = qset => { for (let item of qset.items) { - questionToId.set(hash(sanitizeHelper.desanitize(item.questions[0].text)), item.id) + // btoa encodes question text as a base64 string to prevent failure from special characters + questionToId.set(btoa(sanitizeHelper.desanitize(item.questions[0].text)), item.id) } } @@ -497,7 +486,7 @@ SortItOut.controller("SortItOutEngineCtrl", ["$scope", "$rootScope", "$timeout", $scope.folders.forEach( ({text, items}) => { items.forEach( item => { - const id = questionToId.get(hash(item.text)) + const id = questionToId.get(btoa(item.text)) Materia.Score.submitQuestionForScoring(id, text) }) })