Skip to content

Commit

Permalink
Add generic reverse template helper
Browse files Browse the repository at this point in the history
Can be used to reverse the order of any slice.

Fixes #16.
  • Loading branch information
muesli committed Nov 3, 2020
1 parent c6f6733 commit 3d892f4
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import (
"context"
"flag"
"fmt"
"github.com/KyleBanks/goodreads"
"html/template"
"io/ioutil"
"net/http"
"os"

"github.com/KyleBanks/goodreads"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)
Expand Down Expand Up @@ -52,6 +52,7 @@ func main() {
"goodReadsCurrentlyReading": goodReadsCurrentlyReading,
/* Utils */
"humanize": humanized,
"reverse": reverse,
}).Parse(string(tplIn))
if err != nil {
fmt.Println("Can't parse template:", err)
Expand Down
11 changes: 11 additions & 0 deletions humanize.go → template.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"reflect"
"time"

"github.com/dustin/go-humanize"
Expand All @@ -22,3 +23,13 @@ func humanized(t interface{}) string {
return fmt.Sprintf("%v", t)
}
}

func reverse(s interface{}) interface{} {
n := reflect.ValueOf(s).Len()
swap := reflect.Swapper(s)
for i, j := 0, n-1; i < j; i, j = i+1, j-1 {
swap(i, j)
}

return s
}

0 comments on commit 3d892f4

Please sign in to comment.