Skip to content

Commit

Permalink
Initial commit of text
Browse files Browse the repository at this point in the history
Add text repository which is based on github.com/homeport/gonvenience
and now is its own standalone Go package.
  • Loading branch information
HeavyWombat committed Jun 17, 2019
0 parents commit b91804b
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
language: go
go:
- 1.12.x

install:
- curl --silent --location https://goo.gl/g1CpPX | bash -s v1.0.7

script:
- export GO111MODULE=on
- go mod download
- go mod verify
- ginkgo -r -nodes 4 -randomizeAllSpecs -randomizeSuites -race -trace
- staticcheck ./...
- golint ./...
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 gonvenience

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# text

[![License](https://img.shields.io/github/license/gonvenience/text.svg)](https://github.com/gonvenience/text/blob/master/LICENSE)
[![Go Report Card](https://goreportcard.com/badge/github.com/gonvenience/text)](https://goreportcard.com/report/github.com/gonvenience/text)
[![Build Status](https://travis-ci.org/gonvenience/text.svg?branch=master)](https://travis-ci.org/gonvenience/text)
[![GoDoc](https://godoc.org/github.com/gonvenience/text/pkg?status.svg)](https://godoc.org/github.com/gonvenience/text/pkg)
[![Release](https://img.shields.io/github/release/gonvenience/text.svg)](https://github.com/gonvenience/text/releases/latest)

Golang package with convenience functions for strings
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/gonvenience/text

go 1.12

require (
github.com/gonvenience/bunt v1.0.0
github.com/onsi/ginkgo v1.8.0
github.com/onsi/gomega v1.5.0
)
43 changes: 43 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/gonvenience/bunt v1.0.0 h1:+SozbZgVunILmyX/EXbUNz/LTulUZTo8vXlvHiWZNFM=
github.com/gonvenience/bunt v1.0.0/go.mod h1:lsyhkmNpSAzhVx059BD0fQy5F29rWcS6AHb7UWNlT/s=
github.com/gonvenience/term v1.0.0 h1:joCB/j0Ngmdakd3muuLgAGPMf7DNKdoe708c1I6RiBs=
github.com/gonvenience/term v1.0.0/go.mod h1:wohD4Iqso9Eol7qc2VnNhSFFhZxok5PvO7pZhdrAn4E=
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
github.com/lucasb-eyer/go-colorful v1.0.2 h1:mCMFu6PgSozg9tDNMMK3g18oJBX7oYGrC09mS6CXfO4=
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 h1:BXxTozrOU8zgC5dkpn3J6NTRdoP+hjok/e+ACr4Hibk=
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3/go.mod h1:x1uk6vxTiVuNt6S5R2UYgdhpj3oKojXvOXauHZ7dEnI=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936 h1:kw1v0NlnN+GZcU8Ma8CLF2Zzgjfx95gs3/GN3vYAPpo=
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936/go.mod h1:r1VsdOzOPt1ZSrGZWFoNhsAedKnEd6r9Np1+5blZCWk=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXTqk7U7zPQCb+T4rbU9ZEoU=
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
116 changes: 116 additions & 0 deletions text.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright © 2019 The Homeport Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

/*
Package text contains convenience functions for creating strings
*/
package text

import (
"fmt"
"math/rand"
"strconv"
"strings"
"time"

"github.com/gonvenience/bunt"
)

const chars = "abcdefghijklmnopqrstuvwxyz"

func init() {
rand.Seed(time.Now().UTC().UnixNano())
}

// RandomString creates a string with random content and a given length
func RandomString(length int) string {
if length < 0 {
panic(fmt.Errorf("negative length value"))
}

tmp := make([]byte, length)
for i := range tmp {
tmp[i] = chars[rand.Intn(len(chars))]
}

return string(tmp)
}

// RandomStringWithPrefix creates a string with the provided prefix and
// additional random content so that the whole string has the given length.
func RandomStringWithPrefix(prefix string, length int) string {
if length < 0 {
panic(fmt.Errorf("negative length value"))
}

if len(prefix) > length {
panic(fmt.Errorf("given prefix length exceeds given length"))
}

return prefix + RandomString(length-len(prefix))
}

// FixedLength expands or trims the given text to the provided length
func FixedLength(text string, length int) string {
textLength := bunt.PlainTextLength(text)

switch {
case textLength < length: // padding required
return text + strings.Repeat(" ", length-textLength)

case textLength > length:
const ellipsis = " [...]"
return bunt.Substring(text, 0, length-len(ellipsis)) + ellipsis

default:
return text
}
}

// Plural returns a string with the number and noun in either singular or plural form.
// If one text argument is given, the plural will be done with the plural s. If two
// arguments are provided, the second text is the irregular plural. If more than two
// are provided, then the additional ones are simply ignored.
func Plural(amount int, text ...string) string {
words := [...]string{"no", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"}

var number string
if amount < len(words) {
number = words[amount]
} else {
number = strconv.Itoa(amount)
}

switch len(text) {
case 1:
if amount == 1 {
return fmt.Sprintf("%s %s", number, text[0])
}

return fmt.Sprintf("%s %ss", number, text[0])

default:
if amount == 1 {
return fmt.Sprintf("%s %s", number, text[0])
}

return fmt.Sprintf("%s %s", number, text[1])
}
}
33 changes: 33 additions & 0 deletions text_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright © 2019 The Homeport Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package text_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestText(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "text suite")
}
115 changes: 115 additions & 0 deletions text_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// Copyright © 2019 The Homeport Team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package text_test

import (
"fmt"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"

. "github.com/gonvenience/text"
. "github.com/gonvenience/bunt"
)

var _ = Describe("Generate random strings with fixed length", func() {
Context("Random string with no prefix", func() {
It("should generate a random string with fixed length", func() {
Expect(len(RandomString(32))).To(BeEquivalentTo(32))
})

It("should fail when negative length is given", func() {
defer func() {
Expect(recover()).To(HaveOccurred())
}()

RandomString(-1)
})
})

Context("Random string with given prefix", func() {
It("should generate a random string with fixed length", func() {
Expect(len(RandomStringWithPrefix("foobar", 32))).To(BeEquivalentTo(32))
})

It("should fail when the prefix is already longer than the fixed length", func() {
defer func() {
Expect(recover()).To(HaveOccurred())
}()

RandomStringWithPrefix("foobar", 4)
})

It("should fail when negative length is given", func() {
defer func() {
Expect(recover()).To(HaveOccurred())
}()

RandomStringWithPrefix("foobar", -1)
})
})

Context("Text with given fixed length", func() {
It("should create a string with the text and enough padding to fill it up to the required length", func() {
Expect(FixedLength("Foobar", 10)).To(BeEquivalentTo("Foobar "))
})

It("should trim the text if the text alone exceeds the provided desired length", func() {
Expect(FixedLength("This text is too long", 10)).To(BeEquivalentTo("This [...]"))
})

It("should return the text as-is if it already has the perfect length", func() {
Expect(FixedLength("Foobar", 6)).To(BeEquivalentTo("Foobar"))
})

It("should work with text containing ANSI sequences", func() {
// "This text is too long" 21 characters
// "This text is [...]" 18 characters
actual := FixedLength(Sprintf("*This* text is too long"), 18)
expected := Sprintf("*This* text is [...]")

Expect(fmt.Sprintf("%#v", actual)).To(BeEquivalentTo(fmt.Sprintf("%#v", expected)))
})
})

Context("creating proper texts", func() {
It("should return human readable plurals", func() {
Expect(Plural(0, "foobar")).To(BeEquivalentTo("no foobars"))
Expect(Plural(1, "foobar")).To(BeEquivalentTo("one foobar"))
Expect(Plural(2, "foobar")).To(BeEquivalentTo("two foobars"))
Expect(Plural(3, "foobar")).To(BeEquivalentTo("three foobars"))
Expect(Plural(4, "foobar")).To(BeEquivalentTo("four foobars"))
Expect(Plural(5, "foobar")).To(BeEquivalentTo("five foobars"))
Expect(Plural(6, "foobar")).To(BeEquivalentTo("six foobars"))
Expect(Plural(7, "foobar")).To(BeEquivalentTo("seven foobars"))
Expect(Plural(8, "foobar")).To(BeEquivalentTo("eight foobars"))
Expect(Plural(9, "foobar")).To(BeEquivalentTo("nine foobars"))
Expect(Plural(10, "foobar")).To(BeEquivalentTo("ten foobars"))
Expect(Plural(11, "foobar")).To(BeEquivalentTo("eleven foobars"))
Expect(Plural(12, "foobar")).To(BeEquivalentTo("twelve foobars"))
Expect(Plural(13, "foobar")).To(BeEquivalentTo("13 foobars"))
Expect(Plural(147, "foobar")).To(BeEquivalentTo("147 foobars"))

Expect(Plural(1, "basis", "bases")).To(BeEquivalentTo("one basis"))
Expect(Plural(2, "basis", "bases")).To(BeEquivalentTo("two bases"))
})
})
})

0 comments on commit b91804b

Please sign in to comment.