-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtext.go
98 lines (90 loc) · 2.7 KB
/
text.go
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
// Copyright 2019 by David A. Golden. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
package jfdi
import (
"strings"
"github.com/icrowley/fake"
)
// Word returns a generator that produces a randomly-chosen latin word.
func Word() Generator {
f := Words(1)
return func(c *Context) interface{} {
if c == nil {
c = NewContext()
}
return f(c).([]string)[0]
}
}
// Words returns a generator that produces a slice of randomly-chosen latin word
// of a given length. The argument must be an integer or a generator of
// integers. If the length is negative or zero, the generator panics.
func Words(n interface{}) Generator {
return func(c *Context) interface{} {
if c == nil {
c = NewContext()
}
length, ok := toInt(c, n)
if !ok || length < 0 {
panic("length must be a non-negative int or generate a non-negative int")
}
output := make([]string, length)
for i := 0; i < length; i++ {
output[i] = fake.Word()
}
return output
}
}
// Sentence returns a generator that produces a randomly-generated 'latin sentence'.
func Sentence() Generator {
f := Sentences(1)
return func(c *Context) interface{} {
if c == nil {
c = NewContext()
}
return f(c).([]string)[0]
}
}
// Sentences returns a generator that produces a slice of randomly-generated
// latin 'sentences' of a given slice length. The argument must be an integer
// or a generator of integers. If the length is negative or zero, the
// generator panics.
func Sentences(n interface{}) Generator {
return func(c *Context) interface{} {
if c == nil {
c = NewContext()
}
length, ok := toInt(c, n)
if !ok || length < 0 {
panic("length must be a non-negative int or generate a non-negative int")
}
output := make([]string, length)
for i := 0; i < length; i++ {
output[i] = strings.Title(fake.Word()) + fake.Sentence()
}
return output
}
}
// Join returns a generator that joins a slice of strings with a separator
// string. The first argument must be a slice of string or a generator of
// them; the second argument must be a string or a generator of one. The
// Generator panics if either argument produces an invalid type.
func Join(inputs interface{}, separator interface{}) Generator {
return func(c *Context) interface{} {
if c == nil {
c = NewContext()
}
sep, ok := toStr(c, separator)
if !ok {
panic("separator must be or generate a string")
}
xs := expand(c, inputs)
ys, ok := xs.([]string)
if !ok {
panic("inputs must be or generate a slice of string")
}
return strings.Join(ys, sep)
}
}