An open API service indexing awesome lists of open source software.

https://github.com/bmj2728/utils

a collection of utilities for Go
https://github.com/bmj2728/utils

case-conversion edit-distance go golang levenshtein-distance lorem-ipsum-generator sanitizer string-builder string-generation string-transformation strings-manipulation

Last synced: 3 months ago
JSON representation

a collection of utilities for Go

Awesome Lists containing this project

README

          


Utils - The Missing Go Utilities Package


utils Gopher Logo

[![CodeQL](https://github.com/bmj2728/utils/workflows/CodeQL/badge.svg)](https://github.com/bmj2728/utils/actions?query=workflow%3ACodeQL)
[![Testing](https://github.com/bmj2728/utils/actions/workflows/test.yml/badge.svg)](https://github.com/bmj2728/utils/actions)
[![Linter/Formatter](https://github.com/bmj2728/utils/actions/workflows/ci.yml/badge.svg)](https://github.com/bmj2728/utils/actions)
[![codecov](https://codecov.io/gh/bmj2728/utils/branch/main/graph/badge.svg)](https://codecov.io/gh/bmj2728/utils)

[![Go Report Card](https://goreportcard.com/badge/github.com/bmj2728/utils)](https://goreportcard.com/report/github.com/bmj2728/utils)
[![Go Reference](https://pkg.go.dev/badge/github.com/bmj2728/utils.svg)](https://pkg.go.dev/github.com/bmj2728/utils)

[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)
[![Go Version](https://img.shields.io/github/go-mod/go-version/bmj2728/utils)](https://golang.org/)
[![Release](https://img.shields.io/github/v/release/bmj2728/utils?include_prereleases)](https://github.com/bmj2728/utils/releases)

> **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
```bash
go get github.com/bmj2728/utils
```

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

### Import Examples
```go
// Import the full string utilities package
import "github.com/bmj2728/utils/pkg/strutil"
```
```go
// 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:
```go
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:
```go
import "github.com/bmj2728/utils/pkg/strutil"

// Chain multiple operations
result, err := strutil.New("

Hello World!
").
CleanWhitespace().
SanitizeHTML().
ToLower().
Slugify(50).
Result()
// Result: "hello-world"
```

### 🧹 String Cleaning & Sanitization

```go
// Remove dangerous HTML but keep safe tags
userInput := "alert('xss')

Safe content

"
clean := strutil.SanitizeHTML(userInput) // "

Safe content

"

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

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

```go
// 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:

```go
// 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:

```go
// 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](https://pkg.go.dev/github.com/bmj2728/utils).

## 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**](https://github.com/hbollon/go-edlib) - String comparison and edit distance algorithms for measuring similarity
- [**bluemonday**](https://github.com/microcosm-cc/bluemonday) - HTML sanitizer for safe HTML cleaning
- [**go-sanitize**](https://github.com/mrz1836/go-sanitize) - Powerful string cleaning and sanitization functions
- [**strcase**](https://github.com/iancoleman/strcase) - Converting strings between different case formats
- [**camelcase**](https://github.com/fatih/camelcase) - Splitting camelCase/PascalCase words into components
- [**lorelai**](https://github.com/UltiRequiem/lorelai) - Versatile lorem ipsum generator for placeholder content
- [**go-diacritics**](https://github.com/Regis24GmbH/go-diacritics) - Lightweight diacritics normalization
- [**stripansi**](https://github.com/acarl005/stripansi) - ANSI escape sequence removal for clean output
- [**google/uuid**](https://github.com/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.