Skip to content

🎧 Your Personal Streaming Service

Method for parsing Index Groups

Deluan9d6eb40f6fcc
diff --git a/utils/index_group_parser.go b/utils/index_group_parser.go
new file mode 100644
index 000000000..7946fb55f
--- /dev/null
+++ b/utils/index_group_parser.go
@@ -0,0 +1,40 @@
+// Let's you specify how the index should look like.
+//
+// The specification is a space-separated list of index entries. Normally, each entry is just a single character,
+// but you may also specify multiple characters. For instance, the entry "The" will link to all files and
+// folders starting with "The".
+//
+// You may also create an entry using a group of index characters in paranthesis. For instance, the entry
+// "A-E(ABCDE)" will display as "A-E" and link to all files and folders starting with either
+// A, B, C, D or E. This may be useful for grouping less-frequently used characters (such and X, Y and Z), or
+// for grouping accented characters (such as A, \u00C0 and \u00C1)
+//
+// Files and folders that are not covered by an index entry will be placed under the index entry "#".
+package utils
+
+import (
+ "strings"
+ "regexp"
+)
+
+type IndexGroups map[string]string
+
+func ParseIndexGroups(spec string) IndexGroups {
+ parsed := make(IndexGroups)
+ split := strings.Split(spec, " ")
+ re := regexp.MustCompile(`(.+)\((.+)\)`)
+ for _, g := range split {
+ sub := re.FindStringSubmatch(g)
+ if len(sub) > 0 {
+ i := 0
+ chars := strings.SplitN(sub[2], "", -1)
+ for _, c := range chars {
+ parsed[c] = sub[1]
+ i++
+ }
+ } else {
+ parsed[g] = g
+ }
+ }
+ return parsed
+}
\ No newline at end of file
diff --git a/utils/utils_test.go b/utils/utils_test.go
index bb8807497..7a936c981 100644
--- a/utils/utils_test.go
+++ b/utils/utils_test.go
@@ -3,10 +3,44 @@ package utils
import (
"testing"
. "github.com/smartystreets/goconvey/convey"
+ "fmt"
)
-func TestEmpty(t *testing.T) {
+func TestParseIndexGroup(t *testing.T) {
- Convey("Missing tests", t, nil)
+ Convey("Two simple entries", t, func() {
+ parsed := ParseIndexGroups("A The")
+ So(parsed, ShouldContainKey, "A")
+ So(parsed["A"], ShouldEqual, "A")
+
+ So(parsed, ShouldContainKey, "The")
+ So(parsed["The"], ShouldEqual, "The")
+ })
+
+ Convey("An entry with a group", t, func() {
+ parsed := ParseIndexGroups("A-C(ABC) Z")
+ fmt.Println("\n>>>>>>", parsed)
+
+ So(parsed, ShouldContainKey, "A")
+ So(parsed["A"], ShouldEqual, "A-C")
+ So(parsed, ShouldContainKey, "B")
+ So(parsed["B"], ShouldEqual, "A-C")
+ So(parsed, ShouldContainKey, "C")
+ So(parsed["C"], ShouldEqual, "A-C")
+
+ So(parsed["Z"], ShouldEqual, "Z")
+
+ })
+ Convey("Correctly parses UTF-8", t, func() {
+ parsed := ParseIndexGroups("UTF8(宇A海)")
+ fmt.Println("\n>>>>>>", parsed)
+
+ So(parsed, ShouldContainKey, "宇")
+ So(parsed["宇"], ShouldEqual, "UTF8")
+ So(parsed, ShouldContainKey, "A")
+ So(parsed["A"], ShouldEqual, "UTF8")
+ So(parsed, ShouldContainKey, "æµ·")
+ So(parsed["æµ·"], ShouldEqual, "UTF8")
+ })
}
\ No newline at end of file