Skip to content
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

Remove extra menu separators #11827

Merged
merged 4 commits into from Feb 5, 2018
Merged

Remove extra menu separators #11827

merged 4 commits into from Feb 5, 2018

Conversation

codebytere
Copy link
Member

@codebytere codebytere commented Feb 5, 2018

Removes leading and trailing menu separators and resolves #5869.

Tested manually on MacOS, Windows, and Linux

To-Do:

  • Add specs

@codebytere codebytere requested a review from a team February 5, 2018 04:41

return visibleItems.filter((el, idx, array) => {
const meetsDeleteConditions = !visiblePrev || idx === array.length - 1 || array[idx + 1].type === 'separator'
const toDelete = el.type === 'separator' && meetsDeleteConditions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little hard to scan. I think that a reduce would work wonders here.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with felix this is a little hard to scan. I think the reasons are:

  • meetsDeleteConditions and toDelete are confusingly named, e.g. how can you meet delete conditions if type isn't separator?
  • visibleItems requires a little thought before you can tell how the loop will behave in edge cases

I'm not thinking functionally enough to see how a reduce would work cleanly here and would love enlightenment on this, no snark intended 😄

After poking around with a few approaches, the clearest I came up with was (untested):

function removeExtraSeparators(items) {
  // remove invisible items 
  items = items.filter(e => e.visible !== false)
  // fold adjacent separators together
  items = items.filter((e, idx, arr) => e.type !== 'separator' || idx==0 || arr[idx-1].type!=='separator')
  // remove edge separators
  return items.filter((e, idx, arr) => e.type !== 'separator' || (idx!=0 && idx!=arr.length-1))
}

Marking as Request Changes for cleanup to this section, but the above code is just a suggestion, not a requirement


return visibleItems.filter((el, idx, array) => {
const meetsDeleteConditions = !visiblePrev || idx === array.length - 1 || array[idx + 1].type === 'separator'
const toDelete = el.type === 'separator' && meetsDeleteConditions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with felix this is a little hard to scan. I think the reasons are:

  • meetsDeleteConditions and toDelete are confusingly named, e.g. how can you meet delete conditions if type isn't separator?
  • visibleItems requires a little thought before you can tell how the loop will behave in edge cases

I'm not thinking functionally enough to see how a reduce would work cleanly here and would love enlightenment on this, no snark intended 😄

After poking around with a few approaches, the clearest I came up with was (untested):

function removeExtraSeparators(items) {
  // remove invisible items 
  items = items.filter(e => e.visible !== false)
  // fold adjacent separators together
  items = items.filter((e, idx, arr) => e.type !== 'separator' || idx==0 || arr[idx-1].type!=='separator')
  // remove edge separators
  return items.filter((e, idx, arr) => e.type !== 'separator' || (idx!=0 && idx!=arr.length-1))
}

Marking as Request Changes for cleanup to this section, but the above code is just a suggestion, not a requirement

assert.equal(menu.items.length, 3)
assert.equal(menu.items[0].label, 'a')
assert.equal(menu.items[1].label, 'b')
assert.equal(menu.items[2].label, 'c')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another good edge case to test for would be [ sep, sep, a, b, c, sep, sep ] -> [ a, b, c ]

Copy link
Member

@ckerr ckerr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

@ckerr ckerr merged commit 5240352 into master Feb 5, 2018
@codebytere codebytere deleted the remove-leading-trailing-seps branch February 5, 2018 19:54
sethlu pushed a commit to sethlu/electron that referenced this pull request May 3, 2018
* add function to remove leading/trailing separators

* change const  name for clarity

* add spec to check filtered separators

* clean method and add edge case spec per review
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Remove leading and trailing Menu separators
3 participants