forked from AlecAivazis/survey
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implemented the
Transform
idea (AlecAivazis#104)
* fix some grammar and misspells * Implement AlecAivazis#4 and AlecAivazis#98 * fix README.md -> survey.Title typo
- Loading branch information
1 parent
fddc032
commit 05a1967
Showing
6 changed files
with
147 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package survey | ||
|
||
import ( | ||
"reflect" | ||
"strings" | ||
) | ||
|
||
// TransformString returns a `Transformer` based on the "f" | ||
// function which accepts a string representation of the answer | ||
// and returns a new one, transformed, answer. | ||
// Take for example the functions inside the std `strings` package, | ||
// they can be converted to a compatible `Transformer` by using this function, | ||
// i.e: `TransformString(strings.Title)`, `TransformString(strings.ToUpper)`. | ||
// | ||
// Note that `TransformString` is just a helper, `Transformer` can be used | ||
// to transform any type of answer. | ||
func TransformString(f func(s string) string) Transformer { | ||
return func(ans interface{}) interface{} { | ||
// if the answer value passed in is the zero value of the appropriate type | ||
if isZero(reflect.ValueOf(ans)) { | ||
// skip this `Transformer` by returning a nil value. | ||
// The original answer will be not affected, | ||
// see survey.go#L125. | ||
return nil | ||
} | ||
|
||
// "ans" is never nil here, so we don't have to check that | ||
// see survey.go#L97 for more. | ||
// Make sure that the the answer's value was a typeof string. | ||
s, ok := ans.(string) | ||
if !ok { | ||
return nil | ||
} | ||
|
||
return f(s) | ||
} | ||
} | ||
|
||
// ToLower is a `Transformer`. | ||
// It receives an answer value | ||
// and returns a copy of the "ans" | ||
// with all Unicode letters mapped to their lower case. | ||
// | ||
// Note that if "ans" is not a string then it will | ||
// return a nil value, meaning that the above answer | ||
// will not be affected by this call at all. | ||
func ToLower(ans interface{}) interface{} { | ||
transformer := TransformString(strings.ToLower) | ||
return transformer(ans) | ||
} | ||
|
||
// Title is a `Transformer`. | ||
// It receives an answer value | ||
// and returns a copy of the "ans" | ||
// with all Unicode letters that begin words | ||
// mapped to their title case. | ||
// | ||
// Note that if "ans" is not a string then it will | ||
// return a nil value, meaning that the above answer | ||
// will not be affected by this call at all. | ||
func Title(ans interface{}) interface{} { | ||
transformer := TransformString(strings.Title) | ||
return transformer(ans) | ||
} | ||
|
||
// ComposeTransformers is a variadic function used to create one transformer from many. | ||
func ComposeTransformers(transformers ...Transformer) Transformer { | ||
// return a transformer that calls each one sequentially | ||
return func(ans interface{}) interface{} { | ||
// execute each transformer | ||
for _, t := range transformers { | ||
ans = t(ans) | ||
} | ||
return ans | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package survey | ||
|
||
import ( | ||
"strings" | ||
"testing" | ||
) | ||
|
||
func testStringTransformer(t *testing.T, f func(string) string) { | ||
transformer := TransformString(f) | ||
|
||
tests := []string{ | ||
"hello my name is", | ||
"where are you from", | ||
"does that matter?", | ||
} | ||
|
||
for _, tt := range tests { | ||
if expected, got := f(tt), transformer(tt); expected != got { | ||
t.Errorf("TransformString transformer failed to transform the answer, expected '%s' but got '%s'.", expected, got) | ||
} | ||
} | ||
} | ||
|
||
func TestTransformString(t *testing.T) { | ||
testStringTransformer(t, strings.ToTitle) // all letters titled | ||
testStringTransformer(t, strings.ToLower) // all letters lowercase | ||
} | ||
|
||
func TestComposeTransformers(t *testing.T) { | ||
// create a transformer which makes no sense, | ||
// remember: transformer can be used for any type | ||
// we just test the built'n functions that | ||
// happens to be for strings only. | ||
transformer := ComposeTransformers( | ||
Title, | ||
ToLower, | ||
) | ||
|
||
ans := "my name is" | ||
if expected, got := strings.ToLower(ans), transformer(ans); expected != got { | ||
// the result should be lowercase. | ||
t.Errorf("TestComposeTransformers transformer failed to transform the answer to title->lowercase, expected '%s' but got '%s'.", expected, got) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters