-
Notifications
You must be signed in to change notification settings - Fork 0
/
itemtags.go
123 lines (99 loc) · 2.32 KB
/
itemtags.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
package slug
import (
"strings"
"unicode"
)
const DELIMITER = ","
// check for duplicate entries in a slice
func removeDuplicates(slice []string) []string {
var newSlice []string
encounteredItems := make(map[string]bool)
for _, val := range slice {
// new entry, add it to the found list
if _, found := encounteredItems[val]; !found {
encounteredItems[val] = true
newSlice = append(newSlice, val)
} else {
// found a duplicate
newSlice = append(newSlice, "-")
}
}
return newSlice
}
func getTagSlugListWithBlanks(tags string) []string {
var tagList []string
for _, tag := range strings.Split(tags, DELIMITER) {
tag := GetAsciiSlug(tag)
if len(tag) > 0 {
tagList = append(tagList, tag)
} else {
tagList = append(tagList, "-")
}
}
return tagList
}
func getPlainTagListWithBlanks(tags string) []string {
var tagList []string
for _, tag := range strings.Split(tags, DELIMITER) {
if len(tag) > 0 {
tagList = append(tagList, tag)
} else {
tagList = append(tagList, "-")
}
}
return tagList
}
func GetTagsAndTagSlugs(tags string) ([]string, []string) {
var tagList, slugList []string
sl := getTagSlugListWithBlanks(tags)
ta := getPlainTagListWithBlanks(tags)
// remove duplicates from slugs before loop
sl = removeDuplicates(sl)
for i := 0; i < len(sl); i++ {
if sl[i] != "-" && ta[i] != "-" {
tagList = append(tagList, ta[i])
slugList = append(slugList, sl[i])
}
}
return tagList, slugList
}
func IsItemTag(tag string) bool {
return isTagValid.MatchString(tag)
}
func IsUTF8ItemTag(sl string) bool {
var temp string
// check all the characters before deciding
for _, c := range sl {
if !unicode.IsLetter(c) && !isAsciiNumber.MatchString(string(c)) {
// not a letter or number, is it a hypthen?
if c == ' ' {
temp += " "
} else {
return false
}
} else {
// replace it with any letter
temp += "a"
}
}
return isTagValid.MatchString(temp)
}
func IsItemTagListRegex(tags string) bool {
return isTagListValid.MatchString(tags)
}
func IsItemTagList(tags string) bool {
for _, tag := range strings.Split(tags, DELIMITER) {
if !IsItemTag(tag) {
return false
}
}
return true
}
func IsUTF8ItemTagList(tags string) bool {
for _, tag := range strings.Split(tags, DELIMITER) {
if !IsUTF8ItemTag(tag) {
return false
}
}
return true
}