-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.go
169 lines (122 loc) · 3.65 KB
/
sort.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package internal
import (
"sort"
discord "github.com/WelcomerTeam/Discord/discord"
"github.com/lithammer/fuzzysearch/fuzzy"
)
// sortGuilds returns Guilds in order of most similar to the query passed.
func sortGuilds(source []*discord.Guild, query string) []*discord.Guild {
if len(source) < 2 {
return source
}
var ranks fuzzy.Ranks
for index, source := range source {
rank := accurateString(query, source.Name)
rank.OriginalIndex = index
ranks = append(ranks, rank)
}
sorted := make([]*discord.Guild, 0, len(source))
sort.Sort(ranks)
for _, rank := range ranks {
sorted = append(sorted, source[rank.OriginalIndex])
}
return sorted
}
// sortChannels returns Channels in order of most similar to the query passed.
func sortChannels(source []*discord.Channel, query string) []*discord.Channel {
if len(source) < 2 {
return source
}
var ranks fuzzy.Ranks
for index, channel := range source {
rank := accurateString(query, channel.Name)
rank.OriginalIndex = index
ranks = append(ranks, rank)
}
sorted := make([]*discord.Channel, 0, len(source))
sort.Sort(ranks)
for _, rank := range ranks {
sorted = append(sorted, source[rank.OriginalIndex])
}
return sorted
}
// sortRoles returns Roles in order of most similar to the query passed.
func sortRoles(source []*discord.Role, query string) []*discord.Role {
if len(source) < 2 {
return source
}
var ranks fuzzy.Ranks
for index, source := range source {
rank := accurateString(query, source.Name)
rank.OriginalIndex = index
ranks = append(ranks, rank)
}
sorted := make([]*discord.Role, 0, len(source))
sort.Sort(ranks)
for _, rank := range ranks {
sorted = append(sorted, source[rank.OriginalIndex])
}
return sorted
}
// sortEmojis returns Emojis in order of most similar to the query passed.
func sortEmojis(source []*discord.Emoji, query string) []*discord.Emoji {
if len(source) < 2 {
return source
}
var ranks fuzzy.Ranks
for index, source := range source {
rank := accurateString(query, source.Name)
rank.OriginalIndex = index
ranks = append(ranks, rank)
}
sorted := make([]*discord.Emoji, 0, len(source))
sort.Sort(ranks)
for _, rank := range ranks {
sorted = append(sorted, source[rank.OriginalIndex])
}
return sorted
}
// sortMembers returns Members in order of most similar to the query passed.
func sortMembers(source []*discord.GuildMember, query string) []*discord.GuildMember {
if len(source) < 2 {
return source
}
var ranks fuzzy.Ranks
for index, source := range source {
rank := accurateString(query, source.Nick, source.User.Username+"#"+source.User.Discriminator, source.User.Username)
rank.OriginalIndex = index
ranks = append(ranks, rank)
}
sorted := make([]*discord.GuildMember, 0, len(source))
sort.Sort(ranks)
for _, rank := range ranks {
sorted = append(sorted, source[rank.OriginalIndex])
}
return sorted
}
// sortUsers returns Users in order of most similar to the query passed.
func sortUsers(source []*discord.User, query string) []*discord.User {
if len(source) < 2 {
return source
}
var ranks fuzzy.Ranks
for index, source := range source {
rank := accurateString(query, source.Username+"#"+source.Discriminator, source.Username)
rank.OriginalIndex = index
ranks = append(ranks, rank)
}
sorted := make([]*discord.User, 0, len(source))
sort.Sort(ranks)
for _, rank := range ranks {
sorted = append(sorted, source[rank.OriginalIndex])
}
return sorted
}
// accurateString returns most accurate string closest to query.
func accurateString(query string, source ...string) fuzzy.Rank {
ranks := fuzzy.RankFindNormalizedFold(query, source)
if len(ranks) > 0 {
return ranks[0]
}
return fuzzy.Rank{}
}