Skip to content

Boolean helper #254

Open
Open

Description

@GammaGames

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?

Activity

  1. jgonggrijp commented on Mar 24, 2023

    @jgonggrijp
    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.

  2. GammaGames commented on Mar 24, 2023

    @GammaGames
    Author

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions