tinystring

package module
v0.0.29 Latest Latest
Warning

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

Go to latest
Published: May 8, 2025 License: MIT Imports: 2 Imported by: 0

README

TinyString

TinyString is a lightweight Go library that provides text manipulation with a fluid API, without external dependencies or standard library dependencies.

Features

  • 🚀 Fluid and chainable API
  • 🔄 Common text transformations
  • 🧵 Concurrency safe
  • 📦 No external dependencies
  • 🎯 Easily extensible TinyGo Compatible
  • 🔄 Support for converting any data type to string

Installation

go get github.com/cdvelop/tinystring

Usage

import "github.com/cdvelop/tinystring"

// Basic example with string
text := tinystring.Convert("MÍ téxtO").RemoveTilde().String()
// Result: "MI textO"

// Examples with other data types
numText := tinystring.Convert(42).String()
// Result: "42"

boolText := tinystring.Convert(true).ToUpper().String()
// Result: "TRUE"

floatText := tinystring.Convert(3.14).String()
// Result: "3.14"

// Chaining operations
text := tinystring.Convert("Él Múrcielago Rápido")
    .RemoveTilde()
    .CamelCaseLower()
    .String()
// Result: "elMurcielagoRapido"

// Working with string pointers (avoids extra allocations)
// This method reduces memory allocations by modifying the original string directly
originalText := "Él Múrcielago Rápido"
tinystring.Convert(&originalText).RemoveTilde().CamelCaseLower().Apply()
// originalText is now modified directly: "elMurcielagoRapido"
Available Operations
  • Convert(v any): Initialize text processing with any data type (string, *string, int, float, bool, etc.). When using a string pointer (*string) along with the Apply() method, the original string will be modified directly, avoiding extra memory allocations.
  • Apply(): Updates the original string pointer with the current content. This method should be used when you want to modify the original string directly without additional allocations.
  • String(): Returns the content of the text as a string without modifying any original pointers.
  • RemoveTilde(): Removes accents and diacritics (e.g. "café" -> "cafe")
  • ToLower(): Converts to lowercase (e.g. "HELLO" -> "hello")
  • ToUpper(): Converts to uppercase (e.g. "hello" -> "HELLO")
  • Capitalize(): Capitalizes the first letter of each word (e.g. "hello world" -> "Hello World")
  • CamelCaseLower(): Converts to camelCase (e.g. "hello world" -> "helloWorld")
  • CamelCaseUpper(): Convert to UpperCase (e.g. "hello world" -> "HelloWorld")
  • ToSnakeCaseLower(): Converts to snake_case (e.g. "hello world" -> "hello_world"), With Other Params: ToSnakeCaseLower("-") -> "hello-world"
  • ToSnakeCaseUpper(): Convert to SNAKE_CASE (e.g. "hello world" -> "HELLO_WORLD"), With Other Params: ToSnakeCaseUpper("-") -> "HELLO-WORLD"
  • Split(data, separator string): Divides a string by a separator and returns a slice of substrings
  • Join(sep ...string): Joins elements of a string slice with a specified separator (default: space). (e.g. Convert([]string{"Hello", "World"}).Join() -> "Hello World" or Convert([]string{"Hello", "World"}).Join("-") -> "Hello-World")
  • ParseKeyValue(input string, delimiter string): Extracts the value from a key:value string format (e.g. ParseKeyValue("name:John") -> "John", nil)
  • Replace(old, new any, n ...int): Replaces occurrences of a substring. If n is provided, replaces up to n occurrences. If n < 0 or not provided, replaces all. The old and new parameters can be of any type (string, int, float, bool) and will be converted to string automatically. (e.g. "hello world" -> "hello universe" or "value 123 here" -> "value 456 here")
  • TrimPrefix(prefix string): Removes a specified prefix from the beginning of a string (e.g. "prefix-content" -> "content")
  • TrimSuffix(suffix string): Removes a specified suffix from the end of a string (e.g. "file.txt" -> "file")
  • Trim(): Removes spaces from the beginning and end of a string (e.g. " hello " -> "hello")
  • Contains(text, search string): Checks if a string contains another, returns boolean (e.g. Contains("hello world", "world") -> true)
  • CountOccurrences(text, search string): Counts how many times a string appears in another (e.g. CountOccurrences("hello hello world", "hello") -> 2)
  • Repeat(n int): Repeats the string n times (e.g. "abc".Repeat(3) -> "abcabcabc")
  • Truncate(maxWidth any, reservedChars ...any): Truncates text so that it does not exceed the specified width, adding ellipsis if necessary. If the text is shorter or equal, it remains unchanged. The maxWidth parameter accepts any numeric type. The reservedChars parameter is optional and also accepts any numeric type. (e.g. "Hello, World!".Truncate(10) -> "Hello, ..." or "Hello, World!".Truncate(10, 3) -> "Hell...")
  • TruncateName(maxCharsPerWord any, maxWidth any): Truncates names and surnames in a user-friendly way for displaying in limited spaces like chart labels. It adds abbreviation dots where appropriate and handles the first word specially when there are more than 2 words. Parameters: maxCharsPerWord (maximum characters per word), maxWidth (maximum total length). (e.g. Convert("Jeronimo Dominguez").TruncateName(3, 15) -> "Jer. Dominguez")
  • RoundDecimals(decimals int): Rounds a numeric value to the specified number of decimal places (e.g. Convert(3.12221).RoundDecimals(2).String() -> "3.12")
  • FormatNumber(): Formats a number with thousand separators and removes trailing zeros after the decimal point (e.g. Convert(2189009.00).FormatNumber().String() -> "2.189.009")
