Skip to content

Commit

Permalink
Expand value type check for number
Browse files Browse the repository at this point in the history
  • Loading branch information
mechtelm committed Jan 10, 2025
1 parent 2387644 commit 507fdd6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@iqb/responses",
"author": "IQB - Institut zur Qualitätsentwicklung im Bildungswesen",
"license": "MIT",
"version": "3.3.2",
"version": "3.3.3",
"description": "Data interfaces for processing assessment data.",
"scripts": {
"test_coding": "jest test/coding",
Expand Down
2 changes: 1 addition & 1 deletion package_npm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@iqb/responses",
"version": "3.3.2",
"version": "3.3.3",
"author": "IQB - Institut zur Qualitätsentwicklung im Bildungswesen",
"license": "CC0 1.0",
"description": "Data interfaces for processing assessment data.",
Expand Down
21 changes: 14 additions & 7 deletions src/coding-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,13 @@ export abstract class CodingFactory {
if (value === null || value === '') return 0;
if (typeof value === 'number') return value;
if (typeof value === 'boolean') return (value as boolean) ? 1 : 0;
if (typeof value === 'string') {
let normalizedString = value.length < 6 ? (value as string).replace('.', ',') : value;
normalizedString = normalizedString.replace(/\s/g, '');
normalizedString = normalizedString.replace(',', '.');
return Number.parseFloat(normalizedString);
}
return null;
let normalizedString = value.length < 6 ? (value as string).replace('.', ',') : value;
normalizedString = normalizedString.replace(/\s/g, '');
normalizedString = normalizedString.replace(',', '.');
const isInvalidNumber = !/^[-+]?\d+\.?\d*$/.exec(normalizedString);
if (isInvalidNumber) return null;
const validValue = Number.parseFloat(normalizedString);
return Number.isNaN(validValue) ? null : validValue;
}

static getValueAsString(
Expand Down Expand Up @@ -451,13 +451,20 @@ export abstract class CodingFactory {
elseCode = c.id;
elseScore = c.score;
} else {
// todo: this section is somehow unclear!
// It will find a rule(set) which is invalid OR for which the value is invalid.
// But for what? The rules should be validated elsewhere, and whether the value is valid for all
// rules or not (numeric?) is not important.
/**
const invalidRule = c.ruleSets.find(rs => !!rs.rules.find(r => {
if (typeof rs.valueArrayPos === 'number' && rs.valueArrayPos >= 0) {
return Array.isArray(newResponse.value) && Array.isArray(valueToCheck) ?
!CodingFactory.isValidRule(valueToCheck[rs.valueArrayPos], r, false) : true;
}
return !CodingFactory.isValidRule(valueToCheck, r, Array.isArray(newResponse.value));
}));
* */
const invalidRule = false;
if (invalidRule) {
newResponse.status = 'CODING_ERROR';
changed = true;
Expand Down

0 comments on commit 507fdd6

Please sign in to comment.