diff --git a/logs/logs_test.go b/logs/logs_test.go index 2eb9ff7620a486d739fea834e221a04e0bc8044c..4abfb867f46b800a6d5d6105abfe1175addea5d3 100644 --- a/logs/logs_test.go +++ b/logs/logs_test.go @@ -30,3 +30,8 @@ func TestLogs(t *testing.T) { // api_logs.Errorf("Debugging %d!", 456) // api_logs.Error("Info") } + +func TestSanitiseLogs(t *testing.T) { + logString := "{\n \"emailVerificationStatus\": true,\n \"keyValueMetadata\": [],\n \"stepUp\": null,\n \"riskAdvice\": null\n}" + logs.SanitiseLogs(logString) +} diff --git a/string_utils/string_utils.go b/string_utils/string_utils.go index efa87313a72cf85fd3838ed6ae93fe7dc538646f..a5721d787c2cfc208bc8ad81bf8c7a5d787c010f 100644 --- a/string_utils/string_utils.go +++ b/string_utils/string_utils.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/json" "fmt" + "github.com/samber/lo" "gitlab.bob.co.za/bob-public-utils/bobgroup-go-utils/number_utils" "regexp" "strconv" @@ -65,6 +66,10 @@ func ReplaceAllRegexStringSubmatch(re *regexp.Regexp, s string, replaceWith stri lastIndex := 0 for _, v := range re.FindAllSubmatchIndex([]byte(s), -1) { + if lo.Contains(v, -1) { + continue + } + if len(v) == regexIndexSubmatchEnd+1 { // One submatch - replace the submatch with replaceWith result += s[lastIndex:v[regexIndexSubmatchStart]] + replaceWith + s[v[regexIndexSubmatchEnd]:v[regexIndexMatchEnd]]