Examples
// Remove accents
tinystring.Convert("áéíóú").RemoveTilde().String()
// Result: "aeiou"

// Convert to camelCase
tinystring.Convert("hello world").CamelCaseLower().String()
// Result: "helloWorld"

// Combining operations
tinystring.Convert("HÓLA MÚNDO")
    .RemoveTilde()
    .ToLower()
    .String()
// Result: "hola mundo"

// Converting different data types
tinystring.Convert(123).String()
// Result: "123"

tinystring.Convert(45.67).String()
// Result: "45.67"

tinystring.Convert(true).String()
// Result: "true"

// Convert and transform other data types
tinystring.Convert(456).CamelCaseUpper().String()
// Result: "456"

tinystring.Convert(false).ToUpper().String()
// Result: "FALSE"

// Format number with decimal places
tinystring.Convert(3.12221).RoundDecimals(2).String()
// Result: "3.12"

// Format number with thousand separators
tinystring.Convert(2189009.00).FormatNumber().String()
// Result: "2.189.009"
// Result: "FALSE"

// Split a string by separator
result := tinystring.Split("apple,banana,cherry", ",")
// Result: []string{"apple", "banana", "cherry"}

// Split a string by whitespace (default)
result := tinystring.Split("hello world  test")
// Result: []string{"hello", "world", "test"}

// Split with mixed whitespace characters
result := tinystring.Split("hello\tworld\nnew")
// Result: []string{"hello", "world", "new"}

// Parse key-value string
value, err := tinystring.ParseKeyValue("user:admin")
// Result: value = "admin", err = nil

// Parse with custom delimiter
value, err := tinystring.ParseKeyValue("count=42", "=")
// Result: value = "42", err = nil

// Multiple values with same delimiter
value, err := tinystring.ParseKeyValue("path:usr:local:bin")
// Result: value = "usr:local:bin", err = nil

// Handle error when delimiter is not found
value, err := tinystring.ParseKeyValue("invalidstring")
// Result: value = "", err = error("delimiter ':' not found in string invalidstring")

// Join string slices with default space separator
result := tinystring.Convert([]string{"Hello", "World"}).Join().String()
// Result: "Hello World" 

// Join with custom separator
result := tinystring.Convert([]string{"apple", "banana", "orange"}).Join("-").String()
// Result: "apple-banana-orange"

// Join and chain with other transformations
result := tinystring.Convert([]string{"hello", "world"}).Join().ToUpper().String()
// Result: "HELLO WORLD"

// Replace text
tinystring.Convert("hello world").Replace("world", "universe").String()
// Result: "hello universe"

// Trim prefix and suffix
tinystring.Convert("prefix-content.txt").TrimPrefix("prefix-").TrimSuffix(".txt").String()
// Result: "content"

// Trim spaces and remove file extension
tinystring.Convert("  file.txt  ").Trim().TrimSuffix(".txt").String()
// Result: "file"

// Chain multiple operations
text := tinystring.Convert(" User Name ")
    .Trim()
    .Replace(" ", "_")
    .ToLower()
    .String()
