From 49a460eef81dd92e29da2417cf1e287d74115286 Mon Sep 17 00:00:00 2001 From: dudaodong Date: Tue, 27 Dec 2022 16:57:32 +0800 Subject: [PATCH] doc: update document and add playgound example for strutil package --- README.md | 76 +++++++++++++++++++++++++--------- docs/strutil.md | 10 ++--- docs/strutil_zh-CN.md | 6 +-- strutil/string.go | 52 +++++++++++++---------- strutil/string_example_test.go | 10 ++++- 5 files changed, 103 insertions(+), 51 deletions(-) diff --git a/README.md b/README.md index 981ebdb..5896e58 100644 --- a/README.md +++ b/README.md @@ -478,26 +478,62 @@ import "github.com/duke-git/lancet/v2/strutil" #### Function list: -- **After** : 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)] -- **AfterLast** : [doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#AfterLast) -- [Before](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Before) -- [BeforeLast](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BeforeLast) -- [CamelCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#CamelCase) -- [Capitalize](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Capitalize) -- [IsString](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsString) -- [KebabCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#KebabCase) -- [UpperKebabCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperKebabCase) -- [LowerFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#LowerFirst) -- [UpperFirst](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperFirst) -- [PadEnd](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadEnd) -- [PadStart](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#PadStart) -- [Reverse](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Reverse) -- [SnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SnakeCase) -- [UpperSnakeCase](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#UpperSnakeCase) -- [SplitEx](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#SplitEx) -- [Wrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Wrap) -- [Unwrap](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Unwrap) +- **After** : 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)] +- **AfterLast** : 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)] +[[play](https://go.dev/play/p/1TegARrb8Yn)] +- **Before** : returns the substring before the first occurrence of a specified string in the source string. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Before)] +[[play](https://go.dev/play/p/JAWTZDS4F5w)] +- **BeforeLast** : returns the substring before the last occurrence of a specified string in the source string. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#BeforeLast)] +[[play](https://go.dev/play/p/pJfXXAoG_Te)] +- **CamelCase** : coverts source string to its camelCase string. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#CamelCase)] +[[play](https://go.dev/play/p/9eXP3tn2tUy)] +- **Capitalize** : converts the first character of source string to upper case and the remaining to lower case. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#Capitalize)] +[[play](https://go.dev/play/p/2OAjgbmAqHZ)] +- **IsString** : checks if the parameter value data type is string or not. +[[doc](https://github.com/duke-git/lancet/blob/main/docs/strutil.md#IsString)] +[[play](https://go.dev/play/p/IOgq7oF9ERm)] +- **KebabCase** : 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)] +- **UpperKebabCase** : 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)] +- **LowerFirst** : 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)] +- **UpperFirst** : 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)] +- **PadEnd** : 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--)] +- **PadStart** : 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)] +- **Reverse** : 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)] +- **SnakeCase** : 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)] +- **UpperSnakeCase** : 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)] +- **SplitEx** : 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)] +- **Wrap** : 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)] +- **Unwrap** : 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. diff --git a/docs/strutil.md b/docs/strutil.md index abb711c..6f1b6d2 100644 --- a/docs/strutil.md +++ b/docs/strutil.md @@ -77,7 +77,7 @@ func main() { ### AfterLast -

Creates substring in source string after position when char last appear.

+

Returns the substring after the last occurrence of a specified string in the source string.

