@pie-lib/categorize 2.0.2 → 2.0.3-next.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,127 @@
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
+ };
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 "@pie-element/shared-lodash";
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,16 @@
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>;
@@ -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 CHANGED
@@ -1,19 +1,34 @@
1
1
  {
2
2
  "name": "@pie-lib/categorize",
3
- "version": "2.0.2",
3
+ "version": "2.0.3-next.0",
4
4
  "description": "",
5
- "main": "lib/index.js",
6
- "module": "src/index.js",
7
- "repository": "pie-framework/pie-lib",
8
5
  "dependencies": {
9
6
  "debug": "^4.1.1",
10
- "lodash-es": "^4.17.23"
7
+ "@pie-element/shared-lodash": "0.1.1-next.0"
11
8
  },
12
- "publishConfig": {
13
- "access": "public"
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
+ }
14
17
  },
15
- "scripts": {},
16
- "author": "",
17
- "license": "ISC",
18
- "gitHead": "a09f84cac337a93082ba823f0798a00da2d0d623"
18
+ "files": [
19
+ "dist"
20
+ ],
21
+ "sideEffects": false,
22
+ "scripts": {
23
+ "build": "bun x vite build && bun x tsc --emitDeclarationOnly",
24
+ "dev": "bun x vite",
25
+ "test": "bun x vitest run"
26
+ },
27
+ "devDependencies": {
28
+ "vite": "^8.0.1",
29
+ "typescript": "^5.9.3",
30
+ "@vitejs/plugin-react": "^6.0.1",
31
+ "@types/react": "^18.2.0",
32
+ "@types/react-dom": "^18.2.0"
33
+ }
19
34
  }
package/CHANGELOG.json DELETED
@@ -1,17 +0,0 @@
1
- [
2
- {
3
- "type": "feat",
4
- "scope": "categorize",
5
- "subject": "conclude merge",
6
- "merge": null,
7
- "header": "feat(categorize): conclude merge",
8
- "body": null,
9
- "footer": null,
10
- "notes": [],
11
- "hash": "da8b33228705490c504be56d250f61e14ca5530d",
12
- "gitTags": "",
13
- "committerDate": "2023-01-03 11:36:51 +0200",
14
- "isTagged": true,
15
- "tag": "@pie-lib/categorize@0.7.0"
16
- }
17
- ]