durs / node-activex

Node.JS Implementaion of ActiveXObject

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Provide a helper to work with Enumerables

orellabac opened this issue · comments

Many COM objects use the _NewEnum mechanism to iterate. A Helper can be provided to make this easier.

I tried to create a PR but I found no build scripts. In general my idea is that you can export a class like:

class COMIterator {
    constructor(obj) {
        this.obj = obj;
        this.iter = this.obj._NewEnum;
    }
    [Symbol.iterator]() {
        var localIter = this.iter;
        return function*() {
            if (localIter) {
                var value = undefined;
                do {
                    value = localIter.Next();
                    if (value !== undefined)
                        yield value;
                } while(value !== undefined);
            }
        }();
    }
}

and create a function Iter like

var Iter(obj) => new COMIterator(obj)

and then you can have code like:

  var fso = ActiveXObject("Scripting.FileSystemObject");


    console.log( "Read Drive Letter");
    for(const dir of Iter(drives)) {
      console.log( dir.DriveLetter);
    }