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

Boolean helper #254

Open
GammaGames opened this issue Mar 23, 2023 · 2 comments · May be fixed by #255
Open

Boolean helper #254

GammaGames opened this issue Mar 23, 2023 · 2 comments · May be fixed by #255

Comments

@GammaGames
Copy link

Pretty frequently I come across the problem of needing to check if an optional variable is true or false. The problem is that the server can send it as true, false, undefined, "true", or "false". I added this mixin (based on this SO answer):

_.mixin({
  isTrue: function(str) {
    return JSON.parse((str || "false").toLowerCase());
  }
});

Thoughts on adding this?

@jgonggrijp
Copy link
Contributor

I'm in favor! Although I would like to slap a !! in front to ensure it always returns a boolean, and call it isTrueish or something like that to clarify that is does not only detect the value true but also string equivalents.

If you submit a pull request that also includes tests and documentation, I will merge it.

@GammaGames GammaGames linked a pull request Mar 24, 2023 that will close this issue
@GammaGames
Copy link
Author

GammaGames commented Mar 24, 2023

I wasn't sure where to put it, but since it's primarily dealing with strings that can sometimes be booleans I put it in the string module. Let me know if I messed anything up! :p

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 a pull request may close this issue.

2 participants