-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproperty.go
60 lines (52 loc) · 1.33 KB
/
property.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
package sortprop
import "sort"
// KeyProperties is a slice of Property elements sorted by Key.
type KeyProperties []Property
// ValueProperties is a slice of Property elements sorted by Value.
type ValueProperties []Property
// Property is a key-value pair of strings.
type Property struct {
Key string
Value string
}
// UniqueKeys returns a slice with only one instance of each unique-key property.
// If keeplast is true, the last element of the same key will be kept rather than the first.
// The list will be sorted by Key.
func UniqueKeys(kp KeyProperties, keeplast bool) KeyProperties {
var list KeyProperties
sort.Sort(kp)
for i := 0; i < len(kp); i++ {
if i == 0 {
list = append(list, kp[i])
continue
}
if kp[i].Key == kp[i-1].Key {
if keeplast {
list[len(list)-1] = kp[i]
}
} else {
list = append(list, kp[i])
}
}
return list
}
// UniqueValues returns a slice with only one instance of each unique value-property.
// The list will be sorted by Value.
func UniqueValues(vp ValueProperties, keeplast bool) ValueProperties {
var list ValueProperties
sort.Sort(vp)
for i := 0; i < len(vp); i++ {
if i == 0 {
list = append(list, vp[i])
continue
}
if vp[i].Value == vp[i-1].Value {
if keeplast {
list[len(list)-1] = vp[i]
}
} else {
list = append(list, vp[i])
}
}
return list
}