Censoring

package
v0.2.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 25, 2024 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package censoring provides utilities for filtering strings.

Index

Constants

View Source
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

func (b *Builder) Apply(f func(s string))

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) 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) String

func (b *Builder) String() 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 BuilderOption added in v0.2.7

type BuilderOption func(*Builder)

BuilderOption is a function type that modifies the properties of a Builder.

func WithFilters added in v0.2.7

func WithFilters(filters ...FilterFunc) BuilderOption

WithFilters returns a BuilderOption that sets the filters of a Builder. Filters are functions that modify the output string of the Builder.

func WithLabel added in v0.2.7

func WithLabel(label string) BuilderOption

WithLabel returns a BuilderOption that sets the label of a Builder. If the provided label is not an empty string, it replaces the current label of the Builder. The label is trimmed of any leading or trailing white space before being set.

func WithMode added in v0.2.7

func WithMode(mode CensorValue) BuilderOption

WithMode returns a BuilderOption that sets the censorship mode of a Builder. If the provided mode is true, the Builder will not censor values.

func WithSeparator added in v0.2.7

func WithSeparator(sep rune) BuilderOption

WithSeparator returns a BuilderOption that sets the separator of a Builder. The separator is used to separate values in the output string.

func WithValues added in v0.2.7

func WithValues(values ...any) BuilderOption

WithValues returns a BuilderOption that sets the values of a Builder. Values can be of any type and are converted to strings before being set. If a value is a Builder, its output string is used as the value.

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) Make

func (ctx *Context) Make(options ...BuilderOption) *Builder

Make creates a new Builder with the given BuilderOptions. It initializes the Builder's Context to the given Context, sets its label to DefaultCensorLabel, and initializes its filters and values to empty slices. It also sets the separator to a space character. Each provided BuilderOption is then applied to the Builder in the order they were provided. After applying the BuilderOptions, it splits each value in the Builder's values slice by the Builder's separator. It then replaces the original values with the resulting fields. This operation is performed for each value in the Builder's values slice.

It returns a pointer to the newly created Builder.

func (*Context) WithLabel added in v0.2.7

func (ctx *Context) WithLabel(label string) *Context

WithLabel sets the censorLabel of the Context to the given label. If the given label is an empty string, the censorLabel is set to DefaultCensorLabel. It returns the Context itself to allow for method chaining, following the builder pattern.

func (*Context) WithMode added in v0.2.7

func (ctx *Context) WithMode(mode CensorValue) *Context

WithMode 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.

type FilterFunc

type FilterFunc func(string) bool

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL