How should I change my code to chain generator function by using ES6 proxy?

113
April 14, 2018, at 6:35 PM

I've been spending many hours for the last couple days to make my code work but I can't make it work!!

What I'm trying to accomplish is chaining generator functions like the below by using es6 proxy. It seems that I can add these list operation functions to generator prototype but I want to use proxy instead for my study purpose.

Something that I want to acheive in the end
rangeGenerator
    .map('project function here')
    .filter('predicate function here')
    .forEach('do some last work');

Could anyone please take a look at my code below and tell me what I'm doing wrong and how to fix this, please?

Thanks!!

My Code
function range(end) {
    return function* rangeGf() {
        for (let i = 1; i <= end; i++) {
             yield i;
        }
    }
}
const makeHandler = (options) => {
    const handler = {
        get: function(target, property, resolver) {
            if (property === 'map') {
                return map.bind(target);
            }
            if (property === 'filter') {
                return filter.bind(target);
            }
        }
    };
    function map(project) {
        console.log('project', project);
        const newTarget = this;
        function* mapGf() {
            for (let v of newTarget()) {
                yield v * 2; //hard coding for now...
            }
        }
        // return generator object wrapped by proxy
        return new Proxy(mapGf(), handler);
    }
    function filter(predicate) {
        console.log('predicate', predicate);
        const newTarget = this;
        function* filterGf() {
            for (let v of newTarget) {
                if(v % 3 === 0) { //hard coding for now...
                    yield v; 
                }
            }
        }
        // return generator object wrapped by proxy
        return new Proxy(filterGf(), handler);
    } 
    return handler;
};
const rangeGf = range(10);
const $range = new Proxy(rangeGf, makeHandler())
// for now, I'm trying to make this simple one work!
for (var i of $range.map('project')) {
  console.log(i)
}
For now, I want to make this part work
for (var i of $range.map('project')) {
    console.log(i)
}
READ ALSO
How to use the same HTML render different content via different urls?

How to use the same HTML render different content via different urls?

I have an HTML page and multiple URLs as follows

89
Serving express static files issue

Serving express static files issue

I am having issues w/ serving static files in my current Express app, although I've done a similar setup in a bunch of other appsMy folder structure is as follows:

84
Electron-dl Cannot read property &#39;then&#39; of undefined

Electron-dl Cannot read property 'then' of undefined

I am using electron-dl to download a file but I cant seem to make it workI did almost exactly same as defined in docs but i dont know what is wrong

114
node.js module.exports reference error

node.js module.exports reference error

I have a function that I defined named newPasswordI then reference it at the bottom under modules

114