tinystring

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2025 License: MIT Imports: 0 Imported by: 8

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

Installation

go get github.com/cdvelop/tinystring

Usage

import "github.com/cdvelop/tinystring"

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

// Chaining operations
text := tinystring.Convert("Él Múrcielago Rápido")
    .RemoveTilde()
    .CamelCaseLower()
    .String()
// Result: "elMurcielagoRapido"
Available Operations
  • 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")
  • 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
  • Replace(old, new string): Replaces all occurrences of a substring (e.g. "hello world" -> "hello universe")
  • 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)
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"

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

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

// 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

Contributing

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

License

MIT License

Documentation

Index

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 Split added in v0.0.7

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

Split divides a string by a separator and returns a slice of substrings Note: Strings shorter than 3 characters are returned as is

Types

type Text

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

Text struct to store the content of the text

func Convert

func Convert(s string) *Text

initialize the text struct

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

func (t *Text) RemoveTilde() *Text

Remueve tildes y diacríticos

func (*Text) Replace added in v0.0.7

func (t *Text) Replace(old, newStr string) *Text

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

func (t *Text) String() string

String method to return the content of the text

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

Jump to

Keyboard shortcuts

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