Skip to content

Commit

Permalink
Revamp str package with new funcs; tests fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirvivien committed May 2, 2021
1 parent fdc0b0f commit dc21c26
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 39 deletions.
38 changes: 38 additions & 0 deletions examples/filesys/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"bytes"
"fmt"
"os"

"github.com/vladimirvivien/echo"
"github.com/vladimirvivien/echo/str"
)


// This example uses local git to create a file with commit logs.
func main() {
buf := new(bytes.Buffer)
cmd := `/bin/sh -c "git log --reverse --abbrev-commit --pretty=oneline | cut -d ' ' -f1"`
for _, p := range str.SplitLines(echo.Run(cmd)) {
echo.SetVar("patch", p)
cmd := `/bin/sh -c "git show --abbrev-commit -s --pretty=format:'%h %s (%an) %n' ${patch}"`
buf.WriteString(fmt.Sprintf("%s\n",echo.Run(cmd)))
}

gitfile := "./gitlog.txt"

if w := echo.Write(gitfile).ReadFrom(buf); w.Err() != nil {
fmt.Println(w.Err())
os.Exit(1)
}

// read the file and print
fmt.Println(echo.Read(gitfile).String())

if err := os.Remove(gitfile); err != nil {
fmt.Println(err)
os.Exit(1)
}
}

52 changes: 52 additions & 0 deletions str/functions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package str

import (
"fmt"
)

func IsEmpty(str string) bool {
return String(str).IsEmpty()
}

// SplitLines splits each line from str into []string
func SplitLines(str string) []string {
return String(str).SplitLines()
}

// SplitSpaces splits str by blank chars (space,\t,\n)
func SplitSpaces(str string) []string {
return String(str).SplitSpaces()
}

// Bool returns the bool equivalent of str ("true" = true, etc)
// A parsing error will cause a program panic.
func Bool(str string) bool {
s := String(str)
result := s.Bool()
if s.Err() != nil {
panic(fmt.Sprintf("%s", s.Err()))
}
return result
}

// Int returns the int representation of str.
// A parsing error will cause a program panic.
func Int(str string) int {
s := String(str)
result := s.Int()
if s.Err() != nil {
panic(fmt.Sprintf("%s", s.Err()))
}
return result
}

// Float64 returns the float64 representation of str.
// A parsing error will cause a program panic.
func Float64(str string) float64 {
s := String(str)
result := s.Float64()
if s.Err() != nil {
panic(fmt.Sprintf("%s", s.Err()))
}
return result
}
61 changes: 33 additions & 28 deletions strings/functions.go → str/strings.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package strings
package str

