From ddc50028c3c1db8e4467beccec7bc830ed7970d8 Mon Sep 17 00:00:00 2001 From: fiatjaf Date: Tue, 28 Nov 2023 22:23:51 -0300 Subject: [PATCH] add NormalizeOKMessage() helper. --- normalize.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/normalize.go b/normalize.go index a774f74..168da12 100644 --- a/normalize.go +++ b/normalize.go @@ -5,7 +5,8 @@ import ( "strings" ) -// NormalizeURL normalizes the url and replaces http://, https:// schemes by ws://, wss://. +// NormalizeURL normalizes the url and replaces http://, https:// schemes with ws://, wss:// +// and normalizes the path. func NormalizeURL(u string) string { if u == "" { return "" @@ -32,3 +33,12 @@ func NormalizeURL(u string) string { return p.String() } + +// NormalizeOKMessage takes a string message that is to be sent in an `OK` or `CLOSED` command +// and prefixes it with ": " if it doesn't already have an acceptable prefix. +func NormalizeOKMessage(reason string, prefix string) string { + if idx := strings.Index(reason, ": "); idx == -1 || strings.IndexByte(reason[0:idx], ' ') != -1 { + return prefix + ": " + reason + } + return reason +}