Skip to content
Snippets Groups Projects
Select Git revision
  • f3563244409f9b00cd717452c88524bfc0d5de7d
  • main default protected
  • trading_hours
  • refactor_trading_hours
  • audit_cleaning_cater_for_non_struct_fields
  • remove-info-logs
  • sl-refactor
  • 18-use-scan-for-param-values
  • 17-order-search-results
  • 4-simplify-framework-2
  • 1-http-error
  • v1.297.0
  • v1.296.0
  • v1.295.0
  • v1.294.0
  • v1.293.0
  • v1.292.0
  • v1.291.0
  • v1.290.0
  • v1.289.0
  • v1.288.0
  • v1.287.0
  • v1.286.0
  • v1.285.0
  • v1.284.0
  • v1.283.0
  • v1.282.0
  • v1.281.0
  • v1.280.0
  • v1.279.0
  • v1.278.0
31 results

string_utils.go

Blame
  • string_utils.go 7.39 KiB
    package string_utils
    
    import (
    	"encoding/json"
    	"fmt"
    	"net/url"
    	"regexp"
    	"strconv"
    	"strings"
    	"unicode"
    
    	"github.com/thoas/go-funk"
    	"golang.org/x/text/runes"
    	"golang.org/x/text/transform"
    	"golang.org/x/text/unicode/norm"
    )
    
    const snakeCasePattern = `[a-z]([a-z0-9_]*[a-z0-9])*`
    
    var snakeCaseRegex = regexp.MustCompile("^" + snakeCasePattern + "$")
    
    func IsSnakeCase(name string) bool {
    	return snakeCaseRegex.MatchString(name)
    }
    
    func SnakeToKebabString(s string) string {
    	s = strings.TrimSpace(s)
    
    	re := regexp.MustCompile("(_)")
    	s = re.ReplaceAllString(s, "-")
    
    	return s
    }
    
    // ReplaceNonSpacingMarks removes diacritics e.g. êžů becomes ezu
    func ReplaceNonSpacingMarks(str string) string {
    	t := transform.Chain(norm.NFD, runes.Remove(runes.In(unicode.Mn)), norm.NFC) // Mn: non-spacing marks
    	result, _, _ := transform.String(t, str)
    	return result
    }
    
    func RemoveAllWhiteSpaces(s string) string {
    	return strings.ReplaceAll(strings.ReplaceAll(s, " ", ""), "\t", "")
    }
    
    func ReplaceCaseInsensitive(string, toReplace, replaceWith string) string {
    	regex := regexp.MustCompile("(?i)" + strings.ToLower(toReplace))
    	return regex.ReplaceAllString(string, replaceWith)
    }
    
    // TrimQuotes - trims quotes from a string (ie: "foo" will return foo)
    func TrimQuotes(stringToTrim string) string {
    	if len(stringToTrim) >= 2 {
    		if stringToTrim[0] == '"' && stringToTrim[len(stringToTrim)-1] == '"' {
    			return stringToTrim[1 : len(stringToTrim)-1]
    		}
    	}
    
    	return stringToTrim
    }
    
    // IsNumericString returns true if the string can be converted to a float without error
    func IsNumericString(s string) bool {
    	_, err := strconv.ParseFloat(s, 64)
    	return err == nil
    }
    
    // StandardisePhoneNumber standardises phone numbers with +27 instead of 0 prefix
    func StandardisePhoneNumber(number string) string {
    	// is the first rune/char of the string a 0