Signature: @@ -108,7 +108,7 @@ func main() { ### Before -

Creates substring in source string before position when char first appear.

+

Returns the substring of the source string up to the first occurrence of the specified string.

Signature: @@ -139,7 +139,7 @@ func main() { ### BeforeLast -

Creates substring in source string before position when char first appear.

+

Returns the substring of the source string up to the last occurrence of the specified string.

Signature: @@ -633,7 +633,7 @@ func main() { ### Wrap -

Wrap a string with another string.

+

Wrap a string with given string.

Signature: @@ -670,7 +670,7 @@ func main() { ### Wrap -

Unwrap a given string from anther string. will change str value.

+

Unwrap a given string from anther string. will change source string.

Signature: diff --git a/docs/strutil_zh-CN.md b/docs/strutil_zh-CN.md index 1806ac3..197c3e2 100644 --- a/docs/strutil_zh-CN.md +++ b/docs/strutil_zh-CN.md @@ -78,7 +78,7 @@ func main() { ### AfterLast -

截取源字符串中char最后一次出现时的位置之后的子字符串

+

返回源字符串中指定字符串最后一次出现时的位置之后的子字符串

函数签名: @@ -109,7 +109,7 @@ func main() { ### Before -

截取源字符串中char首次出现时的位置之前的子字符串

+

返回源字符串中指定字符串第一次出现时的位置之前的子字符串

函数签名: @@ -140,7 +140,7 @@ func main() { ### BeforeLast -

截取源字符串中char最后一次出现时的位置之前的子字符串

+

返回源字符串中指定字符串最后一次出现时的位置之前的子字符串

函数签名: diff --git a/strutil/string.go b/strutil/string.go index 522a9bd..9ceff77 100644 --- a/strutil/string.go +++ b/strutil/string.go @@ -9,9 +9,8 @@ import ( "unicode/utf8" ) -// CamelCase covert string to camelCase string. -// non letters and numbers will be ignored -// eg. "Foo-#1😄$_%^&*(1bar" => "foo11Bar" +// CamelCase coverts string to camelCase string. Non letters and numbers will be ignored. +// Play: https://go.dev/play/p/9eXP3tn2tUy func CamelCase(s string) string { 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. +// Play: https://go.dev/play/p/2OAjgbmAqHZ func Capitalize(s string) string { result := make([]rune, len(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. +// Play: https://go.dev/play/p/sBbBxRbs8MM func UpperFirst(s string) string { if len(s) == 0 { return "" @@ -54,6 +55,7 @@ func UpperFirst(s string) string { } // LowerFirst converts the first character of string to lower case. +// Play: https://go.dev/play/p/CbzAyZmtJwL func LowerFirst(s string) string { if len(s) == 0 { return "" @@ -67,6 +69,7 @@ func LowerFirst(s string) string { // PadEnd pads string on the right side if it's shorter than 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 { len1 := len(source) 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. // Padding characters are truncated if they exceed size. +// Play: https://go.dev/play/p/xpTfzArDfvT func PadStart(source string, size int, padStr string) string { len1 := len(source) len2 := len(padStr) @@ -103,39 +107,36 @@ func PadStart(source string, size int, padStr string) string { return fill[0:size-len1] + source } -// KebabCase covert string to kebab-case -// non letters and numbers will be ignored -// eg. "Foo-#1😄$_%^&*(1bar" => "foo-1-1-bar" +// KebabCase coverts string to kebab-case, non letters and numbers will be ignored. +// Play: https://go.dev/play/p/dcZM9Oahw-Y func KebabCase(s string) string { result := splitIntoStrings(s, false) return strings.Join(result, "-") } -// UpperKebabCase covert string to upper KEBAB-CASE -// non letters and numbers will be ignored -// eg. "Foo-#1😄$_%^&*(1bar" => "FOO-1-1-BAR" +// UpperKebabCase coverts string to upper KEBAB-CASE, non letters and numbers will be ignored +// Play: https://go.dev/play/p/zDyKNneyQXk func UpperKebabCase(s string) string { result := splitIntoStrings(s, true) return strings.Join(result, "-") } -// SnakeCase covert string to snake_case -// non letters and numbers will be ignored -// eg. "Foo-#1😄$_%^&*(1bar" => "foo_1_1_bar" +// SnakeCase coverts string to snake_case, non letters and numbers will be ignored +// Play: https://go.dev/play/p/tgzQG11qBuN func SnakeCase(s string) string { result := splitIntoStrings(s, false) return strings.Join(result, "_") } -// UpperSnakeCase covert string to upper SNAKE_CASE -// non letters and numbers will be ignored -// eg. "Foo-#1😄$_%^&*(1bar" => "FOO_1_1_BAR" +// UpperSnakeCase coverts string to upper SNAKE_CASE, non letters and numbers will be ignored +// Play: https://go.dev/play/p/4COPHpnLx38 func UpperSnakeCase(s string) string { result := splitIntoStrings(s, true) 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 { if s == "" || char == "" { return s @@ -144,7 +145,8 @@ func Before(s, char string) string { 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 { if s == "" || char == "" { return s @@ -163,7 +165,8 @@ func After(s, char string) string { 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 { if s == "" || char == "" { return s @@ -173,6 +176,7 @@ func AfterLast(s, char string) string { } // IsString check if the value data type is string or not. +// Play: https://go.dev/play/p/IOgq7oF9ERm func IsString(v any) bool { if v == nil { 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 { r := []rune(s) 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) } -// 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 { if str == "" || wrapWith == "" { return str @@ -207,7 +213,8 @@ func Wrap(str string, wrapWith string) 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 { if str == "" || wrapToken == "" { return str @@ -225,7 +232,8 @@ func Unwrap(str string, wrapToken string) string { 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 { if sep == "" { return []string{} diff --git a/strutil/string_example_test.go b/strutil/string_example_test.go index 47ba100..82a90ef 100644 --- a/strutil/string_example_test.go +++ b/strutil/string_example_test.go @@ -1,6 +1,8 @@ package strutil -import "fmt" +import ( + "fmt" +) func ExampleAfter() { result1 := After("foo", "") @@ -191,6 +193,7 @@ func ExamplePadEnd() { result4 := PadEnd("foo", 4, "bar") result5 := PadEnd("foo", 5, "bar") result6 := PadEnd("foo", 6, "bar") + result7 := PadEnd("foo", 7, "bar") fmt.Println(result1) fmt.Println(result2) @@ -198,6 +201,7 @@ func ExamplePadEnd() { fmt.Println(result4) fmt.Println(result5) fmt.Println(result6) + fmt.Println(result7) // Output: // foo // foo @@ -205,6 +209,7 @@ func ExamplePadEnd() { // foob // fooba // foobar + // foobarb } func ExamplePadStart() { @@ -214,6 +219,7 @@ func ExamplePadStart() { result4 := PadStart("foo", 4, "bar") result5 := PadStart("foo", 5, "bar") result6 := PadStart("foo", 6, "bar") + result7 := PadStart("foo", 7, "bar") fmt.Println(result1) fmt.Println(result2) @@ -221,6 +227,7 @@ func ExamplePadStart() { fmt.Println(result4) fmt.Println(result5) fmt.Println(result6) + fmt.Println(result7) // Output: // foo // foo @@ -228,6 +235,7 @@ func ExamplePadStart() { // bfoo // bafoo // barfoo + // barbfoo } func ExampleReverse() {