Help us shape the future of Go! Take the 2026 Go Developer Survey

to

package module
v0.4.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 5, 2022 License: Apache-2.0 Imports: 9 Imported by: 25

README

Library of Converters in Go 1.18+

Go Version GoDoc License Go Report Card

Documentation

Overview

Package to contains a number of converters that take any number of types and return something transformed from them. It also contains a more granular approach to fmt.Stringer.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dedented added in v0.3.0

func Dedented(in string) string

Dedented discards any initial blank lines with nothing but whitespace in them and then detects the number and type of whitespace characters at the beginning of the first line to the first non-whitespace rune and then subsequently removes that number of runes from every following line treating empty lines as if they had only n number of spaces. Note that if any line does not have n number of initial spaces it the initial runes will still be removed. It is, therefore, up to the content creator to ensure that all lines have the same space indentation.

func FuncName added in v0.1.1

func FuncName(i any) string

FuncName makes a best effort attempt to return the string name of the passed function. Anonymous functions are named "funcN" where N is the order of appearance within the current scope. Note that this function will panic if not passed a function.

Example
package main

import (
	"github.com/rwxrob/fn"
	"github.com/rwxrob/fn/each"
	"github.com/rwxrob/to"
)

func Foo() {}

func main() {

	f1 := func() {}
	f2 := func() {}

	// Foo is defined outside of the ExampleFuncName

	each.Println(fn.Map([]any{f1, f2, Foo, to.Lines}, to.FuncName))

}
Output:
func1
func2
Foo
Lines

func Human added in v0.2.0

func Human(a any) string

Human returns a human-friendly string version of the item, specifically:

  • single-quoted runes
  • double-quoted strings
  • numbers as numbers
  • function names are looked up
  • slices joined with "," and wrapped in []

Anything else is rendered as its fmt.Sprintf("%v",it) form.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/to"
)

type FooStruct struct{}

func (f FooStruct) String() string { return "FOO" }

type HumanFoo struct{}

func (f HumanFoo) Human() string { return "a friendly foo" }

func FooFunc(a any) {}

func main() {
	fmt.Println(to.Human('r'))                    // not number
	fmt.Println(to.Human("string💢good"))          // unescaped
	fmt.Println(to.Human(new(FooStruct)))         // has String()
	fmt.Println(to.Human(new(HumanFoo)))          // has Human()
	fmt.Println(to.Human([]rune{'r', 's', 'm'}))  // commas
	fmt.Println(to.Human([]string{"foo", "bar"})) // also commas
	fmt.Println(to.Human(func() {}))              // func1
	fmt.Println(to.Human(FooFunc))                // FooFunc
}
Output:
'r'
"string💢good"
FOO
a friendly foo
['r','s','m']
["foo","bar"]
func1
FooFunc

func IndentWrapped added in v0.3.2

func IndentWrapped(in string, indent, width int) string

IndentWrapped adds the specified number of spaces to the beginning of every line ensuring that the wrapping is preserved to the specified width. See Wrapped.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/to"
)

func main() {

	description := `
		The y2j command converts YAML (including references and
		anchors) to compressed JSON (with a single training newline) using
		the popular Go yaml.v3 package and its special <yaml:",inline"> tag.
		Because of this only YAML maps are supported as a base type (no
		arrays). An array can easily be done as the value of a map key.

		`

	fmt.Println("Indented:\n" + to.IndentWrapped(description, 7, 60))

}
Output:
Indented:
       The y2j command converts YAML (including references
       and anchors) to compressed JSON (with a single
       training newline) using the popular Go yaml.v3
       package and its special <yaml:",inline"> tag. Because
       of this only YAML maps are supported as a base type
       (no arrays). An array can easily be done as the value
       of a map key.

func Indentation added in v0.3.0

func Indentation[T Text](in T) int

Indentation returns the number of whitespace runes (in bytes) between beginning of the passed string and the first non-whitespace rune.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/to"
)

func main() {
	fmt.Println(to.Indentation("    some"))
	fmt.Println(to.Indentation("  some"))
	fmt.Println(to.Indentation("some"))
	fmt.Println(to.Indentation(" 💚ome"))
}
Output:
4
2
0
1

func Indented added in v0.4.2

func Indented(in string, indent int) string

Indented returns a string with each line indented by the specified number of spaces. Carriage returns are stripped (if found) as a side-effect.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/to"
)

func main() {
	fmt.Println("Indented:\n" + to.Indented("some\nthing", 4))
}
Output:
Indented:
    some
    thing

func Lines added in v0.1.1

func Lines(in any) []string

Lines transforms the input into a string and then divides that string up into lines (\r?\n) suitable for functional map operations.

Example
package main

import (
	"github.com/rwxrob/fn/each"
	"github.com/rwxrob/to"
)

func main() {
	buf := `
some

thing 
here

mkay
`
	each.Print(to.Lines(buf))
}
Output:
something heremkay

func Prefixed added in v0.3.2

func Prefixed(in, pre string) string

Prefixed returns a string where every line is prefixed. Carriage returns (if any) are dropped.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/to"
)

func main() {
	fmt.Println(to.Prefixed("some\nthing", "P  "))
}
Output:
P  some
P  thing

func String

func String(in any) string

String converts whatever is passed to its fmt.Sprintf("%v") string version (but avoids calling it if possible). Be sure you use things with consistent string representations.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/to"
)

type stringer struct{}

func (s stringer) String() string { return "stringer" }

func main() {
	stuff := []any{
		"some", []byte{'t', 'h', 'i', 'n', 'g'},
		1, 2.234, stringer{},
	}
	for _, s := range stuff {
		fmt.Printf("%q ", to.String(s))
	}
}
Output:
"some" "thing" "1" "2.234" "stringer"

func Wrapped added in v0.3.2

func Wrapped(it string, width int) (string, int)

Wrapped will return a word wrapped string at the given boundary width (in bytes) and the count of words contained in the string. All whitespace is compressed to a single space. Any width less than 1 will simply trim and crunch whitespace returning essentially the same string and the word count. If the width is less than any given word at the start of a line than it will be the only word on the line even if the word length exceeds the width. Non attempt at word-hyphenation is made. Note that whitespace is defined as unicode.IsSpace and does not include control characters.

Example
package main

import (
	"fmt"

	"github.com/rwxrob/to"
)

func main() {

	wrapped, count := to.Wrapped("some thing", 3)
	fmt.Printf("%q %v\n", wrapped, count)

}
Output:
"some\nthing" 2

Types

type HumanFriend added in v0.2.0

type HumanFriend interface {
	Human() string
}

HumanFriend implementations have a human readable form that is even friendlier than fmt.Stringer.

type Text added in v0.4.4

type Text interface{ string | []rune }

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL