Documentation ¶
Overview ¶
Package censoring provides utilities for filtering strings.
Index ¶
Constants ¶
const (
DefaultCensorLabel string = "[***]"
)
DefaultCensorLabel is a constant that defines the default label used to replace unacceptable strings when they are filtered. Its default value is "[***]".
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder is a type that provides a fluent interface for constructing a censoring operation. It contains a slice of FilterFuncs, a separator string, a slice of values to be censored, a censor label, a Context, and a CensorValue indicating whether the string is not censored.
func (*Builder) Apply ¶
Apply is a method of the Builder type that applies a given function to the non-censored string representation of the Builder. It joins the Builder's values with the separator to create the non-censored string, and then passes this string to the given function. This method allows you to perform operations on the non-censored string, regardless of the current censor level.
func (*Builder) CensorMode ¶
func (b *Builder) CensorMode(mode CensorValue) *Builder
CensorMode sets the isNotCensored value of the Builder to the negation of the given mode. It returns the Builder itself to allow for method chaining, following the builder pattern.
func (*Builder) IsCensored ¶
func (b *Builder) IsCensored() CensorValue
IsCensored is a method of the Builder type that returns a CensorValue indicating whether the Builder is censored. If the Builder's Context is nil, or if either the Context's notCensored value or the Builder's isNotCensored value is false, it returns Censored. Otherwise, it returns NotCensored.
func (*Builder) SetLabel ¶
SetLabel sets the label of the Builder to the given label. If the given label is an empty string, it sets the label to the censorLabel of the Builder's Context. It returns the Builder itself to allow for method chaining, following the builder pattern.
func (Builder) String ¶
String is a method of the Builder type that returns a string representation of the Builder. If the Builder is not censored, it joins the values with the separator and returns the resulting string. If the Builder is censored, it creates a new slice of censored values, checks each value against the filters, and replaces unacceptable values with the censor label. It then joins the censored values with the separator and returns the resulting string.
type CensorValue ¶
type CensorValue bool
CensorValue is a type that represents whether a string has been censored or not. It is used in conjunction with the FilterFunc type to determine the censoring status of a string.
const ( Censored CensorValue = true NotCensored CensorValue = false )
Censored and NotCensored are constants of type CensorValue that represent the two possible censoring statuses. Censored represents a string that has been censored, while NotCensored represents a string that has not been censored.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is a type that encapsulates the context for a censoring operation. It contains a censorLabel, which is the label used to replace unacceptable strings, and a notCensored value, which indicates whether the string is censored or not.
func NewContext ¶
func NewContext() *Context
NewContext creates a new Context with default values. The default censorLabel is DefaultCensorLabel and the default notCensored value is false. It returns a pointer to the newly created Context.
func (*Context) CensorMode ¶
func (ctx *Context) CensorMode(mode CensorValue) *Context
CensorMode sets the notCensored value of the Context to the negation of the given mode. It returns the Context itself to allow for method chaining, following the builder pattern.
func (*Context) Make ¶
func (ctx *Context) Make(filters []FilterFunc, sep string, values ...any) *Builder
Make creates a new Builder with the given filters, separator, and values. It initializes the Builder's Context to the given Context, sets its isNotCensored value to false, and converts the given values to strings. If the given separator is not an empty string, it splits each value by the separator and appends the resulting fields to the values. It returns a pointer to the newly created Builder.
type FilterFunc ¶
FilterFunc is a function type that defines the filtering criteria for a string. It takes a string as input and returns a boolean indicating whether the string is acceptable or not.