// Result: "user_name"

// Search examples
// Check if a string contains another
result := tinystring.Contains("hello world", "world")
// Result: true

// Count occurrences
count := tinystring.CountOccurrences("abracadabra", "abra")
// Result: 2

// Capitalize each word
tinystring.Convert("hello world").Capitalize().String()
// Result: "Hello World"

// Capitalize with accent removal
tinystring.Convert("hólá múndo")
    .RemoveTilde()
    .Capitalize()
    .String()
// Result: "Hola Mundo"

// Repeat a string multiple times
tinystring.Convert("hello ").Repeat(3).String()
// Result: "hello hello hello "

// Repeat with other transformations
tinystring.Convert("test")
    .ToUpper()
    .Repeat(2)
    .String()
// Result: "TESTTEST"

// Zero or negative repetitions returns an empty string
tinystring.Convert("test").Repeat(0).String()
// Result: ""

// Truncate a long string to specific width
tinystring.Convert("Hello, World!").Truncate(10).String()
// Result: "Hello, ..."

// Truncate with reserved characters (explicitly provided)
tinystring.Convert("Hello, World!").Truncate(10, 3).String()
// Result: "Hell..."

// Text shorter than max width remains unchanged
tinystring.Convert("Hello").Truncate(10).String()
// Result: "Hello"

// Truncate names and surnames for display in charts or limited spaces
tinystring.Convert("Jeronimo Dominguez").TruncateName(3, 15).String()
// Result: "Jer. Dominguez"

// Truncate multiple names and surnames with total length limit
tinystring.Convert("Ana Maria Rodriguez").TruncateName(2, 10).String()
// Result: "An. Mar..."

// Handle first word specially when more than 2 words
tinystring.Convert("Juan Carlos Rodriguez").TruncateName(3, 20).String()
// Result: "Jua. Car. Rodriguez"

// Truncate and transform
tinystring.Convert("hello world")
    .ToUpper()
    .Truncate(8)
    .String()
// Result: "HELLO..."

// Truncate with different numeric types
tinystring.Convert("Hello, World!").Truncate(uint8(10), float64(3)).String()
// Result: "Hell..."

// Chaining truncate and repeat
tinystring.Convert("hello")
    .Truncate(6) // Truncate(6) doesn't change "hello"
    .Repeat(2)
    .String()
// Result: "hellohello"
Working with String Pointers

TinyString supports working directly with string pointers to avoid additional memory allocations. This can be especially useful in performance-critical applications or when processing large volumes of text.

// Create a string variable
text := "Él Múrcielago Rápido"

// Modify it directly using string pointer and Apply()
// No need to reassign the result
Convert(&text).RemoveTilde().ToLower().Apply()

// The original variable is modified
fmt.Println(text) // Output: "el murcielago rapido"

// This approach can reduce memory pressure in high-performance scenarios
// by avoiding temporary string allocations

String() vs Apply()

The library offers two ways to finish a chain of operations:

// 1. Using String() - Returns the result without modifying the original
originalText := "Él Múrcielago Rápido"
result := Convert(&originalText).RemoveTilde().ToLower().String()
fmt.Println(result)        // Output: "el murcielago rapido"
fmt.Println(originalText)  // Output: "Él Múrcielago Rápido" (unchanged)

// 2. Using Apply() - Modifies the original string directly
originalText = "Él Múrcielago Rápido"
Convert(&originalText).RemoveTilde().ToLower().Apply()
fmt.Println(originalText)  // Output: "el murcielago rapido" (modified)

Performance benchmarks show that using string pointers can reduce memory allocations when processing large volumes of text, which can be beneficial in high-throughput applications or systems with memory constraints.

// Sample benchmark results:
BenchmarkMassiveProcessingWithoutPointer-16    114458 ops  10689 ns/op  4576 B/op  214 allocs/op
BenchmarkMassiveProcessingWithPointer-16       105290 ops  11434 ns/op  4496 B/op  209 allocs/op

Contributing

Contributions are welcome. Please open an issue to discuss proposed changes.

License

MIT License

Documentation

Overview

Example (StringPointerBasic)

Estos ejemplos ilustran cómo usar los punteros a strings para evitar asignaciones adicionales

// Creamos una variable string que queremos modificar
myText := "héllô wórld"

