github.com/sohaha/zlsgo@v1.7.13-0.20240501141223-10dd1a906f76/zvalid/format.go (about) 1 package zvalid 2 3 import ( 4 "strings" 5 6 "golang.org/x/crypto/bcrypt" 7 8 "github.com/sohaha/zlsgo/zstring" 9 ) 10 11 // Trim remove leading and trailing spaces 12 func (v Engine) Trim() Engine { 13 return pushQueue(&v, func(v *Engine) *Engine { 14 if notEmpty(v) { 15 v.value = zstring.TrimSpace(v.value) 16 } 17 18 return v 19 }) 20 } 21 22 // RemoveSpace remove all spaces 23 func (v Engine) RemoveSpace() Engine { 24 return pushQueue(&v, func(v *Engine) *Engine { 25 if notEmpty(v) { 26 v.value = strings.Replace(v.value, " ", "", -1) 27 } 28 return v 29 }) 30 } 31 32 // Replace replace text 33 func (v Engine) Replace(old, new string, n int) Engine { 34 return pushQueue(&v, func(v *Engine) *Engine { 35 if notEmpty(v) { 36 v.value = strings.Replace(v.value, old, new, n) 37 } 38 return v 39 }) 40 } 41 42 // ReplaceAll replace all text 43 func (v Engine) ReplaceAll(old, new string) Engine { 44 return v.Replace(old, new, -1) 45 } 46 47 // XSSClean clean html tag 48 func (v Engine) XSSClean() Engine { 49 return pushQueue(&v, func(v *Engine) *Engine { 50 if notEmpty(v) { 51 v.value = zstring.XSSClean(v.value) 52 } 53 return v 54 }) 55 } 56 57 // SnakeCaseToCamelCase snakeCase To CamelCase: hello_world => helloWorld 58 func (v Engine) SnakeCaseToCamelCase(ucfirst bool, delimiter ...string) Engine { 59 return pushQueue(&v, func(v *Engine) *Engine { 60 if notEmpty(v) { 61 v.value = zstring.SnakeCaseToCamelCase(v.value, ucfirst, delimiter...) 62 } 63 return v 64 }) 65 } 66 67 // CamelCaseToSnakeCase camelCase To SnakeCase helloWorld/HelloWorld => hello_world 68 func (v Engine) CamelCaseToSnakeCase(delimiter ...string) Engine { 69 return pushQueue(&v, func(v *Engine) *Engine { 70 if notEmpty(v) { 71 v.value = zstring.CamelCaseToSnakeCase(v.value, delimiter...) 72 } 73 return v 74 }) 75 } 76 77 // EncryptPassword encrypt the password 78 func (v Engine) EncryptPassword(cost ...int) Engine { 79 return pushQueue(&v, func(v *Engine) *Engine { 80 if notEmpty(v) { 81 bcost := bcrypt.DefaultCost 82 if len(cost) > 0 { 83 bcost = cost[0] 84 } 85 if bytes, err := bcrypt.GenerateFromPassword(zstring.String2Bytes(v.value), bcost); err == nil { 86 v.value = zstring.Bytes2String(bytes) 87 } else { 88 v.err = err 89 } 90 } 91 return v 92 }) 93 }