@pie-lib/categorize 0.1.0

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,128 @@
1
+ /**
2
+ * @synced-from pie-lib/packages/categorize/src/index.js
3
+ * @auto-generated
4
+ *
5
+ * This file is automatically synced from pie-elements and converted to TypeScript.
6
+ * Manual edits will be overwritten on next sync.
7
+ * To make changes, edit the upstream JavaScript file and run sync again.
8
+ */
9
+ import { score } from './scoring.js';
10
+ export { score };
11
+ export declare const FooTwo = "foo-two";
12
+ export declare const limitChoices: (choiceId: any, count: any, choices: any) => any;
13
+ /**
14
+ * Limit in an array of arrays.
15
+ * So for an array with [[1,1],[1,1]] if you call limitInArray(1, arr, 3) you'll get: [[1,1], [1]]
16
+ * @param {*} id
17
+ * @param {*} arrays
18
+ * @param {*} limit
19
+ */
20
+ export declare const limitInArrays: (id: any, arrays: any, limit: any) => any;
21
+ /**
22
+ * Limit the id in the array to the limit set.
23
+ * So for an array with [1,1,1,1] if you call limitInArray(1, arr, 2) you'll get: [1,1]
24
+ * @param {string} id
25
+ * @param {string[]} arr
26
+ * @param {number} limit
27
+ */
28
+ export declare const limitInArray: (id: any, arr: any, limit: any) => {
29
+ array: any;
30
+ count: any;
31
+ };
32
+ /**
33
+ * Ensure that there are no extra choices in answer, if a choice.categoryCount is > 0.
34
+ * @param {{category: string, choices: string[]}[]} answer
35
+ * @param {{id:string,categoryCount:number}} choices
36
+ */
37
+ export declare const ensureNoExtraChoicesInAnswer: (answer: any, choices: any) => any;
38
+ /**
39
+ * Ensure that there are no extra choices in alternate responses, if a choice.categoryCount is > 0.
40
+ * @param {{category: string, choices: string[]}[]} answer
41
+ * @param {{id:string,categoryCount:number}} choices
42
+ */
43
+ export declare const ensureNoExtraChoicesInAlternate: (answer: any, choices: any) => any;
44
+ /**
45
+ * Count the number of choice ids in a given answer array
46
+ * @param {string} choiceId
47
+ * @param {{category: string, choices: string[]}[]} answer
48
+ */
49
+ export declare const countInAnswer: (choiceId: any, answer: any) => any;
50
+ export declare const countInChoices: (choiceId: any, choices: any) => any;
51
+ /**
52
+ * Count the number of times a choice has been selected in categories.
53
+ * @param {*} choice
54
+ * @param {*} categories
55
+ */
56
+ export declare const countChosen: (choice: any, categories: any) => any;
57
+ /**
58
+ * @param {string?} categoryId
59
+ * @param {{category:string, choices: string[]}} answers
60
+ */
61
+ export declare const removeCategory: (categoryId: any, answers: any) => any;
62
+ /**
63
+ * @param {string} choiceId
64
+ * @param {{category:string, choices: string[]}} answers
65
+ * @param {string?} categoryId - optional categegory id
66
+ */
67
+ export declare const removeAllChoices: (choiceId: any, answers: any, categoryId: any) => any;
68
+ export declare const rearrangeChoices: (choices: any, indexFrom: any, indexTo: any) => any;
69
+ export declare const verifyAllowMultiplePlacements: (choice: any, categoryId: any, answers: any) => any;
70
+ export declare const removeChoiceFromCategory: (choiceId: any, categoryId: any, choiceIndex: any, answers: any) => any;
71
+ export declare const removeChoiceFromAlternate: (choiceId: any, categoryId: any, choiceIndex: any, altIndex: any, answers: any) => any;
72
+ export declare const moveChoiceToCategory: (choiceId: any, from: any, to: any, choiceIndex: any, answers: any) => any;
73
+ export declare const moveChoiceToAlternate: (choiceId: any, from: any, to: any, choiceIndex: any, answers: any, alternateIndex: any, categoryCount: any) => any;
74
+ export declare const stillSelectable: (h: any, builtCategories: any) => boolean;
75
+ /**
76
+ * returns all choices with property 'correct' set to boolean
77
+ * @param {string[]} possibleResponseChoices
78
+ * @param {Object[]} builtCategoryChoices
79
+ *
80
+ * @returns {Object}, returns builtChoices: BuiltChoice[] and allChoicesAreCorrect: boolean
81
+ * BuiltChoice = { id: choiceId, content: choiceContent, correct: boolean }
82
+ */
83
+ export declare const buildChoices: (possibleResponseChoices: any, builtCategoryChoices: any) => any;
84
+ /**
85
+ * returns corrected answer using this possible correct response
86
+ * @param {Object} possibleResponse, has form: { CategoryId: ChoiceId[] }
87
+ * @param {Object[]} builtCategories
88
+ *
89
+ * @returns {Object}, returns builtCategories: BuiltCategory[] and correct: boolean
90
+ * BuiltCategory = { id: categoryId, label: categoryLabel, correct: boolean, choices: BuiltChoice[] }
91
+ * BuiltChoice = { id: choiceId, content: choiceContent, correct: boolean }
92
+ */
93
+ export declare const getBuiltCategories: (possibleResponse: any, builtCategories: any) => any;
94
+ /**
95
+ * returns all the possible responses by combining the proper alternate responses
96
+ * @param {Object[]} correctResponse
97
+ *
98
+ * @returns {Object[]}, returns Response[]
99
+ * Response = { CategoryId: ChoiceId[] },
100
+ */
101
+ export declare const getAllPossibleResponses: (correctResponse: any) => any;
102
+ /**
103
+ * build the categories to be displayed, where each category has its own list of choices
104
+ * @param {Object[]} categories
105
+ * @param {{id:string}[]} choices
106
+ * @param {{category: string, choices:string[]}[]} answers
107
+ *
108
+ * @returns {Object[]}, returns BuiltCategory[]
109
+ * BuiltCategory = { id: categoryId, label: categoryLabel, choices: BuiltChoice[]},
110
+ * BuiltChoice = { id: choiceId, content: choiceContent }
111
+ */
112
+ export declare const buildCategories: (categories: any, choices: any, answers: any) => any;
113
+ /**
114
+ * build the choice and category state
115
+ * @param {Object[]} categories
116
+ * @param {{id:string}[]} choices
117
+ * @param {{category: string, choices:string[]}[]} answers
118
+ *
119
+ * @param correctResponse
120
+ * @returns { categories: Category[], choices: Choice[], correct: boolean, bestResponse: Response }, where Response: { categoryId: choiceId[] }
121
+ */
122
+ export declare const buildState: (categories: any, choices: any, answers: any, correctResponse: any) => {
123
+ choices: any;
124
+ categories: any;
125
+ correct: any;
126
+ bestResponse: any;
127
+ };
128
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAIH,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAErC,OAAO,EAAE,KAAK,EAAE,CAAC;AAEjB,eAAO,MAAM,MAAM,YAAY,CAAC;AAIhC,eAAO,MAAM,YAAY,GAAI,aAAQ,EAAE,UAAK,EAAE,YAAO,QAapD,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,aAAa,GAAI,OAAE,EAAE,WAAM,EAAE,UAAK,QAiB9C,CAAC;AAEF;;;;;;GAMG;AACH,eAAO,MAAM,YAAY,GAAI,OAAE,EAAE,QAAG,EAAE,UAAK;;;CA0B1C,CAAC;AAEF;;;;GAIG;AACH,eAAO,MAAM,4BAA4B,GAAI,WAAM,EAAE,YAAO,QAwB3D,CAAC;AAEF;;;;GAIG;AAEH,eAAO,MAAM,+BAA+B,GAAI,WAAM,EAAE,YAAO,QAkD9D,CAAC;AACF;;;;GAIG;AACH,eAAO,MAAM,aAAa,GAAI,aAAQ,EAAE,WAAM,QAM7C,CAAC;AAEF,eAAO,MAAM,cAAc,GAAI,aAAQ,EAAE,YAAO,QAAyD,CAAC;AAE1G;;;;GAIG;AACH,eAAO,MAAM,WAAW,GAAI,WAAM,EAAE,eAAU,QAc7C,CAAC;AAEF;;;GAGG;AACH,eAAO,MAAM,cAAc,GAAI,eAAU,EAAE,YAAO,QAAqD,CAAC;AAExG;;;;GAIG;AACH,eAAO,MAAM,gBAAgB,GAAI,aAAQ,EAAE,YAAO,EAAE,eAAU,QAY7D,CAAC;AAEF,eAAO,MAAM,gBAAgB,GAAI,YAAO,EAAE,cAAS,EAAE,YAAO,QAQ3D,CAAC;AAEF,eAAO,MAAM,6BAA6B,GAAI,WAAM,EAAE,eAAU,EAAE,YAAO,QAwBxE,CAAC;AAEF,eAAO,MAAM,wBAAwB,GAAI,aAAQ,EAAE,eAAU,EAAE,gBAAW,EAAE,YAAO,QAmBlF,CAAC;AAEF,eAAO,MAAM,yBAAyB,GAAI,aAAQ,EAAE,eAAU,EAAE,gBAAW,EAAE,aAAQ,EAAE,YAAO,QAc7F,CAAC;AAEF,eAAO,MAAM,oBAAoB,GAAI,aAAQ,EAAE,SAAI,EAAE,OAAE,EAAE,gBAAW,EAAE,YAAO,QA4B5E,CAAC;AAEF,eAAO,MAAM,qBAAqB,GAAI,aAAQ,EAAE,SAAI,EAAE,OAAE,EAAE,gBAAW,EAAE,YAAO,EAAE,mBAAc,EAAE,kBAAa,QAwC5G,CAAC;AAEF,eAAO,MAAM,eAAe,GAAI,MAAC,EAAE,oBAAe,YAQjD,CAAC;AAEF;;;;;;;GAOG;AACH,eAAO,MAAM,YAAY,GAAI,4BAAuB,EAAE,yBAAoB,QAiCzE,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,kBAAkB,GAAI,qBAAgB,EAAE,oBAAe,QAmBjE,CAAC;AAEJ;;;;;;GAMG;AACH,eAAO,MAAM,uBAAuB,GAAI,oBAAe,QA0BtD,CAAC;AAEF;;;;;;;;;GASG;AACH,eAAO,MAAM,eAAe,GAAI,eAAU,EAAE,YAAO,EAAE,YAAO,QAc3D,CAAC;AAEF;;;;;;;;GAQG;AACH,eAAO,MAAM,UAAU,GAAI,eAAU,EAAE,YAAO,EAAE,YAAO,EAAE,oBAAe;;;;;CAoDvE,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,147 @@
1
+ import { score as e } from "./scoring.js";
2
+ import t from "debug";
3
+ import { clone as n, remove as r } from "lodash-es";
4
+ //#region src/index.ts
5
+ var i = "foo-two", a = t("@pie-lib:categorize"), o = (e, t, n) => n.reduce((n, r) => {
6
+ let i = f(e, n.choices);
7
+ return (r !== e || i < t) && n.choices.push(r), n;
8
+ }, { choices: [] }).choices, s = (e, t, n) => (t ||= [], t.reduce((t, r) => {
9
+ let i = c(e, r, n - t.count);
10
+ return t.out.push(i.array), t.count += i.count || 0, t;
11
+ }, {
12
+ out: [],
13
+ count: 0
14
+ }).out), c = (e, t, n) => {
15
+ if (t ||= [], n === 0) {
16
+ let n = t.filter((t) => t !== e);
17
+ return {
18
+ array: n,
19
+ count: t.length - n.length
20
+ };
21
+ } else {
22
+ let r = t.reduce((t, r) => (r === e ? t.count < n && (t.count += 1, t.out.push(r)) : t.out.push(r), t), {
23
+ out: [],
24
+ count: 0
25
+ });
26
+ return {
27
+ array: r.out,
28
+ count: r.count
29
+ };
30
+ }
31
+ }, l = (e, t) => (t ||= [], t.reduce((e, t) => {
32
+ if (a("choice: ----> ", t.id, "categoryCount: ", t.categoryCount), t.categoryCount === void 0 || t.categoryCount === 0) return e;
33
+ {
34
+ let n = e.map((e) => e.choices);
35
+ return s(t.id, n, t.categoryCount).map((t, n) => ({
36
+ category: e[n].category,
37
+ alternateResponses: e[n].alternateResponses,
38
+ choices: t
39
+ }));
40
+ }
41
+ }, e)), u = (e, t) => (t ||= [], t.reduce((e, t) => {
42
+ if (a("choice: ----> ", t.id, "categoryCount: ", t.categoryCount), t.categoryCount === void 0 || t.categoryCount === 0) return e;
43
+ {
44
+ let n = {};
45
+ e.forEach((e) => (e.alternateResponses || []).forEach((e, t) => {
46
+ t in n || (n[t] = []), n[t].push(e);
47
+ }));
48
+ let r = [];
49
+ return Object.keys(n).forEach((e) => {
50
+ n[e] = s(t.id, n[e], t.categoryCount), n[e].forEach((e, t) => {
51
+ r[t] || (r[t] = []), r[t].push(e);
52
+ });
53
+ }), e.map((e, t) => ({
54
+ category: e.category,
55
+ alternateResponses: r[t] || e.alternateResponses,
56
+ choices: e.choices
57
+ }));
58
+ }
59
+ }, e)), d = (e, t) => {
60
+ let n = t.reduce((t, n) => t + f(e, n.choices), 0);
61
+ return a("[countInAnswer] choiceId:", e, t), n;
62
+ }, f = (e, t) => (t || []).filter((t) => t === e).length, p = (e, t) => !e || !e.id || !Array.isArray(t) ? 0 : t.reduce((t, n) => t + (n.choices || []).filter((t) => t && t.id === e.id).length, 0), m = (e, t) => t.filter((t) => t.category !== e), h = (e, t, i) => t.map((t) => {
63
+ if (!i || t.category === i) {
64
+ let i = n(t.choices);
65
+ return r(i, (t) => t === e), {
66
+ ...t,
67
+ choices: i
68
+ };
69
+ } else return t;
70
+ }), g = (e, t, n) => (e.length === 0 || e.splice(t, 1, e.splice(n, 1, e[t])[0]), e), _ = (e, t, n) => n.map((n) => n.category === t ? (n.choices = n.choices.reduce((t, n) => (n.id === e.id ? t.findIndex((t) => t.id === e.id) === -1 && t.push(n) : t.push(n), t), []), n) : (n.choices = (n.choices || []).filter((t) => t !== e.id), n)), v = (e, t, r, i) => (a("[removeChoiceFromCategory] choiceIndex:", r), i.map((i) => {
71
+ if (i.category === t) {
72
+ let t = n(i.choices), a = t.findIndex((t, n) => t === e && n >= r);
73
+ return a !== -1 && t.splice(a, 1), {
74
+ ...i,
75
+ choices: t
76
+ };
77
+ } else return i;
78
+ })), y = (e, t, n, r, i) => (i || []).map((e) => {
79
+ if (e?.category === t) {
80
+ let t = (e?.alternateResponses || []).map((e, t) => (t === r && (e || []).splice(n, 1), e));
81
+ return {
82
+ ...e,
83
+ alternateResponses: t
84
+ };
85
+ }
86
+ return e;
87
+ }), b = (e, t, n, r, i) => (a("[moveChoiceToCategory] choice: ", e, "from: ", t, "to: ", n, "answers: ", i), t === n ? i : (t && (i = v(e, t, r, i)), i.findIndex((e) => e.category === n) === -1 ? (i.push({
88
+ category: n,
89
+ choices: [e]
90
+ }), i) : i.map((t) => t.category === n ? (t.choices = t.choices || [], t.choices.push(e), t) : t))), x = (e, t, n, r, i, o, s) => (a("[moveChoiceToAlternate] choice: ", e, "from: ", t, "to: ", n, "answers: ", i, "chocieIndex: ", r), t === n ? i : i.map((r) => (r.category === n && (s !== 0 && r.alternateResponses && (r.alternateResponses[o] = (r.alternateResponses[o] || []).filter((t) => t !== e)), r.alternateResponses || (r.alternateResponses = [], r.alternateResponses[o] = []), r.alternateResponses[o].push(e)), r.category === t && s !== 0 && r.alternateResponses && r.alternateResponses[o] && (r.alternateResponses[o] = r.alternateResponses[o].filter((t) => t !== e)), r))), S = (e, t) => e.categoryCount > 0 ? p(e, t) < e.categoryCount : !0, C = (e, t) => t.reduce((e, t) => {
91
+ let n = e.copyOfPossibleResponse.findIndex((e) => t && e === t.id);
92
+ return n >= 0 ? (e.builtChoices.push({
93
+ ...t,
94
+ correct: !0
95
+ }), e.copyOfPossibleResponse = [...e.copyOfPossibleResponse.slice(0, n), ...e.copyOfPossibleResponse.slice(n + 1)]) : (e.builtChoices.push({
96
+ ...t,
97
+ correct: !1
98
+ }), e.allChoicesAreCorrect = !1), e;
99
+ }, {
100
+ builtChoices: [],
101
+ copyOfPossibleResponse: [...e],
102
+ allChoicesAreCorrect: !0
103
+ }), w = (e, t) => t.reduce((t, n) => {
104
+ let r = e[n.id] || [], i = n.choices || [], { builtChoices: a, allChoicesAreCorrect: o } = C(r, i), s = i.length === r.length;
105
+ return t.builtCategories.push({
106
+ ...n,
107
+ correct: o && s,
108
+ choices: a
109
+ }), t.correct = t.correct && o && s, t;
110
+ }, {
111
+ builtCategories: [],
112
+ correct: !0
113
+ }), T = (e) => e ? e.reduce((e, t) => (e[0] ? e[0][t.category] = t.choices : e.push({ [t.category]: t.choices }), t.alternateResponses && t.alternateResponses.forEach((n, r) => {
114
+ e[r + 1] ? e[r + 1][t.category] = n : e.push({ [t.category]: n });
115
+ }), e), []) : [], E = (e, t, n) => (e ||= [], n ||= [], t ||= [], e.map((e) => {
116
+ let { choices: r = [] } = n.find((t) => t.category === e.id) || {};
117
+ return {
118
+ ...e,
119
+ choices: r.map((e) => t.find((t) => t.id === e))
120
+ };
121
+ })), D = (e, t, n, r) => {
122
+ e ||= [], t ||= [], n ||= [];
123
+ let i = T(r) || [], a = E(e, t, n), o, s;
124
+ if (i.length) {
125
+ let e = i.reduce((e, t) => {
126
+ let { builtCategories: n, correct: r } = w(t, a);
127
+ return e.bestResponse === null || !e.entirelyCorrect && r ? {
128
+ bestResponse: t,
129
+ builtCategories: n,
130
+ entirelyCorrect: r
131
+ } : e;
132
+ }, {
133
+ bestResponse: null,
134
+ builtCategories: [],
135
+ entirelyCorrect: !1
136
+ });
137
+ a = e.builtCategories, o = e.bestResponse, s = e.entirelyCorrect;
138
+ }
139
+ return {
140
+ choices: t.map((e) => S(e, a) ? e : { empty: !0 }),
141
+ categories: a,
142
+ correct: s,
143
+ bestResponse: o
144
+ };
145
+ };
146
+ //#endregion
147
+ export { i as FooTwo, E as buildCategories, C as buildChoices, D as buildState, p as countChosen, d as countInAnswer, f as countInChoices, u as ensureNoExtraChoicesInAlternate, l as ensureNoExtraChoicesInAnswer, T as getAllPossibleResponses, w as getBuiltCategories, o as limitChoices, c as limitInArray, s as limitInArrays, x as moveChoiceToAlternate, b as moveChoiceToCategory, g as rearrangeChoices, h as removeAllChoices, m as removeCategory, y as removeChoiceFromAlternate, v as removeChoiceFromCategory, e as score, S as stillSelectable, _ as verifyAllowMultiplePlacements };
@@ -0,0 +1,17 @@
1
+ /**
2
+ * @synced-from pie-lib/packages/categorize/src/scoring.js
3
+ * @auto-generated
4
+ *
5
+ * This file is automatically synced from pie-elements and converted to TypeScript.
6
+ * Manual edits will be overwritten on next sync.
7
+ * To make changes, edit the upstream JavaScript file and run sync again.
8
+ */
9
+ /**
10
+ *
11
+ * Calculate score based on the categories model generated from `buildState` and the partial scoring rules.
12
+ * @param {*} categories - this is the categories that you get from `buildState`.
13
+ * @param {*} scoring - scoring config.
14
+ * @see ../index#buildState
15
+ */
16
+ export declare const score: (categories: any, scoring: any) => Promise<unknown>;
17
+ //# sourceMappingURL=scoring.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scoring.d.ts","sourceRoot":"","sources":["../src/scoring.ts"],"names":[],"mappings":"AACA;;;;;;;GAOG;AAgCH;;;;;;GAMG;AAEH,eAAO,MAAM,KAAK,GAAI,eAAU,EAAE,YAAO,qBA+ErC,CAAC"}
@@ -0,0 +1,66 @@
1
+ import e from "debug";
2
+ //#region src/scoring.ts
3
+ var t = e("@pie-lib:categorize:scoring"), n = (e) => !e || !e.weighting ? [] : e.weighting.enabled && e.weighting && e.weighting.rules || [], r = (e, n) => {
4
+ if (t("partial? ", e), !(!e || !e.enabled)) {
5
+ let t = e.rules.find((e) => e.category === n);
6
+ return t && t.rules || [];
7
+ }
8
+ }, i = (e, i) => new Promise((a) => {
9
+ t("categories: ", e, "scoring: ", i);
10
+ let o = n(i), s = e.map((e) => ({
11
+ ...o.find((t) => t.category === e.id) || { points: 1 },
12
+ category: e.id
13
+ }));
14
+ t("weights: ", s);
15
+ let c = e.reduce((e, t) => {
16
+ let n = s.find((e) => e.category === t.id);
17
+ return e += n.points;
18
+ }, 0);
19
+ t("total: ", c);
20
+ let l = e.map((e) => {
21
+ let n = s.find((t) => t.category === e.id).points;
22
+ if (t("category: ", e), e.correct === !0) return {
23
+ category: e.id,
24
+ score: n,
25
+ points: n
26
+ };
27
+ {
28
+ let a = r(i.partial, e.id);
29
+ if (t("partial rules for category: ", a), a) {
30
+ let r = (e.choices || []).filter((e) => e.correct).length;
31
+ t("correctCount: ", r);
32
+ let i = a.find((e) => e.count === r);
33
+ t("rule: ", i), i ||= {
34
+ percent: 0,
35
+ count: r
36
+ };
37
+ let o = n * (i.percent / 100);
38
+ return {
39
+ category: e.id,
40
+ score: o,
41
+ points: n,
42
+ partial: {
43
+ correctChoices: r,
44
+ rule: i
45
+ }
46
+ };
47
+ } else return {
48
+ category: e.id,
49
+ score: 0,
50
+ points: n
51
+ };
52
+ }
53
+ }), u = l.reduce((e, t) => e += t.score, 0);
54
+ t("weightedScore: ", u), a({
55
+ score: parseFloat((u / c).toFixed(2), 10),
56
+ details: {
57
+ weighted: {
58
+ total: c,
59
+ score: u
60
+ },
61
+ categories: l
62
+ }
63
+ });
64
+ });
65
+ //#endregion
66
+ export { i as score };
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@pie-lib/categorize",
3
+ "version": "0.1.0",
4
+ "description": "",
5
+ "dependencies": {
6
+ "debug": "^4.1.1",
7
+ "lodash-es": "^4.17.23"
8
+ },
9
+ "type": "module",
10
+ "main": "./dist/index.js",
11
+ "types": "./dist/index.d.ts",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.ts",
15
+ "default": "./dist/index.js"
16
+ }
17
+ },
18
+ "files": [
19
+ "dist",
20
+ "src"
21
+ ],
22
+ "sideEffects": false,
23
+ "scripts": {
24
+ "build": "bun x vite build && bun x tsc --emitDeclarationOnly",
25
+ "dev": "bun x vite",
26
+ "test": "bun x vitest run"
27
+ }
28
+ }
package/src/index.ts ADDED
@@ -0,0 +1,596 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @synced-from pie-lib/packages/categorize/src/index.js
4
+ * @auto-generated
5
+ *
6
+ * This file is automatically synced from pie-elements and converted to TypeScript.
7
+ * Manual edits will be overwritten on next sync.
8
+ * To make changes, edit the upstream JavaScript file and run sync again.
9
+ */
10
+
11
+ import debug from 'debug';
12
+ import { clone, remove } from 'lodash-es';
13
+ import { score } from './scoring.js';
14
+
15
+ export { score };
16
+
17
+ export const FooTwo = 'foo-two';
18
+
19
+ const log = debug('@pie-lib:categorize');
20
+
21
+ export const limitChoices = (choiceId, count, choices) => {
22
+ const out = choices.reduce(
23
+ (acc, id) => {
24
+ const foundCount = countInChoices(choiceId, acc.choices);
25
+
26
+ if (id !== choiceId || foundCount < count) {
27
+ acc.choices.push(id);
28
+ }
29
+ return acc;
30
+ },
31
+ { choices: [] },
32
+ );
33
+ return out.choices;
34
+ };
35
+
36
+ /**
37
+ * Limit in an array of arrays.
38
+ * So for an array with [[1,1],[1,1]] if you call limitInArray(1, arr, 3) you'll get: [[1,1], [1]]
39
+ * @param {*} id
40
+ * @param {*} arrays
41
+ * @param {*} limit
42
+ */
43
+ export const limitInArrays = (id, arrays, limit) => {
44
+ arrays = arrays || [];
45
+
46
+ const result = arrays.reduce(
47
+ (acc, array) => {
48
+ const l = limit - acc.count;
49
+ const result = limitInArray(id, array, l);
50
+
51
+ acc.out.push(result.array);
52
+ acc.count = acc.count + (result.count || 0);
53
+
54
+ return acc;
55
+ },
56
+ { out: [], count: 0 },
57
+ );
58
+
59
+ return result.out;
60
+ };
61
+
62
+ /**
63
+ * Limit the id in the array to the limit set.
64
+ * So for an array with [1,1,1,1] if you call limitInArray(1, arr, 2) you'll get: [1,1]
65
+ * @param {string} id
66
+ * @param {string[]} arr
67
+ * @param {number} limit
68
+ */
69
+ export const limitInArray = (id, arr, limit) => {
70
+ arr = arr || [];
71
+
72
+ if (limit === 0) {
73
+ const stripped = arr.filter((v) => v !== id);
74
+
75
+ return { array: stripped, count: arr.length - stripped.length };
76
+ } else {
77
+ const result = arr.reduce(
78
+ (acc, v) => {
79
+ if (v === id) {
80
+ if (acc.count < limit) {
81
+ acc.count += 1;
82
+ acc.out.push(v);
83
+ }
84
+ } else {
85
+ acc.out.push(v);
86
+ }
87
+
88
+ return acc;
89
+ },
90
+ { out: [], count: 0 },
91
+ );
92
+
93
+ return { array: result.out, count: result.count };
94
+ }
95
+ };
96
+
97
+ /**
98
+ * Ensure that there are no extra choices in answer, if a choice.categoryCount is > 0.
99
+ * @param {{category: string, choices: string[]}[]} answer
100
+ * @param {{id:string,categoryCount:number}} choices
101
+ */
102
+ export const ensureNoExtraChoicesInAnswer = (answer, choices) => {
103
+ choices = choices || [];
104
+
105
+ const out = choices.reduce((answerArray, choice) => {
106
+ log('choice: ----> ', choice.id, 'categoryCount: ', choice.categoryCount);
107
+
108
+ if (choice.categoryCount === undefined || choice.categoryCount === 0) {
109
+ return answerArray;
110
+ } else {
111
+ const choices = answerArray.map((a) => a.choices);
112
+ const result = limitInArrays(choice.id, choices, choice.categoryCount);
113
+
114
+ const updatedArray = result.map((r, index) => {
115
+ return {
116
+ category: answerArray[index].category,
117
+ alternateResponses: answerArray[index].alternateResponses,
118
+ choices: r,
119
+ };
120
+ });
121
+
122
+ return updatedArray;
123
+ }
124
+ }, answer);
125
+ return out;
126
+ };
127
+
128
+ /**
129
+ * Ensure that there are no extra choices in alternate responses, if a choice.categoryCount is > 0.
130
+ * @param {{category: string, choices: string[]}[]} answer
131
+ * @param {{id:string,categoryCount:number}} choices
132
+ */
133
+
134
+ export const ensureNoExtraChoicesInAlternate = (answer, choices) => {
135
+ choices = choices || [];
136
+
137
+ const out = choices.reduce((answerArray, choice) => {
138
+ log('choice: ----> ', choice.id, 'categoryCount: ', choice.categoryCount);
139
+
140
+ if (choice.categoryCount === undefined || choice.categoryCount === 0) {
141
+ return answerArray;
142
+ } else {
143
+ let alternatePair = {};
144
+
145
+ answerArray.forEach((answer) =>
146
+ (answer.alternateResponses || []).forEach((alternate, index) => {
147
+ if (index in alternatePair) {
148
+ alternatePair[index].push(alternate);
149
+ } else {
150
+ alternatePair[index] = [];
151
+ alternatePair[index].push(alternate);
152
+ }
153
+ }),
154
+ );
155
+
156
+ const recreatedAlternate = [];
157
+
158
+ Object.keys(alternatePair).forEach((k) => {
159
+ const limitAlternate = limitInArrays(choice.id, alternatePair[k], choice.categoryCount);
160
+ alternatePair[k] = limitAlternate;
161
+
162
+ alternatePair[k].forEach((item, index) => {
163
+ if (!recreatedAlternate[index]) {
164
+ recreatedAlternate[index] = [];
165
+ recreatedAlternate[index].push(item);
166
+ } else {
167
+ recreatedAlternate[index].push(item);
168
+ }
169
+ });
170
+ });
171
+
172
+ const updatedArray = answerArray.map((answer, index) => {
173
+ return {
174
+ category: answer.category,
175
+ alternateResponses: recreatedAlternate[index] || answer.alternateResponses,
176
+ choices: answer.choices,
177
+ };
178
+ });
179
+ return updatedArray;
180
+ }
181
+ }, answer);
182
+
183
+ return out;
184
+ };
185
+ /**
186
+ * Count the number of choice ids in a given answer array
187
+ * @param {string} choiceId
188
+ * @param {{category: string, choices: string[]}[]} answer
189
+ */
190
+ export const countInAnswer = (choiceId, answer) => {
191
+ const out = answer.reduce((acc, a) => acc + countInChoices(choiceId, a.choices), 0);
192
+
193
+ log('[countInAnswer] choiceId:', choiceId, answer);
194
+
195
+ return out;
196
+ };
197
+
198
+ export const countInChoices = (choiceId, choices) => (choices || []).filter((c) => c === choiceId).length;
199
+
200
+ /**
201
+ * Count the number of times a choice has been selected in categories.
202
+ * @param {*} choice
203
+ * @param {*} categories
204
+ */
205
+ export const countChosen = (choice, categories) => {
206
+ if (!choice || !choice.id) {
207
+ return 0;
208
+ }
209
+
210
+ if (!Array.isArray(categories)) {
211
+ return 0;
212
+ }
213
+
214
+ return categories.reduce((acc, c) => {
215
+ const count = (c.choices || []).filter((h) => h && h.id === choice.id).length;
216
+
217
+ return acc + count;
218
+ }, 0);
219
+ };
220
+
221
+ /**
222
+ * @param {string?} categoryId
223
+ * @param {{category:string, choices: string[]}} answers
224
+ */
225
+ export const removeCategory = (categoryId, answers) => answers.filter((a) => a.category !== categoryId);
226
+
227
+ /**
228
+ * @param {string} choiceId
229
+ * @param {{category:string, choices: string[]}} answers
230
+ * @param {string?} categoryId - optional categegory id
231
+ */
232
+ export const removeAllChoices = (choiceId, answers, categoryId) => {
233
+ return answers.map((a) => {
234
+ if (!categoryId || a.category === categoryId) {
235
+ const cloned = clone(a.choices);
236
+
237
+ remove(cloned, (v) => v === choiceId);
238
+
239
+ return { ...a, choices: cloned };
240
+ } else {
241
+ return a;
242
+ }
243
+ });
244
+ };
245
+
246
+ export const rearrangeChoices = (choices, indexFrom, indexTo) => {
247
+ if (choices.length === 0) {
248
+ return choices;
249
+ }
250
+
251
+ choices.splice(indexFrom, 1, choices.splice(indexTo, 1, choices[indexFrom])[0]);
252
+
253
+ return choices;
254
+ };
255
+
256
+ export const verifyAllowMultiplePlacements = (choice, categoryId, answers) => {
257
+ return answers.map((a) => {
258
+ if (a.category !== categoryId) {
259
+ a.choices = (a.choices || []).filter((c) => c !== choice.id);
260
+
261
+ return a;
262
+ } else {
263
+ a.choices = a.choices.reduce((acc, currentValue) => {
264
+ if (currentValue.id === choice.id) {
265
+ const foundIndex = acc.findIndex((c) => c.id === choice.id);
266
+
267
+ if (foundIndex === -1) {
268
+ acc.push(currentValue);
269
+ }
270
+ } else {
271
+ acc.push(currentValue);
272
+ }
273
+
274
+ return acc;
275
+ }, []);
276
+
277
+ return a;
278
+ }
279
+ });
280
+ };
281
+
282
+ export const removeChoiceFromCategory = (choiceId, categoryId, choiceIndex, answers) => {
283
+ log('[removeChoiceFromCategory] choiceIndex:', choiceIndex);
284
+
285
+ return answers.map((a) => {
286
+ if (a.category === categoryId) {
287
+ const cloned = clone(a.choices);
288
+ const index = cloned.findIndex((v, index) => {
289
+ return v === choiceId && index >= choiceIndex;
290
+ });
291
+
292
+ if (index !== -1) {
293
+ cloned.splice(index, 1);
294
+ }
295
+
296
+ return { ...a, choices: cloned };
297
+ } else {
298
+ return a;
299
+ }
300
+ });
301
+ };
302
+
303
+ export const removeChoiceFromAlternate = (choiceId, categoryId, choiceIndex, altIndex, answers) => {
304
+ return (answers || []).map((a) => {
305
+ if (a?.category === categoryId) {
306
+ const newAlternates = (a?.alternateResponses || []).map((altArray, idx) => {
307
+ if (idx === altIndex) {
308
+ (altArray || []).splice(choiceIndex, 1);
309
+ return altArray;
310
+ }
311
+ return altArray;
312
+ });
313
+ return { ...a, alternateResponses: newAlternates };
314
+ }
315
+ return a;
316
+ });
317
+ };
318
+
319
+ export const moveChoiceToCategory = (choiceId, from, to, choiceIndex, answers) => {
320
+ log('[moveChoiceToCategory] choice: ', choiceId, 'from: ', from, 'to: ', to, 'answers: ', answers);
321
+
322
+ if (from === to) {
323
+ return answers;
324
+ }
325
+
326
+ if (from) {
327
+ answers = removeChoiceFromCategory(choiceId, from, choiceIndex, answers);
328
+ }
329
+
330
+ const index = answers.findIndex((a) => a.category === to);
331
+ if (index === -1) {
332
+ answers.push({ category: to, choices: [choiceId] });
333
+
334
+ return answers;
335
+ } else {
336
+ return answers.map((a) => {
337
+ if (a.category === to) {
338
+ a.choices = a.choices || [];
339
+ a.choices.push(choiceId);
340
+
341
+ return a;
342
+ }
343
+
344
+ return a;
345
+ });
346
+ }
347
+ };
348
+
349
+ export const moveChoiceToAlternate = (choiceId, from, to, choiceIndex, answers, alternateIndex, categoryCount) => {
350
+ log(
351
+ '[moveChoiceToAlternate] choice: ',
352
+ choiceId,
353
+ 'from: ',
354
+ from,
355
+ 'to: ',
356
+ to,
357
+ 'answers: ',
358
+ answers,
359
+ 'chocieIndex: ',
360
+ choiceIndex,
361
+ );
362
+
363
+ if (from === to) {
364
+ return answers;
365
+ }
366
+
367
+ return answers.map((a) => {
368
+ if (a.category === to) {
369
+ if (categoryCount !== 0 && a.alternateResponses) {
370
+ a.alternateResponses[alternateIndex] = (a.alternateResponses[alternateIndex] || []).filter(
371
+ (resp) => resp !== choiceId,
372
+ );
373
+ }
374
+
375
+ if (!a.alternateResponses) {
376
+ a.alternateResponses = [];
377
+ a.alternateResponses[alternateIndex] = [];
378
+ }
379
+
380
+ a.alternateResponses[alternateIndex].push(choiceId);
381
+ }
382
+
383
+ if (a.category === from && categoryCount !== 0 && a.alternateResponses && a.alternateResponses[alternateIndex]) {
384
+ a.alternateResponses[alternateIndex] = a.alternateResponses[alternateIndex].filter((resp) => resp !== choiceId);
385
+ }
386
+
387
+ return a;
388
+ });
389
+ };
390
+
391
+ export const stillSelectable = (h, builtCategories) => {
392
+ if (h.categoryCount > 0) {
393
+ const count = countChosen(h, builtCategories);
394
+
395
+ return count < h.categoryCount;
396
+ } else {
397
+ return true;
398
+ }
399
+ };
400
+
401
+ /**
402
+ * returns all choices with property 'correct' set to boolean
403
+ * @param {string[]} possibleResponseChoices
404
+ * @param {Object[]} builtCategoryChoices
405
+ *
406
+ * @returns {Object}, returns builtChoices: BuiltChoice[] and allChoicesAreCorrect: boolean
407
+ * BuiltChoice = { id: choiceId, content: choiceContent, correct: boolean }
408
+ */
409
+ export const buildChoices = (possibleResponseChoices, builtCategoryChoices) => {
410
+ return builtCategoryChoices.reduce(
411
+ (acc, builtChoice) => {
412
+ // set correct value on each choice that was selected by user
413
+ const index = acc.copyOfPossibleResponse.findIndex((cRC) => builtChoice && cRC === builtChoice.id);
414
+
415
+ // if the choice exists in the correct response
416
+ // set the correct: true
417
+ if (index >= 0) {
418
+ acc.builtChoices.push({
419
+ ...builtChoice,
420
+ correct: true,
421
+ });
422
+ acc.copyOfPossibleResponse = [
423
+ ...acc.copyOfPossibleResponse.slice(0, index),
424
+ ...acc.copyOfPossibleResponse.slice(index + 1),
425
+ ];
426
+ } else {
427
+ acc.builtChoices.push({
428
+ ...builtChoice,
429
+ correct: false,
430
+ });
431
+ acc.allChoicesAreCorrect = false;
432
+ }
433
+
434
+ return acc;
435
+ },
436
+ {
437
+ builtChoices: [],
438
+ copyOfPossibleResponse: [...possibleResponseChoices],
439
+ allChoicesAreCorrect: true,
440
+ },
441
+ );
442
+ };
443
+
444
+ /**
445
+ * returns corrected answer using this possible correct response
446
+ * @param {Object} possibleResponse, has form: { CategoryId: ChoiceId[] }
447
+ * @param {Object[]} builtCategories
448
+ *
449
+ * @returns {Object}, returns builtCategories: BuiltCategory[] and correct: boolean
450
+ * BuiltCategory = { id: categoryId, label: categoryLabel, correct: boolean, choices: BuiltChoice[] }
451
+ * BuiltChoice = { id: choiceId, content: choiceContent, correct: boolean }
452
+ */
453
+ export const getBuiltCategories = (possibleResponse, builtCategories) =>
454
+ builtCategories.reduce(
455
+ (acc, builtCategory) => {
456
+ const possibleResponseChoices = possibleResponse[builtCategory.id] || []; // the correct choices for this category
457
+ const builtCategoryChoices = builtCategory.choices || []; // the choices in the answer
458
+
459
+ const { builtChoices, allChoicesAreCorrect } = buildChoices(possibleResponseChoices, builtCategoryChoices);
460
+ const allChoicesAreInAnswer = builtCategoryChoices.length === possibleResponseChoices.length;
461
+
462
+ acc.builtCategories.push({
463
+ ...builtCategory,
464
+ correct: allChoicesAreCorrect && allChoicesAreInAnswer,
465
+ choices: builtChoices,
466
+ });
467
+ acc.correct = acc.correct && allChoicesAreCorrect && allChoicesAreInAnswer;
468
+
469
+ return acc;
470
+ },
471
+ { builtCategories: [], correct: true },
472
+ );
473
+
474
+ /**
475
+ * returns all the possible responses by combining the proper alternate responses
476
+ * @param {Object[]} correctResponse
477
+ *
478
+ * @returns {Object[]}, returns Response[]
479
+ * Response = { CategoryId: ChoiceId[] },
480
+ */
481
+ export const getAllPossibleResponses = (correctResponse) => {
482
+ if (!correctResponse) {
483
+ return [];
484
+ }
485
+
486
+ return correctResponse.reduce((acc, cR) => {
487
+ // main response
488
+ if (acc[0]) {
489
+ acc[0][cR.category] = cR.choices;
490
+ } else {
491
+ acc.push({ [cR.category]: cR.choices });
492
+ }
493
+
494
+ // alternate responses
495
+ if (cR.alternateResponses) {
496
+ cR.alternateResponses.forEach((aR, index) => {
497
+ if (acc[index + 1]) {
498
+ acc[index + 1][cR.category] = aR;
499
+ } else {
500
+ acc.push({ [cR.category]: aR });
501
+ }
502
+ });
503
+ }
504
+
505
+ return acc;
506
+ }, []);
507
+ };
508
+
509
+ /**
510
+ * build the categories to be displayed, where each category has its own list of choices
511
+ * @param {Object[]} categories
512
+ * @param {{id:string}[]} choices
513
+ * @param {{category: string, choices:string[]}[]} answers
514
+ *
515
+ * @returns {Object[]}, returns BuiltCategory[]
516
+ * BuiltCategory = { id: categoryId, label: categoryLabel, choices: BuiltChoice[]},
517
+ * BuiltChoice = { id: choiceId, content: choiceContent }
518
+ */
519
+ export const buildCategories = (categories, choices, answers) => {
520
+ categories = categories || [];
521
+ answers = answers || [];
522
+ choices = choices || [];
523
+
524
+ return categories.map((category) => {
525
+ const answer = answers.find((answer) => answer.category === category.id);
526
+ const { choices: answerChoices = [] } = answer || {};
527
+
528
+ return {
529
+ ...category,
530
+ choices: answerChoices.map((chId) => choices.find((ch) => ch.id === chId)),
531
+ };
532
+ });
533
+ };
534
+
535
+ /**
536
+ * build the choice and category state
537
+ * @param {Object[]} categories
538
+ * @param {{id:string}[]} choices
539
+ * @param {{category: string, choices:string[]}[]} answers
540
+ *
541
+ * @param correctResponse
542
+ * @returns { categories: Category[], choices: Choice[], correct: boolean, bestResponse: Response }, where Response: { categoryId: choiceId[] }
543
+ */
544
+ export const buildState = (categories, choices, answers, correctResponse) => {
545
+ categories = categories || [];
546
+ choices = choices || [];
547
+ answers = answers || [];
548
+
549
+ const allResponses = getAllPossibleResponses(correctResponse) || [];
550
+
551
+ let builtCategories = buildCategories(categories, choices, answers);
552
+ let bestResponse;
553
+ let entirelyCorrect;
554
+
555
+ // at least one defined correct response
556
+ if (allResponses.length) {
557
+ const builtData = allResponses.reduce(
558
+ (acc, possibleResponse) => {
559
+ // set correctness for each possible response
560
+ const { builtCategories: categoriesWithChoices, correct } = getBuiltCategories(
561
+ possibleResponse,
562
+ builtCategories,
563
+ );
564
+
565
+ // if bestResponse not found yet OR no correct response found yet and this one is the correct one
566
+ if (acc.bestResponse === null || (!acc.entirelyCorrect && correct)) {
567
+ return {
568
+ bestResponse: possibleResponse,
569
+ builtCategories: categoriesWithChoices,
570
+ entirelyCorrect: correct,
571
+ };
572
+ }
573
+
574
+ return acc;
575
+ },
576
+ {
577
+ bestResponse: null,
578
+ builtCategories: [],
579
+ entirelyCorrect: false,
580
+ },
581
+ );
582
+
583
+ builtCategories = builtData.builtCategories;
584
+ bestResponse = builtData.bestResponse;
585
+ entirelyCorrect = builtData.entirelyCorrect;
586
+ }
587
+
588
+ const filteredChoices = choices.map((ch) => (stillSelectable(ch, builtCategories) ? ch : { empty: true }));
589
+
590
+ return {
591
+ choices: filteredChoices,
592
+ categories: builtCategories,
593
+ correct: entirelyCorrect,
594
+ bestResponse,
595
+ };
596
+ };
package/src/scoring.ts ADDED
@@ -0,0 +1,128 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * @synced-from pie-lib/packages/categorize/src/scoring.js
4
+ * @auto-generated
5
+ *
6
+ * This file is automatically synced from pie-elements and converted to TypeScript.
7
+ * Manual edits will be overwritten on next sync.
8
+ * To make changes, edit the upstream JavaScript file and run sync again.
9
+ */
10
+
11
+ import debug from 'debug';
12
+
13
+ const log = debug('@pie-lib:categorize:scoring');
14
+
15
+ const getWeightingRules = (scoring) => {
16
+ if (!scoring || !scoring.weighting) {
17
+ return [];
18
+ } else {
19
+ if (!scoring.weighting.enabled) {
20
+ return [];
21
+ } else {
22
+ const rules = scoring.weighting ? scoring.weighting.rules || [] : [];
23
+
24
+ return rules;
25
+ }
26
+ }
27
+ };
28
+
29
+ const getPartialRulesForCategory = (partial, categoryId) => {
30
+ log('partial? ', partial);
31
+
32
+ if (!partial || !partial.enabled) {
33
+ return;
34
+ } else {
35
+ const pr = partial.rules.find((pr) => pr.category === categoryId);
36
+
37
+ return pr ? pr.rules || [] : [];
38
+ }
39
+ };
40
+
41
+ /**
42
+ *
43
+ * Calculate score based on the categories model generated from `buildState` and the partial scoring rules.
44
+ * @param {*} categories - this is the categories that you get from `buildState`.
45
+ * @param {*} scoring - scoring config.
46
+ * @see ../index#buildState
47
+ */
48
+
49
+ export const score = (categories, scoring) =>
50
+ new Promise((resolve) => {
51
+ log('categories: ', categories, 'scoring: ', scoring);
52
+
53
+ const weightingRules = getWeightingRules(scoring);
54
+
55
+ const weights = categories.map((c) => {
56
+ const r = weightingRules.find((r) => r.category === c.id) || { points: 1 };
57
+
58
+ return { ...r, category: c.id };
59
+ });
60
+
61
+ log('weights: ', weights);
62
+
63
+ const weightTotal = categories.reduce((total, c) => {
64
+ const r = weights.find((r) => r.category === c.id);
65
+
66
+ return (total += r.points);
67
+ }, 0);
68
+
69
+ log('total: ', weightTotal);
70
+
71
+ const withScore = categories.map((c) => {
72
+ const w = weights.find((r) => r.category === c.id).points;
73
+
74
+ log('category: ', c);
75
+ if (c.correct === true) {
76
+ return { category: c.id, score: w, points: w };
77
+ } else {
78
+ const rules = getPartialRulesForCategory(scoring.partial, c.id);
79
+
80
+ log('partial rules for category: ', rules);
81
+
82
+ if (!rules) {
83
+ return { category: c.id, score: 0, points: w };
84
+ } else {
85
+ const correctCount = (c.choices || []).filter((h) => h.correct).length;
86
+
87
+ log('correctCount: ', correctCount);
88
+
89
+ let rule = rules.find((u) => u.count === correctCount);
90
+
91
+ log('rule: ', rule);
92
+
93
+ rule = rule || { percent: 0, count: correctCount };
94
+
95
+ const score = w * (rule.percent / 100);
96
+
97
+ return {
98
+ category: c.id,
99
+ score,
100
+ points: w,
101
+ partial: {
102
+ correctChoices: correctCount,
103
+ rule: rule,
104
+ },
105
+ };
106
+ }
107
+ }
108
+ });
109
+
110
+ const weightedScore = withScore.reduce((sum, c) => {
111
+ return (sum += c.score);
112
+ }, 0);
113
+
114
+ log('weightedScore: ', weightedScore);
115
+
116
+ const score = parseFloat((weightedScore / weightTotal).toFixed(2), 10);
117
+
118
+ resolve({
119
+ score,
120
+ details: {
121
+ weighted: {
122
+ total: weightTotal,
123
+ score: weightedScore,
124
+ },
125
+ categories: withScore,
126
+ },
127
+ });
128
+ });