The filter
package offers a rich set of utilities for Go developers, focusing on string manipulation, array and slice operations, date and time formatting, number formatting, and mathematical computations. Its goal is to simplify handling common programming tasks. Below is an outline of available features and instructions for getting started.
- Installing
- Basic Usage
- String Functions
- Array Functions
- Date Functions
- Number Functions
- Math Functions
- Map Functions
Install the filter
package with ease using the following Go command:
go get github.com/kaptinlin/filter
Below is an example illustrating the basic usage of the filter
package for string manipulation:
package main
import (
"fmt"
"github.com/kaptinlin/filter"
)
func main() {
fmt.Println(filter.Trim(" hello world ")) // "hello world"
fmt.Println(filter.Replace("hello world", "world", "Go")) // "hello Go"
}
The string Functions provide a range of functions to manipulate and query strings effectively.
Function | Description |
---|---|
Default |
Returns a default value if the string is empty. |
Trim |
Removes leading and trailing whitespace from the string. |
Split |
Divides a string into a slice of strings based on a specified delimiter. |
Replace |
Substitutes all instances of a specified substring with another string. |
Remove |
Eliminates all occurrences of a specified substring from the string. |
Append |
Adds characters to the end of a string. |
Prepend |
Adds characters to the beginning of a string. |
Length |
Returns the number of characters in a string, accounting for UTF-8 encoding. |
Upper |
Converts all characters in a string to uppercase. |
Lower |
Converts all characters in a string to lowercase. |
Titleize |
Capitalizes the first letter of each word in a string. |
Capitalize |
Capitalizes the first letter of a string. |
Camelize |
Converts a string to camelCase. |
Pascalize |
Converts a string to PascalCase. |
Dasherize |
Transforms a string into a lowercased, dash-separated format. |
Slugify |
Converts a string into a URL-friendly "slug", ensuring it is safe for use in URLs and filenames. |
Pluralize |
Determines the singular or plural form of a word based on a numeric value. |
Ordinalize |
Converts a number to its ordinal English form. |
Truncate |
Shortens a string to a specified length and appends "..." if it exceeds that length. |
TruncateWords |
Truncates a string to a specified number of words, appending "..." if it exceeds that limit. |
Array functions help you work with slices, offering tools to modify, analyze, or transform slice data.
Function | Description |
---|---|
Unique |
Removes duplicate elements, leaving only unique ones. |
Join |
Concatenates slice elements into a single string. |
First |
Retrieves the first element of the slice. |
Last |
Returns the last element of the slice. |
Random |
Selects a random element from the slice. |
Reverse |
Reverses the order of elements in the slice. |
Shuffle |
Randomly rearranges the elements within the slice. |
Size |
Determines the size (length) of the slice. |
Max |
Identifies the maximum value in a numerical slice. |
Min |
Finds the minimum value in a numerical slice. |
Sum |
Calculates the sum of all elements in a numerical slice. |
Average |
Computes the average value of a numerical slice. |
Map |
Extracts a slice of values for a specified key. |
Date functions facilitate working with dates, including formatting, parsing, and manipulation.
Function | Description |
---|---|
Date |
Formats a timestamp into a specified format or returns a default datetime string. |
Day |
Extracts and returns the day of the month. |
Month |
Retrieves the month number from a date. |
MonthFull |
Returns the full month name from a date. |
Year |
Extracts and returns the year from a date. |
Week |
Returns the ISO week number of a date. |
Weekday |
Determines the day of the week from a date. |
TimeAgo |
Provides a human-readable string representing the time difference to the present. |
Number functions allows for the formatting of numbers for presentation and readability.
Function | Description |
---|---|
Number |
Formats any numeric value based on a specified format string. |
Bytes |
Converts a numeric value into a human-readable format representing bytes. |
Math functions include a variety of operations for numerical computation and manipulation.
Function | Description |
---|---|
Abs |
Calculates the absolute value of a number. |
AtLeast |
Ensures a number is at least a specified minimum. |
AtMost |
Ensures a number does not exceed a specified maximum. |
Round |
Rounds a number to a specified number of decimal places. |
Floor |
Rounds a number down to the nearest whole number. |
Ceil |
Rounds a number up to the nearest whole number. |
Plus |
Adds two numbers together. |
Minus |
Subtracts one number from another. |
Times |
Multiplies two numbers. |
Divide |
Divides one number by another, with handling for division by zero. |
Modulo |
Calculates the remainder of division of one number by another. |
Map functions provide utilities for extracting and manipulating data from maps, slices, and arrays.
Function | Description |
---|---|
Extract |
Retrieves a nested value from a map, slice, or array using a key path. |
Contributions to the filter
package are welcome. If you'd like to contribute, please follow the contribution guidelines.
This project is licensed under the MIT License - see the LICENSE file for details.