diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..28a1ea7 --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,19 @@ +Copyright (c) 2023 Leon Mika + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..1b45aa1 --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +# scriptx - Extras for Script + +Utilities that extend [github.com/bitfield/script](https://github.com/bitfield/script). + +Currently a WIP. The API is subject to change. \ No newline at end of file diff --git a/fmt_test.go b/fmt_test.go index 9a12a4f..efe7116 100644 --- a/fmt_test.go +++ b/fmt_test.go @@ -31,10 +31,3 @@ func verifyPipeLines(t *testing.T, fn func(p *script.Pipe) *script.Pipe, inLines assert.NoError(t, err) assert.Equal(t, expOut, outLines) } - -func verifyPipeErr(t *testing.T, fn func(p *script.Pipe) *script.Pipe, inLines []string) { - pipe := script.Slice(inLines) - _, err := fn(pipe).Slice() - - assert.Error(t, err) -} diff --git a/scriptx.go b/json.go similarity index 99% rename from scriptx.go rename to json.go index 9c2fc9a..9428215 100644 --- a/scriptx.go +++ b/json.go @@ -19,4 +19,4 @@ func ToJSON(fn func(line string) any) func(r io.Reader, w io.Writer) error { return err }) } -} \ No newline at end of file +} diff --git a/json_test.go b/json_test.go new file mode 100644 index 0000000..a8c08ca --- /dev/null +++ b/json_test.go @@ -0,0 +1,45 @@ +package scriptx_test + +import ( + "testing" + + "github.com/bitfield/script" + "github.com/lmika/scriptx" +) + +func TestToJSON(t *testing.T) { + t.Run("should convert a line to a JSON object", func(t *testing.T) { + verifyPipeLines(t, func(p *script.Pipe) *script.Pipe { + return p.Filter(scriptx.ToJSON(func(line string) any { + return map[string]any{ + "line": line, + "line_length": len(line), + } + })) + }, []string{ + "Just three words", + "She sells sea shells by the \"sea shore\"", + "Another example of a line", + }, []string{ + `{"line":"Just three words","line_length":16}`, + `{"line":"She sells sea shells by the \"sea shore\"","line_length":39}`, + `{"line":"Another example of a line","line_length":25}`, + }) + }) + + t.Run("should convert a line to a JSON array", func(t *testing.T) { + verifyPipeLines(t, func(p *script.Pipe) *script.Pipe { + return p.Filter(scriptx.ToJSON(func(line string) any { + return []any{line, line} + })) + }, []string{ + "line", + "words words", + "foobar", + }, []string{ + `["line","line"]`, + `["words words","words words"]`, + `["foobar","foobar"]`, + }) + }) +} diff --git a/subpipe_test.go b/subpipe_test.go new file mode 100644 index 0000000..d96fb8a --- /dev/null +++ b/subpipe_test.go @@ -0,0 +1,26 @@ +package scriptx_test + +import ( + "testing" + + "github.com/bitfield/script" + "github.com/lmika/scriptx" +) + +func TestSubPipe(t *testing.T) { + t.Run("should run the subpipe for each line in the parent pipe", func(t *testing.T) { + verifyPipeLines(t, func(p *script.Pipe) *script.Pipe { + return p.Filter(scriptx.SubPipe(func (src *script.Pipe) *script.Pipe { + return src.Replace("Line", "Subpipe") + })) + }, []string{ + "Line 1", + "Line 2", + "Line 3", + }, []string{ + "Subpipe 1", + "Subpipe 2", + "Subpipe 3", + }) + }) +} \ No newline at end of file