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
Add path/family sibling traversal methods #5230
Conversation
Codecov Report
@@ Coverage Diff @@
## master #5230 +/- ##
=========================================
Coverage ? 89.25%
=========================================
Files ? 203
Lines ? 9862
Branches ? 2624
=========================================
Hits ? 8802
Misses ? 1060
Partials ? 0
Continue to review full report at Codecov.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if there is no prevSibling
or nextSibling
?
} | ||
|
||
export function forEachNextSibling(callback = Function.prototype) { | ||
let _key = this.key, nextSibling = this.getSibling(++_key); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To improve readability, could you split theses two lines?
To be consistent in the coding style, _key
could be then a const
.
} | ||
} | ||
|
||
export function forEachPrevSibling(callback = Function.prototype) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer the callback to be undefined
and add a check before calling it.
return this.getSibling(this.key + 1); | ||
} | ||
|
||
export function forEachNextSibling(callback = Function.prototype) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just like Array.prototype.forEach
allows a context
argument, it may be worth including that second argument here too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@chitchu Do you have a specific usecase in mind for this? If we add one like this, why not .map
or .filter
or any number of other array methods? I'd probably be more in favor of a getAllNextSiblings
that returned an array, rather than re-implementing array-like behavior.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've renamed the method in favor of getAllNextSiblings
and getAllPrevSiblings
which returns an array. But also accepts a callback
and a thisArgs
parameter. Once a callback
parameter has been provided this will behave similar to Array.prototype.map
.
@xtuc It would return a |
}); | ||
|
||
it("should return traverse sibling nodes", function () { | ||
assert.strictEqual(!!sibling.getNextSibling().node, true, "has property node"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can just do assert.ok(sibling.getNextSibling().node)
here and further down.
let siblings = []; | ||
const hasCallback = callback && typeof callback === "function"; | ||
while (sibling.node) { | ||
siblings = siblings.concat(hasCallback ? callback.call(thisArg, sibling) : sibling); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just push to array? Overwriting and concating array every time is almost definitely slower.
return siblings; | ||
} | ||
|
||
export function getAllPrevSibling(callback, thisArg = this) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not a fan of this kind of API, where we mix callback and return. getAllPrevSibling
sounds weird (vs. getAllPrevSiblings
). Why not just getAllPrevSiblings
to return all siblings, which could then be iterated over with getAllPrevSiblings.forEach(sibling => ...)
for (const name in nonHoistedExportNames) { | ||
hoistedExportsNode = buildExportsAssignment(t.identifier(name), hoistedExportsNode).expression; | ||
} | ||
// avoid creating too long of export assignment to prevent stack overflow |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something went wrong with rebase?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh dear me, indeed it got messed up.
00392df
to
3a42557
Compare
@kangax All requested changes have been completed. |
|
||
it("should return all preceding and succeeding sibling nodes", function () { | ||
assert.ok(sibling.getAllNextSiblings().length, "Has next sibling"); | ||
assert.ok(sibling.getNextSibling().getAllPrevSiblings().length, "Has prev sibling"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want some assert.equal tests here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That makes sense. Need to make sure the method returns the correct number of siblings.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@hzoo Added two tests against getAll(Next,Prev)Siblings().length
looks good? |
LGTM 👍 I think we're missing Flow annotations. @chitchu would you mind adding some on the functions you created? |
* getPrevSibling * getNextSibling * getAllNextSiblings * getAllPrevSiblings
@xtuc Done! I'm not too familiar with Flow type, so I'm not quite sure if I've done them correctly. 😅 |
* getPrevSibling * getNextSibling * getAllNextSiblings * getAllPrevSiblings
Adds path/family sibling traversal methods