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

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.
@@ -0,0 +1,282 @@
1
+ import { score } from '../scoring';
2
+ import util from 'util';
3
+
4
+ describe('scoring', () => {
5
+ describe('score', () => {
6
+ const _assert = (only) => (label, categories, scoring, expected) => {
7
+ const fn = only ? it.only : it;
8
+ fn(label, async () => {
9
+ const result = await score(categories, scoring);
10
+
11
+ expect(result).toMatchObject(expected);
12
+ });
13
+ };
14
+
15
+ const assert = _assert(false);
16
+ assert.only = _assert(true);
17
+
18
+ describe('w/ partial rules', () => {
19
+ assert(
20
+ '1 category w/ 1 correct, partial rule for it @ 20%',
21
+ [
22
+ {
23
+ id: '1',
24
+ correct: false,
25
+ choices: [{ correct: true }],
26
+ },
27
+ ],
28
+ {
29
+ partial: {
30
+ enabled: true,
31
+ rules: [
32
+ {
33
+ category: '1',
34
+ rules: [{ count: 1, percent: 20 }],
35
+ },
36
+ ],
37
+ },
38
+ },
39
+ {
40
+ score: 0.2,
41
+ },
42
+ );
43
+
44
+ assert(
45
+ '2 categories equal weights, 1 correct@20%, 2 correct@85% ',
46
+ [
47
+ {
48
+ id: '1',
49
+ correct: false,
50
+ choices: [{ correct: true }],
51
+ },
52
+ {
53
+ id: '2',
54
+ correct: false,
55
+ choices: [{ correct: true }, { correct: true }],
56
+ },
57
+ ],
58
+ {
59
+ partial: {
60
+ enabled: true,
61
+ rules: [
62
+ {
63
+ category: '1',
64
+ rules: [{ count: 1, percent: 20 }],
65
+ },
66
+ {
67
+ category: '2',
68
+ rules: [{ count: 2, percent: 85 }],
69
+ },
70
+ ],
71
+ },
72
+ },
73
+ {
74
+ score: 0.53,
75
+ },
76
+ );
77
+
78
+ assert(
79
+ '2 categories, weights 1 + 3, 1 correct@20% 2 correct@85%',
80
+ [
81
+ {
82
+ id: '1',
83
+ correct: false,
84
+ choices: [{ correct: true }],
85
+ },
86
+ {
87
+ id: '2',
88
+ correct: false,
89
+ choices: [{ correct: true }, { correct: true }],
90
+ },
91
+ ],
92
+ {
93
+ weighting: {
94
+ enabled: true,
95
+ rules: [{ category: '2', points: 3 }],
96
+ },
97
+ partial: {
98
+ enabled: true,
99
+ rules: [
100
+ {
101
+ category: '1',
102
+ rules: [{ count: 1, percent: 20 }],
103
+ },
104
+ {
105
+ category: '2',
106
+ rules: [{ count: 2, percent: 85 }],
107
+ },
108
+ ],
109
+ },
110
+ },
111
+ {
112
+ score: 0.69,
113
+ },
114
+ );
115
+
116
+ assert(
117
+ '2 categories, weights 1 + 3, 1 correct@20% only',
118
+ [
119
+ {
120
+ id: '1',
121
+ correct: false,
122
+ choices: [{ correct: true }],
123
+ },
124
+ {
125
+ id: '2',
126
+ correct: false,
127
+ choices: [{ correct: true }, { correct: true }],
128
+ },
129
+ ],
130
+ {
131
+ weighting: {
132
+ enabled: true,
133
+ rules: [{ category: '2', points: 3 }],
134
+ },
135
+ partial: {
136
+ enabled: true,
137
+ rules: [
138
+ {
139
+ category: '1',
140
+ rules: [{ count: 1, percent: 20 }],
141
+ },
142
+ ],
143
+ },
144
+ },
145
+ {
146
+ score: 0.05,
147
+ },
148
+ );
149
+ });
150
+
151
+ describe('2 equal weights', () => {
152
+ const a = (categories, expected) =>
153
+ assert(
154
+ '2 equal weights',
155
+ categories,
156
+ {
157
+ weighting: {
158
+ rules: [
159
+ { category: '1', points: 1 },
160
+ { category: '2', points: 1 },
161
+ ],
162
+ },
163
+ },
164
+ expected,
165
+ );
166
+
167
+ a(
168
+ [
169
+ { id: '1', correct: true },
170
+ { id: '2', correct: true },
171
+ ],
172
+ {
173
+ score: 1.0,
174
+ },
175
+ );
176
+ a(
177
+ [
178
+ { id: '1', correct: false },
179
+ { id: '2', correct: true },
180
+ ],
181
+ {
182
+ score: 0.5,
183
+ },
184
+ );
185
+ a(
186
+ [
187
+ { id: '1', correct: false },
188
+ { id: '2', correct: false },
189
+ ],
190
+ {
191
+ score: 0.0,
192
+ },
193
+ );
194
+ });
195
+
196
+ describe('2 unequal weights', () => {
197
+ const a = (categories, expected) =>
198
+ assert(
199
+ '2 unequal weights',
200
+ categories,
201
+ {
202
+ weighting: {
203
+ enabled: true,
204
+ rules: [
205
+ { category: '1', points: 3 },
206
+ { category: '2', points: 1 },
207
+ ],
208
+ },
209
+ },
210
+ expected,
211
+ );
212
+
213
+ a(
214
+ [
215
+ { id: '1', correct: true },
216
+ { id: '2', correct: true },
217
+ ],
218
+ {
219
+ score: 1.0,
220
+ },
221
+ );
222
+ a(
223
+ [
224
+ { id: '1', correct: false },
225
+ { id: '2', correct: true },
226
+ ],
227
+ {
228
+ score: 0.25,
229
+ },
230
+ );
231
+
232
+ a(
233
+ [
234
+ { id: '1', correct: true },
235
+ { id: '2', correct: false },
236
+ ],
237
+ {
238
+ score: 0.75,
239
+ },
240
+ );
241
+
242
+ a(
243
+ [
244
+ { id: '1', correct: false },
245
+ { id: '2', correct: false },
246
+ ],
247
+ {
248
+ score: 0.0,
249
+ },
250
+ );
251
+ });
252
+
253
+ describe('2 unequal weights with recurring number', () => {
254
+ const a = (categories, expected) =>
255
+ assert(
256
+ '2 unequal weights with recurring number',
257
+ categories,
258
+ // [{ id: '1', correct: true }, { id: '2', correct: true }],
259
+ {
260
+ weighting: {
261
+ enabled: true,
262
+ rules: [
263
+ { category: '1', points: 2 },
264
+ { category: '2', points: 1 },
265
+ ],
266
+ },
267
+ },
268
+ expected,
269
+ );
270
+
271
+ a(
272
+ [
273
+ { id: '1', correct: false },
274
+ { id: '2', correct: true },
275
+ ],
276
+ {
277
+ score: 0.33,
278
+ },
279
+ );
280
+ });
281
+ });
282
+ });
@@ -0,0 +1,19 @@
1
+ export const cat = (id) => ({ id });
2
+
3
+ export const cats = (choices) => ({
4
+ choices: choices.map(cat),
5
+ });
6
+
7
+ export const choice = (id, categoryCount, correct) => {
8
+ const out = { id, categoryCount };
9
+
10
+ if (correct === false || correct === true) {
11
+ out.correct = correct;
12
+ }
13
+ return out;
14
+ };
15
+
16
+ export const answer = (category, choices, alternateResponses) => {
17
+ const c = choices ? (Array.isArray(choices) ? choices : [choices]) : [];
18
+ return { alternateResponses, category, choices: c };
19
+ };
package/src/index.js CHANGED
@@ -293,6 +293,22 @@ export const removeChoiceFromCategory = (choiceId, categoryId, choiceIndex, answ
293
293
  });
294
294
  };
295
295
 
296
+ export const removeChoiceFromAlternate = (choiceId, categoryId, choiceIndex, altIndex, answers) => {
297
+ return (answers || []).map((a) => {
298
+ if (a?.category === categoryId) {
299
+ const newAlternates = (a?.alternateResponses || []).map((altArray, idx) => {
300
+ if (idx === altIndex) {
301
+ (altArray || []).splice(choiceIndex, 1);
302
+ return altArray;
303
+ }
304
+ return altArray;
305
+ });
306
+ return { ...a, alternateResponses: newAlternates };
307
+ }
308
+ return a;
309
+ });
310
+ };
311
+
296
312
  export const moveChoiceToCategory = (choiceId, from, to, choiceIndex, answers) => {
297
313
  log('[moveChoiceToCategory] choice: ', choiceId, 'from: ', from, 'to: ', to, 'answers: ', answers);
298
314
 
package/README.md DELETED
@@ -1,5 +0,0 @@
1
- # @pie-lib/categorize
2
-
3
- Some model functions for categorize.
4
-
5
- > This packages has no UI dependencies as it should be able to run server side.gw