From 4cf81f39706c7c2ef00f22e5de37a5bae5525f2d Mon Sep 17 00:00:00 2001 From: Marques Johansson Date: Mon, 22 Apr 2024 14:03:52 +0000 Subject: [PATCH] chore: refactor nprintf to internal/nprintf Signed-off-by: Marques Johansson --- internal/nprintf/nprintf.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 internal/nprintf/nprintf.go diff --git a/internal/nprintf/nprintf.go b/internal/nprintf/nprintf.go new file mode 100644 index 000000000..0b68397ff --- /dev/null +++ b/internal/nprintf/nprintf.go @@ -0,0 +1,23 @@ +package nprintf + +import ( + "fmt" + "regexp" + "strings" +) + +// NPrintf is a helper function to replace placeholders in a string with values from a map +func NPrintf(format string, params map[string]interface{}) string { + for key, val := range params { + var strVal string + switch val.(type) { + case []string: + r := regexp.MustCompile(`" "`) + strVal = r.ReplaceAllString(fmt.Sprintf("%q", val), `", "`) + default: + strVal = fmt.Sprintf("%v", val) + } + format = strings.Replace(format, "%{"+key+"}", strVal, -1) + } + return format +}