Humanizr / Humanizer

Humanizer meets all your .NET needs for manipulating and displaying strings, enums, dates, times, timespans, numbers and quantities
Other
8.68k stars 964 forks source link

Humanize / Dehumanize Boolean #758

Open XtremeOwnageDotCom opened 6 years ago

XtremeOwnageDotCom commented 6 years ago

Add support for dehumanizing boolean.

Ie...

yes -> true y -> true si (spanish) -> true 1 -> true

no -> false n -> false "false" -> false nada(spanish) -> false 0 => false

XtremeOwnageDotCom commented 6 years ago

I am quite certain there is a much better way to do this, but, here is my simple static method I used to fill the purpose for the time being.

        /// <summary>
        /// Simple, stupid method to parse a boolean from a user's input.
        /// </summary>
        /// <param name="Input">Unformatted input text</param>
        /// <returns>Boolean if successfully parsed, NULL if unable to parse.</returns>
        public static bool? Parse(this string Input)
        {
            string raw = Input.Trim().ToLowerInvariant();

            var True = new HashSet<string>()
            {
                "yes",
                "y",
                "true",
                "1",
                "sure"
            };
            var False = new HashSet<string>{
                "no",
                "n",
                "false",
                "0",
            };

            if (True.Contains(raw))
                return true;
            else if (False.Contains(raw))
                return false;
            else
                //Return null - unable to parse results.
                return null;

        }