-
-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathlorem.go
More file actions
84 lines (69 loc) · 1.87 KB
/
Copy pathlorem.go
File metadata and controls
84 lines (69 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package faker
import (
"strings"
)
// Lorem is a faker struct for Lorem
type Lorem struct {
Faker *Faker
}
// Word returns a fake word for Lorem
func (l Lorem) Word() string {
index := l.Faker.IntBetween(0, len(englishWords)-1)
return englishWords[index]
}
// Words returns fake words for Lorem
func (l Lorem) Words(nbWords int) []string {
words := make([]string, 0, nbWords)
for i := 0; i < nbWords; i++ {
words = append(words, l.Word())
}
return words
}
// Sentence returns a fake sentence for Lorem
func (l Lorem) Sentence(nbWords int) string {
return strings.Join(l.Words(nbWords), " ") + "."
}
// Sentences returns fake sentences for Lorem
func (l Lorem) Sentences(nbSentences int) []string {
sentences := make([]string, 0, nbSentences)
for i := 0; i < nbSentences; i++ {
sentences = append(sentences, l.Sentence(l.Faker.RandomNumber(2)))
}
return sentences
}
// Paragraph returns a fake paragraph for Lorem
func (l Lorem) Paragraph(nbSentences int) string {
return strings.Join(l.Sentences(nbSentences), " ")
}
// Paragraphs returns fake paragraphs for Lorem
func (l Lorem) Paragraphs(nbParagraph int) []string {
out := make([]string, 0, nbParagraph)
for i := 0; i < nbParagraph; i++ {
out = append(out, l.Paragraph(l.Faker.RandomNumber(2)))
}
return out
}
// Text returns a fake text for Lorem using randomly selected words up to maxNbChars.
func (l Lorem) Text(maxNbChars int) string {
if maxNbChars <= 0 {
return ""
}
var builder strings.Builder
for builder.Len() < maxNbChars {
word := l.Word()
if builder.Len() > 0 {
if builder.Len()+1+len(word) > maxNbChars {
break
}
builder.WriteByte(' ')
} else if len(word) > maxNbChars {
return word[:maxNbChars]
}
builder.WriteString(word)
}
return builder.String()
}
// Bytes returns fake bytes for Lorem
func (l Lorem) Bytes(maxNbChars int) (out []byte) {
return []byte(l.Text(maxNbChars))
}