stringfy

package module
v0.0.0-...-14a29d4 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2021 License: MIT Imports: 5 Imported by: 2

README

Stringfy Build Status GoDoc

Stringfy is a string manipulation package for GO.


Install:

$ go get github.com/hgsigner/stringfy

Usage:

package main

import (
  	"fmt"
	"github.com/hgsigner/stringfy"
)

func main() {
	es := stringfy.Escape("São Paulo")
 	fmt.Prinln(es) // Sao Paulo
}

Functions:

CamelCase:
package main

import (
  	"fmt"
	"github.com/hgsigner/stringfy"
)

func main() {
	cc := stringfy.CamelCase("fizz_buzz_bazz")  
	fmt.Prinln(cc) // FizzBuzzBazz
}

Escape:
package main

import (
  	"fmt"
	"github.com/hgsigner/stringfy"
)

func main() {
	es := stringfy.Escape("São Paulo")
 	fmt.Prinln(es) // Sao Paulo
}

Parameterize:
package main

import (
  	"fmt"
	"github.com/hgsigner/stringfy"
)

func main() {
	pm := stringfy.Parameterize("São Paulo")
 	fmt.Prinln(pm) // sao-paulo
}

Underscore:
package main

import (
  	"fmt"
	"github.com/hgsigner/stringfy"
)

func main() {
	un := stringfy.Underscore("São Paulo")
 	fmt.Prinln(un) // sao_paulo
}

Plural:
package main

import (
  	"fmt"
	"github.com/hgsigner/stringfy"
)

func main() {
	// Default plural
	pl1 := stringfy.NewPlural()
 	fmt.Println(pl1.Perform(2, "octopus")) // 2 octopi
 	
 	// Custom plural
 	pl2 := stringfy.NewPlural()
 	pl2.Options(stringfy.AddPlural("timesz"))
 	fmt.Println(pl2.Perform(2, "timey")) // 2 timesz
}
Options:
stringfy.AddPlural(string)

Truncate:
package main

import (
  	"fmt"
	"github.com/hgsigner/stringfy"
)

func main() {
	text := "Lorem ipsum dolor sit amet, consectetur adipiscing elit."
 	
 	// Default truncate
 	tr1 := stringfy.NewTruncate()
 	tr1.Perform(text) // Lorem ipsum dolor sit amet,...

 	// Custom truncate
 	tr2 := stringfy.NewTruncate()
 	tr2.Options(stringfy.AddLength(17))
 	tr2.Perform(text) // Lorem ipsum do...

 	tr3 := stringfy.NewTruncate()
 	tr3.Options(stringfy.AddLength(13), stringfy.AddSeparator(" "))
 	tr3.Perform(text) // Lorem ipsum...
}
Options:
stringfy.AddLength(int)       // Default: 30

stringfy.AddOmission(string)  // Default: "..."

stringfy.AddSeparator(string) // Default: ""

Excerpt:
package main

import (
  	"fmt"
	"github.com/hgsigner/stringfy"
)

func main() {
	text := "Lorem ipsum dolor sit amet consectetur adipiscing elit."
 	
 	ex1 := stringfy.NewExcerpt()
 	ex1.Options(stringfy.AddRadius(4))
 	ex1.Perform(text, "sit") // ...lor sit ame...

 	ex2 := stringfy.NewExcerpt()
 	ex2.Options(stringfy.AddRadius(4), stringfy.AddOmission("(...)"))
 	ex2.Perform(text, "sit") // (...)lor sit ame(...)

 	ex3 := stringfy.NewExcerpt()
 	ex3.Options(stringfy.AddRadius(1), stringfy.AddSeparator(" "))
 	ex3.Perform(text, "sit") // ...dolor sit amet...
}
Options:
stringfy.AddRadius(int)       // Default: 100

stringfy.AddOmission(string)  // Default: "..."

stringfy.AddSeparator(string) // Default: ""
WordWrap:
package main

import (
  	"fmt"
	"github.com/hgsigner/stringfy"
)

func main() {
	text := "Lorem ipsum dolor sit amet, consectetur adipiscing elit sil."

	wwr1 := stringfy.NewWordWrap()
 	wwr1.Perform(text) // "Lorem ipsum dolor sit amet, consectetur adipiscing elit sil."

 	wwr2 := stringfy.NewWordWrap()
 	wwr2.Options(stringfy.AddLineWidth(10))
 	wwr2.Perform(text) // "Lorem\nipsum\ndolor sit\namet,\nconsectetur\nadipiscing\nelit sil."

 	wwr3 := stringfy.NewWordWrap()
 	wwr3.Options(stringfy.AddLineWidth(35))
 	wwr3.Perform(text) // "Lorem ipsum dolor sit amet,\nconsectetur adipiscing elit sil."
}
Options:
stringfy.AddLineWidth(int)       // Default: 80

For more information, please refer to the docs. Work in progress. More packages to come.


Licensing

This package is available as open source under the terms of the MIT License.

