commit 2ab574a5e7687eaf12d218282d605f7bccb21f8d
parent 24897a858e52acb2d33ed5411b6e68836a4d4169
Author: Mark Wilkerson <mark@markwilkerson.me>
Date: Tue, 20 Feb 2018 18:27:21 -0800
Initial shiz
Diffstat:
A | README.md | | | 33 | +++++++++++++++++++++++++++++++++ |
A | batch.go | | | 38 | ++++++++++++++++++++++++++++++++++++++ |
A | batch_test.go | | | 42 | ++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 113 insertions(+), 0 deletions(-)
diff --git a/README.md b/README.md
@@ -0,0 +1,33 @@
+# batch
+
+import "github.com/markhuge/batch"
+
+Batch provides helper functions for batching slices into sub slices of a
+specified length. You could think of it as pagination for slices.
+
+## Usage
+
+#### func Str
+
+```go
+func Str(s []string, pageSize int) [][]string
+```
+Str accepts a slice of strings and a page size, then splits up the slice into
+smaller slices of length "pageSize"
+
+#### func StrJoin
+
+```go
+func StrJoin(s []string, pageSize int, delimiter string) []string
+```
+StrJoin is a combination of Str() and strings.Join(). Rather than returning a
+slice of slices, it returns a slice of joined strings
+
+
+## Tests
+
+```
+PASS
+coverage: 100.0% of statements
+ok github.com/markhuge/batch 1.007s
+```
diff --git a/batch.go b/batch.go
@@ -0,0 +1,38 @@
+// Batch provides helper functions for batching slices into sub slices of a
+// specified length. You could think of it as pagination for slices.
+package batch
+
+import (
+ "math"
+ "strings"
+)
+
+// Str accepts a slice of strings and a page size, then splits up the slice
+// into smaller slices of length "pageSize"
+func Str(s []string, pageSize int) [][]string {
+
+ // math.Ceil only works properly with floats :(
+ size := int(math.Ceil(float64(len(s)) / float64(pageSize)))
+ output := make([][]string, size)
+ j := 0
+
+ for i := 0; i < len(s); i += pageSize {
+ end := i + pageSize
+ if end > len(s) {
+ end = len(s)
+ }
+ output[j] = s[i:end]
+ j++
+ }
+ return output
+}
+
+// StrJoin is a combination of Str() and strings.Join(). Rather than returning
+// a slice of slices, it returns a slice of joined strings
+func StrJoin(s []string, pageSize int, delimiter string) []string {
+ var output []string
+ for _, slice := range Str(s, pageSize) {
+ output = append(output, strings.Join(slice, delimiter))
+ }
+ return output
+}
diff --git a/batch_test.go b/batch_test.go
@@ -0,0 +1,42 @@
+package batch_test
+
+import (
+ "testing"
+
+ "github.com/markhuge/batch"
+)
+
+func TestStr(t *testing.T) {
+ input := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"}
+ expected := [][]string{
+ {"a", "b", "c", "d"},
+ {"e", "f", "g", "h"},
+ {"i", "j", "k"},
+ }
+ result := batch.Str(input, 4)
+
+ for i := range result {
+ for j := range result[i] {
+ if expected[i][j] != result[i][j] {
+ t.Fatalf("Expected %s got %s\n", expected[i][j], result[i][j])
+ }
+ }
+ }
+}
+
+func TestStrJoin(t *testing.T) {
+ input := []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k"}
+ expected := []string{
+ "a,b,c,d",
+ "e,f,g,h",
+ "i,j,k",
+ }
+ result := batch.StrJoin(input, 4, ",")
+
+ for i := range result {
+ if expected[i] != result[i] {
+ t.Fatalf("Expected %s, got %s", expected[i], result[i])
+ }
+ }
+
+}