Skip to content

Instantly share code, notes, and snippets.

@gotexis
Created July 28, 2022 06:28
Show Gist options
  • Save gotexis/da2781e09da8414bfdcc0b42ab64cb23 to your computer and use it in GitHub Desktop.
Save gotexis/da2781e09da8414bfdcc0b42ab64cb23 to your computer and use it in GitHub Desktop.
Anwser to Q3 - ( only doing 3.1 Sum )
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