@pie-lib/categorize 0.28.2-next.11 → 0.28.2-next.172
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/CHANGELOG.md +13 -55
- package/esm/index.js +90 -111
- package/esm/index.js.map +1 -1
- package/lib/index.js +51 -162
- package/lib/index.js.map +1 -1
- package/lib/scoring.js +4 -19
- package/lib/scoring.js.map +1 -1
- package/package.json +2 -10
package/esm/index.js
CHANGED
|
@@ -1,19 +1,11 @@
|
|
|
1
1
|
function _extends() {
|
|
2
|
-
_extends = Object.assign
|
|
3
|
-
for (var
|
|
4
|
-
var
|
|
5
|
-
|
|
6
|
-
for (var key in source) {
|
|
7
|
-
if (Object.prototype.hasOwnProperty.call(source, key)) {
|
|
8
|
-
target[key] = source[key];
|
|
9
|
-
}
|
|
10
|
-
}
|
|
2
|
+
return _extends = Object.assign ? Object.assign.bind() : function (n) {
|
|
3
|
+
for (var e = 1; e < arguments.length; e++) {
|
|
4
|
+
var t = arguments[e];
|
|
5
|
+
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
|
|
11
6
|
}
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
return _extends.apply(this, arguments);
|
|
7
|
+
return n;
|
|
8
|
+
}, _extends.apply(null, arguments);
|
|
17
9
|
}
|
|
18
10
|
|
|
19
11
|
var commonjsGlobal = typeof globalThis !== 'undefined' ? globalThis : typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : typeof self !== 'undefined' ? self : {};
|
|
@@ -55,7 +47,7 @@ function requireMs () {
|
|
|
55
47
|
* @api public
|
|
56
48
|
*/
|
|
57
49
|
|
|
58
|
-
ms = function(val, options) {
|
|
50
|
+
ms = function (val, options) {
|
|
59
51
|
options = options || {};
|
|
60
52
|
var type = typeof val;
|
|
61
53
|
if (type === 'string' && val.length > 0) {
|
|
@@ -368,24 +360,62 @@ function requireCommon () {
|
|
|
368
360
|
createDebug.names = [];
|
|
369
361
|
createDebug.skips = [];
|
|
370
362
|
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
363
|
+
const split = (typeof namespaces === 'string' ? namespaces : '')
|
|
364
|
+
.trim()
|
|
365
|
+
.replace(/\s+/g, ',')
|
|
366
|
+
.split(',')
|
|
367
|
+
.filter(Boolean);
|
|
374
368
|
|
|
375
|
-
for (
|
|
376
|
-
if (
|
|
377
|
-
|
|
378
|
-
|
|
369
|
+
for (const ns of split) {
|
|
370
|
+
if (ns[0] === '-') {
|
|
371
|
+
createDebug.skips.push(ns.slice(1));
|
|
372
|
+
} else {
|
|
373
|
+
createDebug.names.push(ns);
|
|
379
374
|
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
380
377
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
378
|
+
/**
|
|
379
|
+
* Checks if the given string matches a namespace template, honoring
|
|
380
|
+
* asterisks as wildcards.
|
|
381
|
+
*
|
|
382
|
+
* @param {String} search
|
|
383
|
+
* @param {String} template
|
|
384
|
+
* @return {Boolean}
|
|
385
|
+
*/
|
|
386
|
+
function matchesTemplate(search, template) {
|
|
387
|
+
let searchIndex = 0;
|
|
388
|
+
let templateIndex = 0;
|
|
389
|
+
let starIndex = -1;
|
|
390
|
+
let matchIndex = 0;
|
|
391
|
+
|
|
392
|
+
while (searchIndex < search.length) {
|
|
393
|
+
if (templateIndex < template.length && (template[templateIndex] === search[searchIndex] || template[templateIndex] === '*')) {
|
|
394
|
+
// Match character or proceed with wildcard
|
|
395
|
+
if (template[templateIndex] === '*') {
|
|
396
|
+
starIndex = templateIndex;
|
|
397
|
+
matchIndex = searchIndex;
|
|
398
|
+
templateIndex++; // Skip the '*'
|
|
399
|
+
} else {
|
|
400
|
+
searchIndex++;
|
|
401
|
+
templateIndex++;
|
|
402
|
+
}
|
|
403
|
+
} else if (starIndex !== -1) { // eslint-disable-line no-negated-condition
|
|
404
|
+
// Backtrack to the last '*' and try to match more characters
|
|
405
|
+
templateIndex = starIndex + 1;
|
|
406
|
+
matchIndex++;
|
|
407
|
+
searchIndex = matchIndex;
|
|
385
408
|
} else {
|
|
386
|
-
|
|
409
|
+
return false; // No match
|
|
387
410
|
}
|
|
388
411
|
}
|
|
412
|
+
|
|
413
|
+
// Handle trailing '*' in template
|
|
414
|
+
while (templateIndex < template.length && template[templateIndex] === '*') {
|
|
415
|
+
templateIndex++;
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
return templateIndex === template.length;
|
|
389
419
|
}
|
|
390
420
|
|
|
391
421
|
/**
|
|
@@ -396,8 +426,8 @@ function requireCommon () {
|
|
|
396
426
|
*/
|
|
397
427
|
function disable() {
|
|
398
428
|
const namespaces = [
|
|
399
|
-
...createDebug.names
|
|
400
|
-
...createDebug.skips.map(
|
|
429
|
+
...createDebug.names,
|
|
430
|
+
...createDebug.skips.map(namespace => '-' + namespace)
|
|
401
431
|
].join(',');
|
|
402
432
|
createDebug.enable('');
|
|
403
433
|
return namespaces;
|
|
@@ -411,21 +441,14 @@ function requireCommon () {
|
|
|
411
441
|
* @api public
|
|
412
442
|
*/
|
|
413
443
|
function enabled(name) {
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
}
|
|
417
|
-
|
|
418
|
-
let i;
|
|
419
|
-
let len;
|
|
420
|
-
|
|
421
|
-
for (i = 0, len = createDebug.skips.length; i < len; i++) {
|
|
422
|
-
if (createDebug.skips[i].test(name)) {
|
|
444
|
+
for (const skip of createDebug.skips) {
|
|
445
|
+
if (matchesTemplate(name, skip)) {
|
|
423
446
|
return false;
|
|
424
447
|
}
|
|
425
448
|
}
|
|
426
449
|
|
|
427
|
-
for (
|
|
428
|
-
if (
|
|
450
|
+
for (const ns of createDebug.names) {
|
|
451
|
+
if (matchesTemplate(name, ns)) {
|
|
429
452
|
return true;
|
|
430
453
|
}
|
|
431
454
|
}
|
|
@@ -433,19 +456,6 @@ function requireCommon () {
|
|
|
433
456
|
return false;
|
|
434
457
|
}
|
|
435
458
|
|
|
436
|
-
/**
|
|
437
|
-
* Convert regexp to namespace
|
|
438
|
-
*
|
|
439
|
-
* @param {RegExp} regxep
|
|
440
|
-
* @return {String} namespace
|
|
441
|
-
* @api private
|
|
442
|
-
*/
|
|
443
|
-
function toNamespace(regexp) {
|
|
444
|
-
return regexp.toString()
|
|
445
|
-
.substring(2, regexp.toString().length - 2)
|
|
446
|
-
.replace(/\.\*\?$/, '*');
|
|
447
|
-
}
|
|
448
|
-
|
|
449
459
|
/**
|
|
450
460
|
* Coerce `val`.
|
|
451
461
|
*
|
|
@@ -610,14 +620,17 @@ function requireBrowser () {
|
|
|
610
620
|
return false;
|
|
611
621
|
}
|
|
612
622
|
|
|
623
|
+
let m;
|
|
624
|
+
|
|
613
625
|
// Is webkit? http://stackoverflow.com/a/16459606/376773
|
|
614
626
|
// document is undefined in react-native: https://github.com/facebook/react-native/pull/1632
|
|
627
|
+
// eslint-disable-next-line no-return-assign
|
|
615
628
|
return (typeof document !== 'undefined' && document.documentElement && document.documentElement.style && document.documentElement.style.WebkitAppearance) ||
|
|
616
629
|
// Is firebug? http://stackoverflow.com/a/398120/376773
|
|
617
630
|
(typeof window !== 'undefined' && window.console && (window.console.firebug || (window.console.exception && window.console.table))) ||
|
|
618
631
|
// Is firefox >= v31?
|
|
619
632
|
// https://developer.mozilla.org/en-US/docs/Tools/Web_Console#Styling_messages
|
|
620
|
-
(typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/) && parseInt(
|
|
633
|
+
(typeof navigator !== 'undefined' && navigator.userAgent && (m = navigator.userAgent.toLowerCase().match(/firefox\/(\d+)/)) && parseInt(m[1], 10) >= 31) ||
|
|
621
634
|
// Double check webkit in userAgent just in case we are in a worker
|
|
622
635
|
(typeof navigator !== 'undefined' && navigator.userAgent && navigator.userAgent.toLowerCase().match(/applewebkit\/(\d+)/));
|
|
623
636
|
}
|
|
@@ -701,7 +714,7 @@ function requireBrowser () {
|
|
|
701
714
|
function load() {
|
|
702
715
|
let r;
|
|
703
716
|
try {
|
|
704
|
-
r = exports$1.storage.getItem('debug');
|
|
717
|
+
r = exports$1.storage.getItem('debug') || exports$1.storage.getItem('DEBUG') ;
|
|
705
718
|
} catch (error) {
|
|
706
719
|
// Swallow
|
|
707
720
|
// XXX (@Qix-) should we be logging these?
|
|
@@ -6276,7 +6289,6 @@ var removeExports = requireRemove();
|
|
|
6276
6289
|
var remove = /*@__PURE__*/getDefaultExportFromCjs(removeExports);
|
|
6277
6290
|
|
|
6278
6291
|
const log$1 = debug('@pie-lib:categorize:scoring');
|
|
6279
|
-
|
|
6280
6292
|
const getWeightingRules = scoring => {
|
|
6281
6293
|
if (!scoring || !scoring.weighting) {
|
|
6282
6294
|
return [];
|
|
@@ -6289,10 +6301,8 @@ const getWeightingRules = scoring => {
|
|
|
6289
6301
|
}
|
|
6290
6302
|
}
|
|
6291
6303
|
};
|
|
6292
|
-
|
|
6293
6304
|
const getPartialRulesForCategory = (partial, categoryId) => {
|
|
6294
6305
|
log$1('partial? ', partial);
|
|
6295
|
-
|
|
6296
6306
|
if (!partial || !partial.enabled) {
|
|
6297
6307
|
return;
|
|
6298
6308
|
} else {
|
|
@@ -6300,6 +6310,7 @@ const getPartialRulesForCategory = (partial, categoryId) => {
|
|
|
6300
6310
|
return pr ? pr.rules || [] : [];
|
|
6301
6311
|
}
|
|
6302
6312
|
};
|
|
6313
|
+
|
|
6303
6314
|
/**
|
|
6304
6315
|
*
|
|
6305
6316
|
* Calculate score based on the categories model generated from `buildState` and the partial scoring rules.
|
|
@@ -6308,7 +6319,6 @@ const getPartialRulesForCategory = (partial, categoryId) => {
|
|
|
6308
6319
|
* @see ../index#buildState
|
|
6309
6320
|
*/
|
|
6310
6321
|
|
|
6311
|
-
|
|
6312
6322
|
const score = (categories, scoring) => new Promise(resolve => {
|
|
6313
6323
|
log$1('categories: ', categories, 'scoring: ', scoring);
|
|
6314
6324
|
const weightingRules = getWeightingRules(scoring);
|
|
@@ -6329,7 +6339,6 @@ const score = (categories, scoring) => new Promise(resolve => {
|
|
|
6329
6339
|
const withScore = categories.map(c => {
|
|
6330
6340
|
const w = weights.find(r => r.category === c.id).points;
|
|
6331
6341
|
log$1('category: ', c);
|
|
6332
|
-
|
|
6333
6342
|
if (c.correct === true) {
|
|
6334
6343
|
return {
|
|
6335
6344
|
category: c.id,
|
|
@@ -6339,7 +6348,6 @@ const score = (categories, scoring) => new Promise(resolve => {
|
|
|
6339
6348
|
} else {
|
|
6340
6349
|
const rules = getPartialRulesForCategory(scoring.partial, c.id);
|
|
6341
6350
|
log$1('partial rules for category: ', rules);
|
|
6342
|
-
|
|
6343
6351
|
if (!rules) {
|
|
6344
6352
|
return {
|
|
6345
6353
|
category: c.id,
|
|
@@ -6390,17 +6398,16 @@ const log = debug('@pie-lib:categorize');
|
|
|
6390
6398
|
const limitChoices = (choiceId, count, choices) => {
|
|
6391
6399
|
const out = choices.reduce((acc, id) => {
|
|
6392
6400
|
const foundCount = countInChoices(choiceId, acc.choices);
|
|
6393
|
-
|
|
6394
6401
|
if (id !== choiceId || foundCount < count) {
|
|
6395
6402
|
acc.choices.push(id);
|
|
6396
6403
|
}
|
|
6397
|
-
|
|
6398
6404
|
return acc;
|
|
6399
6405
|
}, {
|
|
6400
6406
|
choices: []
|
|
6401
6407
|
});
|
|
6402
6408
|
return out.choices;
|
|
6403
6409
|
};
|
|
6410
|
+
|
|
6404
6411
|
/**
|
|
6405
6412
|
* Limit in an array of arrays.
|
|
6406
6413
|
* So for an array with [[1,1],[1,1]] if you call limitInArray(1, arr, 3) you'll get: [[1,1], [1]]
|
|
@@ -6408,7 +6415,6 @@ const limitChoices = (choiceId, count, choices) => {
|
|
|
6408
6415
|
* @param {*} arrays
|
|
6409
6416
|
* @param {*} limit
|
|
6410
6417
|
*/
|
|
6411
|
-
|
|
6412
6418
|
const limitInArrays = (id, arrays, limit) => {
|
|
6413
6419
|
arrays = arrays || [];
|
|
6414
6420
|
const result = arrays.reduce((acc, array) => {
|
|
@@ -6423,6 +6429,7 @@ const limitInArrays = (id, arrays, limit) => {
|
|
|
6423
6429
|
});
|
|
6424
6430
|
return result.out;
|
|
6425
6431
|
};
|
|
6432
|
+
|
|
6426
6433
|
/**
|
|
6427
6434
|
* Limit the id in the array to the limit set.
|
|
6428
6435
|
* So for an array with [1,1,1,1] if you call limitInArray(1, arr, 2) you'll get: [1,1]
|
|
@@ -6430,10 +6437,8 @@ const limitInArrays = (id, arrays, limit) => {
|
|
|
6430
6437
|
* @param {string[]} arr
|
|
6431
6438
|
* @param {number} limit
|
|
6432
6439
|
*/
|
|
6433
|
-
|
|
6434
6440
|
const limitInArray = (id, arr, limit) => {
|
|
6435
6441
|
arr = arr || [];
|
|
6436
|
-
|
|
6437
6442
|
if (limit === 0) {
|
|
6438
6443
|
const stripped = arr.filter(v => v !== id);
|
|
6439
6444
|
return {
|
|
@@ -6450,7 +6455,6 @@ const limitInArray = (id, arr, limit) => {
|
|
|
6450
6455
|
} else {
|
|
6451
6456
|
acc.out.push(v);
|
|
6452
6457
|
}
|
|
6453
|
-
|
|
6454
6458
|
return acc;
|
|
6455
6459
|
}, {
|
|
6456
6460
|
out: [],
|
|
@@ -6462,17 +6466,16 @@ const limitInArray = (id, arr, limit) => {
|
|
|
6462
6466
|
};
|
|
6463
6467
|
}
|
|
6464
6468
|
};
|
|
6469
|
+
|
|
6465
6470
|
/**
|
|
6466
6471
|
* Ensure that there are no extra choices in answer, if a choice.categoryCount is > 0.
|
|
6467
6472
|
* @param {{category: string, choices: string[]}[]} answer
|
|
6468
6473
|
* @param {{id:string,categoryCount:number}} choices
|
|
6469
6474
|
*/
|
|
6470
|
-
|
|
6471
6475
|
const ensureNoExtraChoicesInAnswer = (answer, choices) => {
|
|
6472
6476
|
choices = choices || [];
|
|
6473
6477
|
const out = choices.reduce((answerArray, choice) => {
|
|
6474
6478
|
log('choice: ----> ', choice.id, 'categoryCount: ', choice.categoryCount);
|
|
6475
|
-
|
|
6476
6479
|
if (choice.categoryCount === undefined || choice.categoryCount === 0) {
|
|
6477
6480
|
return answerArray;
|
|
6478
6481
|
} else {
|
|
@@ -6490,6 +6493,7 @@ const ensureNoExtraChoicesInAnswer = (answer, choices) => {
|
|
|
6490
6493
|
}, answer);
|
|
6491
6494
|
return out;
|
|
6492
6495
|
};
|
|
6496
|
+
|
|
6493
6497
|
/**
|
|
6494
6498
|
* Ensure that there are no extra choices in alternate responses, if a choice.categoryCount is > 0.
|
|
6495
6499
|
* @param {{category: string, choices: string[]}[]} answer
|
|
@@ -6500,7 +6504,6 @@ const ensureNoExtraChoicesInAlternate = (answer, choices) => {
|
|
|
6500
6504
|
choices = choices || [];
|
|
6501
6505
|
const out = choices.reduce((answerArray, choice) => {
|
|
6502
6506
|
log('choice: ----> ', choice.id, 'categoryCount: ', choice.categoryCount);
|
|
6503
|
-
|
|
6504
6507
|
if (choice.categoryCount === undefined || choice.categoryCount === 0) {
|
|
6505
6508
|
return answerArray;
|
|
6506
6509
|
} else {
|
|
@@ -6543,45 +6546,42 @@ const ensureNoExtraChoicesInAlternate = (answer, choices) => {
|
|
|
6543
6546
|
* @param {string} choiceId
|
|
6544
6547
|
* @param {{category: string, choices: string[]}[]} answer
|
|
6545
6548
|
*/
|
|
6546
|
-
|
|
6547
6549
|
const countInAnswer = (choiceId, answer) => {
|
|
6548
6550
|
const out = answer.reduce((acc, a) => acc + countInChoices(choiceId, a.choices), 0);
|
|
6549
6551
|
log('[countInAnswer] choiceId:', choiceId, answer);
|
|
6550
6552
|
return out;
|
|
6551
6553
|
};
|
|
6552
6554
|
const countInChoices = (choiceId, choices) => (choices || []).filter(c => c === choiceId).length;
|
|
6555
|
+
|
|
6553
6556
|
/**
|
|
6554
6557
|
* Count the number of times a choice has been selected in categories.
|
|
6555
6558
|
* @param {*} choice
|
|
6556
6559
|
* @param {*} categories
|
|
6557
6560
|
*/
|
|
6558
|
-
|
|
6559
6561
|
const countChosen = (choice, categories) => {
|
|
6560
6562
|
if (!choice || !choice.id) {
|
|
6561
6563
|
return 0;
|
|
6562
6564
|
}
|
|
6563
|
-
|
|
6564
6565
|
if (!Array.isArray(categories)) {
|
|
6565
6566
|
return 0;
|
|
6566
6567
|
}
|
|
6567
|
-
|
|
6568
6568
|
return categories.reduce((acc, c) => {
|
|
6569
6569
|
const count = (c.choices || []).filter(h => h && h.id === choice.id).length;
|
|
6570
6570
|
return acc + count;
|
|
6571
6571
|
}, 0);
|
|
6572
6572
|
};
|
|
6573
|
+
|
|
6573
6574
|
/**
|
|
6574
6575
|
* @param {string?} categoryId
|
|
6575
6576
|
* @param {{category:string, choices: string[]}} answers
|
|
6576
6577
|
*/
|
|
6577
|
-
|
|
6578
6578
|
const removeCategory = (categoryId, answers) => answers.filter(a => a.category !== categoryId);
|
|
6579
|
+
|
|
6579
6580
|
/**
|
|
6580
6581
|
* @param {string} choiceId
|
|
6581
6582
|
* @param {{category:string, choices: string[]}} answers
|
|
6582
6583
|
* @param {string?} categoryId - optional categegory id
|
|
6583
6584
|
*/
|
|
6584
|
-
|
|
6585
6585
|
const removeAllChoices = (choiceId, answers, categoryId) => {
|
|
6586
6586
|
return answers.map(a => {
|
|
6587
6587
|
if (!categoryId || a.category === categoryId) {
|
|
@@ -6599,7 +6599,6 @@ const rearrangeChoices = (choices, indexFrom, indexTo) => {
|
|
|
6599
6599
|
if (choices.length === 0) {
|
|
6600
6600
|
return choices;
|
|
6601
6601
|
}
|
|
6602
|
-
|
|
6603
6602
|
choices.splice(indexFrom, 1, choices.splice(indexTo, 1, choices[indexFrom])[0]);
|
|
6604
6603
|
return choices;
|
|
6605
6604
|
};
|
|
@@ -6612,14 +6611,12 @@ const verifyAllowMultiplePlacements = (choice, categoryId, answers) => {
|
|
|
6612
6611
|
a.choices = a.choices.reduce((acc, currentValue) => {
|
|
6613
6612
|
if (currentValue.id === choice.id) {
|
|
6614
6613
|
const foundIndex = acc.findIndex(c => c.id === choice.id);
|
|
6615
|
-
|
|
6616
6614
|
if (foundIndex === -1) {
|
|
6617
6615
|
acc.push(currentValue);
|
|
6618
6616
|
}
|
|
6619
6617
|
} else {
|
|
6620
6618
|
acc.push(currentValue);
|
|
6621
6619
|
}
|
|
6622
|
-
|
|
6623
6620
|
return acc;
|
|
6624
6621
|
}, []);
|
|
6625
6622
|
return a;
|
|
@@ -6634,11 +6631,9 @@ const removeChoiceFromCategory = (choiceId, categoryId, choiceIndex, answers) =>
|
|
|
6634
6631
|
const index = cloned.findIndex((v, index) => {
|
|
6635
6632
|
return v === choiceId && index >= choiceIndex;
|
|
6636
6633
|
});
|
|
6637
|
-
|
|
6638
6634
|
if (index !== -1) {
|
|
6639
6635
|
cloned.splice(index, 1);
|
|
6640
6636
|
}
|
|
6641
|
-
|
|
6642
6637
|
return _extends({}, a, {
|
|
6643
6638
|
choices: cloned
|
|
6644
6639
|
});
|
|
@@ -6655,30 +6650,24 @@ const removeChoiceFromAlternate = (choiceId, categoryId, choiceIndex, altIndex,
|
|
|
6655
6650
|
(altArray || []).splice(choiceIndex, 1);
|
|
6656
6651
|
return altArray;
|
|
6657
6652
|
}
|
|
6658
|
-
|
|
6659
6653
|
return altArray;
|
|
6660
6654
|
});
|
|
6661
6655
|
return _extends({}, a, {
|
|
6662
6656
|
alternateResponses: newAlternates
|
|
6663
6657
|
});
|
|
6664
6658
|
}
|
|
6665
|
-
|
|
6666
6659
|
return a;
|
|
6667
6660
|
});
|
|
6668
6661
|
};
|
|
6669
6662
|
const moveChoiceToCategory = (choiceId, from, to, choiceIndex, answers) => {
|
|
6670
6663
|
log('[moveChoiceToCategory] choice: ', choiceId, 'from: ', from, 'to: ', to, 'answers: ', answers);
|
|
6671
|
-
|
|
6672
6664
|
if (from === to) {
|
|
6673
6665
|
return answers;
|
|
6674
6666
|
}
|
|
6675
|
-
|
|
6676
6667
|
if (from) {
|
|
6677
6668
|
answers = removeChoiceFromCategory(choiceId, from, choiceIndex, answers);
|
|
6678
6669
|
}
|
|
6679
|
-
|
|
6680
6670
|
const index = answers.findIndex(a => a.category === to);
|
|
6681
|
-
|
|
6682
6671
|
if (index === -1) {
|
|
6683
6672
|
answers.push({
|
|
6684
6673
|
category: to,
|
|
@@ -6692,36 +6681,29 @@ const moveChoiceToCategory = (choiceId, from, to, choiceIndex, answers) => {
|
|
|
6692
6681
|
a.choices.push(choiceId);
|
|
6693
6682
|
return a;
|
|
6694
6683
|
}
|
|
6695
|
-
|
|
6696
6684
|
return a;
|
|
6697
6685
|
});
|
|
6698
6686
|
}
|
|
6699
6687
|
};
|
|
6700
6688
|
const moveChoiceToAlternate = (choiceId, from, to, choiceIndex, answers, alternateIndex, categoryCount) => {
|
|
6701
6689
|
log('[moveChoiceToAlternate] choice: ', choiceId, 'from: ', from, 'to: ', to, 'answers: ', answers, 'chocieIndex: ', choiceIndex);
|
|
6702
|
-
|
|
6703
6690
|
if (from === to) {
|
|
6704
6691
|
return answers;
|
|
6705
6692
|
}
|
|
6706
|
-
|
|
6707
6693
|
return answers.map(a => {
|
|
6708
6694
|
if (a.category === to) {
|
|
6709
6695
|
if (categoryCount !== 0 && a.alternateResponses) {
|
|
6710
6696
|
a.alternateResponses[alternateIndex] = (a.alternateResponses[alternateIndex] || []).filter(resp => resp !== choiceId);
|
|
6711
6697
|
}
|
|
6712
|
-
|
|
6713
6698
|
if (!a.alternateResponses) {
|
|
6714
6699
|
a.alternateResponses = [];
|
|
6715
6700
|
a.alternateResponses[alternateIndex] = [];
|
|
6716
6701
|
}
|
|
6717
|
-
|
|
6718
6702
|
a.alternateResponses[alternateIndex].push(choiceId);
|
|
6719
6703
|
}
|
|
6720
|
-
|
|
6721
6704
|
if (a.category === from && categoryCount !== 0 && a.alternateResponses && a.alternateResponses[alternateIndex]) {
|
|
6722
6705
|
a.alternateResponses[alternateIndex] = a.alternateResponses[alternateIndex].filter(resp => resp !== choiceId);
|
|
6723
6706
|
}
|
|
6724
|
-
|
|
6725
6707
|
return a;
|
|
6726
6708
|
});
|
|
6727
6709
|
};
|
|
@@ -6733,6 +6715,7 @@ const stillSelectable = (h, builtCategories) => {
|
|
|
6733
6715
|
return true;
|
|
6734
6716
|
}
|
|
6735
6717
|
};
|
|
6718
|
+
|
|
6736
6719
|
/**
|
|
6737
6720
|
* returns all choices with property 'correct' set to boolean
|
|
6738
6721
|
* @param {string[]} possibleResponseChoices
|
|
@@ -6741,13 +6724,13 @@ const stillSelectable = (h, builtCategories) => {
|
|
|
6741
6724
|
* @returns {Object}, returns builtChoices: BuiltChoice[] and allChoicesAreCorrect: boolean
|
|
6742
6725
|
* BuiltChoice = { id: choiceId, content: choiceContent, correct: boolean }
|
|
6743
6726
|
*/
|
|
6744
|
-
|
|
6745
6727
|
const buildChoices = (possibleResponseChoices, builtCategoryChoices) => {
|
|
6746
6728
|
return builtCategoryChoices.reduce((acc, builtChoice) => {
|
|
6747
6729
|
// set correct value on each choice that was selected by user
|
|
6748
|
-
const index = acc.copyOfPossibleResponse.findIndex(cRC => builtChoice && cRC === builtChoice.id);
|
|
6749
|
-
// set the correct: true
|
|
6730
|
+
const index = acc.copyOfPossibleResponse.findIndex(cRC => builtChoice && cRC === builtChoice.id);
|
|
6750
6731
|
|
|
6732
|
+
// if the choice exists in the correct response
|
|
6733
|
+
// set the correct: true
|
|
6751
6734
|
if (index >= 0) {
|
|
6752
6735
|
acc.builtChoices.push(_extends({}, builtChoice, {
|
|
6753
6736
|
correct: true
|
|
@@ -6759,7 +6742,6 @@ const buildChoices = (possibleResponseChoices, builtCategoryChoices) => {
|
|
|
6759
6742
|
}));
|
|
6760
6743
|
acc.allChoicesAreCorrect = false;
|
|
6761
6744
|
}
|
|
6762
|
-
|
|
6763
6745
|
return acc;
|
|
6764
6746
|
}, {
|
|
6765
6747
|
builtChoices: [],
|
|
@@ -6767,6 +6749,7 @@ const buildChoices = (possibleResponseChoices, builtCategoryChoices) => {
|
|
|
6767
6749
|
allChoicesAreCorrect: true
|
|
6768
6750
|
});
|
|
6769
6751
|
};
|
|
6752
|
+
|
|
6770
6753
|
/**
|
|
6771
6754
|
* returns corrected answer using this possible correct response
|
|
6772
6755
|
* @param {Object} possibleResponse, has form: { CategoryId: ChoiceId[] }
|
|
@@ -6776,10 +6759,8 @@ const buildChoices = (possibleResponseChoices, builtCategoryChoices) => {
|
|
|
6776
6759
|
* BuiltCategory = { id: categoryId, label: categoryLabel, correct: boolean, choices: BuiltChoice[] }
|
|
6777
6760
|
* BuiltChoice = { id: choiceId, content: choiceContent, correct: boolean }
|
|
6778
6761
|
*/
|
|
6779
|
-
|
|
6780
6762
|
const getBuiltCategories = (possibleResponse, builtCategories) => builtCategories.reduce((acc, builtCategory) => {
|
|
6781
6763
|
const possibleResponseChoices = possibleResponse[builtCategory.id] || []; // the correct choices for this category
|
|
6782
|
-
|
|
6783
6764
|
const builtCategoryChoices = builtCategory.choices || []; // the choices in the answer
|
|
6784
6765
|
|
|
6785
6766
|
const {
|
|
@@ -6797,6 +6778,7 @@ const getBuiltCategories = (possibleResponse, builtCategories) => builtCategorie
|
|
|
6797
6778
|
builtCategories: [],
|
|
6798
6779
|
correct: true
|
|
6799
6780
|
});
|
|
6781
|
+
|
|
6800
6782
|
/**
|
|
6801
6783
|
* returns all the possible responses by combining the proper alternate responses
|
|
6802
6784
|
* @param {Object[]} correctResponse
|
|
@@ -6804,12 +6786,10 @@ const getBuiltCategories = (possibleResponse, builtCategories) => builtCategorie
|
|
|
6804
6786
|
* @returns {Object[]}, returns Response[]
|
|
6805
6787
|
* Response = { CategoryId: ChoiceId[] },
|
|
6806
6788
|
*/
|
|
6807
|
-
|
|
6808
6789
|
const getAllPossibleResponses = correctResponse => {
|
|
6809
6790
|
if (!correctResponse) {
|
|
6810
6791
|
return [];
|
|
6811
6792
|
}
|
|
6812
|
-
|
|
6813
6793
|
return correctResponse.reduce((acc, cR) => {
|
|
6814
6794
|
// main response
|
|
6815
6795
|
if (acc[0]) {
|
|
@@ -6818,9 +6798,9 @@ const getAllPossibleResponses = correctResponse => {
|
|
|
6818
6798
|
acc.push({
|
|
6819
6799
|
[cR.category]: cR.choices
|
|
6820
6800
|
});
|
|
6821
|
-
}
|
|
6822
|
-
|
|
6801
|
+
}
|
|
6823
6802
|
|
|
6803
|
+
// alternate responses
|
|
6824
6804
|
if (cR.alternateResponses) {
|
|
6825
6805
|
cR.alternateResponses.forEach((aR, index) => {
|
|
6826
6806
|
if (acc[index + 1]) {
|
|
@@ -6832,10 +6812,10 @@ const getAllPossibleResponses = correctResponse => {
|
|
|
6832
6812
|
}
|
|
6833
6813
|
});
|
|
6834
6814
|
}
|
|
6835
|
-
|
|
6836
6815
|
return acc;
|
|
6837
6816
|
}, []);
|
|
6838
6817
|
};
|
|
6818
|
+
|
|
6839
6819
|
/**
|
|
6840
6820
|
* build the categories to be displayed, where each category has its own list of choices
|
|
6841
6821
|
* @param {Object[]} categories
|
|
@@ -6846,7 +6826,6 @@ const getAllPossibleResponses = correctResponse => {
|
|
|
6846
6826
|
* BuiltCategory = { id: categoryId, label: categoryLabel, choices: BuiltChoice[]},
|
|
6847
6827
|
* BuiltChoice = { id: choiceId, content: choiceContent }
|
|
6848
6828
|
*/
|
|
6849
|
-
|
|
6850
6829
|
const buildCategories = (categories, choices, answers) => {
|
|
6851
6830
|
categories = categories || [];
|
|
6852
6831
|
answers = answers || [];
|
|
@@ -6861,6 +6840,7 @@ const buildCategories = (categories, choices, answers) => {
|
|
|
6861
6840
|
});
|
|
6862
6841
|
});
|
|
6863
6842
|
};
|
|
6843
|
+
|
|
6864
6844
|
/**
|
|
6865
6845
|
* build the choice and category state
|
|
6866
6846
|
* @param {Object[]} categories
|
|
@@ -6870,7 +6850,6 @@ const buildCategories = (categories, choices, answers) => {
|
|
|
6870
6850
|
* @param correctResponse
|
|
6871
6851
|
* @returns { categories: Category[], choices: Choice[], correct: boolean, bestResponse: Response }, where Response: { categoryId: choiceId[] }
|
|
6872
6852
|
*/
|
|
6873
|
-
|
|
6874
6853
|
const buildState = (categories, choices, answers, correctResponse) => {
|
|
6875
6854
|
categories = categories || [];
|
|
6876
6855
|
choices = choices || [];
|
|
@@ -6878,16 +6857,18 @@ const buildState = (categories, choices, answers, correctResponse) => {
|
|
|
6878
6857
|
const allResponses = getAllPossibleResponses(correctResponse) || [];
|
|
6879
6858
|
let builtCategories = buildCategories(categories, choices, answers);
|
|
6880
6859
|
let bestResponse;
|
|
6881
|
-
let entirelyCorrect;
|
|
6860
|
+
let entirelyCorrect;
|
|
6882
6861
|
|
|
6862
|
+
// at least one defined correct response
|
|
6883
6863
|
if (allResponses.length) {
|
|
6884
6864
|
const builtData = allResponses.reduce((acc, possibleResponse) => {
|
|
6885
6865
|
// set correctness for each possible response
|
|
6886
6866
|
const {
|
|
6887
6867
|
builtCategories: categoriesWithChoices,
|
|
6888
6868
|
correct
|
|
6889
|
-
} = getBuiltCategories(possibleResponse, builtCategories);
|
|
6869
|
+
} = getBuiltCategories(possibleResponse, builtCategories);
|
|
6890
6870
|
|
|
6871
|
+
// if bestResponse not found yet OR no correct response found yet and this one is the correct one
|
|
6891
6872
|
if (acc.bestResponse === null || !acc.entirelyCorrect && correct) {
|
|
6892
6873
|
return {
|
|
6893
6874
|
bestResponse: possibleResponse,
|
|
@@ -6895,7 +6876,6 @@ const buildState = (categories, choices, answers, correctResponse) => {
|
|
|
6895
6876
|
entirelyCorrect: correct
|
|
6896
6877
|
};
|
|
6897
6878
|
}
|
|
6898
|
-
|
|
6899
6879
|
return acc;
|
|
6900
6880
|
}, {
|
|
6901
6881
|
bestResponse: null,
|
|
@@ -6906,7 +6886,6 @@ const buildState = (categories, choices, answers, correctResponse) => {
|
|
|
6906
6886
|
bestResponse = builtData.bestResponse;
|
|
6907
6887
|
entirelyCorrect = builtData.entirelyCorrect;
|
|
6908
6888
|
}
|
|
6909
|
-
|
|
6910
6889
|
const filteredChoices = choices.map(ch => stillSelectable(ch, builtCategories) ? ch : {
|
|
6911
6890
|
empty: true
|
|
6912
6891
|
});
|