gookit/goutil

[FEAT] Conversion Functions with Default Values?

KiddoV opened this issue · 2 comments

KiddoV commented

Would be nice to have some utils to convert to a design data type, if error or not the same type then return the default vals:

I have a scenarios where I want to convert a float and want the value to be 0.0 if failed:

test := mathutil.QuietFloat(math.NaN) //=> test = NaN, I want it to always be a float type instead (0.0 if failed).

The idea...

utils.FloatOrDefault(in any, default float64)
test := utils.FloatOrDefault("test", 0.0) //=> Error, return test = 0.0

utils.StringOrDefault(in any, default string)
test := utils.StringOrDefault(nil, "hello") //=> Error, return test = "hello"

...

Something like that. you get the idea 🙂
Thanks,

inhere commented

Hi @KiddoV added and released on v0.6.12

KiddoV commented

Nice. Thank you!