const fs = require('fs'); const sortObject = obj => Object.keys(obj).sort().reduce((res, key) => (res[key] = obj[key], res), {}); module.exports = (dpercs, dlangs) => { matches = {}; keys = Object.keys(dpercs); keys.forEach(key => { if (key == "total") { return; } matches[key] = { langs: {}, best: { lvl1: null, lvl2: null, lvl3: null, lvl4: null, lvl5: null, } } dlgkeys = Object.keys(dlangs); dlgkeys.forEach(lk => { if (dlangs[lk]) { inLang = JSON.parse(fs.readFileSync("./data/" + lk + ".dat").toString())[key].average; inWord = dpercs[key]; diff = inLang - inWord; if (diff < 0) { diff = diff - (diff * 2); } // console.log("Lang: " + inLang + "\nWord: " + inWord + "\nDiff: " + diff); matches[key].langs[diff] = lk; matches[key].langs = sortObject(matches[key].langs); okeys = Object.keys(matches[key].langs); matches[key].best.lvl1 = matches[key].langs[okeys[0]]; matches[key].best.lvl2 = matches[key].langs[okeys[1]]; matches[key].best.lvl3 = matches[key].langs[okeys[2]]; matches[key].best.lvl4 = matches[key].langs[okeys[3]]; matches[key].best.lvl5 = matches[key].langs[okeys[4]]; } }) }) return matches; }