import (
"bytes"
Expand All @@ -12,62 +12,67 @@ var (
notSpaceRegex = regexp.MustCompile(`\S`)
)

type String struct {
type Str struct {
val string
err error
}

func New(str string) *String {
return &String{val:str}
func String(str string) *Str {
return &Str{val: str}
}

// String returns the string value
func (s *String) String() string {
func (s *Str) String() string {
return s.val
}

// Err returns any captured error
func (s *String) Err() error {
func (s *Str) Err() error {
return s.err
}


// IsEmpty returns true if len(s) == 0
func (s *String) IsEmpty() bool {
func (s *Str) IsEmpty() bool {
return s.val == ""
}

// Streq returns true if both strings are equal
func (s *String) Eq(val1 string) bool {
func (s *Str) Eq(val1 string) bool {
return strings.EqualFold(s.val,val1)
}


// Split s.val using the sep as delimiter
func (s *String) Split(sep string) []string {
func (s *Str) Split(sep string) []string {
return strings.Split(s.val, sep)
}

// SplitLines splits s.val using \n as delimiter
func (s *Str) SplitLines() []string {
return strings.Split(s.val, "\n")
}

// SplitSpaces properly splits s.val into []elements
// separated by one or more Unicode.IsSpace characters
// i.e. SplitSpaces("ab cd e\tf\ng") returns 5 elements
func (s *String) SplitSpaces() []string{
func (s *Str) SplitSpaces() []string{
return notSpaceRegex.Split(s.val, -1)
}

func (s *String) SplitRegex(exp string) []string {
func (s *Str) SplitRegex(exp string) []string {
return regexp.MustCompile(exp).Split(s.val, -1)
}


// Bytes returns []byte(s.val)
func (s *String) Bytes() []byte {
func (s *Str) Bytes() []byte {
return []byte(s.val)
}

// ToBool converts s.val from string to a bool representation
// Check s.Error() for parsing errors
func (s *String) Bool() bool {
// Check s.Err() for parsing errors
func (s *Str) Bool() bool {
val, err := strconv.ParseBool(s.val)
if err != nil {
s.err = err
Expand All @@ -76,8 +81,8 @@ func (s *String) Bool() bool {
}

// ToInt converts s.val from string to a int representation
// Check s.Error() for parsing errors
func (s *String) Int() int {
// Check s.Err() for parsing errors
func (s *Str) Int() int {
val, err := strconv.Atoi(s.val)
if err != nil {
s.err = err
Expand All @@ -87,77 +92,77 @@ func (s *String) Int() int {

// ToFloat converts s.val from string to a float64 representation
// Check s.Error() for parsing errors
func (s *String) Float64() float64 {
func (s *Str) Float64() float64 {
val, err := strconv.ParseFloat(s.val, 64)
if err != nil {
s.err = err
}
return val
}

func (s *String) Reader() io.Reader {
func (s *Str) Reader() io.Reader {
return bytes.NewReader([]byte(s.val))
}


// Lower returns val as lower case
func (s *String) ToLower() *String {
func (s *Str) ToLower() *Str {
s.val = strings.ToLower(s.val)
return s
}

// Upper returns val as upper case
func (s *String) ToUpper() *String {
func (s *Str) ToUpper() *Str {
s.val = strings.ToUpper(s.val)
return s
}

func(s *String) ToTitle() *String {
func(s *Str) ToTitle() *Str {
s.val = strings.ToTitle(s.val)
return s
}

// Trim removes spaces around a val
func (s *String) TrimSpaces() *String {
func (s *Str) TrimSpaces() *Str {
s.val = strings.TrimSpace(s.val)
return s
}

// TrimLeft removes each character in cutset at the
// start of s.val
func (s *String) TrimLeft(cutset string) *String {
func (s *Str) TrimLeft(cutset string) *Str {
s.val = strings.TrimLeft(s.val, cutset)
return s
}

// TrimRight removes each character in cutset removed at the
// start of s.val
func (s *String) TrimRight(cutset string) *String {
func (s *Str) TrimRight(cutset string) *Str {
s.val = strings.TrimRight(s.val, cutset)
return s
}

// Trim removes each character in cutset from around s.val
func (s *String) Trim(cutset string) *String {
func (s *Str) Trim(cutset string) *Str {
s.val = strings.Trim(s.val, cutset)
return s
}

// ReplaceAll replaces all occurrences of old with new in s.val
func (s *String) ReplaceAll(old, new string) *String {
func (s *Str) ReplaceAll(old, new string) *Str {
s.val = strings.ReplaceAll(s.val, old, new)
return s
}

// Concat concatenates val1 to s.val
func (s *String) Concat(vals...string) *String {
func (s *Str) Concat(vals...string) *Str {
s.val = strings.Join(append([]string{s.val}, vals...), "")
return s
}

// CopyTo copies s.val unto dest
// Check s.Error() for copy error.
func (s *String) CopyTo(dest io.Writer) * String {
func (s *Str) CopyTo(dest io.Writer) *Str {
if _, err := io.Copy(dest, bytes.NewBufferString(s.val)); err != nil {
s.err = err
}
Expand Down
22 changes: 11 additions & 11 deletions strings/functions_test.go → str/strings_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package strings
package str

import (
"bytes"
Expand All @@ -25,7 +25,7 @@ func TestString_Concat(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T){
s := New("Foo").Concat(test.items...)
s := String("Foo").Concat(test.items...)
if s.String() != test.expected {
t.Errorf("expecting %s, got %s", test.expected, s)
}
Expand All @@ -35,51 +35,51 @@ func TestString_Concat(t *testing.T) {

func TestString_CopyTo(t *testing.T) {
var b bytes.Buffer
New("HelloWorld").CopyTo(&b)
String("HelloWorld").CopyTo(&b)
if b.String() != "HelloWorld" {
t.Fatal("CopyTo failed")
}

}

func TestString_Eq(t *testing.T) {
if !New("Hello, 世界").Eq("Hello, 世界") {
if !String("Hello, 世界").Eq("Hello, 世界") {
t.Fatal("Eq failed")
}
}

func TestString_IsEmpty(t *testing.T) {
if New("Hello").IsEmpty() {
if String("Hello").IsEmpty() {
t.Fatal("IsEmpty failed")
}
if !New("").IsEmpty() {
if !String("").IsEmpty() {
t.Fatal("IsEmpty failed")
}
}

func TestString_Reader(t *testing.T) {
b := new(bytes.Buffer)
b.ReadFrom(New("Hello").Reader())
b.ReadFrom(String("Hello").Reader())
if b.String() != "Hello" {
t.Fatal("Reader failed")
}
}

func TestString_ReplaceAll(t *testing.T) {
s := New("lieave in oeane hourea").ReplaceAll("ea","")
s := String("lieave in oeane hourea").ReplaceAll("ea","")
if s.String() != "live in one hour" {
t.Fatal("ReplaceAll failed")
}
}

func TestString_ToBool(t *testing.T) {
if New("true").Bool() != true {
if String("true").Bool() != true {
t.Error("ToBool true unexpected failure")
}
if New("false").Bool() != false {
if String("false").Bool() != false {
t.Error("ToBool false unexpected failure")
}
s := New("foo")
s := String("foo")
_ = s.Bool()
if s.Err() == nil {
t.Error("ToBool expecting error, but got none")
Expand Down

0 comments on commit dc21c26

Please sign in to comment.