arangodb / arangojs

The official ArangoDB JavaScript driver.
https://arangodb.github.io/arangojs
Apache License 2.0
601 stars 107 forks source link

Update Documentation for .reduce() #730

Closed jmathew closed 2 years ago

jmathew commented 3 years ago

There's a minor error in the documentation for .reduce() on https://arangodb.github.io/arangojs/7.0.0-rc.2/classes/_cursor_.arraycursor.html#reduce image

In addition to the above I'd recommend updating the examples to be more apples to apples. The examples are supporting the note "Most complex uses of the reduce method can be replaced with simpler code using ArrayCursor.forEach or the for await syntax." That suggests the example should highlight only the additional complexity introduced by .reduce. The example adds complexity to the .reduce example by introducing an unnecessary ternary as well. To make it more equivalent:

// BAD! NEEDLESSLY COMPLEX!
const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
const result = await cursor.reduce((accumulator, currentValue) => {
  // Use the same if statement as in the good example. 
  if (currentValue % 2 === 0) { 
    accumulator["even"].push(currentValue);
  } else {
    accumulator["odd"].push(currentValue);
  }
  return accumulator;
}, { odd: [], even: [] });
console.log(result); // { odd: [1, 3, 5], even: [2, 4] }

// GOOD! MUCH SIMPLER!
const cursor = await db.query(aql`FOR x IN 1..5 RETURN x`);
const odd = [];
const even = [];
for await (const item of cursor) {
  if (item % 2 === 0) {
    even.push(item);
  } else {
    odd.push(item);
  }
}
console.log({ odd, even }); // { odd: [1, 3, 5], even: [2, 4] }

I can make this update in a PR if there's some documentation somewhere on how to update the documentation. Seems I'd need to make a PR to the gh-pages branch with changes made in the devel folder. I think there's some syntax highlighting tool that I'm missing for updating the example code though. Is there some documentation on updating the documentation?

pluma commented 3 years ago

Thanks for spotting this.

The docs are generated from the source in the main branch, the code example actually comes from the typescript source file for that method (src/cursor.ts I think).

If you want to provide a PR, keep in mind that for legal reasons we need a signed Contributor License Agreement (CLA) before we can merge your code into the project.

You can download the form from our website: https://www.arangodb.com/documents/cla.pdf

Once you've filled out and signed it, please send it to us via e-mail: cla@arangodb.com or fax: +49-221-2722999-88.

jmathew commented 3 years ago

That makes sense, thanks. WRT to the CLA, I'll leave the PR to someone already ready to contribute since this is a relatively quick PR.

pluma commented 2 years ago

This was fixed in 20c8e12.