github.com/yaoapp/kun@v0.9.0/str/str.go (about) 1 package str 2 3 import ( 4 "database/sql/driver" 5 "fmt" 6 "regexp" 7 "strings" 8 9 "github.com/yaoapp/kun/any" 10 ) 11 12 // String type of string 13 type String string 14 15 var matchFirstCap = regexp.MustCompile("(.)([A-Z][a-z]+)") 16 var matchAllCap = regexp.MustCompile("([a-z0-9])([A-Z])") 17 18 // Of make a new string 19 func Of(value interface{}) String { 20 return String(fmt.Sprintf("%v", value)) 21 } 22 23 // Bind apply data to the given template 24 func Bind(input string, data interface{}) string { 25 row := any.Of(data).Map().Dot() 26 reg := regexp.MustCompile("{{[ ]*([^\\s]+[ ]*)}}") 27 matchs := reg.FindAllStringSubmatch(input, -1) 28 replaces := map[string]string{} 29 for _, match := range matchs { 30 name := match[0] 31 if _, has := replaces[name]; has { 32 continue 33 } 34 key := match[1] 35 replaces[name] = "" 36 if row.Has(key) && row.Get(key) != nil { 37 replaces[name] = any.Of(row.Get(key)).CString() 38 } 39 } 40 41 for name, value := range replaces { 42 input = strings.ReplaceAll(input, name, value) 43 } 44 45 return input 46 } 47 48 // Bind apply data to the given template 49 func (s String) Bind(data interface{}) string { 50 return Bind(string(s), data) 51 } 52 53 // After The After method returns everything after the given value in a string 54 func After() {} 55 56 // After The After method returns everything after the given value in a string 57 func (s String) After() {} 58 59 // AfterLast The AfterLast method returns everything after the last occurrence of the given value in a string 60 func AfterLast() {} 61 62 // AfterLast The AfterLast method returns everything after the last occurrence of the given value in a string 63 func (s String) AfterLast() {} 64 65 // Append The Append method appends the given values to the string 66 func (s String) Append() {} 67 68 // ASCII The ASCII method will attempt to transliterate the string into an ASCII value 69 func ASCII() {} 70 71 // ASCII The ASCII method will attempt to transliterate the string into an ASCII value 72 func (s String) ASCII() {} 73 74 // BaseName The BaseName method will return the trailing name component of the given string 75 func BaseName() {} 76 77 // BaseName The BaseName method will return the trailing name component of the given string 78 func (s String) BaseName() {} 79 80 // Before The Before method returns everything before the given value in a string 81 func Before() {} 82 83 // Before The Before method returns everything before the given value in a string 84 func (s String) Before() {} 85 86 // BeforeLast The BeforeLast method returns everything before the last occurrence of the given value in a string 87 func BeforeLast() {} 88 89 // BeforeLast The BeforeLast method returns everything before the last occurrence of the given value in a string 90 func (s String) BeforeLast() {} 91 92 // Between The Between method returns the portion of a string between two values 93 func Between() {} 94 95 // Between The Between method returns the portion of a string between two values 96 func (s String) Between() {} 97 98 // Camel The Camel method converts the given string to camelCase 99 func Camel() {} 100 101 // Camel The Camel method converts the given string to camelCase 102 func (s String) Camel() {} 103 104 // Contains The Contains method determines if the given string contains the given value. This method is case sensitive 105 func Contains() {} 106 107 // Contains The Contains method determines if the given string contains the given value. This method is case sensitive 108 func (s String) Contains() {} 109 110 // ContainsAll The ContainsAll method determines if the given string contains all of the values in a given array 111 func ContainsAll() {} 112 113 // ContainsAll The ContainsAll method determines if the given string contains all of the values in a given array 114 func (s String) ContainsAll() {} 115 116 // DirName The DirName method returns the parent directory portion of the given string 117 func DirName() {} 118 119 // DirName The DirName method returns the parent directory portion of the given string 120 func (s String) DirName() {} 121 122 // EndsWith The EndsWith method determines if the given string ends with the given value 123 func EndsWith() {} 124 125 // EndsWith The EndsWith method determines if the given string ends with the given value 126 func (s String) EndsWith() {} 127 128 // Exactly The Exactly method determines if the given string is an exact match with another string 129 func Exactly() {} 130 131 // Exactly The Exactly method determines if the given string is an exact match with another string 132 func (s String) Exactly() {} 133 134 // Explode The Explode method splits the string by the given delimiter and returns a collection containing each section of the split string: 135 func Explode() {} 136 137 // Explode The Explode method splits the string by the given delimiter and returns a collection containing each section of the split string: 138 func (s String) Explode() {} 139 140 // Finish The Finish method adds a single instance of the given value to a string if it does not already end with that value 141 func Finish() {} 142 143 // Finish The Finish method adds a single instance of the given value to a string if it does not already end with that value 144 func (s String) Finish() {} 145 146 // Is The Is method determines if a given string matches a given pattern. Asterisks may be used as wildcard values 147 func Is() {} 148 149 // Is The Is method determines if a given string matches a given pattern. Asterisks may be used as wildcard values 150 func (s String) Is() {} 151 152 // IsASCII The IsASCII method determines if a given string is 7 bit ASCII 153 func IsASCII() {} 154 155 // IsASCII The IsASCII method determines if a given string is 7 bit ASCII 156 func (s String) IsASCII() {} 157 158 // IsEmpty The isEmpty method determines if the given string is empty 159 func (s String) IsEmpty() {} 160 161 // IsNotEmpty The IsNotEmpty method determines if the given string is not empty 162 func (s String) IsNotEmpty() {} 163 164 // IsUUID The IsUUID method determines if the given string is a valid UUID 165 func IsUUID() {} 166 167 // IsUUID The IsUUID method determines if the given string is a valid UUID 168 func (s String) IsUUID() {} 169 170 // Kebab The Kebab method converts the given string to kebab-case 171 func Kebab() {} 172 173 // Kebab The Kebab method converts the given string to kebab-case 174 func (s String) Kebab() {} 175 176 // Length The Length method returns the length of the given string 177 func Length(s string) int { 178 return len(s) 179 } 180 181 // Length The Length method returns the length of the given string 182 func (s String) Length() int { 183 return Length(string(s)) 184 } 185 186 // Limit The limit method truncates the given string to the specified length 187 func Limit() {} 188 189 // Limit The limit method truncates the given string to the specified length 190 func (s String) Limit() {} 191 192 // Lower The Lower method converts the given string to lowercase 193 func Lower() {} 194 195 // Lower The Lower method converts the given string to lowercase 196 func (s String) Lower() {} 197 198 // Ltrim The Ltrim method trims the left side of the string 199 func Ltrim() {} 200 201 // Ltrim The Ltrim method trims the left side of the string 202 func (s String) Ltrim() {} 203 204 // Markdown The Markdown method converts GitHub flavored Markdown into HTML 205 func Markdown() {} 206 207 // Markdown The Markdown method converts GitHub flavored Markdown into HTML 208 func (s String) Markdown() {} 209 210 // Match The Match method will return the portion of a string that matches a given regular expression pattern 211 func Match() {} 212 213 // Match The Match method will return the portion of a string that matches a given regular expression pattern 214 func (s String) Match() {} 215 216 // MatchAll The MatchAll method will return a collection containing the portions of a string that match a given regular expression pattern 217 func MatchAll() {} 218 219 // MatchAll The MatchAll method will return a collection containing the portions of a string that match a given regular expression pattern 220 func (s String) MatchAll() {} 221 222 // OrderedUUID The OrderedUUID method generates a "timestamp first" UUID that may be efficiently stored in an indexed database column. 223 // Each UUID that is generated using this method will be sorted after UUIDs previously generated using the method 224 func OrderedUUID() {} 225 226 // PadBoth The PadBoth method padding both sides of a string with another string until the final string reaches a desired length 227 func PadBoth() {} 228 229 // PadBoth The PadBoth method padding both sides of a string with another string until the final string reaches a desired length 230 func (s String) PadBoth() {} 231 232 // PadLeft The PadLeft method padding the left side of a string with another string until the final string reaches a desired length 233 func PadLeft() {} 234 235 // PadLeft The PadLeft method padding the left side of a string with another string until the final string reaches a desired length 236 func (s String) PadLeft() {} 237 238 // PadRight The PadRight method padding the right side of a string with another string until the final string reaches a desired length 239 func PadRight() {} 240 241 // PadRight The PadRight method padding the right side of a string with another string until the final string reaches a desired length 242 func (s String) PadRight() {} 243 244 // Pipe The pipe method allows you to transform the string by passing its current value to the given callable 245 func Pipe() {} 246 247 // Pipe The pipe method allows you to transform the string by passing its current value to the given callable 248 func (s String) Pipe() {} 249 250 // Random The Random method generates a random string of the specified length. 251 func Random() {} 252 253 // Prepend The Prepend method prepends the given values onto the string 254 func Prepend() {} 255 256 // Prepend The Prepend method prepends the given values onto the string 257 func (s String) Prepend() {} 258 259 // Remove The Remove method removes the given value or array of values from the string: 260 func Remove() {} 261 262 // Remove The Remove method removes the given value or array of values from the string: 263 func (s String) Remove() {} 264 265 // Replace The Replace method replaces a given string within the string: 266 func Replace(s string, old string, new string, n int) string { 267 return strings.Replace(s, old, new, n) 268 } 269 270 // Replace The Replace method replaces a given string within the string: 271 func (s String) Replace(old string, new string, n int) string { 272 return strings.Replace(string(s), old, new, n) 273 } 274 275 // ReplaceArray The ReplaceArray method replaces a given value in the string sequentially using an array 276 func ReplaceArray() {} 277 278 // ReplaceArray The ReplaceArray method replaces a given value in the string sequentially using an array 279 func (s String) ReplaceArray() {} 280 281 // ReplaceFirst The ReplaceFirst method replaces the first occurrence of a given value in a string 282 func ReplaceFirst() {} 283 284 // ReplaceFirst The ReplaceFirst method replaces the first occurrence of a given value in a string 285 func (s String) ReplaceFirst() {} 286 287 // ReplaceLast The ReplaceLast method replaces the last occurrence of a given value in a string 288 func ReplaceLast() {} 289 290 // ReplaceLast The ReplaceLast method replaces the last occurrence of a given value in a string 291 func (s String) ReplaceLast() {} 292 293 // ReplaceMatches The replaceMatches method replaces all portions of a string matching a pattern with the given replacement string: 294 func ReplaceMatches() {} 295 296 // ReplaceMatches The replaceMatches method replaces all portions of a string matching a pattern with the given replacement string: 297 func (s String) ReplaceMatches() {} 298 299 // Rtrim The Rtrim method trims the right side of the given string 300 func Rtrim() {} 301 302 // Rtrim The Rtrim method trims the right side of the given string 303 func (s String) Rtrim() {} 304 305 // Slug The Slug method generates a URL friendly "slug" from the given string 306 func Slug() {} 307 308 // Slug The Slug method generates a URL friendly "slug" from the given string 309 func (s String) Slug() {} 310 311 // Snake The Snake method converts the given string to snake_case 312 func Snake(str string) string { 313 snake := matchFirstCap.ReplaceAllString(str, "${1}_${2}") 314 snake = matchAllCap.ReplaceAllString(snake, "${1}_${2}") 315 return strings.ToLower(snake) 316 } 317 318 // Snake The Snake method converts the given string to snake_case 319 func (s String) Snake() string { 320 return Snake(string(s)) 321 } 322 323 // Split The Split method splits a string into a collection using a regular expression 324 func Split() {} 325 326 // Split The Split method splits a string into a collection using a regular expression 327 func (s String) Split() {} 328 329 // Start The Start method adds a single instance of the given value to a string if it does not already start with that value 330 func Start() {} 331 332 // Start The Start method adds a single instance of the given value to a string if it does not already start with that value 333 func (s String) Start() {} 334 335 // StartsWith The StartsWith method determines if the given string begins with the given value 336 func StartsWith() {} 337 338 // StartsWith The StartsWith method determines if the given string begins with the given value 339 func (s String) StartsWith() {} 340 341 // Studly The Studly method converts the given string to StudlyCase 342 func Studly() {} 343 344 // Studly The Studly method converts the given string to StudlyCase 345 func (s String) Studly() {} 346 347 // Substr The Substr method returns the portion of string specified by the start and length parameters 348 func Substr() {} 349 350 // Substr The Substr method returns the portion of string specified by the start and length parameters 351 func (s String) Substr() {} 352 353 // SubstrCount The Str::substrCount method returns the number of occurrences of a given value in the given string 354 func SubstrCount() {} 355 356 // Tap The Tap method passes the string to the given closure, 357 // allowing you to examine and interact with the string while not affecting the string itself. 358 // The original string is returned by the tap method regardless of what is returned by the closure 359 func Tap() {} 360 361 // Tap The Tap method passes the string to the given closure, 362 // allowing you to examine and interact with the string while not affecting the string itself. 363 // The original string is returned by the tap method regardless of what is returned by the closure 364 func (s String) Tap() {} 365 366 // Test The Test method determines if a string matches the given regular expression pattern 367 func Test() {} 368 369 // Test The Test method determines if a string matches the given regular expression pattern 370 func (s String) Test() {} 371 372 // Title The Title method converts the given string to Title Case 373 func Title() {} 374 375 // Title The Title method converts the given string to Title Case 376 func (s String) Title() {} 377 378 // Trim The Trim method trims the given string 379 func Trim() {} 380 381 // Trim The Trim method trims the given string 382 func (s String) Trim() {} 383 384 // Ucfirst The Str::Ucfirst method returns the given string with the first character capitalized 385 func Ucfirst() {} 386 387 // Ucfirst The Str::Ucfirst method returns the given string with the first character capitalized 388 func (s String) Ucfirst() {} 389 390 // Upper The Upper method converts the given string to uppercase 391 func Upper() {} 392 393 // Upper The Upper method converts the given string to uppercase 394 func (s String) Upper() {} 395 396 // When The When method invokes the given closure if a given condition is true. The closure will receive the fluent string instance 397 func (s String) When() {} 398 399 // WhenEmpty The WhenEmpty method invokes the given closure if the string is empty. If the closure returns a value, that value will also be returned by the whenEmpty method. If the closure does not return a value, the fluent string instance will be returned 400 func (s String) WhenEmpty() {} 401 402 // UUID The UUID method generates a UUID (version 4) 403 func UUID() {} 404 405 // WordCount The WordCount function returns the number of words that a string contains 406 func WordCount() {} 407 408 // WordCount The WordCount function returns the number of words that a string contains 409 func (s String) WordCount() {} 410 411 // Words The Words method limits the number of words in a string. 412 // An additional string may be passed to this method via its third argument to specify which string should be appended to the end of the truncated string 413 func Words() {} 414 415 // Words The Words method limits the number of words in a string. 416 // An additional string may be passed to this method via its third argument to specify which string should be appended to the end of the truncated string 417 func (s String) Words() {} 418 419 // MarshalJSON for json MarshalJSON 420 func (s *String) MarshalJSON() ([]byte, error) { 421 return []byte(*s), nil 422 } 423 424 // UnmarshalJSON for json UnmarshalJSON 425 func (s *String) UnmarshalJSON(data []byte) error { 426 *s = String(strings.Trim(string(data), `"`)) 427 return nil 428 } 429 430 // Scan for db scan 431 func (s *String) Scan(src interface{}) error { 432 *s = Of(src) 433 return nil 434 } 435 436 // Value for db driver value 437 func (s *String) Value() (driver.Value, error) { 438 return string(*s), nil 439 }