Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sjordan1975 opened this issue Oct 15, 2016 · 5 comments
Assignees
Labels
Milestone

Comments

@sjordan1975
Copy link

sjordan1975 commented Oct 15, 2016

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
Copy link
Owner

grtjn commented Oct 31, 2016

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

@jmakeig
Copy link

jmakeig commented Oct 31, 2016

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
Copy link
Collaborator

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

@grtjn
Copy link
Owner

grtjn commented Jun 27, 2018

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 grtjn self-assigned this Jun 27, 2018
@grtjn grtjn added the bug label Jun 27, 2018
@grtjn grtjn added this to the 0.5.0 milestone Jun 27, 2018
@grtjn grtjn closed this as completed Jun 27, 2018
@grtjn
Copy link
Owner

grtjn commented Jun 27, 2018

@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..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants