Created
July 28, 2022 06:28
-
-
Save gotexis/da2781e09da8414bfdcc0b42ab64cb23 to your computer and use it in GitHub Desktop.
Anwser to Q3 - ( only doing 3.1 Sum )
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const CsvReader = require('./csv-reader'); | |
// reusing what we've done before. | |
const exisMath = { | |
sum (...args) { | |
return args.reduce((acc, i) => { | |
return parseFloat(acc) + parseFloat(i) | |
}, 0) | |
}, | |
} | |
class CsvReducer { | |
constructor (stream, { reduce: reduceMethod }) { | |
this.stream = stream | |
this.reduceMethod = reduceMethod | |
this.reader = new CsvReader(stream); | |
const [column, reducer] = Object.entries(this.reduceMethod)[0]; | |
this.column = column; | |
this.reduceFn = exisMath[reducer] | |
} | |
async reduce () { | |
const output = { | |
[this.column]: 0 | |
} | |
this.reader.on('row', row => { | |
output[this.column] = this.reduceFn(output[this.column], row[this.column]) | |
}) | |
await this.reader.done(); | |
return output | |
} | |
} | |
module.exports = CsvReducer |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment