forked from episphere/quest
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustomMathJSImplementation.js
More file actions
432 lines (354 loc) · 13.1 KB
/
customMathJSImplementation.js
File metadata and controls
432 lines (354 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import { getStateManager } from './stateManager.js';
import { create, all } from 'https://cdn.skypack.dev/pin/mathjs@v13.0.3-l5exVmFmmRoBpcv9HZ2w/mode=imports,min/optimized/mathjs.js';
import { moduleParams } from './questionnaire.js';
export const math = create(all);
// Strip '_<num>' when an id that _0, _1, _2, etc. as a suffix.
export const stripIDSuffixRegex = /^([a-zA-Z0-9]+_[a-zA-Z0-9]+)_\d+$/
/**
* YearMonth class for use in mathjs to handle the month class
*/
export class YearMonth {
constructor(str) {
if (str?.isYearMonth) {
this.month = str.month;
this.year = str.year;
} else {
let x = str.match(/^(\d+)-(\d+)$/);
if (!x) {
throw new Error("Invalid YearMonth format. Expected 'YYYY-MM'.");
}
this.month = parseInt(x[2]).toLocaleString(navigator.language, { minimumIntegerDigits: 2 });
this.year = x[1];
}
}
get isYearMonth() {
return true;
}
toString() {
return `${this.year}-${this.month}`;
}
add(n) {
let m = parseInt(this.month) + n;
let yr = parseInt(this.year) + ((m > 12) ? 1 : 0);
let mon = (m % 12) || 12;
return new YearMonth(`${yr}-${mon}`).toString();
}
subtract(n) {
let m = parseInt(this.month) - n;
let yr = parseInt(this.year) - ((m > 0) ? 0 : 1);
let mon = ((m + 12) % 12) || 12;
return new YearMonth(`${yr}-${mon}`).toString();
}
subMonth(ym) {
return (12 * (parseInt(this.year) - parseInt(ym.year)) + parseInt(this.month) - parseInt(ym.month));
}
}
export const customMathJSFunctions = {
exists: function (x) {
if (x == null || x === '') return false;
if (typeof x === 'number') return true;
if (x.toString().includes('.')) {
return !math.isUndefined(this.getKeyedValue(x));
}
const existingResponse = this.appState.findResponseValue(x);
switch (typeof existingResponse) {
case 'object':
return Array.isArray(existingResponse) ? existingResponse.length > 0 : Object.keys(existingResponse).length > 0;
case 'string':
return existingResponse.length > 0;
case 'undefined':
return false;
default:
moduleParams.errorLogger(`unhandled case in EXISTS check. Error: ${x} is not a valid response type.`);
return false;
}
},
doesNotExist: function (x) {
return !this.exists(x)
},
noneExist: function (...ids) {
// if you give me no ids, none of them exist therefore true...
// loop through all the ids of any exists then return false...
return ids.every(id => this.doesNotExist(id))
},
someExist: function (...ids) {
return ids.some(id => this.exists(id))
},
allExist: function (...ids) {
return ids.every(id => this.exists(id))
},
getKeyedValue: function(x) {
// handle keys with dot notation. E.g. valueOrDefault("D_378988419.D_807765962")
if (!x.toString().includes('.')) {
console.error('invalid use of getKeyedValue (called on key without dot notation');
return undefined;
}
// Skip sentences and other non-key values such as multi-sentence survey text & responses
if (!/^[A-Za-z0-9_.]+$/.test(x)) {
return undefined;
}
const array = x.toString().split('.');
const key = array.shift();
const obj = this._value(key);
// Return early if the initial object is undefined
if (math.isUndefined(obj)) return undefined;
return array.reduce((prev, curr) => {
if (math.isUndefined(prev)) return undefined;
return prev[curr] ?? undefined;
}, obj);
},
_value: function (x) {
// x is a hardcoded number in some cases. E.g. valueOrDefault("D_378988419","D_807765962",125)
if (typeof x === 'number') return x;
if (!this.exists(x)) return null
if (x.toString().includes('.')) return this.getKeyedValue(x);
return this.appState.findResponseValue(x);
},
valueEquals: function (id, value) {
// if id is not passed in return FALSE
if (this.doesNotExist(id)) return false;
let element_value = this._value(id);
// catch if we have a combobox...
if (element_value[id]) {
element_value = element_value[id]
}
// if the element does not exist return FALSE
return (element_value == value)
},
equals: function(id, value){
return this.valueEquals(id,value)
},
valueIsOneOf: function (id, ...values) {
if (this.doesNotExist(id)) return false;
// compare as strings so "1" == "1"
values = values.map(v => v.toString())
let test_values = this._value(id);
// catch if we have a combobox...
if (test_values[id]) {
test_values = test_values[id]
}
// Check for concept id arrays as strings. Convert to array for comparison.
if (typeof test_values === 'string' && /^\d{9}(,\d{9})*$/.test(test_values)) {
test_values = test_values.split(',');
}
if (Array.isArray(test_values)) {
return (test_values.some(v => values.includes(v.toString())))
}
return values.includes(test_values.toString())
},
/**
* checks whether the value for id is
* between the values of lowerLimit and upperLimit inclusively
* lowerLimit <= value(id) <= upperlimit
*
* if you pass in an array of ids, it uses the first id that exists. The
* array is passed into valueOrDefault.
*
* @param {Number} lowerLimit The lowest acceptable value
* @param {Number} upperLimit the highest acceptable value
* @param {Array} ids An array of values, passed into valueOrDefault.
* @return {boolean} is lowerLimit <= value(id) <= upperLimit
*/
valueIsBetween: function (lowerLimit, upperLimit, ...ids) {
if (lowerLimit === undefined || upperLimit === undefined || ids === undefined) return false;
let value = undefined;
value = (ids.length > 1) ? this.valueOrDefault(ids.shift(), ids) : this._value(ids.shift())
// for this function to work, value, lowerLimit, and
// upperLimit MUST be numeric....
if (!isNaN(value) && !isNaN(lowerLimit) && !isNaN(value)) {
return (parseFloat(lowerLimit) <= value && value <= parseFloat(upperLimit))
}
return false
},
/**
* Given a comma separated value of Conditions and values, returns a string of all the values that exist.
* separated by a comma or the optional separator
*
* i.e. existingValues(exists("ID1"),displaytext,exists("ID2"),displaytext)
*
* @param {args} the args should be condition1, VAL1, condition2, VAL2, (optional)sep=,
*
*/
existingValues: function (args) {
if (!args) return ""
let argArray = math.parse(args).args
let sep = ", "
if (argArray[argArray.length - 1].name == "sep") {
sep = argArray.pop().evaluate()
}
// we better have (id/value PAIRS)
argArray = argArray.reduce((prev, current, index, array) => {
// skip the ids...
if (index % 2 == 0) return prev
// see if the id exists, if so keep the value
if (array[index - 1].evaluate()) prev.push(this.valueOrDefault(current.evaluate(), current.evaluate()))
return prev
}, [])
return argArray.join(sep)
},
// if the value of id is a string
// return the string length, otherwise
// return -1
valueLength: function(id){
// if id is not passed in return FALSE
if (this.doesNotExist(id)) return false;
let element_value = this._value(id);
if (typeof element_value === 'string'){
return element_value.length
}
return -1;
},
dateCompare: function (month1, year1, month2, year2) {
if ([month1, month2].some((m) => { let m1 = parseInt(m); m1 < 0 || m1 > 11 })) {
throw 'DateCompareError:months need to be from 0 (Jan) to 11 (Dec)'
}
if ([year1, year2].some((yr) => isNaN(yr))) {
throw 'DateCompareError:years need to be numeric'
}
let date1 = (new Date(year1, month1)).getTime()
let date2 = (new Date(year2, month2)).getTime()
return (date1 < date2) ? -1 : (date1 == date2) ? 0 : 1
},
// Get the value of the cell input for the radio or checkbox from the form - table - tbody - tr - td - input structure.
// id is the id of the specific radio button or checkbox. This is evaluated on a per-id basis, so if a row
// has multiple radio buttons or checkboxes, each radio button will be evaluated separately.
isSelected: function (radioOrCheckboxID) {
const questionProcessor = this.appState.getQuestionProcessor();
const cellInputValue = questionProcessor.findGridRadioCheckboxEle(radioOrCheckboxID);
if (!cellInputValue) {
return false;
}
const match = radioOrCheckboxID.match(stripIDSuffixRegex);
if (!match || !match[1]) return false;
const baseRadioCheckboxID = match[1];
const responseValue = this.appState.findResponseValue(baseRadioCheckboxID);
if (!responseValue) {
return false;
}
return cellInputValue === responseValue;
},
someSelected: function (...ids) {
return (ids.some(id => this.isSelected(id)))
},
noneSelected: function(...ids){
return (!ids.some(id => this.isSelected(id)))
},
// defaultValue accepts an Id and a value or a Id/Value
// If only 1 default value is given, first it looks it up
// if it does not exist assume it is a value...
// If 2 default values are given, look up the first, if it
// does not exist, return the second as a value...
valueOrDefault: function (x, ...defaultValue) {
let v = this._value(x)
let indx = 0;
while (v == null && defaultValue.length > indx) {
v = this._value(defaultValue[indx])
if (v == null) indx++
}
if (v == null) v = defaultValue[defaultValue.length - 1]
return (v);
},
selectionCount: function(x, countReset=false) {
let [questionId, name] = x.split(':')
name = name ?? questionId
if (!this.exists(questionId)) return 0
let responseValue = this._value(questionId);
if (Array.isArray(responseValue) || Array.isArray(responseValue[name])) {
responseValue = Array.isArray(responseValue) ? responseValue : responseValue[name]
if (countReset){
return responseValue.length;
}
// (legacy notes) BUG FIX: if the data-reset ("none of the above") is selected
// there is a chance that nothing is selected (v.length==0) in that case you will the
// selector will find nothing. Use the "?" because you cannot find the dataset on a null object.
const questionHTML = this.appState.getQuestionHTMLByID(questionId);
if (questionHTML) {
// Find the checked input and check if it has the dataset["reset"] attribute
const inputs = Array.from(questionHTML.querySelectorAll('input'));
const inputChecked = inputs
.filter(input => input.name === name && input.checked && input.dataset?.reset !== undefined)
.find(input => input.dataset?.reset);
return inputChecked ? 0 : responseValue.length;
}
return responseValue.length;
}
return 0;
},
yearMonth: function (str) {
let isYM = /^(\d+)-(\d+)$/.test(str)
if (isYM) {
return new YearMonth(str)
}
let value = this._value(str)
isYM = /^(\d+)-(\d+)$/.test(value)
if (isYM) {
return new YearMonth(value)
}
return false;
},
YearMonth: YearMonth,
}
// Tell mathjs about the YearMonth class
math.typed.addType({
name: 'YearMonth',
test: function (x) {
return x && x.isYearMonth
}
})
// Add custom math.js handling for YearMonth
// Define string and number handling before adding the custom functions to mathjs.
const add = math.typed('add', {
'YearMonth, number': function (dte, m) {
return dte.add(m);
},
'number, YearMonth': function (m, dte) {
return dte.add(m);
},
'number, number': function (a, b) {
return a + b;
},
'string, string': function (a, b) {
return parseFloat(a) + parseFloat(b);
},
'any, any': function (a, b) {
return math.add(parseFloat(a), parseFloat(b));
}
});
const subtract = math.typed('subtract', {
'YearMonth, number': function (dte, m) {
return dte.subtract(m);
},
'YearMonth, YearMonth': function (dte2, dte1) {
return dte2.subMonth(dte1);
},
'number, number': function (a, b) {
return a - b;
},
'string, string': function (a, b) {
return parseFloat(a) - parseFloat(b);
},
'any, any': function (a, b) {
return math.subtract(parseFloat(a), parseFloat(b));
}
});
/**
* Initialize the custom mathjs functions.
* Add the custom functions to the customMathJSFunctions object.
* Add the appState to the customMathJSFunctions object since it is used often.
* Bind the custom functions to the customMathJSFunctions object (appState isn't a function, so it doesn't get bound).
* Add the custom functions to mathjs and override any existing mathjs functions.
*/
export const initializeCustomMathJSFunctions = () => {
customMathJSFunctions.add = add;
customMathJSFunctions.subtract = subtract;
customMathJSFunctions.appState = getStateManager();
const boundFunctions = Object.keys(customMathJSFunctions).reduce((acc, key) => {
if (typeof customMathJSFunctions[key] === 'function') {
acc[key] = customMathJSFunctions[key].bind(customMathJSFunctions);
} else {
acc[key] = customMathJSFunctions[key];
}
return acc;
}, {});
math.import({ boundFunctions }, { override: true });
}