strings

package
v0.0.0-...-c864ae1 Latest Latest
Warning

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

Go to latest
Published: Apr 13, 2023 License: BSD-2-Clause, BSD-3-Clause, MIT Imports: 6 Imported by: 0

Documentation

Overview

Package strings offers an extension of functionality to the default golang strings package.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseCaster

type BaseCaster struct {
	// contains filtered or unexported fields
}

BaseCaster allows strings to be cast into other base types where possible. With a fluent interface, it allows the caller fine-tuned control over how strings are cast to other base-types.

func (*BaseCaster) Cast

func (bs *BaseCaster) Cast(valueAsString string) interface{}

Cast attempts to convert the value supplied as a string to another Golang base type, depending on how the BaseCaster has been configured. If it cannot, the value is returned with its original string type.

Example
caster := new(BaseCaster).WithNumbersAsFloats()

fortyTwoAsString := "42"
sevenAsString := "7"

fortyTwo, fortyTwoIsFloat := caster.Cast(fortyTwoAsString).(float64)
seven, sevenIsFloat := caster.Cast(sevenAsString).(float64)

var actualResult float64
if sevenIsFloat && fortyTwoIsFloat {
	actualResult = fortyTwo / seven
}

fmt.Printf("%v", actualResult)
Output:

6

func (*BaseCaster) WithNumbersAsFloats

func (bs *BaseCaster) WithNumbersAsFloats() *BaseCaster

WithNumbersAsFloats attempts to convert all numbers to floating point number base type.

type Converter

type Converter struct {
	// contains filtered or unexported fields
}

func NewConverter

func NewConverter() *Converter

func (*Converter) Convert

func (c *Converter) Convert(value interface{}) string

func (*Converter) Localised

func (c *Converter) Localised() *Converter

func (*Converter) NotPaddingZeros

func (c *Converter) NotPaddingZeros() *Converter

func (*Converter) PaddingZeros

func (c *Converter) PaddingZeros() *Converter

func (*Converter) QuotingStrings

func (c *Converter) QuotingStrings() *Converter

func (*Converter) WithFloatingPointPrecision

func (c *Converter) WithFloatingPointPrecision(precision int) *Converter

type FluentBuilder

type FluentBuilder struct {
	strings.Builder
}

FluentBuilder offers a 'fluent' interface (see https://en.wikipedia.org/wiki/Fluent_interface) to the golang strings.Builder, allowing us to chain .Add() calls

func (*FluentBuilder) Add

func (fb *FluentBuilder) Add(strings ...string) *FluentBuilder

Add appends the contents of each supplied strings to its buffer, returning a reference to the FluentBuilder, allowing a chaining of a number of Add() calls

Example
PetsAndFoods := new(FluentBuilder).Add("cat", "-dog-", "canary, ").Add("pie", "-cake-", "tart").String()
fmt.Print(PetsAndFoods)
Output:

cat-dog-canary, pie-cake-tart

func (*FluentBuilder) AddIf

func (fb *FluentBuilder) AddIf(shouldAdd bool, strings ...string) *FluentBuilder

AddIf appends the contents of each supplied strings to its buffer, returning a reference to the FluentBuilder, only when shouldAdd is true, allowing a chaining of a number of Add() calls

Jump to

Keyboard shortcuts

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