chore: separate DBresponse classes

This commit is contained in:
buzz-lightsnack-2007 2025-04-07 20:49:39 +08:00
parent 048b42f6cd
commit f6ec954c6f
4 changed files with 54 additions and 47 deletions

View file

@ -0,0 +1,53 @@
/*
Modify data returned by the database connector scripts. s
*/
class DBParser {
mappings;
data;
constructor(DATA = {}, MAPPINGS = {}) {
this.data = DATA;
this.mappings = MAPPINGS;
}
/*
Modify the data for output.
@return {Object} cleaned data
*/
format () {
let ENTRIES;
/*
Map certain new keys to their orignal counterparts.
*/
const map = (ROW) => {
let RESULT = {};
Object.entries(this.mappings).forEach(([NEW, OLD]) => {
if (Object.keys(ROW).includes(OLD)) {
RESULT[NEW] = (typeof ROW[OLD]).includes(`str`) ? ROW[OLD].trim() : ROW[OLD];
}
});
return RESULT;
};
if (this.data instanceof Array) {
ENTRIES = [];
(this.data.length) ? this.data.forEach((SELECTED) => {
if (SELECTED) {
let CLEANED = map(SELECTED);
(Object.keys(CLEANED).length) ? ENTRIES.push(CLEANED) : false;
};
}) : false;
} else if (this.data && this.data instanceof Object) {
ENTRIES = map(this.data);
};
this.cleaned = ENTRIES;
return (this.cleaned);
}
}
module.exports = DBParser;