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 +}