Status: Pre-release (approaching v0.1.0) - API may change
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.
This project aims to provide a set of utility packages that follow these principles:
utils/
├── strutil/ # String manipulation & validation (currently in development)
├── httputil/ # HTTP client patterns
├── fileutl/ # File operations
├── jsonutil/ # JSON utilities
├── cryptoutil/ # Common crypto patterns
├── configutil/ # Configuration management
├── sliceutil/ # Generic slice operations
├── validationutil/ # Input validation
├── errorutil/ # Error handling
└── testutil/ # Test helpers
The project has recently added several new features:
Upcoming features planned for future releases:
strutil
)The strutil
package provides comprehensive string manipulation, validation, and sanitization functions. It offers both a functional API and a fluent builder pattern.
The package provides standardized error constants for validation failures:
// Error constants for validation
ErrInvalidEmail = "invalid email address"
ErrInvalidURL = "invalid URL"
ErrInvalidUUID = "invalid UUID"
ErrInvalidLengthRange = "invalid length range"
ErrInvalidLength = "invalid length"
ErrInvalidEmpty = "empty string"
ErrInvalidEmptyAfterNormalization = "empty string after whitespace normalization"
ErrInvalidNotAlphaNumeric = "string contains non-alphanumeric characters"
These constants are used throughout the package for consistent error messaging and can be checked when using the builder API’s validation methods.
The functional API provides standalone functions for string operations:
// Generate a UUID
strutil.GenerateUUID()
// Validate an email
strutil.IsEmail(email)
// Create a URL-friendly slug
strutil.Slugify(rawText, 50)
// Keep only alphabetic characters
strutil.KeepAlpha(input, false)
// Keep only alphanumeric characters
strutil.KeepAlphaNumeric(input, true)
// HTML sanitization
strutil.StripHTML(input)
strutil.SanitizeHTML(input)
// Lorem ipsum generation
strutil.LoremWord()
strutil.LoremSentence()
strutil.LoremParagraph()
strutil.LoremEmail()
strutil.LoremURL()
The string builder API allows for chaining multiple operations:
// Chain multiple operations
strutil.New(input).
CleanWhitespace().
Truncate(100, "...").
SanitizeHTML(allowedTags).
String()
// Validation with error handling
strutil.New(input).
RequireNotEmpty().
RequireEmail().
Result()
The following functions have been fully implemented and are ready for use:
strutil
)GenerateUUID()
- Generates a random UUIDGenerateUUIDV7()
- Generates a UUID v7 (recommended for new applications)RandomString(length)
- Generates a random alphanumeric stringRandomHex(length)
- Generates a random hexadecimal stringRandomUrlSafe(length)
- Generates a random URL-safe stringLoremWord()
- Generates a random lorem ipsum wordLoremWords(count)
- Generates multiple lorem ipsum wordsLoremSentence()
- Generates a lorem ipsum sentenceLoremSentenceCustom(count)
- Generates a custom-length lorem ipsum sentenceLoremSentences(count)
- Generates multiple lorem ipsum sentencesLoremSentencesCustom(count, length)
- Generates multiple custom-length lorem ipsum sentencesLoremSentencesVariable(count, min, max)
- Generates variable-length lorem ipsum sentencesLoremParagraph()
- Generates a lorem ipsum paragraphLoremParagraphs(count)
- Generates multiple lorem ipsum paragraphsLoremDomain()
- Generates a random domain nameLoremURL()
- Generates a random URLLoremEmail()
- Generates a random email addressIsEmail(s)
- Validates email addressesIsURL(s)
- Validates URLsIsDomain(domain)
- Validates domain namesIsUUID(s)
- Validates UUIDsIsValidLength(s, min, max)
- Validates string lengthIsEmpty(s)
- Checks if a string is emptyIsEmptyNormalized(s)
- Checks if a string is empty after normalizationIsAlphaNumericString(s)
- Checks if a string contains only alphanumeric charactersIsAlphaString(s)
- Checks if a string contains only alphabetic charactersToUpper(s)
- Converts a string to uppercaseToLower(s)
- Converts a string to lowercaseCapitalize(s)
- Capitalizes the first letter of a stringUncapitalize(s)
- Converts the first letter of a string to lowercaseToTitleCase(s)
- Converts a string to title caseSplitCamelCase(s)
- Splits a camelCase string into space-separated wordsSplitPascalCase(s)
- Splits a PascalCase string into space-separated wordsToSnakeCase(s, scream)
- Converts a string to snake_case or SCREAMING_SNAKE_CASEToSnakeCaseWithIgnore(s, scream, ignore)
- Converts a string to snake_case with custom ignore charactersToKebabCase(s, scream)
- Converts a string to kebab-case or SCREAMING-KEBAB-CASEToCamelCase(s)
- Converts a string to camelCaseToPascalCase(s)
- Converts a string to PascalCaseToDelimited(s, delim, ignore, scream)
- Converts a string to a custom delimited formatReplaceWhitespace(s, replacement)
- Replaces whitespace with a specified stringReplaceSpaces(s, replacement)
- Replaces spaces with a specified stringTrim(s)
- Trims whitespace from both ends of a stringTrimLeft(s)
- Trims whitespace from the left side of a stringTrimRight(s)
- Trims whitespace from the right side of a stringAlphaReplace(s, replacement)
- Replaces non-alphabetic charactersAlphaNumericReplace(s, replacement)
- Replaces non-alphanumeric charactersSlugify(s, length)
- Creates a URL-friendly slugTruncate(s, length, suffix)
- Truncates a string to a specified lengthKeepAlpha(s, ws)
- Keeps only alphabetic charactersKeepAlphaNumeric(s, ws)
- Keeps only alphanumeric charactersCleanWhitespace(s)
- Removes all whitespaceNormalizeWhitespace(s)
- Normalizes whitespaceCollapseWhitespace(s)
- Collapses consecutive whitespace charactersNormalizeDiacritics(s)
- Replaces diacritics with their ASCII equivalentStripHTML(s)
- Removes all HTML tagsSanitizeHTML(s)
- Sanitizes HTML using UGC policySanitizeHTMLCustom(s, allowedTags)
- Sanitizes HTML with custom allowed tagsEscapeHTML(s)
- Escapes HTML special charactersLevenshteinDistance(s1, s2)
- Calculates the Levenshtein distance between two stringsDamerauLevenshteinDistance(s1, s2)
- Calculates the Damerau-Levenshtein distance between two stringsOSADamerauLevenshteinDistance(s1, s2)
- Calculates the Optimal String Alignment variant of Damerau-Levenshtein distanceversion
)GetVersion()
- Returns the current version stringGetBuildInfo()
- Returns complete build informationIsValidSemVer(version)
- Validates semantic versioning formatThe BuildInfo
struct also provides methods:
String()
- Returns a human-readable version stringIsDevelopment()
- Checks if this is a development buildGetShortCommit()
- Returns the short commit hashgo get github.com/bmj2728/utils
Import the specific package you need:
import "github.com/bmj2728/utils/strutil"
Then use the functions or builders as needed:
// Using functional API
strutil.Slugify("Hello World!")
// Using string builder API
strutil.New(userInput).
CleanWhitespace().
RequireNotEmpty().
Result()
This project follows Go’s standard testing practices. Each package includes comprehensive tests to ensure functionality, edge cases, and regression prevention.
To run all tests in the project:
go test ./...
For verbose output:
go test -v ./...
To run tests for a specific package:
go test ./strutil
This project uses GitHub Actions for continuous integration. The workflow automatically runs:
The CI/CD workflows run on all pull requests and pushes to the main branch, ensuring code quality, security, and functionality are maintained. Weekly security scans are also scheduled to catch newly discovered vulnerabilities.
All workflows use concurrency groups to avoid redundant runs and optimize CI/CD resources.
This project leverages several excellent open-source libraries:
This project is licensed under the terms of the LICENSE file included in the repository.
Contributions are welcome! Please feel free to submit a Pull Request.