providers: Add slice functions provider

This commit is contained in:
2024-12-07 22:18:45 -05:00
parent 7f64686323
commit d76374719d
2 changed files with 67 additions and 0 deletions

65
providers/slice/slice.go Normal file
View File

@@ -0,0 +1,65 @@
package slice
import "fmt"
import "html/template"
import "git.tebibyte.media/sashakoshka/step"
const hiddenPrefix = "."
var _ step.FuncProvider = new(Provider)
// Provider provides slice functions.
type Provider struct {
}
// FuncMap fulfills the step.FuncProvider interface.
func (this *Provider) FuncMap () template.FuncMap {
return template.FuncMap {
"sAppend": funcSAppend,
"iAppend": funcIAppend,
"fAppend": funcFAppend,
}
}
func funcSAppend (slice any, addition string) []string {
switch slice := slice.(type) {
case []string:
newSlice := make([]string, len(slice) + 1)
copy(newSlice, slice)
newSlice[len(slice)] = addition
return newSlice
case string:
return []string { slice, addition }
default:
return funcSAppend(fmt.Sprint(slice), addition)
}
}
func funcIAppend (slice any, addition int) []int {
switch slice := slice.(type) {
case []int:
newSlice := make([]int, len(slice) + 1)
copy(newSlice, slice)
newSlice[len(slice)] = addition
return newSlice
case int:
return []int { slice, addition }
default:
return funcIAppend(0, addition)
}
}
func funcFAppend (slice any, addition float64) []float64 {
switch slice := slice.(type) {
case []float64:
newSlice := make([]float64, len(slice) + 1)
copy(newSlice, slice)
newSlice[len(slice)] = addition
return newSlice
case float64:
return []float64 { slice, addition }
default:
return funcFAppend(0, addition)
}
}