Merge pull request #12872 from joshuaspence/log
Add a logarithm function
This commit is contained in:
commit
9e221f3811
|
@ -81,6 +81,7 @@ func Funcs() map[string]ast.Function {
|
||||||
"jsonencode": interpolationFuncJSONEncode(),
|
"jsonencode": interpolationFuncJSONEncode(),
|
||||||
"length": interpolationFuncLength(),
|
"length": interpolationFuncLength(),
|
||||||
"list": interpolationFuncList(),
|
"list": interpolationFuncList(),
|
||||||
|
"log": interpolationFuncLog(),
|
||||||
"lower": interpolationFuncLower(),
|
"lower": interpolationFuncLower(),
|
||||||
"map": interpolationFuncMap(),
|
"map": interpolationFuncMap(),
|
||||||
"max": interpolationFuncMax(),
|
"max": interpolationFuncMax(),
|
||||||
|
@ -489,6 +490,17 @@ func interpolationFuncCeil() ast.Function {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// interpolationFuncLog returns the logarithnm.
|
||||||
|
func interpolationFuncLog() ast.Function {
|
||||||
|
return ast.Function{
|
||||||
|
ArgTypes: []ast.Type{ast.TypeFloat, ast.TypeFloat},
|
||||||
|
ReturnType: ast.TypeFloat,
|
||||||
|
Callback: func(args []interface{}) (interface{}, error) {
|
||||||
|
return math.Log(args[0].(float64)) / math.Log(args[1].(float64)), nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// interpolationFuncChomp removes trailing newlines from the given string
|
// interpolationFuncChomp removes trailing newlines from the given string
|
||||||
func interpolationFuncChomp() ast.Function {
|
func interpolationFuncChomp() ast.Function {
|
||||||
newlines := regexp.MustCompile(`(?:\r\n?|\n)*\z`)
|
newlines := regexp.MustCompile(`(?:\r\n?|\n)*\z`)
|
||||||
|
|
|
@ -370,6 +370,34 @@ func TestInterpolateFuncCeil(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestInterpolateFuncLog(t *testing.T) {
|
||||||
|
testFunction(t, testFunctionConfig{
|
||||||
|
Cases: []testFunctionCase{
|
||||||
|
{
|
||||||
|
`${log(1, 10)}`,
|
||||||
|
"0",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`${log(10, 10)}`,
|
||||||
|
"1",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
|
||||||
|
{
|
||||||
|
`${log(0, 10)}`,
|
||||||
|
"-Inf",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
`${log(10, 0)}`,
|
||||||
|
"-0",
|
||||||
|
false,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
func TestInterpolateFuncChomp(t *testing.T) {
|
func TestInterpolateFuncChomp(t *testing.T) {
|
||||||
testFunction(t, testFunctionConfig{
|
testFunction(t, testFunctionConfig{
|
||||||
Cases: []testFunctionCase{
|
Cases: []testFunctionCase{
|
||||||
|
|
|
@ -278,6 +278,8 @@ The supported built-in functions are:
|
||||||
* `${list("a", "b", "c")}` returns a list of `"a", "b", "c"`.
|
* `${list("a", "b", "c")}` returns a list of `"a", "b", "c"`.
|
||||||
* `${list()}` returns an empty list.
|
* `${list()}` returns an empty list.
|
||||||
|
|
||||||
|
* `log(x, base)` - Returns the logarithm of `x`.
|
||||||
|
|
||||||
* `lookup(map, key, [default])` - Performs a dynamic lookup into a map
|
* `lookup(map, key, [default])` - Performs a dynamic lookup into a map
|
||||||
variable. The `map` parameter should be another variable, such
|
variable. The `map` parameter should be another variable, such
|
||||||
as `var.amis`. If `key` does not exist in `map`, the interpolation will
|
as `var.amis`. If `key` does not exist in `map`, the interpolation will
|
||||||
|
|
Loading…
Reference in New Issue