@pie-lib/controller-utils 0.19.0 → 0.19.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/esm/index.js +15 -15
- package/esm/index.js.map +1 -1
- package/package.json +2 -2
- package/LICENSE.md +0 -5
package/esm/index.js
CHANGED
|
@@ -2,7 +2,7 @@ import get from 'lodash/get';
|
|
|
2
2
|
import shuffle from 'lodash/shuffle';
|
|
3
3
|
import isEmpty from 'lodash/isEmpty';
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
const enabled = (config, env, defaultValue) => {
|
|
6
6
|
// if model.partialScoring = false
|
|
7
7
|
// - if env.partialScoring = false || env.partialScoring = true => use dichotomous scoring
|
|
8
8
|
// else if model.partialScoring = true || undefined
|
|
@@ -27,25 +27,25 @@ var partialScoring = /*#__PURE__*/Object.freeze({
|
|
|
27
27
|
enabled: enabled
|
|
28
28
|
});
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
const lg = n => console[n].bind(console, 'controller-utils:');
|
|
31
31
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
32
|
+
const debug = lg('debug');
|
|
33
|
+
const log = lg('log');
|
|
34
|
+
const warn = lg('warn');
|
|
35
|
+
const error = lg('error');
|
|
36
|
+
const compact = arr => {
|
|
37
37
|
if (Array.isArray(arr)) {
|
|
38
38
|
return arr.filter(v => v !== null && v !== undefined);
|
|
39
39
|
}
|
|
40
40
|
|
|
41
41
|
return arr;
|
|
42
42
|
};
|
|
43
|
-
|
|
43
|
+
const getShuffledChoices = (choices, session, updateSession, choiceKey) => new Promise(resolve => {
|
|
44
44
|
var _session$data;
|
|
45
45
|
|
|
46
46
|
log('updateSession type: ', typeof updateSession);
|
|
47
47
|
log('session: ', session);
|
|
48
|
-
|
|
48
|
+
const currentShuffled = compact((session == null ? void 0 : (_session$data = session.data) == null ? void 0 : _session$data.shuffledValues) || (session == null ? void 0 : session.shuffledValues) || []);
|
|
49
49
|
|
|
50
50
|
if (!session) {
|
|
51
51
|
// eslint-disable-next-line quotes
|
|
@@ -54,20 +54,20 @@ var getShuffledChoices = (choices, session, updateSession, choiceKey) => new Pro
|
|
|
54
54
|
} else if (!isEmpty(currentShuffled)) {
|
|
55
55
|
var _session$data2;
|
|
56
56
|
|
|
57
|
-
debug('use shuffledValues to sort the choices...', (_session$data2 = session.data)
|
|
57
|
+
debug('use shuffledValues to sort the choices...', (_session$data2 = session.data) == null ? void 0 : _session$data2.shuffledValues);
|
|
58
58
|
resolve(compact(currentShuffled.map(v => choices.find(c => c[choiceKey] === v))));
|
|
59
59
|
} else {
|
|
60
|
-
|
|
60
|
+
const shuffledChoices = shuffle(choices);
|
|
61
61
|
|
|
62
62
|
if (updateSession && typeof updateSession === 'function') {
|
|
63
63
|
try {
|
|
64
64
|
//Note: session.id refers to the id of the element within a session
|
|
65
|
-
|
|
65
|
+
const shuffledValues = compact(shuffledChoices.map(c => c[choiceKey]));
|
|
66
66
|
log('try to save shuffledValues to session...', shuffledValues);
|
|
67
67
|
log('call updateSession... ', session.id, session.element);
|
|
68
68
|
|
|
69
69
|
if (isEmpty(shuffledValues)) {
|
|
70
|
-
error(
|
|
70
|
+
error(`shuffledValues is an empty array? - refusing to call updateSession: shuffledChoices: ${JSON.stringify(shuffledChoices)}, key: ${choiceKey}`);
|
|
71
71
|
} else {
|
|
72
72
|
updateSession(session.id, session.element, {
|
|
73
73
|
shuffledValues
|
|
@@ -97,7 +97,7 @@ var getShuffledChoices = (choices, session, updateSession, choiceKey) => new Pro
|
|
|
97
97
|
* @returns {boolean}
|
|
98
98
|
*/
|
|
99
99
|
|
|
100
|
-
|
|
100
|
+
const lockChoices = (model, session, env) => {
|
|
101
101
|
if (model.lockChoiceOrder) {
|
|
102
102
|
return true;
|
|
103
103
|
}
|
|
@@ -108,7 +108,7 @@ var lockChoices = (model, session, env) => {
|
|
|
108
108
|
return true;
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
|
|
111
|
+
const role = get(env, 'role', 'student');
|
|
112
112
|
|
|
113
113
|
if (role === 'instructor') {
|
|
114
114
|
// TODO: .. in the future the instructor can toggle between ordinal and shuffled here, so keeping this code until then
|
package/esm/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sources":["../src/partial-scoring.js","../src/persistence.js"],"sourcesContent":["export const enabled = (config, env, defaultValue) => {\n // if model.partialScoring = false\n // - if env.partialScoring = false || env.partialScoring = true => use dichotomous scoring\n // else if model.partialScoring = true || undefined\n // - if env.partialScoring = false, use dichotomous scoring\n // - else if env.partialScoring = true, use partial scoring\n config = config || {};\n env = env || {};\n\n if (config.partialScoring === false) {\n return false;\n }\n\n if (env.partialScoring === false) {\n return false;\n }\n\n return typeof defaultValue === 'boolean' ? defaultValue : true;\n};\n","import get from 'lodash/get';\nimport shuffle from 'lodash/shuffle';\nimport isEmpty from 'lodash/isEmpty';\n\n// eslint-disable-next-line no-console\nconst lg = (n) => console[n].bind(console, 'controller-utils:');\nconst debug = lg('debug');\nconst log = lg('log');\nconst warn = lg('warn');\nconst error = lg('error');\n\nexport const compact = (arr) => {\n if (Array.isArray(arr)) {\n return arr.filter((v) => v !== null && v !== undefined);\n }\n return arr;\n};\n\nexport const getShuffledChoices = (choices, session, updateSession, choiceKey) =>\n new Promise((resolve) => {\n log('updateSession type: ', typeof updateSession);\n log('session: ', session);\n\n const currentShuffled = compact(session?.data?.shuffledValues || session?.shuffledValues || []);\n\n if (!session) {\n // eslint-disable-next-line quotes\n warn(\"unable to save shuffled choices because there's no session.\");\n resolve(undefined);\n } else if (!isEmpty(currentShuffled)) {\n debug('use shuffledValues to sort the choices...', session.data?.shuffledValues);\n resolve(compact(currentShuffled.map((v) => choices.find((c) => c[choiceKey] === v))));\n } else {\n const shuffledChoices = shuffle(choices);\n\n if (updateSession && typeof updateSession === 'function') {\n try {\n //Note: session.id refers to the id of the element within a session\n const shuffledValues = compact(shuffledChoices.map((c) => c[choiceKey]));\n log('try to save shuffledValues to session...', shuffledValues);\n log('call updateSession... ', session.id, session.element);\n if (isEmpty(shuffledValues)) {\n error(\n `shuffledValues is an empty array? - refusing to call updateSession: shuffledChoices: ${JSON.stringify(\n shuffledChoices,\n )}, key: ${choiceKey}`,\n );\n } else {\n updateSession(session.id, session.element, { shuffledValues }).catch((e) =>\n // eslint-disable-next-line no-console\n console.error('update session failed for: ', session.id, e),\n );\n }\n } catch (e) {\n warn('unable to save shuffled order for choices');\n error(e);\n }\n } else {\n warn('unable to save shuffled choices, shuffle will happen every time.');\n }\n //save this shuffle to the session for later retrieval\n resolve(shuffledChoices);\n }\n });\n\n/**\n * If we return:\n * - true - that means that the order of the choices will be ordinal (as is created in the configure item)\n * - false - that means the getShuffledChoices above will be called and that in turn means that we either\n * return the shuffled values on the session (if any exists) or we shuffle the choices\n * @param model - model to check if we should lock order\n * @param session - session to check if we should lock order\n * @param env - env to check if we should lock order\n * @returns {boolean}\n */\nexport const lockChoices = (model, session, env) => {\n if (model.lockChoiceOrder) {\n return true;\n }\n\n log('lockChoiceOrder: ', get(env, ['@pie-element', 'lockChoiceOrder'], false));\n\n if (get(env, ['@pie-element', 'lockChoiceOrder'], false)) {\n return true;\n }\n\n const role = get(env, 'role', 'student');\n\n if (role === 'instructor') {\n // TODO: .. in the future the instructor can toggle between ordinal and shuffled here, so keeping this code until then\n /*const alreadyShuffled = hasShuffledValues(session);\n\n if (alreadyShuffled) {\n return false;\n }\n\n return true;*/\n return true;\n }\n\n // here it's a student, so don't lock and it will shuffle if needs be\n return false;\n};\n"],"names":["enabled","config","env","defaultValue","partialScoring","lg","n","console","bind","debug","log","warn","error","compact","arr","Array","isArray","filter","v","undefined","getShuffledChoices","choices","session","updateSession","choiceKey","Promise","resolve","currentShuffled","data","shuffledValues","isEmpty","map","find","c","shuffledChoices","shuffle","id","element","JSON","stringify","catch","e","lockChoices","model","lockChoiceOrder","get","role"],"mappings":";;;;AAAO,
|
|
1
|
+
{"version":3,"file":"index.js","sources":["../src/partial-scoring.js","../src/persistence.js"],"sourcesContent":["export const enabled = (config, env, defaultValue) => {\n // if model.partialScoring = false\n // - if env.partialScoring = false || env.partialScoring = true => use dichotomous scoring\n // else if model.partialScoring = true || undefined\n // - if env.partialScoring = false, use dichotomous scoring\n // - else if env.partialScoring = true, use partial scoring\n config = config || {};\n env = env || {};\n\n if (config.partialScoring === false) {\n return false;\n }\n\n if (env.partialScoring === false) {\n return false;\n }\n\n return typeof defaultValue === 'boolean' ? defaultValue : true;\n};\n","import get from 'lodash/get';\nimport shuffle from 'lodash/shuffle';\nimport isEmpty from 'lodash/isEmpty';\n\n// eslint-disable-next-line no-console\nconst lg = (n) => console[n].bind(console, 'controller-utils:');\nconst debug = lg('debug');\nconst log = lg('log');\nconst warn = lg('warn');\nconst error = lg('error');\n\nexport const compact = (arr) => {\n if (Array.isArray(arr)) {\n return arr.filter((v) => v !== null && v !== undefined);\n }\n return arr;\n};\n\nexport const getShuffledChoices = (choices, session, updateSession, choiceKey) =>\n new Promise((resolve) => {\n log('updateSession type: ', typeof updateSession);\n log('session: ', session);\n\n const currentShuffled = compact(session?.data?.shuffledValues || session?.shuffledValues || []);\n\n if (!session) {\n // eslint-disable-next-line quotes\n warn(\"unable to save shuffled choices because there's no session.\");\n resolve(undefined);\n } else if (!isEmpty(currentShuffled)) {\n debug('use shuffledValues to sort the choices...', session.data?.shuffledValues);\n resolve(compact(currentShuffled.map((v) => choices.find((c) => c[choiceKey] === v))));\n } else {\n const shuffledChoices = shuffle(choices);\n\n if (updateSession && typeof updateSession === 'function') {\n try {\n //Note: session.id refers to the id of the element within a session\n const shuffledValues = compact(shuffledChoices.map((c) => c[choiceKey]));\n log('try to save shuffledValues to session...', shuffledValues);\n log('call updateSession... ', session.id, session.element);\n if (isEmpty(shuffledValues)) {\n error(\n `shuffledValues is an empty array? - refusing to call updateSession: shuffledChoices: ${JSON.stringify(\n shuffledChoices,\n )}, key: ${choiceKey}`,\n );\n } else {\n updateSession(session.id, session.element, { shuffledValues }).catch((e) =>\n // eslint-disable-next-line no-console\n console.error('update session failed for: ', session.id, e),\n );\n }\n } catch (e) {\n warn('unable to save shuffled order for choices');\n error(e);\n }\n } else {\n warn('unable to save shuffled choices, shuffle will happen every time.');\n }\n //save this shuffle to the session for later retrieval\n resolve(shuffledChoices);\n }\n });\n\n/**\n * If we return:\n * - true - that means that the order of the choices will be ordinal (as is created in the configure item)\n * - false - that means the getShuffledChoices above will be called and that in turn means that we either\n * return the shuffled values on the session (if any exists) or we shuffle the choices\n * @param model - model to check if we should lock order\n * @param session - session to check if we should lock order\n * @param env - env to check if we should lock order\n * @returns {boolean}\n */\nexport const lockChoices = (model, session, env) => {\n if (model.lockChoiceOrder) {\n return true;\n }\n\n log('lockChoiceOrder: ', get(env, ['@pie-element', 'lockChoiceOrder'], false));\n\n if (get(env, ['@pie-element', 'lockChoiceOrder'], false)) {\n return true;\n }\n\n const role = get(env, 'role', 'student');\n\n if (role === 'instructor') {\n // TODO: .. in the future the instructor can toggle between ordinal and shuffled here, so keeping this code until then\n /*const alreadyShuffled = hasShuffledValues(session);\n\n if (alreadyShuffled) {\n return false;\n }\n\n return true;*/\n return true;\n }\n\n // here it's a student, so don't lock and it will shuffle if needs be\n return false;\n};\n"],"names":["enabled","config","env","defaultValue","partialScoring","lg","n","console","bind","debug","log","warn","error","compact","arr","Array","isArray","filter","v","undefined","getShuffledChoices","choices","session","updateSession","choiceKey","Promise","resolve","currentShuffled","data","shuffledValues","isEmpty","map","find","c","shuffledChoices","shuffle","id","element","JSON","stringify","catch","e","lockChoices","model","lockChoiceOrder","get","role"],"mappings":";;;;AAAO,MAAMA,OAAO,GAAG,CAACC,MAAD,EAASC,GAAT,EAAcC,YAAd,KAA+B;AACpD;AACA;AACA;AACA;AACA;AACAF,EAAAA,MAAM,GAAGA,MAAM,IAAI,EAAnB;AACAC,EAAAA,GAAG,GAAGA,GAAG,IAAI,EAAb;;AAEA,EAAA,IAAID,MAAM,CAACG,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A;AAGI,IAAA,OAAO,IAAP;AACD,EAAA,CAvBiD;;;AA0BlD,EAAA,OAAO,KAAP;AACD;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pie-lib/controller-utils",
|
|
3
|
-
"version": "0.19.
|
|
3
|
+
"version": "0.19.2",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "lib/index.js",
|
|
6
6
|
"module": "src/index.js",
|
|
@@ -14,7 +14,7 @@
|
|
|
14
14
|
"scripts": {},
|
|
15
15
|
"author": "",
|
|
16
16
|
"license": "ISC",
|
|
17
|
-
"gitHead": "
|
|
17
|
+
"gitHead": "8a327571bd64249e4c88c0c8e750d16d6213f535",
|
|
18
18
|
"exports": {
|
|
19
19
|
".": {
|
|
20
20
|
"import": "./esm/index.js",
|
package/LICENSE.md
DELETED
|
@@ -1,5 +0,0 @@
|
|
|
1
|
-
Copyright 2019 CoreSpring Inc
|
|
2
|
-
|
|
3
|
-
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
|
|
4
|
-
|
|
5
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|