-
Notifications
You must be signed in to change notification settings - Fork 14
/
duplication_checker.go
63 lines (59 loc) · 2.04 KB
/
duplication_checker.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
package main
import (
"fmt"
"strings"
linq "github.com/ahmetb/go-linq/v3"
)
// DupStyleSectionsChecker group and count duplicated css classes
func DupStyleSectionsChecker(styleList []StyleSection) []SectionSummary {
groups := []SectionSummary{}
linq.From(styleList).GroupBy(func(script interface{}) interface{} {
return script.(StyleSection).valueHash // hash value as key
}, func(script interface{}) interface{} {
return script
}).Where(func(group interface{}) bool {
return len(group.(linq.Group).Group) > 1
}).OrderByDescending( // sort groups by its counts
func(group interface{}) interface{} {
return len(group.(linq.Group).Group)
}).SelectT( // get structs out of groups
func(group linq.Group) interface{} {
names := []string{}
for _, styleSection := range group.Group {
names = append(names, fmt.Sprintf("%s << %s", styleSection.(StyleSection).name, styleSection.(StyleSection).filePath))
}
return SectionSummary{
names: names,
value: strings.Join(group.Group[0].(StyleSection).value, "\n"),
count: len(names),
}
}).ToSlice(&groups)
return groups
}
// DupScriptsChecker group and count duplicated css colors or long css lines.
func DupScriptsChecker(longScriptList []Script) []ScriptSummary {
groups := []ScriptSummary{}
linq.From(longScriptList).GroupBy(func(script interface{}) interface{} {
return script.(Script).hashValue // script hashed value as key
}, func(script interface{}) interface{} {
return script
}).Where(func(group interface{}) bool {
return len(group.(linq.Group).Group) > 1
}).OrderByDescending( // sort groups by its length
func(group interface{}) interface{} {
return len(group.(linq.Group).Group)
}).SelectT( // get structs out of groups
func(group linq.Group) interface{} {
scripts := []Script{}
for _, group := range group.Group {
scripts = append(scripts, group.(Script))
}
return ScriptSummary{
scripts: scripts,
hashValue: group.Key.(uint64),
value: scripts[0].value,
count: len(scripts),
}
}).ToSlice(&groups)
return groups
}