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

Take Advantage of Higher-Order Functions for Atomic Changes #4

Closed
aklatzke opened this issue May 1, 2018 · 0 comments
Closed

Take Advantage of Higher-Order Functions for Atomic Changes #4

aklatzke opened this issue May 1, 2018 · 0 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@aklatzke
Copy link

aklatzke commented May 1, 2018

    reqTypes.forEach(item => {
      item = item.toLowerCase()
      let lowerCaseProjectTypes = availableTypes.projectTypes.map(value => value.toLowerCase())
      let lowerCaseGlobalTypes = availableTypes.globalTypes.map(value => value.toLowerCase())
      
      if (lowerCaseProjectTypes.indexOf(item) !== -1) {
        added.proj.push(availableTypes.projectTypes[lowerCaseProjectTypes.indexOf(item)])
      } else if (lowerCaseGlobalTypes.indexOf(item) !== -1) {
        added.global.push(availableTypes.globalTypes[lowerCaseGlobalTypes.indexOf(item)])
      } else {
        missing.push(item)
      }
    })

This is totally up to you depending on how you prefer to structure things, but it seems like you're embracing the functional style and using higher order functions where available. You can use a .map() here to handle the atomic change (toLowerCase) on each option. You can also use the ES6 Array.includes to clean up some of the messy !== -1 checking.

This is definitely more of a preference kind of thing so feel free to just close this one if you prefer. The (very small) changes I'd recommend are the following:

    reqTypes
     .map(item => item.toLowerCase())
     .forEach(item => {
      let lowerCaseProjectTypes = availableTypes.projectTypes.map(value => value.toLowerCase())
      let lowerCaseGlobalTypes = availableTypes.globalTypes.map(value => value.toLowerCase())
      
      if (lowerCaseProjectTypes.includes(item)) {
        added.proj.push(availableTypes.projectTypes[lowerCaseProjectTypes.indexOf(item)])
      } else if (lowerCaseGlobalTypes.includes(item)) {
        added.global.push(availableTypes.globalTypes[lowerCaseGlobalTypes.indexOf(item)])
      } else {
        missing.push(item)
      }
    })
@shanepadgett shanepadgett self-assigned this May 1, 2018
@shanepadgett shanepadgett added the enhancement New feature or request label May 1, 2018
@shanepadgett shanepadgett added this to the 1.0.5 milestone May 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants