/
strings.go
152 lines (121 loc) · 2.71 KB
/
strings.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
package strings
import (
"bytes"
"encoding/json"
"net/url"
"regexp"
"strings"
"time"
"github.com/dustin/go-humanize"
"github.com/russross/blackfriday"
"github.com/tdewolff/minify"
"github.com/tdewolff/minify/css"
"github.com/tdewolff/minify/html"
"github.com/tdewolff/minify/js"
)
/**
* Returns the minified version of a string
* You must set the mimetype manually
*/
func MinifyCode(mimetype string, code string) (string, error) {
m := minify.New()
m.AddFunc("text/css", css.Minify)
m.AddFunc("text/html", html.Minify)
m.AddFunc("text/javascript", js.Minify)
return m.String(mimetype, code)
}
/**
* Runs a string through blackfriday
*/
func Markdown(s string) string {
bits := []byte(s)
newBits := blackfriday.MarkdownCommon(bits)
return bytes.NewBuffer(newBits).String()
}
/**
* Converts float64 to an int (for comparison)
*/
func FloatToInt(n float64) int {
return int(n)
}
/**
* Passes a date string through the time package
* Order of arguments: layout, format, timezone
*/
func DateFormat(date string, args ...string) string {
t, err := time.Parse(args[0], date)
if err != nil {
return date
}
if len(args) > 2 {
loc, err := time.LoadLocation(args[2])
if err != nil {
return date
}
return t.In(loc).Format(args[1])
}
if args[1] == "humanize" {
return humanize.Time(t)
}
return t.Format(args[1])
}
/**
* Addition/Subtraction
*/
func Add(add int, initial int) int {
return initial + add
}
func Subtract(sub int, initial int) int {
return initial - sub
}
/**
* Replace a portion of a string
*/
func Replace(from, to, input string) string {
return strings.Replace(input, from, to, -1)
}
/**
* Regex functions
*/
func MatchRe(pattern string, input string) bool {
re := regexp.MustCompile(pattern)
return re.MatchString(input)
}
func FindRe(pattern string, input string) string {
re := regexp.MustCompile(pattern)
return re.FindString(input)
}
func FindSubRe(pattern string, input string) []string {
re := regexp.MustCompile(pattern)
return re.FindStringSubmatch(input)
}
func ReplaceRe(pattern string, to string, input string) string {
re := regexp.MustCompile(pattern)
return re.ReplaceAllString(input, to)
}
/**
* String conversions
*/
func ToLower(s string) string {
return strings.ToLower(s)
}
func ToUpper(s string) string {
return strings.ToUpper(s)
}
func Unescape(s string) (string, error) {
return url.QueryUnescape(s)
}
/**
* Trims characters from both sides
* Note: the cutset is required
*/
func Trim(cutset string, s string) string {
return strings.Trim(s, cutset)
}
/**
* Returns a marshaled JSON string
*/
func Marshal(d interface{}) string {
b, _ := json.Marshal(d)
return string(b)
}