]> git.lizzy.rs Git - go-anidb.git/commitdiff
misc: Allow adding/removing arbitrary episodes to an EpisodeList
authorDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Wed, 17 Jul 2013 17:57:56 +0000 (14:57 -0300)
committerDiogo Franco (Kovensky) <diogomfranco@gmail.com>
Wed, 17 Jul 2013 17:57:56 +0000 (14:57 -0300)
misc/episodelist.go
misc/episodelist_test.go

index 7f7d0abd8e2edc752587cbb36919c6ad9885575a..61be68bf0257717d16854bf415440ea053c5c452 100644 (file)
@@ -25,6 +25,19 @@ func RangesToList(ranges ...*EpisodeRange) EpisodeList {
        return EpisodeList(ranges)
 }
 
+func ContainerToList(ec EpisodeContainer) EpisodeList {
+       switch v := ec.(type) {
+       case *Episode:
+               return EpisodeToList(v)
+       case *EpisodeRange:
+               return RangesToList(v)
+       case EpisodeList:
+               return v
+       default:
+               panic("unimplemented")
+       }
+}
+
 // Converts the EpisodeList into the AniDB API list format.
 func (el EpisodeList) String() string {
        scales := map[EpisodeType]int{}
@@ -196,3 +209,16 @@ func (el EpisodeList) Less(i, j int) bool {
 func (el EpisodeList) Swap(i, j int) {
        el[i], el[j] = el[j], el[i]
 }
+
+func (el *EpisodeList) Add(ec EpisodeContainer) {
+       *el = append(*el, ContainerToList(ec)...)
+       *el = el.Simplify()
+}
+
+func (el *EpisodeList) Sub(ep *Episode) {
+       el2 := make(EpisodeList, 0, len(*el))
+       for _, r := range *el {
+               el2 = append(el2, r.Split(ep)...)
+       }
+       *el = append(*el, el2.Simplify()...)
+}
index a7567469e756badf55af9216e392ec81c8d59ca4..470e5d607b7325d8b89a5916baa9ed1c08e0e566 100644 (file)
@@ -11,3 +11,28 @@ func ExampleEpisodeList_Simplify() {
 
        // Output: 01-03,05,10-15,S1,S3-,C07-C10
 }
+
+func ExampleEpisodeList_Add() {
+       a := misc.ParseEpisodeList("1-3")
+       a.Add(misc.ParseEpisode("3.1"))
+       fmt.Println(a)
+
+       a.Add(misc.ParseEpisode("4.0"))
+       fmt.Println(a)
+
+       a.Add(misc.ParseEpisode("4"))
+       fmt.Println(a)
+
+       a.Add(misc.ParseEpisode("5.1"))
+       fmt.Println(a)
+
+       a.Add(misc.ParseEpisode("6"))
+       fmt.Println(a)
+
+       // Output:
+       // 1-3
+       // 1-4.0
+       // 1-4
+       // 1-4,5.1
+       // 1-4,5.1,6
+}