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

Add is.present #278

Open
philliplongman opened this issue Sep 8, 2017 · 3 comments
Open

Add is.present #278

philliplongman opened this issue Sep 8, 2017 · 3 comments

Comments

@philliplongman
Copy link

I would love to see an is.present() function, like the present? method in Rails. Basically, it returns true if the value is truthy and not empty. So an empty array, object, or string would not be present, nor would null, undefined, and NaN.

@philliplongman
Copy link
Author

philliplongman commented Sep 8, 2017

It's really useful for confirming ensuring the data you're about to use is there, in a concise way.

So, this code, which has to check that an array has been returned from the server before it can check if it contains anything:

  renderPendingRequestsTab() {
    if (this.state.pending_employees && this.state.pending_employees.length > 0) {
      // itterate
    }
    else {
      // don't itterate 
    }
  }

Now does the same check in one step, because both null and [] will return false:

  renderPendingRequestsTab() {
    if (is.present(this.state.pending_employees)) {
      // itterate
    }
    else {
      // don't itterate 
    }
  }

@jt3k
Copy link
Contributor

jt3k commented Sep 10, 2017

can be a is.falsy and is.truthy has the same functionality?

@philliplongman
Copy link
Author

They don't have the same functionality. To be present something must be both truthy and not blank. Empty arrays and objects are truthy, empty strings are falsy. All three are empty and therefor not present.

Also, 0 is falsy in JavaScript, but would be considered truthy and therefor present in Ruby. My preference would be for 0 to be present even though it's falsy—since the primary use of is.present is to confirm you've gotten a result, and if you're expecting a number, 0 is a successful result. But if people prefer 0 be not present since it's falsy, I wouldn't fight about it.

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

No branches or pull requests

3 participants