| 1 |
package sprig |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math" |
| 6 |
"strconv" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/spf13/cast" |
| 10 |
"github.com/shopspring/decimal" |
| 11 |
) |
| 12 |
|
| 13 |
// toFloat64 converts 64-bit floats |
| 14 |
func toFloat64(v interface{}) float64 { |
| 15 |
return cast.ToFloat64(v) |
| 16 |
} |
| 17 |
|
| 18 |
func toInt(v interface{}) int { |
| 19 |
return cast.ToInt(v) |
| 20 |
} |
| 21 |
|
| 22 |
// toInt64 converts integer types to 64-bit integers |
| 23 |
func toInt64(v interface{}) int64 { |
| 24 |
return cast.ToInt64(v) |
| 25 |
} |
| 26 |
|
| 27 |
func max(a interface{}, i ...interface{}) int64 { |
| 28 |
aa := toInt64(a) |
| 29 |
for _, b := range i { |
| 30 |
bb := toInt64(b) |
| 31 |
if bb > aa { |
| 32 |
aa = bb |
| 33 |
} |
| 34 |
} |
| 35 |
return aa |
| 36 |
} |
| 37 |
|
| 38 |
func maxf(a interface{}, i ...interface{}) float64 { |
| 39 |
aa := toFloat64(a) |
| 40 |
for _, b := range i { |
| 41 |
bb := toFloat64(b) |
| 42 |
aa = math.Max(aa, bb) |
| 43 |
} |
| 44 |
return aa |
| 45 |
} |
| 46 |
|
| 47 |
func min(a interface{}, i ...interface{}) int64 { |
| 48 |
aa := toInt64(a) |
| 49 |
for _, b := range i { |
| 50 |
bb := toInt64(b) |
| 51 |
if bb < aa { |
| 52 |
aa = bb |
| 53 |
} |
| 54 |
} |
| 55 |
return aa |
| 56 |
} |
| 57 |
|
| 58 |
func minf(a interface{}, i ...interface{}) float64 { |
| 59 |
aa := toFloat64(a) |
| 60 |
for _, b := range i { |
| 61 |
bb := toFloat64(b) |
| 62 |
aa = math.Min(aa, bb) |
| 63 |
} |
| 64 |
return aa |
| 65 |
} |
| 66 |
|
| 67 |
func until(count int) []int { |
| 68 |
step := 1 |
| 69 |
if count < 0 { |
| 70 |
step = -1 |
| 71 |
} |
| 72 |
return untilStep(0, count, step) |
| 73 |
} |
| 74 |
|
| 75 |
func untilStep(start, stop, step int) []int { |
| 76 |
v := []int{} |
| 77 |
|
| 78 |
if stop < start { |
| 79 |
if step >= 0 { |
| 80 |
return v |
| 81 |
} |
| 82 |
for i := start; i > stop; i += step { |
| 83 |
v = append(v, i) |
| 84 |
} |
| 85 |
return v |
| 86 |
} |
| 87 |
|
| 88 |
if step <= 0 { |
| 89 |
return v |
| 90 |
} |
| 91 |
for i := start; i < stop; i += step { |
| 92 |
v = append(v, i) |
| 93 |
} |
| 94 |
return v |
| 95 |
} |
| 96 |
|
| 97 |
func floor(a interface{}) float64 { |
| 98 |
aa := toFloat64(a) |
| 99 |
return math.Floor(aa) |
| 100 |
} |
| 101 |
|
| 102 |
func ceil(a interface{}) float64 { |
| 103 |
aa := toFloat64(a) |
| 104 |
return math.Ceil(aa) |
| 105 |
} |
| 106 |
|
| 107 |
func round(a interface{}, p int, rOpt ...float64) float64 { |
| 108 |
roundOn := .5 |
| 109 |
if len(rOpt) > 0 { |
| 110 |
roundOn = rOpt[0] |
| 111 |
} |
| 112 |
val := toFloat64(a) |
| 113 |
places := toFloat64(p) |
| 114 |
|
| 115 |
var round float64 |
| 116 |
pow := math.Pow(10, places) |
| 117 |
digit := pow * val |
| 118 |
_, div := math.Modf(digit) |
| 119 |
if div >= roundOn { |
| 120 |
round = math.Ceil(digit) |
| 121 |
} else { |
| 122 |
round = math.Floor(digit) |
| 123 |
} |
| 124 |
return round / pow |
| 125 |
} |
| 126 |
|
| 127 |
// converts unix octal to decimal |
| 128 |
func toDecimal(v interface{}) int64 { |
| 129 |
result, err := strconv.ParseInt(fmt.Sprint(v), 8, 64) |
| 130 |
if err != nil { |
| 131 |
return 0 |
| 132 |
} |
| 133 |
return result |
| 134 |
} |
| 135 |
|
| 136 |
func seq(params ...int) string { |
| 137 |
increment := 1 |
| 138 |
switch len(params) { |
| 139 |
case 0: |
| 140 |
return "" |
| 141 |
case 1: |
| 142 |
start := 1 |
| 143 |
end := params[0] |
| 144 |
if end < start { |
| 145 |
increment = -1 |
| 146 |
} |
| 147 |
return intArrayToString(untilStep(start, end+increment, increment), " ") |
| 148 |
case 3: |
| 149 |
start := params[0] |
| 150 |
end := params[2] |
| 151 |
step := params[1] |
| 152 |
if end < start { |
| 153 |
increment = -1 |
| 154 |
if step > 0 { |
| 155 |
return "" |
| 156 |
} |
| 157 |
} |
| 158 |
return intArrayToString(untilStep(start, end+increment, step), " ") |
| 159 |
case 2: |
| 160 |
start := params[0] |
| 161 |
end := params[1] |
| 162 |
step := 1 |
| 163 |
if end < start { |
| 164 |
step = -1 |
| 165 |
} |
| 166 |
return intArrayToString(untilStep(start, end+step, step), " ") |
| 167 |
default: |
| 168 |
return "" |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
func intArrayToString(slice []int, delimeter string) string { |
| 173 |
return strings.Trim(strings.Join(strings.Fields(fmt.Sprint(slice)), delimeter), "[]") |
| 174 |
} |
| 175 |
|
| 176 |
// performs a float and subsequent decimal.Decimal conversion on inputs, |
| 177 |
// and iterates through a and b executing the mathmetical operation f |
| 178 |
func execDecimalOp(a interface{}, b []interface{}, f func(d1, d2 decimal.Decimal) decimal.Decimal) float64 { |
| 179 |
prt := decimal.NewFromFloat(toFloat64(a)) |
| 180 |
for _, x := range b { |
| 181 |
dx := decimal.NewFromFloat(toFloat64(x)) |
| 182 |
prt = f(prt, dx) |
| 183 |
} |
| 184 |
rslt, _ := prt.Float64() |
| 185 |
return rslt |
| 186 |
} |