github.com/mundipagg/boleto-api@v0.0.0-20230620145841-3f9ec742599f/util/string.go (about) 1 package util 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 8 "encoding/json" 9 10 "github.com/tdewolff/minify" 11 "github.com/tdewolff/minify/css" 12 "github.com/tdewolff/minify/html" 13 "github.com/tdewolff/minify/js" 14 jm "github.com/tdewolff/minify/json" 15 ) 16 17 // PadLeft insere um caractere a esquerda de um texto 18 func PadLeft(value, char string, total uint) string { 19 s := "%" + char + strconv.Itoa(int(total)) + "s" 20 return fmt.Sprintf(s, value) 21 } 22 23 //Stringify convete objeto para JSON 24 func Stringify(o interface{}) string { 25 b, _ := json.Marshal(o) 26 return string(b) 27 } 28 29 //ParseJSON converte string para um objeto GO 30 func ParseJSON(s string, o interface{}) interface{} { 31 json.Unmarshal([]byte(s), o) 32 return o 33 } 34 35 //IsDigit Verifica se um caracter é um dígito numérico de acordo com o código decimal da Tabela ASCII, 36 // onde o '0' representa o valor 48 e o '9' o valor 57 37 func IsDigit(r rune) bool { 38 return (r >= 48 && r <= 57) 39 } 40 41 //IsBasicCharacter Verifica se um caracter é uma letra sem acento, maiúscula ou minúscula, de acordo com o código decimal da Tabela ASCII 42 // onde o 'A' representa o valor 65 e o 'Z' o valor 90 e o 'a' representa o valor 97 e 'z' o valor 122 43 //true para 0123456789 44 func IsBasicCharacter(r rune) bool { 45 return (r >= 65 && r <= 90) || (r >= 97 && r <= 122) 46 } 47 48 //IsCaixaSpecialCharacter Verifica se um caracter especial é aceito Caixa Econômica, de acordo com o código decimal da Tabela ASCII 49 // sendo aceito os seguinte caracteres: 50 // esp 32 ' 39 : 58 51 // ! 33 ( 40 ; 59 52 // ) 41 = 61 53 // * 42 54 // + 43 ? 63 55 // , 44 56 // - 45 _ 95 57 // . 46 58 // / 47 59 // 60 // OBS: Apesar de descritos como aceitos, os caracteres & (38) < (60) e > (62) foram removidos pois não 61 // estão disponíveis para XML. Testamos seus respectivos encodes: & < > entretanto recebemos a 62 // resposta (66) CARACTER INVALIDO. 63 func IsCaixaSpecialCharacter(r rune) bool { 64 caixaSpecialCharacters := []rune{32, 33, 39, 40, 41, 42, 43, 44, 45, 46, 47, 58, 59, 61, 63, 95} 65 for _, c := range caixaSpecialCharacters { 66 if r == c { 67 return true 68 } 69 } 70 return false 71 } 72 73 //Remove o caractete \t de uma string 74 func SanitizeBody(body string) string { 75 return strings.Replace(body, "\t", "", -1) 76 } 77 78 //MinifyString Minifica uma string de acordo com um determinado formato 79 func MinifyString(mString, tp string) string { 80 m := minify.New() 81 m.Add("text/html", &html.Minifier{ 82 KeepDocumentTags: true, 83 KeepEndTags: true, 84 KeepWhitespace: false, 85 KeepConditionalComments: true, 86 }) 87 m.AddFunc("text/css", css.Minify) 88 m.AddFunc("text/javascript", js.Minify) 89 m.AddFunc("application/json", jm.Minify) 90 91 s, err := m.String(tp, mString) 92 93 if err != nil { 94 return mString 95 } 96 97 return s 98 }