aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorStuart Skelton <stuarts@broadbean.com>2017-08-12 17:35:05 +0100
committerStuart Skelton <stuarts@broadbean.com>2017-08-12 17:59:24 +0100
commitb1607ce3bfaf54936338d8639087dee52f7d7bc2 (patch)
tree50c57801c9f1edb7ad1b80aa314c5c340f97d9f3
parent180dd57d27f8d7b0add8005ee46026e6f89fabbb (diff)
Have a util function to pluralize todo.
-rw-r--r--todolist/app.go2
-rw-r--r--todolist/util.go7
-rw-r--r--todolist/util_test.go13
3 files changed, 21 insertions, 1 deletions
diff --git a/todolist/app.go b/todolist/app.go
index d4513ac..f63c41b 100644
--- a/todolist/app.go
+++ b/todolist/app.go
@@ -44,7 +44,7 @@ func (a *App) DeleteTodo(input string) {
}
a.TodoList.Delete(ids...)
a.Save()
- fmt.Println("Todo deleted.")
+ fmt.Printf("%s deleted.\n", pluralize(len(ids), "Todo", "Todos"))
}
func (a *App) CompleteTodo(input string) {
diff --git a/todolist/util.go b/todolist/util.go
index 34f9dd1..ff3dbba 100644
--- a/todolist/util.go
+++ b/todolist/util.go
@@ -52,3 +52,10 @@ func getNearestMonday(t time.Time) time.Time {
}
}
}
+
+func pluralize(count int, singular, plural string) string {
+ if count > 1 {
+ return plural
+ }
+ return singular
+}
diff --git a/todolist/util_test.go b/todolist/util_test.go
new file mode 100644
index 0000000..40ecb54
--- /dev/null
+++ b/todolist/util_test.go
@@ -0,0 +1,13 @@
+package todolist
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestPluralize(t *testing.T) {
+ assert := assert.New(t)
+ assert.Equal("todo", pluralize(1, "todo", "todos"))
+ assert.Equal("todos", pluralize(2, "todo", "todos"))
+}