Documentation
¶
Index ¶
- func Contains(text, search string) bool
- func CountOccurrences(text, search string) int
- func ParseKeyValue(input string, delimiters ...string) (value string, err error)
- func Split(data string, separator ...string) (result []string)
- type Text
- func (t *Text) CamelCaseLower() *Text
- func (t *Text) CamelCaseUpper() *Text
- func (t *Text) Capitalize() *Text
- func (t *Text) FormatNumber() *Text
- func (t *Text) Join(sep ...string) *Text
- func (t *Text) RemoveTilde() *Text
- func (t *Text) Repeat(n int) *Text
- func (t *Text) Replace(old, newStr string) *Text
- func (t *Text) RoundDecimals(decimals int) *Text
- func (t *Text) String() string
- func (t *Text) ToLower() *Text
- func (t *Text) ToSnakeCaseLower(sep ...string) *Text
- func (t *Text) ToSnakeCaseUpper(sep ...string) *Text
- func (t *Text) ToUpper() *Text
- func (t *Text) Trim() *Text
- func (t *Text) TrimPrefix(prefix string) *Text
- func (t *Text) TrimSuffix(suffix string) *Text
- func (t *Text) Truncate(maxWidth any, reservedChars ...any) *Text
- func (t *Text) TruncateName(maxCharsPerWord, maxWidth any) *Text
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶ added in v0.0.8
Contains checks if the string 'search' is present in 'text' Returns true if found, false otherwise This matches the behavior of the standard library strings.Contains
func CountOccurrences ¶ added in v0.0.8
CountOccurrences checks how many times the string 'search' is present in 'text' eg: "hello world" with search "world" will return 1
func ParseKeyValue ¶ added in v0.0.10
ParseKeyValue extracts the value part from a "key:value" formatted string. By default, it uses ":" as the delimiter but accepts an optional custom delimiter. The function returns the value part and an error (nil if successful).
Examples:
value, err := ParseKeyValue("name:John") // value = "John", err = nil value, err := ParseKeyValue("data=123", "=") // value = "123", err = nil value, err := ParseKeyValue("invalid-string") // value = "", err = error containing "delimiter ':' not found in string invalid-string"
Types ¶
type Text ¶
type Text struct {
// contains filtered or unexported fields
}
Text struct to store the content of the text
func Convert ¶
initialize the text struct with any type of value supports string, int, float, bool, []string and their variants
func (*Text) CamelCaseLower ¶
converts text to camelCase (first word lowercase) eg: "Hello world" -> "helloWorld"
func (*Text) CamelCaseUpper ¶
converts text to PascalCase (all words capitalized) eg: "hello world" -> "HelloWorld"
func (*Text) Capitalize ¶ added in v0.0.11
Capitalize transforms the first letter of each word to uppercase and the rest to lowercase. For example: "hello world" -> "Hello World"
func (*Text) FormatNumber ¶ added in v0.0.17
FormatNumber formats a numeric value with thousand separators (dots) and removes trailing zeros after the decimal point Example: Convert(2189009.00).FormatNumber().String() returns "2.189.009"
func (*Text) Join ¶ added in v0.0.24
Join concatenates the elements of a string slice to create a single string. If no separator is provided, it uses a space as default. Can be called with varargs to specify a custom separator. eg: Convert([]string{"Hello", "World"}).Join() => "Hello World" eg: Convert([]string{"Hello", "World"}).Join("-") => "Hello-World"
func (*Text) Repeat ¶ added in v0.0.13
Repeat returns the string s repeated n times. If n is less than or equal to zero, or if s is empty, it returns an empty string. eg: Convert("abc").Repeat(3) => "abcabcabc"
func (*Text) Replace ¶ added in v0.0.7
Replace replaces all occurrences of old with new in the text content eg: "hello world" with old "world" and new "universe" will return "hello universe"
func (*Text) RoundDecimals ¶ added in v0.0.17
RoundDecimals formats a float value by rounding it to the specified number of decimal places Example: Convert(3.12221).RoundDecimals(2).String() returns "3.12"
func (*Text) ToSnakeCaseLower ¶
snakeCase converts a string to snake_case format with optional separator. If no separator is provided, underscore "_" is used as default. Example:
Input: "camelCase" -> Output: "camel_case" Input: "PascalCase", "-" -> Output: "pascal-case" Input: "APIResponse" -> Output: "api_response" Input: "user123Name", "." -> Output: "user123.name"
ToSnakeCaseLower converts text to snake_case format
func (*Text) ToSnakeCaseUpper ¶
ToSnakeCaseUpper converts text to Snake_Case format
func (*Text) Trim ¶ added in v0.0.7
Trim removes spaces at the beginning and end of the text content eg: " hello world " will return "hello world"
func (*Text) TrimPrefix ¶ added in v0.0.9
TrimPrefix removes the specified prefix from the text content if it exists eg: "prefix-hello" with prefix "prefix-" will return "hello"
func (*Text) TrimSuffix ¶ added in v0.0.7
TrimSuffix removes the specified suffix from the text content if it exists eg: "hello.txt" with suffix ".txt" will return "hello"
func (*Text) Truncate ¶ added in v0.0.13
Truncate truncates a text so that it does not exceed the specified width. If the text is longer, it truncates it and adds "..." if there is space. If the text is shorter or equal to the width, it remains unchanged. The reservedChars parameter indicates how many characters should be reserved for suffixes. This parameter is optional - if not provided, no characters are reserved (equivalent to passing 0). eg: Convert("Hello, World!").Truncate(10) => "Hello, ..." eg: Convert("Hello, World!").Truncate(10, 3) => "Hell..." eg: Convert("Hello").Truncate(10) => "Hello"
func (*Text) TruncateName ¶ added in v0.0.20
TruncateName truncates names and surnames in a user-friendly way for display in limited spaces like chart labels. It adds abbreviation dots where appropriate. This method processes the first word differently if there are more than 2 words in the text.
Parameters:
- maxCharsPerWord: maximum number of characters to keep per word (any numeric type)
- maxWidth: maximum total length for the final string (any numeric type)
Examples:
- Convert("Jeronimo Dominguez").TruncateName(3, 15) => "Jer. Dominguez"
- Convert("Ana Maria Rodriguez").TruncateName(2, 10) => "An. Mar..."
- Convert("Juan").TruncateName(3, 5) => "Juan"