feat: add raw olx settings parser (#332)

This commit is contained in:
Kristin Aoki
2023-05-15 15:12:32 -04:00
committed by GitHub
parent 084d61ffa1
commit be8f9ecc86
8 changed files with 304 additions and 128 deletions

View File

@@ -1,13 +1,23 @@
import { XMLParser } from 'fast-xml-parser';
import { popuplateItem } from './SettingsParser';
const SETTING_KEYS = [
'max_attempts',
'weight',
'showanswer',
'show_reset_button',
'rerandomize',
];
class ReactStateSettingsParser {
constructor(problemState) {
this.problemState = problemState;
this.problem = problemState.problem;
this.rawOLX = problemState.rawOLX;
}
getSettings() {
let settings = {};
const stateSettings = this.problemState.settings;
const stateSettings = this.problem.settings;
settings = popuplateItem(settings, 'number', 'max_attempts', stateSettings.scoring.attempts);
settings = popuplateItem(settings, 'weight', 'weight', stateSettings.scoring);
@@ -19,6 +29,34 @@ class ReactStateSettingsParser {
return settings;
}
parseRawOlxSettings() {
const rawOlxSettings = this.getSettings();
// console.log(rawOlxSettings);
// console.log(this.rawOLX);
const parserOptions = {
ignoreAttributes: false,
alwaysCreateTextNode: true,
numberParseOptions: {
leadingZeros: false,
hex: false,
},
};
const parser = new XMLParser(parserOptions);
const olx = parser.parse(this.rawOLX);
const settingAttributes = Object.keys(olx.problem).filter(tag => tag.startsWith('@_'));
settingAttributes.forEach(attribute => {
const attributeKey = attribute.substring(2);
if (SETTING_KEYS.includes(attributeKey)) {
if (attributeKey === 'max_attempts' || attributeKey === 'weight') {
rawOlxSettings[attributeKey] = parseInt(olx.problem[attribute]);
} else {
rawOlxSettings[attributeKey] = olx.problem[attribute];
}
}
});
return rawOlxSettings;
}
}
export default ReactStateSettingsParser;