1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 17:22:27 +08:00

doc: update document and add playgound example for strutil package

This commit is contained in:
dudaodong
2022-12-27 16:57:32 +08:00
parent a58e52e53c
commit 49a460eef8
5 changed files with 103 additions and 51 deletions

View File

@@ -478,26 +478,62 @@ import "github.com/duke-git/lancet/v2/strutil"
#### Function list: #### Function list:
- **<big>After</big>** : returns the substring after the first occurrence of a specified string in the source string. - **<big>After</big>** : returns the substring after the first occurrence of a specified string in the source string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#After)] [[play](https://go.dev/play/p/RbCOQqCDA7m)] [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#After)]
- **<big>AfterLast</big>** : [doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#AfterLast) [[play](https://go.dev/play/p/RbCOQqCDA7m)]
- [Before](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Before) - **<big>AfterLast</big>** : returns the substring after the last occurrence of a specified string in the source string. [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#AfterLast)]
- [BeforeLast](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BeforeLast) [[play](https://go.dev/play/p/1TegARrb8Yn)]
- [CamelCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#CamelCase) - **<big>Before</big>** : returns the substring before the first occurrence of a specified string in the source string.
- [Capitalize](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Capitalize) [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Before)]
- [IsString](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsString) [[play](https://go.dev/play/p/JAWTZDS4F5w)]
- [KebabCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#KebabCase) - **<big>BeforeLast</big>** : returns the substring before the last occurrence of a specified string in the source string.
- [UpperKebabCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperKebabCase) [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BeforeLast)]
- [LowerFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#LowerFirst) [[play](https://go.dev/play/p/pJfXXAoG_Te)]
- [UpperFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperFirst) - **<big>CamelCase</big>** : coverts source string to its camelCase string.
- [PadEnd](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadEnd) [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#CamelCase)]
- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadStart) [[play](https://go.dev/play/p/9eXP3tn2tUy)]
- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Reverse) - **<big>Capitalize</big>** : converts the first character of source string to upper case and the remaining to lower case.
- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SnakeCase) [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Capitalize)]
- [UpperSnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperSnakeCase) [[play](https://go.dev/play/p/2OAjgbmAqHZ)]
- [SplitEx](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx) - **<big>IsString</big>** : checks if the parameter value data type is string or not.
- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap) [[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsString)]
- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Unwrap) [[play](https://go.dev/play/p/IOgq7oF9ERm)]
- **<big>KebabCase</big>** : coverts string to kebab-case string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#KebabCase)]
[[play](https://go.dev/play/p/dcZM9Oahw-Y)]
- **<big>UpperKebabCase</big>** : coverts string to upper KEBAB-CASE string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperKebabCase)]
[[play](https://go.dev/play/p/zDyKNneyQXk)]
- **<big>LowerFirst</big>** : converts the first character of string to lower case.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#LowerFirst)]
[[play](https://go.dev/play/p/CbzAyZmtJwL)]
- **<big>UpperFirst</big>** : converts the first character of string to upper case.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperFirst)]
[[play](https://go.dev/play/p/sBbBxRbs8MM)]
- **<big>PadEnd</big>** : pads string with given characters on the right side if it's shorter than limit size. Padding characters are truncated if they exceed size.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadEnd)]
[[play](https://go.dev/play/p/9xP8rN0vz--)]
- **<big>PadStart</big>** : pads string with given characters on the left side if it's shorter than limit size. Padding characters are truncated if they exceed size.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadStart)]
[[play](https://go.dev/play/p/xpTfzArDfvT)]
- **<big>Reverse</big>** : returns string whose char order is reversed to the given string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Reverse)]
[[play](https://go.dev/play/p/adfwalJiecD)]
- **<big>SnakeCase</big>** : coverts string to snake_case string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SnakeCase)]
[[play](https://go.dev/play/p/tgzQG11qBuN)]
- **<big>UpperSnakeCase</big>** : coverts string to upper SNAKE_CASE string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperSnakeCase)]
[[play](https://go.dev/play/p/4COPHpnLx38)]
- **<big>SplitEx</big>** : split a given string which can control the result slice contains empty string or not.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx)]
[[play](https://go.dev/play/p/Us-ySSbWh-3)]
- **<big>Wrap</big>** : wrap a string with given string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap)]
[[play](https://go.dev/play/p/KoZOlZDDt9y)]
- **<big>Unwrap</big>** : unwrap a given string from anther string. will change source string.
[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Unwrap)]
[[play](https://go.dev/play/p/Ec2q4BzCpG-)]
### 19. System package contain some functions about os, runtime, shell command. ### 19. System package contain some functions about os, runtime, shell command.

View File

@@ -77,7 +77,7 @@ func main() {
### <span id="AfterLast">AfterLast</span> ### <span id="AfterLast">AfterLast</span>
<p>Creates substring in source string after position when char last appear.</p> <p>Returns the substring after the last occurrence of a specified string in the source string.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -108,7 +108,7 @@ func main() {
### <span id="Before">Before</span> ### <span id="Before">Before</span>
<p>Creates substring in source string before position when char first appear.</p> <p>Returns the substring of the source string up to the first occurrence of the specified string.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -139,7 +139,7 @@ func main() {
### <span id="BeforeLast">BeforeLast</span> ### <span id="BeforeLast">BeforeLast</span>
<p>Creates substring in source string before position when char first appear.</p> <p>Returns the substring of the source string up to the last occurrence of the specified string.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -633,7 +633,7 @@ func main() {
### <span id="Wrap">Wrap</span> ### <span id="Wrap">Wrap</span>
<p>Wrap a string with another string.</p> <p>Wrap a string with given string.</p>
<b>Signature:</b> <b>Signature:</b>
@@ -670,7 +670,7 @@ func main() {
### <span id="Wrap">Wrap</span> ### <span id="Wrap">Wrap</span>
<p>Unwrap a given string from anther string. will change str value.</p> <p>Unwrap a given string from anther string. will change source string.</p>
<b>Signature:</b> <b>Signature:</b>

View File

@@ -78,7 +78,7 @@ func main() {
### <span id="AfterLast">AfterLast</span> ### <span id="AfterLast">AfterLast</span>
<p>截取源字符串中char最后一次出现时的位置之后的子字符串</p> <p>返回源字符串中指定字符串最后一次出现时的位置之后的子字符串</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -109,7 +109,7 @@ func main() {
### <span id="Before">Before</span> ### <span id="Before">Before</span>
<p>截取源字符串中char首次出现时的位置之前的子字符串</p> <p>返回源字符串中指定字符串第一次出现时的位置之前的子字符串</p>
<b>函数签名:</b> <b>函数签名:</b>
@@ -140,7 +140,7 @@ func main() {
### <span id="BeforeLast">BeforeLast</span> ### <span id="BeforeLast">BeforeLast</span>
<p>截取源字符串中char最后一次出现时的位置之前的子字符串</p> <p>返回源字符串中指定字符串最后一次出现时的位置之前的子字符串</p>
<b>函数签名:</b> <b>函数签名:</b>

View File

@@ -9,9 +9,8 @@ import (
"unicode/utf8" "unicode/utf8"
) )
// CamelCase covert string to camelCase string. // CamelCase coverts string to camelCase string. Non letters and numbers will be ignored.
// non letters and numbers will be ignored // Play: https://go.dev/play/p/9eXP3tn2tUy
// eg. "Foo-#1😄$_%^&*(1bar" => "foo11Bar"
func CamelCase(s string) string { func CamelCase(s string) string {
var builder strings.Builder var builder strings.Builder
@@ -28,6 +27,7 @@ func CamelCase(s string) string {
} }
// Capitalize converts the first character of a string to upper case and the remaining to lower case. // Capitalize converts the first character of a string to upper case and the remaining to lower case.
// Play: https://go.dev/play/p/2OAjgbmAqHZ
func Capitalize(s string) string { func Capitalize(s string) string {
result := make([]rune, len(s)) result := make([]rune, len(s))
for i, v := range s { for i, v := range s {
@@ -42,6 +42,7 @@ func Capitalize(s string) string {
} }
// UpperFirst converts the first character of string to upper case. // UpperFirst converts the first character of string to upper case.
// Play: https://go.dev/play/p/sBbBxRbs8MM
func UpperFirst(s string) string { func UpperFirst(s string) string {
if len(s) == 0 { if len(s) == 0 {
return "" return ""
@@ -54,6 +55,7 @@ func UpperFirst(s string) string {
} }
// LowerFirst converts the first character of string to lower case. // LowerFirst converts the first character of string to lower case.
// Play: https://go.dev/play/p/CbzAyZmtJwL
func LowerFirst(s string) string { func LowerFirst(s string) string {
if len(s) == 0 { if len(s) == 0 {
return "" return ""
@@ -67,6 +69,7 @@ func LowerFirst(s string) string {
// PadEnd pads string on the right side if it's shorter than size. // PadEnd pads string on the right side if it's shorter than size.
// Padding characters are truncated if they exceed size. // Padding characters are truncated if they exceed size.
// Play: https://go.dev/play/p/9xP8rN0vz--
func PadEnd(source string, size int, padStr string) string { func PadEnd(source string, size int, padStr string) string {
len1 := len(source) len1 := len(source)
len2 := len(padStr) len2 := len(padStr)
@@ -86,6 +89,7 @@ func PadEnd(source string, size int, padStr string) string {
// PadStart pads string on the left side if it's shorter than size. // PadStart pads string on the left side if it's shorter than size.
// Padding characters are truncated if they exceed size. // Padding characters are truncated if they exceed size.
// Play: https://go.dev/play/p/xpTfzArDfvT
func PadStart(source string, size int, padStr string) string { func PadStart(source string, size int, padStr string) string {
len1 := len(source) len1 := len(source)
len2 := len(padStr) len2 := len(padStr)
@@ -103,39 +107,36 @@ func PadStart(source string, size int, padStr string) string {
return fill[0:size-len1] + source return fill[0:size-len1] + source
} }
// KebabCase covert string to kebab-case // KebabCase coverts string to kebab-case, non letters and numbers will be ignored.
// non letters and numbers will be ignored // Play: https://go.dev/play/p/dcZM9Oahw-Y
// eg. "Foo-#1😄$_%^&*(1bar" => "foo-1-1-bar"
func KebabCase(s string) string { func KebabCase(s string) string {
result := splitIntoStrings(s, false) result := splitIntoStrings(s, false)
return strings.Join(result, "-") return strings.Join(result, "-")
} }
// UpperKebabCase covert string to upper KEBAB-CASE // UpperKebabCase coverts string to upper KEBAB-CASE, non letters and numbers will be ignored
// non letters and numbers will be ignored // Play: https://go.dev/play/p/zDyKNneyQXk
// eg. "Foo-#1😄$_%^&*(1bar" => "FOO-1-1-BAR"
func UpperKebabCase(s string) string { func UpperKebabCase(s string) string {
result := splitIntoStrings(s, true) result := splitIntoStrings(s, true)
return strings.Join(result, "-") return strings.Join(result, "-")
} }
// SnakeCase covert string to snake_case // SnakeCase coverts string to snake_case, non letters and numbers will be ignored
// non letters and numbers will be ignored // Play: https://go.dev/play/p/tgzQG11qBuN
// eg. "Foo-#1😄$_%^&*(1bar" => "foo_1_1_bar"
func SnakeCase(s string) string { func SnakeCase(s string) string {
result := splitIntoStrings(s, false) result := splitIntoStrings(s, false)
return strings.Join(result, "_") return strings.Join(result, "_")
} }
// UpperSnakeCase covert string to upper SNAKE_CASE // UpperSnakeCase coverts string to upper SNAKE_CASE, non letters and numbers will be ignored
// non letters and numbers will be ignored // Play: https://go.dev/play/p/4COPHpnLx38
// eg. "Foo-#1😄$_%^&*(1bar" => "FOO_1_1_BAR"
func UpperSnakeCase(s string) string { func UpperSnakeCase(s string) string {
result := splitIntoStrings(s, true) result := splitIntoStrings(s, true)
return strings.Join(result, "_") return strings.Join(result, "_")
} }
// Before create substring in source string before position when char first appear // Before returns the substring of the source string up to the first occurrence of the specified string.
// Play: https://go.dev/play/p/JAWTZDS4F5w
func Before(s, char string) string { func Before(s, char string) string {
if s == "" || char == "" { if s == "" || char == "" {
return s return s
@@ -144,7 +145,8 @@ func Before(s, char string) string {
return s[0:i] return s[0:i]
} }
// BeforeLast create substring in source string before position when char last appear // BeforeLast returns the substring of the source string up to the last occurrence of the specified string.
// Play: https://go.dev/play/p/pJfXXAoG_Te
func BeforeLast(s, char string) string { func BeforeLast(s, char string) string {
if s == "" || char == "" { if s == "" || char == "" {
return s return s
@@ -163,7 +165,8 @@ func After(s, char string) string {
return s[i+len(char):] return s[i+len(char):]
} }
// AfterLast create substring in source string after position when char last appear // AfterLast returns the substring after the last occurrence of a specified string in the source string.
// Play: https://go.dev/play/p/1TegARrb8Yn
func AfterLast(s, char string) string { func AfterLast(s, char string) string {
if s == "" || char == "" { if s == "" || char == "" {
return s return s
@@ -173,6 +176,7 @@ func AfterLast(s, char string) string {
} }
// IsString check if the value data type is string or not. // IsString check if the value data type is string or not.
// Play: https://go.dev/play/p/IOgq7oF9ERm
func IsString(v any) bool { func IsString(v any) bool {
if v == nil { if v == nil {
return false return false
@@ -185,7 +189,8 @@ func IsString(v any) bool {
} }
} }
// Reverse return string whose char order is reversed to the given string // Reverse returns string whose char order is reversed to the given string.
// Play: https://go.dev/play/p/adfwalJiecD
func Reverse(s string) string { func Reverse(s string) string {
r := []rune(s) r := []rune(s)
for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 { for i, j := 0, len(r)-1; i < j; i, j = i+1, j-1 {
@@ -194,7 +199,8 @@ func Reverse(s string) string {
return string(r) return string(r)
} }
// Wrap a string with another string. // Wrap a string with given string.
// Play: https://go.dev/play/p/KoZOlZDDt9y
func Wrap(str string, wrapWith string) string { func Wrap(str string, wrapWith string) string {
if str == "" || wrapWith == "" { if str == "" || wrapWith == "" {
return str return str
@@ -207,7 +213,8 @@ func Wrap(str string, wrapWith string) string {
return sb.String() return sb.String()
} }
// Unwrap a given string from anther string. will change str value // Unwrap a given string from anther string. will change source string.
// Play: https://go.dev/play/p/Ec2q4BzCpG-
func Unwrap(str string, wrapToken string) string { func Unwrap(str string, wrapToken string) string {
if str == "" || wrapToken == "" { if str == "" || wrapToken == "" {
return str return str
@@ -225,7 +232,8 @@ func Unwrap(str string, wrapToken string) string {
return str return str
} }
// SplitEx split a given string whether the result contains empty string // SplitEx split a given string which can control the result slice contains empty string or not.
// Play: https://go.dev/play/p/Us-ySSbWh-3
func SplitEx(s, sep string, removeEmptyString bool) []string { func SplitEx(s, sep string, removeEmptyString bool) []string {
if sep == "" { if sep == "" {
return []string{} return []string{}

View File

@@ -1,6 +1,8 @@
package strutil package strutil
import "fmt" import (
"fmt"
)
func ExampleAfter() { func ExampleAfter() {
result1 := After("foo", "") result1 := After("foo", "")
@@ -191,6 +193,7 @@ func ExamplePadEnd() {
result4 := PadEnd("foo", 4, "bar") result4 := PadEnd("foo", 4, "bar")
result5 := PadEnd("foo", 5, "bar") result5 := PadEnd("foo", 5, "bar")
result6 := PadEnd("foo", 6, "bar") result6 := PadEnd("foo", 6, "bar")
result7 := PadEnd("foo", 7, "bar")
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
@@ -198,6 +201,7 @@ func ExamplePadEnd() {
fmt.Println(result4) fmt.Println(result4)
fmt.Println(result5) fmt.Println(result5)
fmt.Println(result6) fmt.Println(result6)
fmt.Println(result7)
// Output: // Output:
// foo // foo
// foo // foo
@@ -205,6 +209,7 @@ func ExamplePadEnd() {
// foob // foob
// fooba // fooba
// foobar // foobar
// foobarb
} }
func ExamplePadStart() { func ExamplePadStart() {
@@ -214,6 +219,7 @@ func ExamplePadStart() {
result4 := PadStart("foo", 4, "bar") result4 := PadStart("foo", 4, "bar")
result5 := PadStart("foo", 5, "bar") result5 := PadStart("foo", 5, "bar")
result6 := PadStart("foo", 6, "bar") result6 := PadStart("foo", 6, "bar")
result7 := PadStart("foo", 7, "bar")
fmt.Println(result1) fmt.Println(result1)
fmt.Println(result2) fmt.Println(result2)
@@ -221,6 +227,7 @@ func ExamplePadStart() {
fmt.Println(result4) fmt.Println(result4)
fmt.Println(result5) fmt.Println(result5)
fmt.Println(result6) fmt.Println(result6)
fmt.Println(result7)
// Output: // Output:
// foo // foo
// foo // foo
@@ -228,6 +235,7 @@ func ExamplePadStart() {
// bfoo // bfoo
// bafoo // bafoo
// barfoo // barfoo
// barbfoo
} }
func ExampleReverse() { func ExampleReverse() {