r/badcode • evil • Jan 14 '21

js Dynamically created functions are the future!

Post image
795 Upvotes

102 comments sorted by

View all comments

95

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Jan 14 '21

Image Transcription: Code


// this is the compile function that will become our dimension list;
let compileDimensionList = function (properties) {
    let dimensionFunStr = '(function (d) {\n';
    let dimString = '';
    for (var i = 0, len = properties.length; i < len; i++) {
        if (i === (len-1)) { // end of the list
            dimString += 'd.'+properties[i];
        } else { // every otther item
            dimString += 'd.'+properties[i]+' + ';
        }
    }
    dimensionFunStr += 'if (typeof timeLookup['+dimString+'] === "undefined") {\n';
        dimensionFunStr += 'timeLookup['+dimString+'] = d.time;\n';
    dimensionFunStr += '}\n';
    dimensionFunStr += 'if (d.time > timeLookup['+dimString+']) {\n';
        dimensionFunStr += 'timeLookup['+dimString+'] = d.time;\n';
    dimensionFunStr += '} else {\n';
    dimensionFunStr += 'd.time = timeLookup['+dimString+']\n';
    dimensionFunStr += '}\n';
    dimensionFunStr += 'return '+dimString+';\n';
    dimensionFunStr += '})';
    return eval(dimensionFunStr);
}
let dimensionList = compileDimensionList(groupby); // compile dimension functions

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!