Cleanup utility functions and add comments

This commit is contained in:
Jeffrey Picard 2021-06-23 15:29:23 -04:00
parent 931d786c52
commit 87bf89a109

View file

@ -16,41 +16,36 @@ func StringSplitArg(stringToSplit, separator string) []interface{} {
return splitInterface return splitInterface
} }
func Normalize(s string) string { // NormalizeName Normalize names to remove weird characters and account to capitalization
func NormalizeName(s string) string {
c := cases.Fold() c := cases.Fold()
return c.String(norm.NFD.String(s)) return c.String(norm.NFD.String(s))
} }
// ReverseBytesInPlace reverse the bytes. thanks, Satoshi 😒
func ReverseBytes(s []byte) { func ReverseBytesInPlace(s []byte) {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i] s[i], s[j] = s[j], s[i]
} }
} }
// convert txid to txHash // TxIdToTxHash convert the txid to a hash for returning from the hub
func ToHash(txid string) []byte { func TxIdToTxHash(txid string) []byte {
t, err := hex.DecodeString(txid) t, err := hex.DecodeString(txid)
if err != nil { if err != nil {
return nil return nil
} }
// reverse the bytes. thanks, Satoshi 😒 ReverseBytesInPlace(t)
for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 {
t[i], t[j] = t[j], t[i]
}
return t return t
} }
// convert txHash to txid // TxHashToTxId convert the txHash from the response format back to an id
func FromHash(txHash []byte) string { func TxHashToTxId(txHash []byte) string {
t := make([]byte, len(txHash)) t := make([]byte, len(txHash))
copy(t, txHash) copy(t, txHash)
// reverse the bytes. thanks, Satoshi 😒 ReverseBytesInPlace(t)
for i, j := 0, len(txHash)-1; i < j; i, j = i+1, j-1 {
txHash[i], txHash[j] = txHash[j], txHash[i]
}
return hex.EncodeToString(t) return hex.EncodeToString(t)