Skip to content

bmj2728/utils

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

38 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Utils - The Missing Go Utilities Package

utils Gopher Logo

CodeQL Testing Linter/Formatter codecov

Go Report Card Go Reference

License: MIT Go Version Release

Status: v0.3.1 πŸš€β€”Stable API with comprehensive testing

A comprehensive collection of utility packages for Go, designed to fill the gaps in the standard library and provide a consistent, well-tested set of tools for common programming tasks.

Overview

This project aims to provide a set of utility packages that follow these principles:

  • Simple: Easy to understand and use
  • Consistent: Predictable APIs across all packages
  • Well-tested: High test coverage and robust error handling
  • Performant: Optimized for speed and memory usage
  • Modular: Use only what you need

Installation πŸ“¦

Full Library

go get github.com/bmj2728/utils

Specific Components

# String utilities only
go get github.com/bmj2728/utils/pkg/strutil
# Version utilities only  
go get github.com/bmj2728/utils/pkg/version

Import Examples

// Import the full string utilities package
import "github.com/bmj2728/utils/pkg/strutil"
// Import specific utilities
import (
    "github.com/bmj2728/utils/pkg/strutil"
    "github.com/bmj2728/utils/pkg/version"
)

Key Features ✨

πŸ”€ String Utilities (strutil)

The heart of the library - comprehensive string manipulation with dual APIs!

  • 🎯 Dual API Design: Choose your style - functional for simplicity, builder for chaining
  • 🧹 Sanitization & Cleaning: HTML sanitization, whitespace normalization, character filtering
  • πŸ“ String Comparison: Levenshtein, Jaro-Winkler, LCS, and more algorithms
  • πŸ“Š Comparison Manager: Track and organize multiple comparison results (optional)
  • πŸ• History Tracking: Revert transformations with full history (optional)
  • πŸ”§ Text Transformation: Case conversion, slug generation, truncation, padding
  • βœ… Validation: Email, URL, numeric, and custom pattern validation
  • 🎲 Generation: Lorem ipsum text, emails, and placeholder content

πŸ“Š Version Utilities (version)

Build-time version management made simple!

  • πŸ“‹ Version Information: Embedded build-time version and build details
  • πŸ” Semantic Validation: Validate and parse semantic versioning format

Usage Guide πŸš€

🎭 Choose Your API Style

Functional API - Direct function calls for simple operations:

import "github.com/bmj2728/utils/pkg/strutil"

// Simple operations
cleaned := strutil.CleanWhitespace("  hello   world  ")  // "hello world"
slug := strutil.Slugify("Hello World!", 20)              // "hello-world"
isValid := strutil.IsEmail("user@example.com")           // true

Builder API - Fluent chaining for complex operations:

import "github.com/bmj2728/utils/pkg/strutil"

// Chain multiple operations
result, err := strutil.New("  <div>Hello World!</div>  ").
    CleanWhitespace().
    SanitizeHTML().
    ToLower().
    Slugify(50).
    Result()
// Result: "hello-world"

🧹 String Cleaning & Sanitization

// Remove dangerous HTML but keep safe tags
userInput := "<script>alert('xss')</script><p>Safe content</p>"
clean := strutil.SanitizeHTML(userInput)  // "<p>Safe content</p>"

// Clean whitespace and normalize
messy := "  \t  hello    world  \n  "
tidy := strutil.CleanWhitespace(messy)  // "hello world"

// Remove non-printable characters
withControl := "hello\x00\x01world"
printable := strutil.RemoveNonPrintable(withControl)  // "helloworld"

πŸ”§ Text Transformation

// Case conversions
text := "hello_world"
camel := strutil.ToCamelCase(text)     // "helloWorld"
pascal := strutil.ToPascalCase(text)   // "HelloWorld"
kebab := strutil.ToKebabCase(text)     // "hello-world"

// String manipulation
original := "Hello World"
prepended := strutil.Prepend(original, "*********")      // "*********Hello World"
truncated := strutil.Truncate(original, 5, "...")  // "Hello..."

βœ… Validation & Generation

// Validation
strutil.IsEmail("test@example.com")     // true
strutil.IsURL("https://example.com")    // true
strutil.IsNumeric("12345", true)        // true

// Lorem ipsum generation
sentence := strutil.LoremSentence()     // "Dapibus dictum sollicitudin congue dignissim hendrerit massa commodo."
email := strutil.LoremEmail()           // "lorem@ipsum.amet"
paragraph := strutil.LoremParagraph()   // Full paragraph of lorem text

πŸ“Š Advanced: Comparison Manager (Optional)

Track multiple string comparison results in one place:

// Create a builder with comparison manager
manager := strutil.New("hello world").
    WithComparisonManager().
    LevenshteinDistance("hello there").
    Similarity("hello there", strutil.JaroWinkler).
    LCSBacktrack("hello there").
    GetComparisonManager()

// Accessing results
levDist, err := manager.GetComparisonResult(strutil.LevDist, "hello there").(*strutil.ComparisonResultInt).
GetScoreInt()
sim, err := manager.GetSimilarityResult(strutil.JaroWinkler, "hello there").GetScore()
lcs := manager.GetLCSResult(strutil.LCSBacktrackWord, "hello there").GetResult()[0]


// Get all results for analysis
allComparisons := manager.GetComparisonResultsMap()
allSimilarities := manager.GetSimilarityResultsMap()

πŸ• Advanced: History Tracking (Optional)

Track transformations and revert when needed:

// Enable history tracking
result, err := strutil.New("  Hello WORLD!  ").
    WithHistory(10).                    // Track history, rotating after the tenth change
    Trim().                             // "Hello WORLD!"
    ToLower().                          // "hello world!"
    ToTitle().                          // "Hello World!"
    Slugify(20).                        // "hello-world"
    Result()

// Access transformation history
history := strutil.New("  Hello WORLD!  ").
    WithHistory(10).
    Trim().
    ToLower().
    GetHistory()

fmt.Println(history.GetAll())  // ["  Hello WORLD!  ", "Hello WORLD!", "hello world!"]

// Revert to previous states
reverted, err := strutil.New("  Hello WORLD!  ").
    WithHistory(10).
    CleanWhitespace().
    ToLower().
    RevertToPrevious().  // Back to "Hello WORLD!"
    Result()

Documentation πŸ“š

For complete documentation of all available functions and their usage, please refer to the Go Reference Documentation.

Roadmap πŸ—ΊοΈ

Exciting utilities coming in future releases:

  • πŸ“ fileoputils - Safe file operations leveraging Go 1.24+ features
  • πŸ—„οΈ dbutils - Database utilities and connection management
  • πŸ• sliceutils - Advanced slice manipulation and algorithms
  • πŸ”’ floatutils - Floating-point utilities and math helpers
  • 🐚 shellutils - Shell command execution and process management
  • 🌐 netutils - Network utilities and HTTP helpers

Acknowledgements πŸ™

This project stands on the shoulders of giants! We leverage these excellent open-source libraries:

  • go-edlib - String comparison and edit distance algorithms for measuring similarity
  • bluemonday - HTML sanitizer for safe HTML cleaning
  • go-sanitize - Powerful string cleaning and sanitization functions
  • strcase - Converting strings between different case formats
  • camelcase - Splitting camelCase/PascalCase words into components
  • lorelai - Versatile lorem ipsum generator for placeholder content
  • go-diacritics - Lightweight diacritics normalization
  • stripansi - ANSI escape sequence removal for clean output
  • google/uuid - Robust UUID implementation

License

This project is licensed under the terms of the LICENSE file included in the repository.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.