From 5be4a7305b526c73d659292e9a46142d4251bc36 Mon Sep 17 00:00:00 2001 From: "Diogo Franco (Kovensky)" Date: Wed, 17 Jul 2013 14:57:56 -0300 Subject: [PATCH] misc: Allow adding/removing arbitrary episodes to an EpisodeList --- misc/episodelist.go | 26 ++++++++++++++++++++++++++ misc/episodelist_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/misc/episodelist.go b/misc/episodelist.go index 7f7d0ab..61be68b 100644 --- a/misc/episodelist.go +++ b/misc/episodelist.go @@ -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()...) +} diff --git a/misc/episodelist_test.go b/misc/episodelist_test.go index a756746..470e5d6 100644 --- a/misc/episodelist_test.go +++ b/misc/episodelist_test.go @@ -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 +} -- 2.44.0