Merge pull request #10 from status-im/operators

feat: operators
This commit is contained in:
Iuri Matias 2019-08-21 10:46:16 -04:00 committed by GitHub
commit bd2721bf8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 12 deletions

View File

@ -1,12 +1,4 @@
const EventSyncer = require('./eventSyncer.js') const EventSyncer = require('./eventSyncer.js');
EventSyncer.operators = require('./operators');
const eventSyncer = new EventSyncer(web3); module.exports = EventSyncer;
eventSyncer.init(() => {});
// eventSyncer.trackEvent('contractEvent', ((x) => x.from === "0x123")).pipe(map(x => x.rating), myscan, mymap).subscribe((v) => {
// console.dir("current average is " + v)
// })
// return Event

55
src/operators.js Normal file
View File

@ -0,0 +1,55 @@
const { of, pipe } = require('rxjs');
const { map, pluck, reduce } = require('rxjs/operators');
function $takeProps() {
const args = Object.values(arguments);
return pipe(
map(v => {
const r = {};
args.forEach(a => {
r[a] = v[a];
});
return r;
}),
);
}
/*
// ex 1
of({a: 1, b:2, e: 1}, {a: 0, c: 1, b:3}, {a: 0, d: 1, b:1})
.pipe($takeProps("a", "b", "e"))
.subscribe((v) => console.log(v));
*/
function $average(cb) {
return pipe(
reduce((accum, curr) => {
let currentValue;
if (typeof cb === 'string' || cb instanceof String){
currentValue = curr[cb];
} else if(typeof cb === "function") {
currentValue = cb(curr);
} else {
currentValue = curr;
}
return {
sum: accum.sum + currentValue,
count: accum.count + 1
}
}, { sum: 0, count: 0 }),
map(o => o.sum / o.count)
);
}
/*
of(10, 3, 4)
.pipe($average())
.subscribe((v) => console.log(v));
*/
module.exports = {
$takeProps,
$average
};

View File

@ -2,7 +2,7 @@ const path = require('path');
const webConfig = { const webConfig = {
target: 'web', target: 'web',
entry: path.join(__dirname, "src/eventSyncer.js"), entry: path.join(__dirname, "src/index.js"),
output: { output: {
path: path.resolve(__dirname, "dist"), path: path.resolve(__dirname, "dist"),
filename: 'browser.js', filename: 'browser.js',