@pie-lib/categorize 0.8.17 → 0.9.0-beta.2

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