@pie-lib/categorize 0.28.2-next.11 → 0.28.2-next.217

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/index.js CHANGED
@@ -1,7 +1,6 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
@@ -13,41 +12,29 @@ Object.defineProperty(exports, "score", {
13
12
  }
14
13
  });
15
14
  exports.verifyAllowMultiplePlacements = exports.stillSelectable = void 0;
16
-
17
15
  var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
18
-
19
16
  var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
20
-
21
17
  var _debug = _interopRequireDefault(require("debug"));
22
-
23
18
  var _clone = _interopRequireDefault(require("lodash/clone"));
24
-
25
19
  var _remove = _interopRequireDefault(require("lodash/remove"));
26
-
27
20
  var _scoring = require("./scoring");
28
-
29
- function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
30
-
31
- function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { (0, _defineProperty2["default"])(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
32
-
33
- var FooTwo = 'foo-two';
34
- exports.FooTwo = FooTwo;
21
+ function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; }
22
+ function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2["default"])(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; }
23
+ var FooTwo = exports.FooTwo = 'foo-two';
35
24
  var log = (0, _debug["default"])('@pie-lib:categorize');
36
-
37
- var limitChoices = function limitChoices(choiceId, count, choices) {
25
+ var limitChoices = exports.limitChoices = function limitChoices(choiceId, count, choices) {
38
26
  var out = choices.reduce(function (acc, id) {
39
27
  var foundCount = countInChoices(choiceId, acc.choices);
40
-
41
28
  if (id !== choiceId || foundCount < count) {
42
29
  acc.choices.push(id);
43
30
  }
44
-
45
31
  return acc;
46
32
  }, {
47
33
  choices: []
48
34
  });
49
35
  return out.choices;
50
36
  };
37
+
51
38
  /**
52
39
  * Limit in an array of arrays.
53
40
  * So for an array with [[1,1],[1,1]] if you call limitInArray(1, arr, 3) you'll get: [[1,1], [1]]
@@ -55,11 +42,7 @@ var limitChoices = function limitChoices(choiceId, count, choices) {
55
42
  * @param {*} arrays
56
43
  * @param {*} limit
57
44
  */
58
-
59
-
60
- exports.limitChoices = limitChoices;
61
-
62
- var limitInArrays = function limitInArrays(id, arrays, limit) {
45
+ var limitInArrays = exports.limitInArrays = function limitInArrays(id, arrays, limit) {
63
46
  arrays = arrays || [];
64
47
  var result = arrays.reduce(function (acc, array) {
65
48
  var l = limit - acc.count;
@@ -73,6 +56,7 @@ var limitInArrays = function limitInArrays(id, arrays, limit) {
73
56
  });
74
57
  return result.out;
75
58
  };
59
+
76
60
  /**
77
61
  * Limit the id in the array to the limit set.
78
62
  * So for an array with [1,1,1,1] if you call limitInArray(1, arr, 2) you'll get: [1,1]
@@ -80,13 +64,8 @@ var limitInArrays = function limitInArrays(id, arrays, limit) {
80
64
  * @param {string[]} arr
81
65
  * @param {number} limit
82
66
  */
83
-
84
-
85
- exports.limitInArrays = limitInArrays;
86
-
87
- var limitInArray = function limitInArray(id, arr, limit) {
67
+ var limitInArray = exports.limitInArray = function limitInArray(id, arr, limit) {
88
68
  arr = arr || [];
89
-
90
69
  if (limit === 0) {
91
70
  var stripped = arr.filter(function (v) {
92
71
  return v !== id;
@@ -105,7 +84,6 @@ var limitInArray = function limitInArray(id, arr, limit) {
105
84
  } else {
106
85
  acc.out.push(v);
107
86
  }
108
-
109
87
  return acc;
110
88
  }, {
111
89
  out: [],
@@ -117,27 +95,22 @@ var limitInArray = function limitInArray(id, arr, limit) {
117
95
  };
118
96
  }
119
97
  };
98
+
120
99
  /**
121
100
  * Ensure that there are no extra choices in answer, if a choice.categoryCount is > 0.
122
101
  * @param {{category: string, choices: string[]}[]} answer
123
102
  * @param {{id:string,categoryCount:number}} choices
124
103
  */
125
-
126
-
127
- exports.limitInArray = limitInArray;
128
-
129
- var ensureNoExtraChoicesInAnswer = function ensureNoExtraChoicesInAnswer(answer, choices) {
104
+ var ensureNoExtraChoicesInAnswer = exports.ensureNoExtraChoicesInAnswer = function ensureNoExtraChoicesInAnswer(answer, choices) {
130
105
  choices = choices || [];
131
106
  var out = choices.reduce(function (answerArray, choice) {
132
107
  log('choice: ----> ', choice.id, 'categoryCount: ', choice.categoryCount);
133
-
134
108
  if (choice.categoryCount === undefined || choice.categoryCount === 0) {
135
109
  return answerArray;
136
110
  } else {
137
111
  var _choices = answerArray.map(function (a) {
138
112
  return a.choices;
139
113
  });
140
-
141
114
  var result = limitInArrays(choice.id, _choices, choice.categoryCount);
142
115
  var updatedArray = result.map(function (r, index) {
143
116
  return {
@@ -151,20 +124,17 @@ var ensureNoExtraChoicesInAnswer = function ensureNoExtraChoicesInAnswer(answer,
151
124
  }, answer);
152
125
  return out;
153
126
  };
127
+
154
128
  /**
155
129
  * Ensure that there are no extra choices in alternate responses, if a choice.categoryCount is > 0.
156
130
  * @param {{category: string, choices: string[]}[]} answer
157
131
  * @param {{id:string,categoryCount:number}} choices
158
132
  */
159
133
 
160
-
161
- exports.ensureNoExtraChoicesInAnswer = ensureNoExtraChoicesInAnswer;
162
-
163
- var ensureNoExtraChoicesInAlternate = function ensureNoExtraChoicesInAlternate(answer, choices) {
134
+ var ensureNoExtraChoicesInAlternate = exports.ensureNoExtraChoicesInAlternate = function ensureNoExtraChoicesInAlternate(answer, choices) {
164
135
  choices = choices || [];
165
136
  var out = choices.reduce(function (answerArray, choice) {
166
137
  log('choice: ----> ', choice.id, 'categoryCount: ', choice.categoryCount);
167
-
168
138
  if (choice.categoryCount === undefined || choice.categoryCount === 0) {
169
139
  return answerArray;
170
140
  } else {
@@ -209,43 +179,31 @@ var ensureNoExtraChoicesInAlternate = function ensureNoExtraChoicesInAlternate(a
209
179
  * @param {string} choiceId
210
180
  * @param {{category: string, choices: string[]}[]} answer
211
181
  */
212
-
213
-
214
- exports.ensureNoExtraChoicesInAlternate = ensureNoExtraChoicesInAlternate;
215
-
216
- var countInAnswer = function countInAnswer(choiceId, answer) {
182
+ var countInAnswer = exports.countInAnswer = function countInAnswer(choiceId, answer) {
217
183
  var out = answer.reduce(function (acc, a) {
218
184
  return acc + countInChoices(choiceId, a.choices);
219
185
  }, 0);
220
186
  log('[countInAnswer] choiceId:', choiceId, answer);
221
187
  return out;
222
188
  };
223
-
224
- exports.countInAnswer = countInAnswer;
225
-
226
- var countInChoices = function countInChoices(choiceId, choices) {
189
+ var countInChoices = exports.countInChoices = function countInChoices(choiceId, choices) {
227
190
  return (choices || []).filter(function (c) {
228
191
  return c === choiceId;
229
192
  }).length;
230
193
  };
194
+
231
195
  /**
232
196
  * Count the number of times a choice has been selected in categories.
233
197
  * @param {*} choice
234
198
  * @param {*} categories
235
199
  */
236
-
237
-
238
- exports.countInChoices = countInChoices;
239
-
240
- var countChosen = function countChosen(choice, categories) {
200
+ var countChosen = exports.countChosen = function countChosen(choice, categories) {
241
201
  if (!choice || !choice.id) {
242
202
  return 0;
243
203
  }
244
-
245
204
  if (!Array.isArray(categories)) {
246
205
  return 0;
247
206
  }
248
-
249
207
  return categories.reduce(function (acc, c) {
250
208
  var count = (c.choices || []).filter(function (h) {
251
209
  return h && h.id === choice.id;
@@ -253,29 +211,23 @@ var countChosen = function countChosen(choice, categories) {
253
211
  return acc + count;
254
212
  }, 0);
255
213
  };
214
+
256
215
  /**
257
216
  * @param {string?} categoryId
258
217
  * @param {{category:string, choices: string[]}} answers
259
218
  */
260
-
261
-
262
- exports.countChosen = countChosen;
263
-
264
- var removeCategory = function removeCategory(categoryId, answers) {
219
+ var removeCategory = exports.removeCategory = function removeCategory(categoryId, answers) {
265
220
  return answers.filter(function (a) {
266
221
  return a.category !== categoryId;
267
222
  });
268
223
  };
224
+
269
225
  /**
270
226
  * @param {string} choiceId
271
227
  * @param {{category:string, choices: string[]}} answers
272
228
  * @param {string?} categoryId - optional categegory id
273
229
  */
274
-
275
-
276
- exports.removeCategory = removeCategory;
277
-
278
- var removeAllChoices = function removeAllChoices(choiceId, answers, categoryId) {
230
+ var removeAllChoices = exports.removeAllChoices = function removeAllChoices(choiceId, answers, categoryId) {
279
231
  return answers.map(function (a) {
280
232
  if (!categoryId || a.category === categoryId) {
281
233
  var cloned = (0, _clone["default"])(a.choices);
@@ -290,21 +242,14 @@ var removeAllChoices = function removeAllChoices(choiceId, answers, categoryId)
290
242
  }
291
243
  });
292
244
  };
293
-
294
- exports.removeAllChoices = removeAllChoices;
295
-
296
- var rearrangeChoices = function rearrangeChoices(choices, indexFrom, indexTo) {
245
+ var rearrangeChoices = exports.rearrangeChoices = function rearrangeChoices(choices, indexFrom, indexTo) {
297
246
  if (choices.length === 0) {
298
247
  return choices;
299
248
  }
300
-
301
249
  choices.splice(indexFrom, 1, choices.splice(indexTo, 1, choices[indexFrom])[0]);
302
250
  return choices;
303
251
  };
304
-
305
- exports.rearrangeChoices = rearrangeChoices;
306
-
307
- var verifyAllowMultiplePlacements = function verifyAllowMultiplePlacements(choice, categoryId, answers) {
252
+ var verifyAllowMultiplePlacements = exports.verifyAllowMultiplePlacements = function verifyAllowMultiplePlacements(choice, categoryId, answers) {
308
253
  return answers.map(function (a) {
309
254
  if (a.category !== categoryId) {
310
255
  a.choices = (a.choices || []).filter(function (c) {
@@ -317,24 +262,19 @@ var verifyAllowMultiplePlacements = function verifyAllowMultiplePlacements(choic
317
262
  var foundIndex = acc.findIndex(function (c) {
318
263
  return c.id === choice.id;
319
264
  });
320
-
321
265
  if (foundIndex === -1) {
322
266
  acc.push(currentValue);
323
267
  }
324
268
  } else {
325
269
  acc.push(currentValue);
326
270
  }
327
-
328
271
  return acc;
329
272
  }, []);
330
273
  return a;
331
274
  }
332
275
  });
333
276
  };
334
-
335
- exports.verifyAllowMultiplePlacements = verifyAllowMultiplePlacements;
336
-
337
- var removeChoiceFromCategory = function removeChoiceFromCategory(choiceId, categoryId, choiceIndex, answers) {
277
+ var removeChoiceFromCategory = exports.removeChoiceFromCategory = function removeChoiceFromCategory(choiceId, categoryId, choiceIndex, answers) {
338
278
  log('[removeChoiceFromCategory] choiceIndex:', choiceIndex);
339
279
  return answers.map(function (a) {
340
280
  if (a.category === categoryId) {
@@ -342,11 +282,9 @@ var removeChoiceFromCategory = function removeChoiceFromCategory(choiceId, categ
342
282
  var index = cloned.findIndex(function (v, index) {
343
283
  return v === choiceId && index >= choiceIndex;
344
284
  });
345
-
346
285
  if (index !== -1) {
347
286
  cloned.splice(index, 1);
348
287
  }
349
-
350
288
  return _objectSpread(_objectSpread({}, a), {}, {
351
289
  choices: cloned
352
290
  });
@@ -355,10 +293,7 @@ var removeChoiceFromCategory = function removeChoiceFromCategory(choiceId, categ
355
293
  }
356
294
  });
357
295
  };
358
-
359
- exports.removeChoiceFromCategory = removeChoiceFromCategory;
360
-
361
- var removeChoiceFromAlternate = function removeChoiceFromAlternate(choiceId, categoryId, choiceIndex, altIndex, answers) {
296
+ var removeChoiceFromAlternate = exports.removeChoiceFromAlternate = function removeChoiceFromAlternate(choiceId, categoryId, choiceIndex, altIndex, answers) {
362
297
  return (answers || []).map(function (a) {
363
298
  if ((a === null || a === void 0 ? void 0 : a.category) === categoryId) {
364
299
  var newAlternates = ((a === null || a === void 0 ? void 0 : a.alternateResponses) || []).map(function (altArray, idx) {
@@ -366,35 +301,26 @@ var removeChoiceFromAlternate = function removeChoiceFromAlternate(choiceId, cat
366
301
  (altArray || []).splice(choiceIndex, 1);
367
302
  return altArray;
368
303
  }
369
-
370
304
  return altArray;
371
305
  });
372
306
  return _objectSpread(_objectSpread({}, a), {}, {
373
307
  alternateResponses: newAlternates
374
308
  });
375
309
  }
376
-
377
310
  return a;
378
311
  });
379
312
  };
380
-
381
- exports.removeChoiceFromAlternate = removeChoiceFromAlternate;
382
-
383
- var moveChoiceToCategory = function moveChoiceToCategory(choiceId, from, to, choiceIndex, answers) {
313
+ var moveChoiceToCategory = exports.moveChoiceToCategory = function moveChoiceToCategory(choiceId, from, to, choiceIndex, answers) {
384
314
  log('[moveChoiceToCategory] choice: ', choiceId, 'from: ', from, 'to: ', to, 'answers: ', answers);
385
-
386
315
  if (from === to) {
387
316
  return answers;
388
317
  }
389
-
390
318
  if (from) {
391
319
  answers = removeChoiceFromCategory(choiceId, from, choiceIndex, answers);
392
320
  }
393
-
394
321
  var index = answers.findIndex(function (a) {
395
322
  return a.category === to;
396
323
  });
397
-
398
324
  if (index === -1) {
399
325
  answers.push({
400
326
  category: to,
@@ -408,21 +334,15 @@ var moveChoiceToCategory = function moveChoiceToCategory(choiceId, from, to, cho
408
334
  a.choices.push(choiceId);
409
335
  return a;
410
336
  }
411
-
412
337
  return a;
413
338
  });
414
339
  }
415
340
  };
416
-
417
- exports.moveChoiceToCategory = moveChoiceToCategory;
418
-
419
- var moveChoiceToAlternate = function moveChoiceToAlternate(choiceId, from, to, choiceIndex, answers, alternateIndex, categoryCount) {
341
+ var moveChoiceToAlternate = exports.moveChoiceToAlternate = function moveChoiceToAlternate(choiceId, from, to, choiceIndex, answers, alternateIndex, categoryCount) {
420
342
  log('[moveChoiceToAlternate] choice: ', choiceId, 'from: ', from, 'to: ', to, 'answers: ', answers, 'chocieIndex: ', choiceIndex);
421
-
422
343
  if (from === to) {
423
344
  return answers;
424
345
  }
425
-
426
346
  return answers.map(function (a) {
427
347
  if (a.category === to) {
428
348
  if (categoryCount !== 0 && a.alternateResponses) {
@@ -430,28 +350,21 @@ var moveChoiceToAlternate = function moveChoiceToAlternate(choiceId, from, to, c
430
350
  return resp !== choiceId;
431
351
  });
432
352
  }
433
-
434
353
  if (!a.alternateResponses) {
435
354
  a.alternateResponses = [];
436
355
  a.alternateResponses[alternateIndex] = [];
437
356
  }
438
-
439
357
  a.alternateResponses[alternateIndex].push(choiceId);
440
358
  }
441
-
442
359
  if (a.category === from && categoryCount !== 0 && a.alternateResponses && a.alternateResponses[alternateIndex]) {
443
360
  a.alternateResponses[alternateIndex] = a.alternateResponses[alternateIndex].filter(function (resp) {
444
361
  return resp !== choiceId;
445
362
  });
446
363
  }
447
-
448
364
  return a;
449
365
  });
450
366
  };
451
-
452
- exports.moveChoiceToAlternate = moveChoiceToAlternate;
453
-
454
- var stillSelectable = function stillSelectable(h, builtCategories) {
367
+ var stillSelectable = exports.stillSelectable = function stillSelectable(h, builtCategories) {
455
368
  if (h.categoryCount > 0) {
456
369
  var count = countChosen(h, builtCategories);
457
370
  return count < h.categoryCount;
@@ -459,6 +372,7 @@ var stillSelectable = function stillSelectable(h, builtCategories) {
459
372
  return true;
460
373
  }
461
374
  };
375
+
462
376
  /**
463
377
  * returns all choices with property 'correct' set to boolean
464
378
  * @param {string[]} possibleResponseChoices
@@ -467,18 +381,15 @@ var stillSelectable = function stillSelectable(h, builtCategories) {
467
381
  * @returns {Object}, returns builtChoices: BuiltChoice[] and allChoicesAreCorrect: boolean
468
382
  * BuiltChoice = { id: choiceId, content: choiceContent, correct: boolean }
469
383
  */
470
-
471
-
472
- exports.stillSelectable = stillSelectable;
473
-
474
- var buildChoices = function buildChoices(possibleResponseChoices, builtCategoryChoices) {
384
+ var buildChoices = exports.buildChoices = function buildChoices(possibleResponseChoices, builtCategoryChoices) {
475
385
  return builtCategoryChoices.reduce(function (acc, builtChoice) {
476
386
  // set correct value on each choice that was selected by user
477
387
  var index = acc.copyOfPossibleResponse.findIndex(function (cRC) {
478
388
  return builtChoice && cRC === builtChoice.id;
479
- }); // if the choice exists in the correct response
480
- // set the correct: true
389
+ });
481
390
 
391
+ // if the choice exists in the correct response
392
+ // set the correct: true
482
393
  if (index >= 0) {
483
394
  acc.builtChoices.push(_objectSpread(_objectSpread({}, builtChoice), {}, {
484
395
  correct: true
@@ -490,7 +401,6 @@ var buildChoices = function buildChoices(possibleResponseChoices, builtCategoryC
490
401
  }));
491
402
  acc.allChoicesAreCorrect = false;
492
403
  }
493
-
494
404
  return acc;
495
405
  }, {
496
406
  builtChoices: [],
@@ -498,6 +408,7 @@ var buildChoices = function buildChoices(possibleResponseChoices, builtCategoryC
498
408
  allChoicesAreCorrect: true
499
409
  });
500
410
  };
411
+
501
412
  /**
502
413
  * returns corrected answer using this possible correct response
503
414
  * @param {Object} possibleResponse, has form: { CategoryId: ChoiceId[] }
@@ -507,20 +418,14 @@ var buildChoices = function buildChoices(possibleResponseChoices, builtCategoryC
507
418
  * BuiltCategory = { id: categoryId, label: categoryLabel, correct: boolean, choices: BuiltChoice[] }
508
419
  * BuiltChoice = { id: choiceId, content: choiceContent, correct: boolean }
509
420
  */
510
-
511
-
512
- exports.buildChoices = buildChoices;
513
-
514
- var getBuiltCategories = function getBuiltCategories(possibleResponse, builtCategories) {
421
+ var getBuiltCategories = exports.getBuiltCategories = function getBuiltCategories(possibleResponse, builtCategories) {
515
422
  return builtCategories.reduce(function (acc, builtCategory) {
516
423
  var possibleResponseChoices = possibleResponse[builtCategory.id] || []; // the correct choices for this category
517
-
518
424
  var builtCategoryChoices = builtCategory.choices || []; // the choices in the answer
519
425
 
520
426
  var _buildChoices = buildChoices(possibleResponseChoices, builtCategoryChoices),
521
- builtChoices = _buildChoices.builtChoices,
522
- allChoicesAreCorrect = _buildChoices.allChoicesAreCorrect;
523
-
427
+ builtChoices = _buildChoices.builtChoices,
428
+ allChoicesAreCorrect = _buildChoices.allChoicesAreCorrect;
524
429
  var allChoicesAreInAnswer = builtCategoryChoices.length === possibleResponseChoices.length;
525
430
  acc.builtCategories.push(_objectSpread(_objectSpread({}, builtCategory), {}, {
526
431
  correct: allChoicesAreCorrect && allChoicesAreInAnswer,
@@ -533,6 +438,7 @@ var getBuiltCategories = function getBuiltCategories(possibleResponse, builtCate
533
438
  correct: true
534
439
  });
535
440
  };
441
+
536
442
  /**
537
443
  * returns all the possible responses by combining the proper alternate responses
538
444
  * @param {Object[]} correctResponse
@@ -540,24 +446,19 @@ var getBuiltCategories = function getBuiltCategories(possibleResponse, builtCate
540
446
  * @returns {Object[]}, returns Response[]
541
447
  * Response = { CategoryId: ChoiceId[] },
542
448
  */
543
-
544
-
545
- exports.getBuiltCategories = getBuiltCategories;
546
-
547
- var getAllPossibleResponses = function getAllPossibleResponses(correctResponse) {
449
+ var getAllPossibleResponses = exports.getAllPossibleResponses = function getAllPossibleResponses(correctResponse) {
548
450
  if (!correctResponse) {
549
451
  return [];
550
452
  }
551
-
552
453
  return correctResponse.reduce(function (acc, cR) {
553
454
  // main response
554
455
  if (acc[0]) {
555
456
  acc[0][cR.category] = cR.choices;
556
457
  } else {
557
458
  acc.push((0, _defineProperty2["default"])({}, cR.category, cR.choices));
558
- } // alternate responses
559
-
459
+ }
560
460
 
461
+ // alternate responses
561
462
  if (cR.alternateResponses) {
562
463
  cR.alternateResponses.forEach(function (aR, index) {
563
464
  if (acc[index + 1]) {
@@ -567,10 +468,10 @@ var getAllPossibleResponses = function getAllPossibleResponses(correctResponse)
567
468
  }
568
469
  });
569
470
  }
570
-
571
471
  return acc;
572
472
  }, []);
573
473
  };
474
+
574
475
  /**
575
476
  * build the categories to be displayed, where each category has its own list of choices
576
477
  * @param {Object[]} categories
@@ -581,11 +482,7 @@ var getAllPossibleResponses = function getAllPossibleResponses(correctResponse)
581
482
  * BuiltCategory = { id: categoryId, label: categoryLabel, choices: BuiltChoice[]},
582
483
  * BuiltChoice = { id: choiceId, content: choiceContent }
583
484
  */
584
-
585
-
586
- exports.getAllPossibleResponses = getAllPossibleResponses;
587
-
588
- var buildCategories = function buildCategories(categories, choices, answers) {
485
+ var buildCategories = exports.buildCategories = function buildCategories(categories, choices, answers) {
589
486
  categories = categories || [];
590
487
  answers = answers || [];
591
488
  choices = choices || [];
@@ -593,11 +490,9 @@ var buildCategories = function buildCategories(categories, choices, answers) {
593
490
  var answer = answers.find(function (answer) {
594
491
  return answer.category === category.id;
595
492
  });
596
-
597
493
  var _ref = answer || {},
598
- _ref$choices = _ref.choices,
599
- answerChoices = _ref$choices === void 0 ? [] : _ref$choices;
600
-
494
+ _ref$choices = _ref.choices,
495
+ answerChoices = _ref$choices === void 0 ? [] : _ref$choices;
601
496
  return _objectSpread(_objectSpread({}, category), {}, {
602
497
  choices: answerChoices.map(function (chId) {
603
498
  return choices.find(function (ch) {
@@ -607,6 +502,7 @@ var buildCategories = function buildCategories(categories, choices, answers) {
607
502
  });
608
503
  });
609
504
  };
505
+
610
506
  /**
611
507
  * build the choice and category state
612
508
  * @param {Object[]} categories
@@ -616,27 +512,24 @@ var buildCategories = function buildCategories(categories, choices, answers) {
616
512
  * @param correctResponse
617
513
  * @returns { categories: Category[], choices: Choice[], correct: boolean, bestResponse: Response }, where Response: { categoryId: choiceId[] }
618
514
  */
619
-
620
-
621
- exports.buildCategories = buildCategories;
622
-
623
- var buildState = function buildState(categories, choices, answers, correctResponse) {
515
+ var buildState = exports.buildState = function buildState(categories, choices, answers, correctResponse) {
624
516
  categories = categories || [];
625
517
  choices = choices || [];
626
518
  answers = answers || [];
627
519
  var allResponses = getAllPossibleResponses(correctResponse) || [];
628
520
  var builtCategories = buildCategories(categories, choices, answers);
629
521
  var bestResponse;
630
- var entirelyCorrect; // at least one defined correct response
522
+ var entirelyCorrect;
631
523
 
524
+ // at least one defined correct response
632
525
  if (allResponses.length) {
633
526
  var builtData = allResponses.reduce(function (acc, possibleResponse) {
634
527
  // set correctness for each possible response
635
528
  var _getBuiltCategories = getBuiltCategories(possibleResponse, builtCategories),
636
- categoriesWithChoices = _getBuiltCategories.builtCategories,
637
- correct = _getBuiltCategories.correct; // if bestResponse not found yet OR no correct response found yet and this one is the correct one
638
-
529
+ categoriesWithChoices = _getBuiltCategories.builtCategories,
530
+ correct = _getBuiltCategories.correct;
639
531
 
532
+ // if bestResponse not found yet OR no correct response found yet and this one is the correct one
640
533
  if (acc.bestResponse === null || !acc.entirelyCorrect && correct) {
641
534
  return {
642
535
  bestResponse: possibleResponse,
@@ -644,7 +537,6 @@ var buildState = function buildState(categories, choices, answers, correctRespon
644
537
  entirelyCorrect: correct
645
538
  };
646
539
  }
647
-
648
540
  return acc;
649
541
  }, {
650
542
  bestResponse: null,
@@ -655,7 +547,6 @@ var buildState = function buildState(categories, choices, answers, correctRespon
655
547
  bestResponse = builtData.bestResponse;
656
548
  entirelyCorrect = builtData.entirelyCorrect;
657
549
  }
658
-
659
550
  var filteredChoices = choices.map(function (ch) {
660
551
  return stillSelectable(ch, builtCategories) ? ch : {
661
552
  empty: true
@@ -668,6 +559,4 @@ var buildState = function buildState(categories, choices, answers, correctRespon
668
559
  bestResponse: bestResponse
669
560
  };
670
561
  };
671
-
672
- exports.buildState = buildState;
673
562
  //# sourceMappingURL=index.js.map