Documentation

Overview

Package stringfy is a simple string manipulation for Go.

Example of usage:

package main

import (
  "fmt"
  "github.com/hgsigner/stringfy"
)

func main() {
  es := stringfy.Escape("São Paulo")
  fmt.Prinln(es) // Sao Paulo

  cc := stringfy.CamelCase("fizz_buzz_bazz")
  fmt.Prinln(cc) // FizzBuzzBazz

  un := stringfy.Parameterize("São Paulo")
  fmt.Prinln(un) // sao-paulo

  un := stringfy.Underscore("São Paulo")
  fmt.Prinln(un) // sao_paulo

  p := stringfy.NewPlural()
  p.Perform(2, "octopus")
  fmt.Println(p) // 2 octopi
}

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CamelCase

func CamelCase(st string) string

CamelCase formats strings to CamalCase, escaping special characters from the text.

func Escape

func Escape(s string) string

Escape special characters from a text. Reads through the rules' table and mateches if a given character is present in the passed string. If so, it will replace it.

func Parameterize

func Parameterize(s string) string

Parameterize performs parameterization on a string.

func Underscore

func Underscore(s string) string

Underscore pattern on a string.

Types

type Excerpter

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

Excerpter struct

func NewExcerpt

func NewExcerpt() *Excerpter

NewExcerpt creates a new instace of the Excerpter struct with its defaults.

func (*Excerpter) Options

func (ex *Excerpter) Options(options ...interface{})

Options sets RadiusOption, OmissionOption and SeparatorOption

func (*Excerpter) Perform

func (ex *Excerpter) Perform(text, phrase string) (string, error)

Perform excerpt of a given text. Receices a text and a phrase.

type LengthOption

type LengthOption func(lengther)

LengthOption is a function that accepts interface lengther

func AddLength

func AddLength(l int) LengthOption

AddLength adds a custom length

type LineWidthOption

type LineWidthOption func(lineWidther)

LineWidthOption is a function that accepts interface lineWidther

func AddLineWidth

func AddLineWidth(lw int) LineWidthOption

AddLineWidth Adds a custom line width

type OmissionOption

type OmissionOption func(omissioner)

OmissionOption function that accepts interface omissioner

func AddOmission

func AddOmission(om string) OmissionOption

AddOmission adds a custom omission

type OptPlural

type OptPlural func(*PluralSet)

OptPlural is a type func(*PluralSet)

func AddPlural

func AddPlural(pl string) OptPlural

AddPlural adds custom plural to the word

type PluralSet

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

PluralSet struct

func NewPlural

func NewPlural() *PluralSet

NewPlural initializes the PluralSet struct

Example
package main

import (
	"fmt"

	"github.com/hgsigner/stringfy"
)

func main() {
	p1 := stringfy.NewPlural()
	fmt.Println(p1.Perform(2, "octopus"))

	p2 := stringfy.NewPlural()
	fmt.Println(p2.Perform(2, "post"))

	p3 := stringfy.NewPlural()
	fmt.Println(p3.Perform(2, "jeans"))

	//Custom plural
	pc := stringfy.NewPlural()
	pc.Options(stringfy.AddPlural("boatys"))
	fmt.Println(pc.Perform(2, "boat"))
}
Output:

2 octopi
2 posts
2 jeans
2 boatys

func (*PluralSet) Options

func (ps *PluralSet) Options(options ...OptPlural)

Options set the options passed to plural

func (*PluralSet) Perform

func (ps *PluralSet) Perform(count int, singular string) string

Perform the pluralization. It accepts a connt, a singular version of the word and, optionaly, the pluralized version of the word. E.g pluralize.AddPlural("boatys")

type RadiusOption

type RadiusOption func(radiuser)

RadiusOption is a function that accepts interface radiuser

func AddRadius

func AddRadius(rad int) RadiusOption

AddRadius adds a custom separator

type SeparatorOption

type SeparatorOption func(separatorer)

SeparatorOption is a function that accepts interface separatorer

func AddSeparator

func AddSeparator(sep string) SeparatorOption

AddSeparator adds a custom separator

type Truncater

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

Truncater struct

func NewTruncate

func NewTruncate() *Truncater

NewTruncate creates a new instance of the Truncater struct if its defaults.

func (*Truncater) Options

func (t *Truncater) Options(options ...interface{})

Options sets LengthOption, OmissionOption and SeparatorOption

func (*Truncater) Perform

func (t *Truncater) Perform(text string) string

Perform the truncation of a given text.

type WordWraper

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

WordWraper struct

func NewWordWrap

func NewWordWrap() *WordWraper

NewWordWrap creates a new instance of the WordWraper struct if its defaults.

func (*WordWraper) Options

func (wwr *WordWraper) Options(options ...interface{})

Options sets LineWidthOption

func (*WordWraper) Perform

func (wwr *WordWraper) Perform(text string) string

Perform the wraping of a given text.

Jump to

Keyboard shortcuts

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