Skip to content

Commit b91804b

Browse files
committed
Initial commit of text
Add text repository which is based on github.com/homeport/gonvenience and now is its own standalone Go package.
0 parents  commit b91804b

File tree

8 files changed

+361
-0
lines changed

8 files changed

+361
-0
lines changed

.travis.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
---
2+
language: go
3+
go:
4+
- 1.12.x
5+
6+
install:
7+
- curl --silent --location https://goo.gl/g1CpPX | bash -s v1.0.7
8+
9+
script:
10+
- export GO111MODULE=on
11+
- go mod download
12+
- go mod verify
13+
- ginkgo -r -nodes 4 -randomizeAllSpecs -randomizeSuites -race -trace
14+
- staticcheck ./...
15+
- golint ./...

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 gonvenience
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# text
2+
3+
[![License](https://img.shields.io/github/license/gonvenience/text.svg)](https://github.com/gonvenience/text/blob/master/LICENSE)
4+
[![Go Report Card](https://goreportcard.com/badge/github.com/gonvenience/text)](https://goreportcard.com/report/github.com/gonvenience/text)
5+
[![Build Status](https://travis-ci.org/gonvenience/text.svg?branch=master)](https://travis-ci.org/gonvenience/text)
6+
[![GoDoc](https://godoc.org/github.com/gonvenience/text/pkg?status.svg)](https://godoc.org/github.com/gonvenience/text/pkg)
7+
[![Release](https://img.shields.io/github/release/gonvenience/text.svg)](https://github.com/gonvenience/text/releases/latest)
8+
9+
Golang package with convenience functions for strings

go.mod

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module github.com/gonvenience/text
2+
3+
go 1.12
4+
5+
require (
6+
github.com/gonvenience/bunt v1.0.0
7+
github.com/onsi/ginkgo v1.8.0
8+
github.com/onsi/gomega v1.5.0
9+
)

go.sum

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
github.com/DATA-DOG/go-sqlmock v1.3.3/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM=
2+
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
3+
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
4+
github.com/gonvenience/bunt v1.0.0 h1:+SozbZgVunILmyX/EXbUNz/LTulUZTo8vXlvHiWZNFM=
5+
github.com/gonvenience/bunt v1.0.0/go.mod h1:lsyhkmNpSAzhVx059BD0fQy5F29rWcS6AHb7UWNlT/s=
6+
github.com/gonvenience/term v1.0.0 h1:joCB/j0Ngmdakd3muuLgAGPMf7DNKdoe708c1I6RiBs=
7+
github.com/gonvenience/term v1.0.0/go.mod h1:wohD4Iqso9Eol7qc2VnNhSFFhZxok5PvO7pZhdrAn4E=
8+
github.com/hpcloud/tail v1.0.0 h1:nfCOvKYfkgYP8hkirhJocXT2+zOD8yUNjXaWfTlyFKI=
9+
github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU=
10+
github.com/lucasb-eyer/go-colorful v1.0.2 h1:mCMFu6PgSozg9tDNMMK3g18oJBX7oYGrC09mS6CXfO4=
11+
github.com/lucasb-eyer/go-colorful v1.0.2/go.mod h1:0MS4r+7BZKSJ5mw4/S5MPN+qHFF1fYclkSPilDOKW0s=
12+
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3 h1:BXxTozrOU8zgC5dkpn3J6NTRdoP+hjok/e+ACr4Hibk=
13+
github.com/mattn/go-ciede2000 v0.0.0-20170301095244-782e8c62fec3/go.mod h1:x1uk6vxTiVuNt6S5R2UYgdhpj3oKojXvOXauHZ7dEnI=
14+
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
15+
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
16+
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936 h1:kw1v0NlnN+GZcU8Ma8CLF2Zzgjfx95gs3/GN3vYAPpo=
17+
github.com/mitchellh/go-ps v0.0.0-20170309133038-4fdf99ab2936/go.mod h1:r1VsdOzOPt1ZSrGZWFoNhsAedKnEd6r9Np1+5blZCWk=
18+
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
19+
github.com/onsi/ginkgo v1.8.0 h1:VkHVNpR4iVnU8XQR6DBm8BqYjN7CRzw+xKUbVVbbW9w=
20+
github.com/onsi/ginkgo v1.8.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
21+
github.com/onsi/gomega v1.5.0 h1:izbySO9zDPmjJ8rDjLvkA2zJHIo+HkYXHnf7eN7SSyo=
22+
github.com/onsi/gomega v1.5.0/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
23+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
24+
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8 h1:1wopBVtVdWnn03fZelqdXTqk7U7zPQCb+T4rbU9ZEoU=
25+
golang.org/x/crypto v0.0.0-20190611184440-5c40567a22f8/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
26+
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
27+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3 h1:0GoQqolDA55aaLxZyTzK/Y2ePZzZTUrRacwib7cNsYQ=
28+
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
29+
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
30+
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
31+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
32+
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
33+
golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5/tI9ujCIVX+P5KiHuI=
34+
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
35+
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
36+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
37+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
38+
gopkg.in/fsnotify.v1 v1.4.7 h1:xOHLXZwVvI9hhs+cLKq5+I5onOuwQLhQwiu63xxlHs4=
39+
gopkg.in/fsnotify.v1 v1.4.7/go.mod h1:Tz8NjZHkW78fSQdbUxIjBTcgA1z1m8ZHf0WmKUhAMys=
40+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkepLTh2hOroT7a+7czfdQ=
41+
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw=
42+
gopkg.in/yaml.v2 v2.2.1 h1:mUhvW9EsL+naU5Q3cakzfE91YhliOondGd6ZrsDBHQE=
43+
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=

text.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// Copyright © 2019 The Homeport Team
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
/*
22+
Package text contains convenience functions for creating strings
23+
*/
24+
package text
25+
26+
import (
27+
"fmt"
28+
"math/rand"
29+
"strconv"
30+
"strings"
31+
"time"
32+
33+
"github.com/gonvenience/bunt"
34+
)
35+
36+
const chars = "abcdefghijklmnopqrstuvwxyz"
37+
38+
func init() {
39+
rand.Seed(time.Now().UTC().UnixNano())
40+
}
41+
42+
// RandomString creates a string with random content and a given length
43+
func RandomString(length int) string {
44+
if length < 0 {
45+
panic(fmt.Errorf("negative length value"))
46+
}
47+
48+
tmp := make([]byte, length)
49+
for i := range tmp {
50+
tmp[i] = chars[rand.Intn(len(chars))]
51+
}
52+
53+
return string(tmp)
54+
}
55+
56+
// RandomStringWithPrefix creates a string with the provided prefix and
57+
// additional random content so that the whole string has the given length.
58+
func RandomStringWithPrefix(prefix string, length int) string {
59+
if length < 0 {
60+
panic(fmt.Errorf("negative length value"))
61+
}
62+
63+
if len(prefix) > length {
64+
panic(fmt.Errorf("given prefix length exceeds given length"))
65+
}
66+
67+
return prefix + RandomString(length-len(prefix))
68+
}
69+
70+
// FixedLength expands or trims the given text to the provided length
71+
func FixedLength(text string, length int) string {
72+
textLength := bunt.PlainTextLength(text)
73+
74+
switch {
75+
case textLength < length: // padding required
76+
return text + strings.Repeat(" ", length-textLength)
77+
78+
case textLength > length:
79+
const ellipsis = " [...]"
80+
return bunt.Substring(text, 0, length-len(ellipsis)) + ellipsis
81+
82+
default:
83+
return text
84+
}
85+
}
86+
87+
// Plural returns a string with the number and noun in either singular or plural form.
88+
// If one text argument is given, the plural will be done with the plural s. If two
89+
// arguments are provided, the second text is the irregular plural. If more than two
90+
// are provided, then the additional ones are simply ignored.
91+
func Plural(amount int, text ...string) string {
92+
words := [...]string{"no", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "eleven", "twelve"}
93+
94+
var number string
95+
if amount < len(words) {
96+
number = words[amount]
97+
} else {
98+
number = strconv.Itoa(amount)
99+
}
100+
101+
switch len(text) {
102+
case 1:
103+
if amount == 1 {
104+
return fmt.Sprintf("%s %s", number, text[0])
105+
}
106+
107+
return fmt.Sprintf("%s %ss", number, text[0])
108+
109+
default:
110+
if amount == 1 {
111+
return fmt.Sprintf("%s %s", number, text[0])
112+
}
113+
114+
return fmt.Sprintf("%s %s", number, text[1])
115+
}
116+
}

text_suite_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Copyright © 2019 The Homeport Team
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package text_test
22+
23+
import (
24+
"testing"
25+
26+
. "github.com/onsi/ginkgo"
27+
. "github.com/onsi/gomega"
28+
)
29+
30+
func TestText(t *testing.T) {
31+
RegisterFailHandler(Fail)
32+
RunSpecs(t, "text suite")
33+
}

text_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// Copyright © 2019 The Homeport Team
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
package text_test
22+
23+
import (
24+
"fmt"
25+
26+
. "github.com/onsi/ginkgo"
27+
. "github.com/onsi/gomega"
28+
29+
. "github.com/gonvenience/text"
30+
. "github.com/gonvenience/bunt"
31+
)
32+
33+
var _ = Describe("Generate random strings with fixed length", func() {
34+
Context("Random string with no prefix", func() {
35+
It("should generate a random string with fixed length", func() {
36+
Expect(len(RandomString(32))).To(BeEquivalentTo(32))
37+
})
38+
39+
It("should fail when negative length is given", func() {
40+
defer func() {
41+
Expect(recover()).To(HaveOccurred())
42+
}()
43+
44+
RandomString(-1)
45+
})
46+
})
47+
48+
Context("Random string with given prefix", func() {
49+
It("should generate a random string with fixed length", func() {
50+
Expect(len(RandomStringWithPrefix("foobar", 32))).To(BeEquivalentTo(32))
51+
})
52+
53+
It("should fail when the prefix is already longer than the fixed length", func() {
54+
defer func() {
55+
Expect(recover()).To(HaveOccurred())
56+
}()
57+
58+
RandomStringWithPrefix("foobar", 4)
59+
})
60+
61+
It("should fail when negative length is given", func() {
62+
defer func() {
63+
Expect(recover()).To(HaveOccurred())
64+
}()
65+
66+
RandomStringWithPrefix("foobar", -1)
67+
})
68+
})
69+
70+
Context("Text with given fixed length", func() {
71+
It("should create a string with the text and enough padding to fill it up to the required length", func() {
72+
Expect(FixedLength("Foobar", 10)).To(BeEquivalentTo("Foobar "))
73+
})
74+
75+
It("should trim the text if the text alone exceeds the provided desired length", func() {
76+
Expect(FixedLength("This text is too long", 10)).To(BeEquivalentTo("This [...]"))
77+
})
78+
79+
It("should return the text as-is if it already has the perfect length", func() {
80+
Expect(FixedLength("Foobar", 6)).To(BeEquivalentTo("Foobar"))
81+
})
82+
83+
It("should work with text containing ANSI sequences", func() {
84+
// "This text is too long" 21 characters
85+
// "This text is [...]" 18 characters
86+
actual := FixedLength(Sprintf("*This* text is too long"), 18)
87+
expected := Sprintf("*This* text is [...]")
88+
89+
Expect(fmt.Sprintf("%#v", actual)).To(BeEquivalentTo(fmt.Sprintf("%#v", expected)))
90+
})
91+
})
92+
93+
Context("creating proper texts", func() {
94+
It("should return human readable plurals", func() {
95+
Expect(Plural(0, "foobar")).To(BeEquivalentTo("no foobars"))
96+
Expect(Plural(1, "foobar")).To(BeEquivalentTo("one foobar"))
97+
Expect(Plural(2, "foobar")).To(BeEquivalentTo("two foobars"))
98+
Expect(Plural(3, "foobar")).To(BeEquivalentTo("three foobars"))
99+
Expect(Plural(4, "foobar")).To(BeEquivalentTo("four foobars"))
100+
Expect(Plural(5, "foobar")).To(BeEquivalentTo("five foobars"))
101+
Expect(Plural(6, "foobar")).To(BeEquivalentTo("six foobars"))
102+
Expect(Plural(7, "foobar")).To(BeEquivalentTo("seven foobars"))
103+
Expect(Plural(8, "foobar")).To(BeEquivalentTo("eight foobars"))
104+
Expect(Plural(9, "foobar")).To(BeEquivalentTo("nine foobars"))
105+
Expect(Plural(10, "foobar")).To(BeEquivalentTo("ten foobars"))
106+
Expect(Plural(11, "foobar")).To(BeEquivalentTo("eleven foobars"))
107+
Expect(Plural(12, "foobar")).To(BeEquivalentTo("twelve foobars"))
108+
Expect(Plural(13, "foobar")).To(BeEquivalentTo("13 foobars"))
109+
Expect(Plural(147, "foobar")).To(BeEquivalentTo("147 foobars"))
110+
111+
Expect(Plural(1, "basis", "bases")).To(BeEquivalentTo("one basis"))
112+
Expect(Plural(2, "basis", "bases")).To(BeEquivalentTo("two bases"))
113+
})
114+
})
115+
})

0 commit comments

Comments
 (0)