49 lines
No EOL
1.1 KiB
JavaScript
49 lines
No EOL
1.1 KiB
JavaScript
/*
|
|
Modify data returned by the database connector scripts. s
|
|
*/
|
|
class DBParser {
|
|
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.
|
|
*/
|
|
function mapKeys (ORIGINAL = {}, MAPPINGS) {
|
|
let RESULT = {};
|
|
|
|
Object.entries(MAPPINGS).forEach(([NEW, OLD]) => {
|
|
RESULT[NEW] = (ORIGINAL[OLD] != null ? (typeof ORIGINAL[OLD]).includes(`str`) : false) ? ORIGINAL[OLD].trim() : ORIGINAL[OLD];
|
|
});
|
|
|
|
return RESULT;
|
|
}
|
|
|
|
if (this.data instanceof Array) {
|
|
ENTRIES = [];
|
|
|
|
(this.data.length) ? this.data.forEach((SELECTED) => {
|
|
if (SELECTED) {
|
|
let CLEANED = mapKeys(SELECTED, this.mappings);
|
|
(Object.keys(CLEANED).length) ? ENTRIES.push(CLEANED) : false;
|
|
};
|
|
}) : false;
|
|
} else if (this.data && this.data instanceof Object) {
|
|
ENTRIES = mapKeys(this.data, this.mappings);
|
|
};
|
|
|
|
this.cleaned = ENTRIES;
|
|
return (this.cleaned);
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = DBParser; |