text

package module
v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 2024 License: MIT Imports: 3 Imported by: 0

README

text - String Utilities Package

The purpose of text is to provide string utilities, but with special considerations relating to web-service(s), cloud providers, and external APIs.

Interfacing with packages such as AWS and Stripe GO SDKs often require working with pointer inputs and outputs. Such cases can result in undesired, unexpected behavior. When writing API services, tracing issues can become difficult without warnings or general logging.

That's where the text package's variadic options are perhaps the most notable; configurations can be set that will log unexpected nil or zeroth input arguments.

Documentation

Official godoc documentation (with examples) can be found at the Package Registry.

Usage

Add Package Dependency
go get -u github.com/poly-gun/text
Import & Implement

main.go

package main

import (
    "github.com/poly-gun/text"
)

func main() {
    const example = "v-value"

    ptr := text.Pointer(example) // initialize ptr as a reference to example

    // --> optionally construct text.Options for logging
    ptr = text.Pointer(example, text.Variadic(o text.Options) {
        o.Log = true // output a warning if string is empty
    })

    ...
}

Contributions

See the Contributing Guide for additional details on getting started.

Documentation

Overview

Package text provides string utilities for the american-english language. Additional functions such as Pointer and Dereference are also provided.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Dereference

func Dereference(v *string, settings ...Variadic) string

Dereference dereferences the given string pointer and returns the string value it points to.

The function accepts optional settings of type Variadic that configure the behavior of dereferencing. If the pointer is nil and the [Options.Log] directive is true, a warning log will be printed and an empty string will be returned.

The function returns the string value of the pointer.

Example
package main

import (
	"fmt"

	"github.com/poly-gun/text"
)

func main() {
	// initialize a string pointer of underlying value "example"
	pointer := text.Pointer("example")

	// establish variable "v" of type string
	v := text.Dereference(pointer, func(o *text.Options) {
		o.Log = true // log if the pointer is nil
	})

	fmt.Println(v)

	// establish variable "v" of type string, without using functional options.
	// v = text.Dereference(pointer)

}
Output:

example

func Lowercase

func Lowercase(v string, settings ...Variadic) string

Lowercase will cast a string to all-lower casing.

Example
package main

import (
	"fmt"

	"github.com/poly-gun/text"
)

func main() {
	v := "Field-Name"

	fmt.Println(text.Lowercase(v))
}
Output:

field-name

func Pointer

func Pointer(v string, settings ...Variadic) *string

Pointer creates a pointer to the specified string value.

The function accepts optional settings of type Variadic that configures the behavior of the pointer creation. If the Log option in the settings is true and the value is an empty string, a warning log will be printed.

The function returns a pointer to the string parameter.

Example
package main

import (
	"fmt"

	"github.com/poly-gun/text"
)

func main() {
	// create a pointer of type string with reference value: "example"
	pointer := text.Pointer("example", func(o *text.Options) {
		o.Log = true // log if the string value is an empty string
	})

	fmt.Println(*(pointer))

	// create a pointer of type string with reference value: "example", without using functional options.
	// pointer = text.Pointer("example")

}
Output:

example

func Title

func Title(v string, settings ...Variadic) string

Title will cast a string to title casing. If no options are provided, cases.NoLower will be appended by default.

Example
package main

import (
	"fmt"

	"github.com/poly-gun/text"
)

func main() {
	// v represents a name that should be otherwise capitalized (titled)
	v := "jacob b. sanders"

	fmt.Println(text.Title(v))
}
Output:

Jacob B. Sanders

func Uppercase

func Uppercase(v string, settings ...Variadic) string

Uppercase will cast a string to an uppercase casing.

Types

type Options

type Options struct {
	// Log represents an optional flag that will log when potential, unexpected behavior could occur. E.g.
	// when using the [Dereference] function, log a warning that the pointer was nil. Defaults to false.
	Log bool

	// Language represents the language tag used in the [Options] struct. [Pointer] defaults to [language.AmericanEnglish].
	//
	//   - See [language.Tag] for type information.
	Language language.Tag

	// Options represents an array of [cases.Option]. These are only applicable to certain casing functions. Defaults to an empty array.
	Options []cases.Option
}

Options is the configuration structure optionally mutated via the Variadic constructor used throughout the package.

type Variadic

type Variadic func(o *Options)

Variadic represents a functional constructor for the Options type. Typical callers of Variadic won't need to perform nil checks as all implementations first construct an Options reference using packaged default(s).

Example
package main

import (
	"fmt"

	"github.com/poly-gun/text"
)

func main() {
	v := text.Dereference(nil, func(o *text.Options) {
		o.Log = true
	})

	pointer := text.Pointer("example")
	v = text.Dereference(pointer, func(o *text.Options) {
		o.Log = true
	})

	fmt.Println(v)
}
Output:

example

Jump to

Keyboard shortcuts

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