summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDave Henderson <dhenderson@gmail.com>2022-12-29 15:00:49 +0000
committerGitHub <noreply@github.com>2022-12-29 15:00:49 +0000
commitf8a636898f316ce0d4a9a12335e9633c218f9e8a (patch)
tree49891d1400fbbb05804c662bd1e4a480f4a59ce0
parentdb2957873903b397188d96dd4119f1ed9c2a0ea9 (diff)
parentfde6cbeb6868f38d1bb02454a5f6413c91935f5b (diff)
Merge pull request #1587 from hairyhenderson/strings-skip-lines
Add strings.SkipLines function
-rw-r--r--docs-src/content/functions/strings.yml23
-rw-r--r--docs/content/functions/strings.md36
-rw-r--r--funcs/strings.go5
-rw-r--r--strings/strings.go20
-rw-r--r--strings/strings_test.go15
5 files changed, 99 insertions, 0 deletions
diff --git a/docs-src/content/functions/strings.yml b/docs-src/content/functions/strings.yml
index e4796bd3..709288b6 100644
--- a/docs-src/content/functions/strings.yml
+++ b/docs-src/content/functions/strings.yml
@@ -137,6 +137,29 @@ funcs:
- |
$ gomplate -i '{{ (coll.Slice "foo" "bar" "baz") | strings.Sort }}'
[bar baz foo]
+ - name: strings.SkipLines
+ description: |
+ Skips the given number of lines (each ending in a `\n`), returning the
+ remainder.
+
+ If `skip` is greater than the number of lines in `in`, an empty string is
+ returned.
+ pipeline: true
+ arguments:
+ - name: skip
+ required: true
+ description: the number of lines to skip - must be a positive number
+ - name: in
+ required: true
+ description: the input string
+ examples:
+ - |
+ $ gomplate -i '{{ "foo\nbar\nbaz" | strings.SkipLines 2 }}'
+ baz
+ - |
+ $ gomplate -i '{{ strings.SkipLines 1 "foo\nbar\nbaz" }}'
+ bar
+ baz
- name: strings.Split
description: |
_Not to be confused with [`split`](#split), which is deprecated._
diff --git a/docs/content/functions/strings.md b/docs/content/functions/strings.md
index 57d3965f..37859d5a 100644
--- a/docs/content/functions/strings.md
+++ b/docs/content/functions/strings.md
@@ -207,6 +207,42 @@ $ gomplate -i '{{ (coll.Slice "foo" "bar" "baz") | strings.Sort }}'
[bar baz foo]
```
+## `strings.SkipLines`
+
+Skips the given number of lines (each ending in a `\n`), returning the
+remainder.
+
+If `skip` is greater than the number of lines in `in`, an empty string is
+returned.
+
+### Usage
+
+```go
+strings.SkipLines skip in
+```
+```go
+in | strings.SkipLines skip
+```
+
+### Arguments
+
+| name | description |
+|------|-------------|
+| `skip` | _(required)_ the number of lines to skip - must be a positive number |
+| `in` | _(required)_ the input string |
+
+### Examples
+
+```console
+$ gomplate -i '{{ "foo\nbar\nbaz" | strings.SkipLines 2 }}'
+baz
+```
+```console
+$ gomplate -i '{{ strings.SkipLines 1 "foo\nbar\nbaz" }}'
+bar
+baz
+```
+
## `strings.Split`
_Not to be confused with [`split`](#split), which is deprecated._
diff --git a/funcs/strings.go b/funcs/strings.go
index ab59c0c9..040d2e64 100644
--- a/funcs/strings.go
+++ b/funcs/strings.go
@@ -184,6 +184,11 @@ func (StringFuncs) Repeat(count int, s interface{}) (string, error) {
return strings.Repeat(str, count), nil
}
+// SkipLines -
+func (StringFuncs) SkipLines(skip int, in string) (string, error) {
+ return gompstrings.SkipLines(skip, in)
+}
+
// Sort -
//
// Deprecated: use [CollFuncs.Sort] instead
diff --git a/strings/strings.go b/strings/strings.go
index 269e760f..073375e7 100644
--- a/strings/strings.go
+++ b/strings/strings.go
@@ -2,6 +2,7 @@
package strings
import (
+ "fmt"
"regexp"
"sort"
"strings"
@@ -125,3 +126,22 @@ func WordWrap(in string, opts WordWrapOpts) string {
opts = wwDefaults(opts)
return goutils.WrapCustom(in, int(opts.Width), opts.LBSeq, false)
}
+
+// SkipLines - skip the given number of lines (ending with \n) from the string.
+// If skip is greater than the number of lines in the string, an empty string is
+// returned.
+func SkipLines(skip int, in string) (string, error) {
+ if skip < 0 {
+ return "", fmt.Errorf("skip must be >= 0")
+ }
+ if skip == 0 {
+ return in, nil
+ }
+
+ lines := strings.SplitN(in, "\n", skip+1)
+ if skip >= len(lines) {
+ return "", nil
+ }
+
+ return lines[skip], nil
+}
diff --git a/strings/strings_test.go b/strings/strings_test.go
index e79614f9..46ca6d86 100644
--- a/strings/strings_test.go
+++ b/strings/strings_test.go
@@ -126,3 +126,18 @@ that has been set.`
// in = strings.ReplaceAll(out, "\n", " ")
// assert.Equal(t, out, WordWrap(in, WordWrapOpts{Width: 100}))
}
+
+func TestSkipLines(t *testing.T) {
+ out, _ := SkipLines(2, "\nfoo\nbar\n\nbaz")
+ assert.Equal(t, "bar\n\nbaz", out)
+
+ out, _ = SkipLines(0, "foo\nbar\n\nbaz")
+ assert.Equal(t, "foo\nbar\n\nbaz", out)
+
+ _, err := SkipLines(-1, "foo\nbar\n\nbaz")
+ assert.Error(t, err)
+
+ out, err = SkipLines(4, "foo\nbar\n\nbaz")
+ assert.NoError(t, err)
+ assert.Equal(t, "", out)
+}