Files
memecoin-botV2/.pnpm-store/v10/files/d2/10ff7e602fe1b5bcae2c5a3f4a4d463f739a97db773159d8cf373ba028d9812cffa5cd6fab144f20b5fde318cb19a7dd2913a3ab41fd4b7a9e9d0b7477e102

41 lines
1.1 KiB
Plaintext

'use strict';
const {Writable} = require('stream');
const defaultInitial = 0;
const defaultReducer = (acc, value) => value;
class Reduce extends Writable {
constructor(options) {
super(Object.assign({}, options, {objectMode: true}));
this.accumulator = defaultInitial;
this._reducer = defaultReducer;
if (options) {
'initial' in options && (this.accumulator = options.initial);
'reducer' in options && (this._reducer = options.reducer);
}
}
_write(chunk, encoding, callback) {
const result = this._reducer.call(this, this.accumulator, chunk);
if (result && typeof result.then == 'function') {
result.then(
value => {
this.accumulator = value;
callback(null);
},
error => callback(error)
);
} else {
this.accumulator = result;
callback(null);
}
}
static make(reducer, initial) {
return new Reduce(typeof reducer == 'object' ? reducer : {reducer, initial});
}
}
Reduce.reduce = Reduce.make;
Reduce.make.Constructor = Reduce;
module.exports = Reduce;