Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 24 additions & 10 deletions to/to.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
42 changes: 35 additions & 7 deletions to/to_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,33 +94,33 @@ 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:
// "foo\nbar"
// "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:
// ""
Expand Down Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
}