diff --git a/to/to.go b/to/to.go index 5f72623..a623892 100644 --- a/to/to.go +++ b/to/to.go @@ -489,35 +489,49 @@ func Visible(in string) string { // determine the conversion logic and returns the converted value if // successful. If conversion fails, it returns [this] as a fallback. func Type[T any](from string, this T) T { - var result any = this - var err error + var result T switch any(this).(type) { case bool: - result = is.Truthy(from) + b := is.Truthy(from) + result = any(b).(T) case int: - result, err = strconv.Atoi(from) + i64, err := strconv.ParseInt(from, 10, 64) if err != nil { return this } + + if int64(int(i64)) != i64 { + return this + } + result = any(int(i64)).(T) case float64: - result, err = strconv.ParseFloat(from, 64) + f64, err := strconv.ParseFloat(from, 64) if err != nil { return this } + result = any(f64).(T) case string: - result = from + result = any(from).(T) + default: + return this } - return result.(T) + return result } // TrimVisible removes anything but unicode.IsPrint and then trims. It // does not crunch spaces, however. -func TrimVisible(in string) string { return strings.TrimSpace(Visible(in)) } +func TrimVisible(in string) string { + return strings.TrimSpace(Visible(in)) +} // TrimCrunchSpace is same as CrunchSpace but trims initial and trailing // space. -func TrimCrunchSpace(in string) string { return strings.TrimSpace(CrunchSpace(in)) } +func TrimCrunchSpace(in string) string { + return strings.TrimSpace(CrunchSpace(in)) +} // TrimCrunchSpaceVisible is same as CrunchSpaceVisible but trims initial and trailing // space. -func TrimCrunchSpaceVisible(in string) string { return strings.TrimSpace(CrunchSpaceVisible(in)) } +func TrimCrunchSpaceVisible(in string) string { + return strings.TrimSpace(CrunchSpaceVisible(in)) +} diff --git a/to/to_test.go b/to/to_test.go index 8c3e58f..87ee797 100644 --- a/to/to_test.go +++ b/to/to_test.go @@ -94,19 +94,19 @@ func ExampleHuman() { // FooFunc } -func ExampleDedent_simple() { +func ExampleDedented_simple() { fmt.Printf("%q\n", to.Dedented("\n foo\n bar")) // Output: // "foo\nbar" } -func ExampleDedent_tabs_or_Spaces() { +func ExampleDedented_tabs_or_Spaces() { fmt.Printf("%q\n", to.Dedented("\n\t\tfoo\n\t\tbar")) // Output: // "foo\nbar" } -func ExampleDedent_multiple_Blank_Lines() { +func ExampleDedented_multiple_blank_lines() { fmt.Printf("%q\n", to.Dedented("\n\n \n\n foo\n bar")) fmt.Printf("%q\n", to.Dedented("\n \n\n \n some")) // Output: @@ -114,13 +114,13 @@ func ExampleDedent_multiple_Blank_Lines() { // "some" } -func ExampleDedent_accidental_Chop() { +func ExampleDedented_accidental_chop() { fmt.Printf("%q\n", to.Dedented("\n\n \n\n foo\n bar")) // Output: // "foo\nar" } -func ExampleDedent_single_blank_line() { +func ExampleDedented_single_blank_line() { fmt.Printf("%q\n", to.Dedented(" \n")) // Output: // "" @@ -344,8 +344,14 @@ func ExampleTrimCrunchSpace() { } func ExampleTrimCrunchSpaceVisible() { - fmt.Printf("%q\n", to.TrimCrunchSpaceVisible(" here \033 is\nsome ")) - fmt.Printf("%q\n", to.TrimCrunchSpaceVisible(" here \033 is \nsome ")) + fmt.Printf( + "%q\n", + to.TrimCrunchSpaceVisible(" here \033 is\nsome "), + ) + fmt.Printf( + "%q\n", + to.TrimCrunchSpaceVisible(" here \033 is \nsome "), + ) // Output: // "here is some" @@ -371,6 +377,19 @@ func ExampleType() { fmt.Println("hello, hello", to.Type("hello", "default")) + fmt.Println("abc, 10", to.Type("abc", 10)) + fmt.Println("abc, 2.0", to.Type("abc", 2.0)) + fmt.Println("1.23, 0", to.Type("1.23", 0)) + fmt.Println( + "999999999999999999999, 0", + to.Type("999999999999999999999", 0), + ) + fmt.Println("yes, false", to.Type("yes", false)) + fmt.Println("0, true", to.Type("0", true)) + fmt.Println(" , 0", to.Type(" ", 0)) + fmt.Println(" , true", to.Type(" ", true)) + fmt.Println("123, int64(0)", to.Type("123", int64(0))) + // Output: // true, false true // false, false false @@ -386,4 +405,13 @@ func ExampleType() { // -14, 0.0 -14 // "", 0.0 0 // hello, hello hello + // abc, 10 10 + // abc, 2.0 2 + // 1.23, 0 0 + // 999999999999999999999, 0 0 + // yes, false false + // 0, true false + // , 0 0 + // , true false + // 123, int64(0) 0 }