Extract Reverse func
This commit is contained in:
parent
7a064d33df
commit
c9925a4320
6
index.go
6
index.go
|
@ -139,11 +139,7 @@ func (h *indexHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
indexPage.Me = sess.Me
|
indexPage.Me = sess.Me
|
||||||
|
|
||||||
if len(moments) > 0 {
|
if len(moments) > 0 {
|
||||||
a := moments
|
Reverse(moments)
|
||||||
for i := len(a)/2 - 1; i >= 0; i-- {
|
|
||||||
opp := len(a) - 1 - i
|
|
||||||
a[i], a[opp] = a[opp], a[i]
|
|
||||||
}
|
|
||||||
lastMoment := moments[0]
|
lastMoment := moments[0]
|
||||||
indexPage.LastMomentSeconds = lastMoment.Time.Unix()
|
indexPage.LastMomentSeconds = lastMoment.Time.Unix()
|
||||||
}
|
}
|
||||||
|
|
9
util.go
9
util.go
|
@ -13,3 +13,12 @@ func RandStringBytes(n int) string {
|
||||||
}
|
}
|
||||||
return string(b)
|
return string(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Reverse(moments []Moment) {
|
||||||
|
a := moments
|
||||||
|
|
||||||
|
for i := len(a)/2 - 1; i >= 0; i-- {
|
||||||
|
opp := len(a) - 1 - i
|
||||||
|
a[i], a[opp] = a[opp], a[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
30
util_test.go
Normal file
30
util_test.go
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestReverse(t *testing.T) {
|
||||||
|
moments := []Moment{
|
||||||
|
{Key: "2018-01-01", Memo: "test", Time: time.Now()},
|
||||||
|
{Key: "2018-01-02", Memo: "test2", Time: time.Now()},
|
||||||
|
{Key: "2018-01-03", Memo: "test3", Time: time.Now()},
|
||||||
|
{Key: "2018-01-04", Memo: "test4", Time: time.Now()},
|
||||||
|
}
|
||||||
|
|
||||||
|
Reverse(moments)
|
||||||
|
|
||||||
|
if moments[0].Key != "2018-01-04" {
|
||||||
|
t.Errorf("wrong 1st key %q != %q", moments[0].Key, "2018-01-04")
|
||||||
|
}
|
||||||
|
if moments[1].Key != "2018-01-03" {
|
||||||
|
t.Errorf("wrong 2nd key %q != %q", moments[1].Key, "2018-01-03")
|
||||||
|
}
|
||||||
|
if moments[2].Key != "2018-01-02" {
|
||||||
|
t.Errorf("wrong 3rd key %q != %q", moments[2].Key, "2018-01-02")
|
||||||
|
}
|
||||||
|
if moments[3].Key != "2018-01-01" {
|
||||||
|
t.Errorf("wrong 4th key %q != %q", moments[3].Key, "2018-01-01")
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user