Skip to content

Commit

Permalink
refactored handling of answers
Browse files Browse the repository at this point in the history
  • Loading branch information
upthebuzzard committed Mar 18, 2017
1 parent 70d03d5 commit b9353d2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
30 changes: 26 additions & 4 deletions src/js/crossword_parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@
var knownIds = {};
crossword.knownIds = knownIds;
var maxId = 0;
var answers = {

crossword.answers = {
across : [],
down : []
};
crossword.answers = answers;

for(let grouping of ['across', 'down']){
let prev = groupingPrev[grouping];
Expand Down Expand Up @@ -287,6 +287,18 @@
// check answer within bounds
// and unpack the answerCSV

// convert "ANSWER,PARTS-INTO,NUMBERS" into number csv e.g. "6,5-4,6" (etc)
if ( /^[A-Z,\-]+$/.test(clue.answerCSV) ) {
clue.numericCSV = clue.answerCSV.replace(/[A-Z]+/g, match => {return match.length.toString() } );
} else {
clue.numericCSV = clue.answerCSV;
}

// and if the answer is solely Xs, replace that with the number csv
if ( /^[X,\-]+$/.test(clue.answerCSV) ) {
clue.answerCSV = clue.numericCSV;
}

let answerPieces = clue.answerCSV.split(/[,-]/);
let words = answerPieces.map(p => {
if (/^[0-9]+$/.test(p)) {
Expand All @@ -308,7 +320,7 @@
clueError("answer too long for crossword");
break;
}
answers[grouping].push(wordsString);
crossword.answers[grouping].push(wordsString);

clue.wordsLengths = words.map(function(w){
return w.length;
Expand Down Expand Up @@ -494,13 +506,23 @@
crossword[grouping].forEach( function(clue) {
let item = [
parseInt(clue.id),
clue.body,
clue.body + ' (' + clue.numericCSV + ')',
clue.wordsLengths,
];
spec.clues[grouping].push(item);
});
});

{
// if the answers are just placeholders (lots of Xs)
// assume they are not to be displayed,
// so delete them from the spec
let concatAllAnswerWordsStrings = spec.answers.across.join('') + spec.answers.down.join('');
if ( /^X+$/.test(concatAllAnswerWordsStrings) ) {
delete spec['answers'];
}
}

return spec;
}

Expand Down
4 changes: 2 additions & 2 deletions src/js/oCrossword.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ function buildGrid(
const tempLi = document.createElement('li');
const tempSpan = document.createElement('span');
const answerLength = across[2].filter(isFinite).filter(isFinite).reduce((a,b)=>a+b,0);
tempSpan.textContent = across[0] + '. ' + across[1] + ` (${across[2].filter(isFinite).join(', ')})`;
tempSpan.textContent = across[0] + '. ' + across[1];
tempLi.dataset.oCrosswordNumber = across[0];
tempLi.dataset.oCrosswordAnswerLength = answerLength;
tempLi.dataset.oCrosswordDirection = 'across';
Expand All @@ -76,7 +76,7 @@ function buildGrid(
const tempLi = document.createElement('li');
const tempSpan = document.createElement('span');
const answerLength = down[2].filter(isFinite).filter(isFinite).reduce((a,b)=>a+b,0);
tempSpan.textContent = down[0] + '. ' + down[1] + ` (${down[2].filter(isFinite).join(', ')})`;
tempSpan.textContent = down[0] + '. ' + down[1];
tempLi.dataset.oCrosswordNumber = down[0];
tempLi.dataset.oCrosswordAnswerLength = answerLength;
tempLi.dataset.oCrosswordDirection = 'down';
Expand Down

0 comments on commit b9353d2

Please sign in to comment.