// En lugar de crear una nueva variable con el resultado,
// modificamos directamente la variable original usando Apply()
Convert(&myText).RemoveTilde().ToLower().Apply()

// La variable original ha sido modificada
fmt.Println(myText)
Output:

hello world
Example (StringPointerCamelCase)
// Ejemplo de uso con múltiples transformaciones
originalText := "Él Múrcielago Rápido"

// Las transformaciones modifican la variable original directamente
// usando el método Apply() para actualizar el puntero
Convert(&originalText).RemoveTilde().CamelCaseLower().Apply()

fmt.Println(originalText)
Output:

elMurcielagoRapido
Example (StringPointerEfficiency)
// En aplicaciones de alto rendimiento, reducir asignaciones de memoria
// puede ser importante para evitar la presión sobre el garbage collector
// Método tradicional (crea nuevas asignaciones de memoria)
traditionalText := "Texto con ACENTOS"
processedText := Convert(traditionalText).RemoveTilde().ToLower().String()
fmt.Println(processedText)

// Método con punteros (modifica directamente la variable original)
directText := "Otro TEXTO con ACENTOS"
Convert(&directText).RemoveTilde().ToLower().Apply()
fmt.Println(directText)
Output:

texto con acentos
otro texto con acentos

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains added in v0.0.8

func Contains(text, search string) bool

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

func CountOccurrences(text, search string) int

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

func ParseKeyValue(input string, delimiters ...string) (value string, err error)

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"

func Split added in v0.0.7

func Split(data string, separator ...string) (result []string)

Types

type Text

type Text struct {
	// contains filtered or unexported fields
}

Text struct to store the content of the text

func Convert

func Convert(v any) *Text

initialize the text struct with any type of value supports string, *string, int, float, bool, []string and their variants use String() to get the final string value or Apply() to update the original string pointer eg:

original := "hello world"
Convert(&original).ToUpper().Apply()
// or
out :=  Convert("hello world").ToUpper().String()

func (*Text) Apply added in v0.0.28

func (t *Text) Apply()

Apply updates the original string pointer with the current content. This method should be used when you want to modify the original string directly without additional allocations.

func (*Text) CamelCaseLower

func (t *Text) CamelCaseLower() *Text

converts text to camelCase (first word lowercase) eg: "Hello world" -> "helloWorld"

func (*Text) CamelCaseUpper

func (t *Text) CamelCaseUpper() *Text

converts text to PascalCase (all words capitalized) eg: "hello world" -> "HelloWorld"

func (*Text) Capitalize added in v0.0.11

func (t *Text) Capitalize() *Text

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

func (t *Text) FormatNumber() *Text

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

func (t *Text) Join(sep ...string) *Text

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) RemoveTilde

func (t *Text) RemoveTilde() *Text

Remueve tildes y diacríticos

func (*Text) Repeat added in v0.0.13

func (t *Text) Repeat(n int) *Text

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

func (t *Text) Replace(oldAny, newAny any, n ...int) *Text

Replace replaces up to n occurrences of old with new in the text content If n < 0, there is no limit on the number of replacements eg: "hello world" with old "world" and new "universe" will return "hello universe" Old and new can be any type, they will be converted to string using anyToString

func (*Text) RoundDecimals added in v0.0.17

func (t *Text) RoundDecimals(decimals int) *Text

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) String

func (t *Text) String() string

String method to return the content of the text without modifying any original pointers

func (*Text) ToLower

func (t *Text) ToLower() *Text

convert to lower case eg: "HELLO WORLD" -> "hello world"

func (*Text) ToSnakeCaseLower

func (t *Text) ToSnakeCaseLower(sep ...string) *Text

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

func (t *Text) ToSnakeCaseUpper(sep ...string) *Text

ToSnakeCaseUpper converts text to Snake_Case format

func (*Text) ToUpper

func (t *Text) ToUpper() *Text

convert to upper case eg: "hello world" -> "HELLO WORLD"

func (*Text) Trim added in v0.0.7

func (t *Text) Trim() *Text

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

func (t *Text) TrimPrefix(prefix string) *Text

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

func (t *Text) TrimSuffix(suffix string) *Text

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

func (t *Text) Truncate(maxWidth any, reservedChars ...any) *Text

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

func (t *Text) TruncateName(maxCharsPerWord, maxWidth any) *Text

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"

Jump to

Keyboard shortcuts

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