-
Notifications
You must be signed in to change notification settings - Fork 32
/
spaces.go
108 lines (89 loc) · 2.04 KB
/
spaces.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
99
100
101
102
103
104
105
106
107
108
package processors
import (
"regexp"
"strings"
)
// RemoveNewLines removes newlines from string.
type RemoveNewLines struct{}
func (p RemoveNewLines) Name() string {
return "remove-newlines"
}
func (p RemoveNewLines) Alias() []string {
return []string{"remove-new-lines", "trim-newlines", "trim-new-lines"}
}
func (p RemoveNewLines) Transform(data []byte, f ...Flag) (string, error) {
separator := " "
for _, flag := range f {
if flag.Short == "s" {
x, ok := flag.Value.(string)
if ok {
separator = x
}
}
}
str := regexp.MustCompile(`[\r\n]+`).
ReplaceAllString(strings.TrimSpace(string(data)), separator)
return str, nil
}
func (p RemoveNewLines) Flags() []Flag {
return []Flag{
{
Name: "separator",
Short: "s",
Desc: "Separator to split multiple lines",
Value: "",
Type: FlagString,
},
}
}
func (p RemoveNewLines) Title() string {
return "Remove all new lines"
}
func (p RemoveNewLines) Description() string {
return "Remove all new lines"
}
func (p RemoveNewLines) FilterValue() string {
return p.Title()
}
// RemoveSpaces removes all the spaces from string.
type RemoveSpaces struct{}
func (p RemoveSpaces) Name() string {
return "remove-spaces"
}
func (p RemoveSpaces) Alias() []string {
return []string{"remove-space", "trim-spaces", "trim-space"}
}
func (p RemoveSpaces) Transform(data []byte, f ...Flag) (string, error) {
separator := ""
for _, flag := range f {
if flag.Short == "s" {
x, ok := flag.Value.(string)
if ok {
separator = x
}
}
}
str := regexp.MustCompile(`[\s\r\n]+`).
ReplaceAllString(strings.TrimSpace(string(data)), separator)
return str, nil
}
func (p RemoveSpaces) Flags() []Flag {
return []Flag{
{
Name: "separator",
Short: "s",
Desc: "Separator to split spaces",
Value: "",
Type: FlagString,
},
}
}
func (p RemoveSpaces) Title() string {
return "Remove all spaces + new lines"
}
func (p RemoveSpaces) Description() string {
return "Remove all spaces + new lines"
}
func (p RemoveSpaces) FilterValue() string {
return p.Title()
}