grtjn / marklogic-typescript-definitions

TypeScript Definition files for MarkLogic built-in functions, available as npm module
Apache License 2.0
9 stars 2 forks source link

Add the Iterator Symbol to functions which return nodes (ex. cts.search) #16

Open sjordan1975 opened 7 years ago

sjordan1975 commented 7 years ago

Here is a snippet of JS that works in QConsole:

let results = [];

var items = cts.search(
    cts.andQuery([
      cts.directoryQuery("/searchable/"),
      cts.elementWordQuery(
      xs.QName("maintenance-committee"),
        "AV-003", "case-insensitive")]));

for (item of items){
   var value = item.xpath("/searchable/db-meta/maintenance-committee/string()");

   results.push(value);
}

Here is the same code written in TS:

let results = [];

let items = cts.search(
    cts.andQuery([
      cts.directoryQuery("/searchable/"),
      cts.elementWordQuery(
      xs.QName("maintenance-committee"),
        “AV-003", "case-insensitive")])).toArray();

for (let item of items){
    let value: string = item.xpath("/searchable/db-meta/maintenance-committee/string()”);

results.push(value);

}

Without the .toArray(), the resulting JS output from the TS transpiler doesn’t work.

grtjn commented 7 years ago

@christyharagan, what do you make of this? Is the transpiler just doing a bad job, or is there something more serious going on?

jmakeig commented 7 years ago

You should never use toArray() when you don’t have to. The whole point of the iterator (i.e. what you get by calling the Symbol.iterator method) is to not have to eagerly evaluate all of the results. cts.search() returns a ValueIterator (in MarkLogic 8) which is (confusingly) bother an iterator and and iterable. I don't know the TypeScript-specific issue here, but here’s a polyfill for ES2016 Map type that exposes its interface.

Edit: Here’s a better explanation. I can’t tell if IterableIterator is built-in to TypeScript or something one has to define herself, though.

christyharagan commented 7 years ago

No, it was just poor typing effort on my behalf. It was a good catch by Salim. I'll put a fix in...

grtjn commented 6 years ago

Christy probably fixed this in one of the 0.3.0 to 0.4.2 releases, but it has now moved over to Sequence. Hopefully that works equally well..

grtjn commented 6 years ago

@sjordan1975 Would you be able to run a test with the current code in this repo? It is tagged with 0.5.0, but not yet released on NPM..