feat: add raw olx settings parser (#332)
This commit is contained in:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user