Skip to content

Commit a153026

Browse files
Roshan RanabhatRoshan Ranabhat
Roshan Ranabhat
authored and
Roshan Ranabhat
committed
feat: add new helpers and optimize existing
1 parent 3769c2b commit a153026

File tree

6 files changed

+2797
-321
lines changed

6 files changed

+2797
-321
lines changed

README.md

Lines changed: 185 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,55 +13,70 @@ Convert string to camel case, snake case, kebab case / slugify, custom delimiter
1313

1414
<table>
1515
<tr>
16+
<td><a href="#acronym-string">Acronym</a></td>
1617
<td><a href="#betweenstart-end-string-stringmanipulation">Between</a></td>
1718
<td><a href="#boolean-bool">Boolean</a></td>
18-
<td><a href="#camelcaserule-string-string">CamelCase</a></td>
1919
</tr>
2020
<tr>
21+
<td><a href="#camelcaserule-string-string">CamelCase</a></td>
2122
<td><a href="#containsallcheck-string-bool">ContainsAll</a></td>
23+
<td><a href="#containssubstring-string-bool">Contains</a></td>
24+
</tr>
25+
<tr>
2226
<td><a href="#delimiteddelimiter-string-rule-string-stringmanipulation">Delimited</a></td>
27+
<td><a href="#firstlength-int-string">First</a></td>
2328
<td><a href="#get-string">Get</a></td>
2429
</tr>
2530
<tr>
31+
<td><a href="#isempty-bool">IsEmpty</a></td>
2632
<td><a href="#kebabcaserule-string-stringmanipulation">KebabCase</a></td>
33+
<td><a href="#lastlength-int-string">Last</a></td>
34+
</tr>
35+
<tr>
2736
<td><a href="#lcfirst-string">LcFirst</a></td>
2837
<td><a href="#lines-string">Lines</a></td>
38+
<td><a href="#padlength-int-with-padtype-string-string">Pad</a></td>
2939
</tr>
3040
<tr>
31-
<td><a href="#padlength-int-with-padtype-string-string">Pad</a></td>
41+
<td><a href="#pascalcaserule-string-string">PascalCase</a></td>
42+
<td><a href="#prefixstring-string">Prefix</a></td>
3243
<td><a href="#removespecialcharacter-string">RemoveSpecialCharacter</a></td>
33-
<td><a href="#replacefirstsearch-replace-string-string">ReplaceFirst</a></td>
3444
</tr>
3545
<tr>
46+
<td><a href="#replaceallsearch-replace-string-stringmanipulation">ReplaceAll</a></td>
47+
<td><a href="#replacefirstsearch-replace-string-string">ReplaceFirst</a></td>
3648
<td><a href="#replacelastsearch-replace-string-string">ReplaceLast</a></td>
49+
</tr>
50+
<tr>
3751
<td><a href="#reverse-string">Reverse</a></td>
52+
<td><a href="#sentencecaserule-string-stringmanipulation">SentenceCase</a></td>
3853
<td><a href="#shuffle-string">Shuffle</a></td>
3954
</tr>
4055
<tr>
41-
<td><a href="#surroundwith-string-string">Surround</a></td>
56+
<td><a href="#slugifywithcountcount-int-stringmanipulation">SlugifyWithCount</a></td>
4257
<td><a href="#snakecaserule-string-stringmanipulation">SnakeCase</a></td>
58+
<td><a href="#substringstart-end-int-stringmanipulation">Substring</a></td>
59+
</tr>
60+
<tr>
61+
<td><a href="#suffixstring-string">Suffix</a></td>
62+
<td><a href="#surroundwith-string-string">Surround</a></td>
4363
<td><a href="#teaselength-int-indicator-string-string">Tease</a></td>
4464
</tr>
4565
<tr>
66+
<td><a href="#title-string">Title</a></td>
4667
<td><a href="#tolower-string">ToLower</a></td>
4768
<td><a href="#toupper-string">ToUpper</a></td>
69+
</tr>
70+
<tr>
71+
<td><a href="#trimcutset-string-stringmanipulation">Trim</a></td>
72+
<td><a href="#truncatewordscount-int-suffix-string-stringmanipulation">TruncateWords</a></td>
4873
<td><a href="#ucfirst-string">UcFirst</a></td>
4974
</tr>
50-
<tr>
51-
<td><a href="#firstlength-int-string">First</a></td>
52-
<td><a href="#lastlength-int-string">Last</a></td>
53-
<td><a href="#prefixstring-string">Prefix</a></td>
54-
</tr>
55-
<tr>
56-
<td><a href="#suffixstring-string">Suffix</a></td>
57-
<td><a href="#acronym-string">Acronym</a></td>
58-
<td><a href="#title-string">Title</a></td>
59-
</tr>
60-
<tr>
61-
<td><a href="#pascalcaserule-string-string">PascalCase</a></td>
62-
<td></td>
75+
<tr>
76+
<td><a href="#wordcount-int">WordCount</a></td>
77+
<td><a href="#substringstart-end-int-stringmanipulation">Substring</a></td>
6378
<td></td>
64-
</tr>
79+
</tr>
6580
</table>
6681

6782

@@ -138,6 +153,16 @@ look how it omitted ?## from string. If you dont want to omit anything and since
138153
fmt.Println(camelCase.CamelCase()) // thisIsOneMessedUpStringCanWeReallyCamelCaseIt?##
139154
```
140155

156+
#### Contains(substring string) bool
157+
158+
Contains checks if the string contains the specified substring and returns a boolean value. This is a wrapper around Go's standard strings.Contains function that fits into the Stringy interface.
159+
160+
```go
161+
str := stringy.New("Hello World")
162+
fmt.Println(str.Contains("World")) // true
163+
fmt.Println(str.Contains("Universe")) // false
164+
```
165+
141166
#### ContainsAll(check ...string) bool
142167

143168
ContainsAll is variadic function which takes slice of strings as param and checks if they are present in input and returns boolean value accordingly.
@@ -173,6 +198,33 @@ First returns first n characters from provided input. It removes all spaces in s
173198

174199
Get simply returns result and can be chained on function which returns StringManipulation interface view above examples
175200

201+
```go
202+
getString := stringy.New("hello roshan")
203+
fmt.Println(getString.Get()) // hello roshan
204+
```
205+
206+
#### IsEmpty() bool
207+
IsEmpty checks if the string is empty or contains only whitespace characters. It returns true for empty strings or strings containing only spaces, tabs, or newlines.
208+
209+
```go
210+
emptyStr := stringy.New("")
211+
fmt.Println(emptyStr.IsEmpty()) // true
212+
213+
whitespaceStr := stringy.New(" \t\n")
214+
fmt.Println(whitespaceStr.IsEmpty()) // true
215+
216+
normalStr := stringy.New("Hello")
217+
fmt.Println(normalStr.IsEmpty()) // false
218+
219+
emptyStr := stringy.New("")
220+
fmt.Println(emptyStr.IsEmpty()) // true
221+
222+
whitespaceStr := stringy.New(" \t\n")
223+
fmt.Println(whitespaceStr.IsEmpty()) // true
224+
225+
normalStr := stringy.New("Hello")
226+
fmt.Println(normalStr.IsEmpty()) // false
227+
176228

177229
#### KebabCase(rule ...string) StringManipulation
178230

@@ -249,6 +301,15 @@ ReplaceFirst takes two param search and replace. It returns string by searching
249301
fmt.Println(replaceFirst.ReplaceFirst("name", "nombre")) // Hello My nombre is Roshan and his name is Alis.
250302
```
251303

304+
### ReplaceAll(search, replace string) StringManipulation
305+
ReplaceAll replaces all occurrences of a search string with a replacement string. It complements the existing ReplaceFirst and ReplaceLast methods and provides a chainable wrapper around Go's strings.ReplaceAll function.
306+
```go
307+
go str := stringy.New("Hello World World")
308+
fmt.Println(str.ReplaceAll("World", "Universe").Get()) // Hello Universe Universe
309+
310+
// Chain with other methods
311+
fmt.Println(str.ReplaceAll("World", "Universe").ToUpper()) // HELLO UNIVERSE UNIVERSE
312+
```
252313
253314
#### ReplaceLast(search, replace string) string
254315
@@ -269,6 +330,19 @@ Reverse function reverses the passed strings it can be chained on function which
269330
fmt.Println(reverse.Reverse()) // tset ylno si sihT
270331
```
271332
333+
#### SentenceCase(rule ...string) StringManipulation
334+
335+
SentenceCase is a variadic function that takes one parameter: slice of strings named rule. It converts text from various formats (camelCase, snake_case, kebab-case, etc.) to sentence case format, where the first word is capitalized and the rest are lowercase, with words separated by spaces. Rule parameter helps to omit characters you want to omit from the string. By default, special characters like "_", "-", ".", " " are treated as word separators.
336+
337+
```go
338+
str := stringy.New("thisIsCamelCase_with_snake_too")
339+
fmt.Println(str.SentenceCase().Get()) // This is camel case with snake too
340+
341+
mixedStr := stringy.New("THIS-IS-KEBAB@and#special&chars")
342+
fmt.Println(mixedStr.SentenceCase("@", " ", "#", " ", "&", " ").Get()) // This is kebab and special chars
343+
```
344+
You can chain ToUpper which will make the result all uppercase or Get which will return the result as it is. The first word is automatically capitalized, and all other words are lowercase.
345+
272346
273347
#### Shuffle() string
274348
@@ -301,6 +375,19 @@ SnakeCase is variadic function that takes one Param slice of strings named rule
301375
```
302376
You can chain to upper which with make result all uppercase or ToLower which will make result all lower case or Get which will return result as it is.
303377
378+
#### Substring(start, end int) StringManipulation
379+
Substring extracts part of a string from the start position (inclusive) to the end position (exclusive). It handles multi-byte characters correctly and has safety checks for out-of-bounds indices.
380+
```go
381+
// Basic usage
382+
go str := stringy.New("Hello World")
383+
fmt.Println(str.Substring(0, 5).Get()) // Hello
384+
fmt.Println(str.Substring(6, 11).Get()) // World
385+
386+
// With multi-byte characters
387+
str = stringy.New("Hello 世界")
388+
fmt.Println(str.Substring(6, 8).Get()) // 世界
389+
```
390+
304391
305392
#### Tease(length int, indicator string) string
306393
@@ -330,6 +417,20 @@ ToLower makes all string of user input to lowercase and it can be chained on fun
330417
fmt.Println(snakeCase.SnakeCase("?", "").ToLower()) // this_is_one_messed_up_string_can_we_really_snake_case_it
331418
```
332419
420+
### Trim(cutset ...string) StringManipulation
421+
Trim removes leading and trailing whitespace or specified characters from the string. If no characters are specified, it trims whitespace by default. It can be chained with other methods that return StringManipulation interface.
422+
```go
423+
trimString := stringy.New(" Hello World ")
424+
fmt.Println(trimString.Trim().Get()) // Hello World
425+
426+
specialTrim := stringy.New("!!!Hello World!!!")
427+
fmt.Println(specialTrim.Trim("!").Get()) // Hello World
428+
429+
chainedTrim := stringy.New(" hello world ")
430+
fmt.Println(chainedTrim.Trim().UcFirst()) // Hello world
431+
```
432+
You can chain ToUpper which will make the result all uppercase, ToLower which will make the result all lowercase, or Get which will return the result as it is.
433+
333434
334435
#### ToUpper() string
335436
@@ -373,6 +474,9 @@ Suffix makes sure string has been suffixed with a given string and avoids adding
373474
374475
#### Acronym() string
375476
477+
SlugifyWithCount(count int) StringManipulation
478+
SlugifyWithCount creates a URL-friendly slug with an optional uniqueness counter appended. This is useful for creating unique URL slugs for blog posts, articles, or database entries.
479+
376480
Acronym func returns acronym of input string. You can chain ToUpper() which with make result all upercase or ToLower() which will make result all lower case or Get which will return result as it is
377481
378482
```go
@@ -395,9 +499,71 @@ look how it omitted ?## from string. If you dont want to omit anything and since
395499
fmt.Println(pascalCase.PascalCase()) // ThisIsOneMessedUpStringCanWeReallyCamelCaseIt?##
396500
```
397501
502+
#### SlugifyWithCount(count int) StringManipulation
503+
SlugifyWithCount creates a URL-friendly slug with an optional uniqueness counter appended. This is useful for creating unique URL slugs for blog posts, articles, or database entries.
504+
```go
505+
slug := stringy.New("Hello World")
506+
fmt.Println(slug.SlugifyWithCount(1).Get()) // hello-world-1
507+
fmt.Println(slug.SlugifyWithCount(2).ToUpper()) // HELLO-WORLD-2
508+
```
398509
399-
## Running the tests
510+
#### TruncateWords(count int, suffix string) StringManipulation
511+
TruncateWords truncates the string to a specified number of words and appends a suffix. This is useful for creating previews or summaries of longer text.
512+
513+
```go
514+
truncate := stringy.New("This is a long sentence that needs to be truncated.")
515+
fmt.Println(truncate.TruncateWords(5, "...").Get()) // This is a long sentence...
516+
fmt.Println(truncate.TruncateWords(3, "...").ToUpper()) // THIS IS A LONG...
517+
```
518+
519+
#### WordCount() int
520+
WordCount returns the number of words in the string. It uses whitespace as the word separator and can be chained with other methods.
521+
522+
```go
523+
wordCount := stringy.New("Hello World")
524+
fmt.Println(wordCount.WordCount()) // 2
525+
526+
multiByteCount := stringy.New("Hello 世界")
527+
fmt.Println(multiByteCount.WordCount()) // 2
528+
```
529+
530+
#### Substring(start, end int) StringManipulation
531+
Substring extracts part of a string from the start position (inclusive) to the end position (exclusive). It handles multi-byte characters correctly and has safety checks for out-of-bounds indices.
400532
533+
```go
534+
// Basic usage
535+
str := stringy.New("Hello World")
536+
fmt.Println(str.Substring(0, 5).Get()) // Hello
537+
fmt.Println(str.Substring(6, 11).Get()) // World
538+
539+
// With multi-byte characters
540+
str = stringy.New("Hello 世界")
541+
fmt.Println(str.Substring(6, 8).Get()) // 世界
542+
```
543+
544+
#### Contains(substring string) bool
545+
Contains checks if the string contains the specified substring and returns a boolean value. This is a wrapper around Go's standard strings.Contains function that fits into the Stringy interface.
546+
547+
```go
548+
str := stringy.New("Hello World")
549+
fmt.Println(str.Contains("World")) // true
550+
fmt.Println(str.Contains("Universe")) // false
551+
```
552+
553+
#### ReplaceAll(search, replace string) StringManipulation
554+
ReplaceAll replaces all occurrences of a search string with a replacement string. It complements the existing ReplaceFirst and ReplaceLast methods and provides a chainable wrapper around Go's strings.ReplaceAll function.
555+
556+
```go
557+
str := stringy.New("Hello World World")
558+
fmt.Println(str.ReplaceAll("World", "Universe").Get()) // Hello Universe Universe
559+
560+
// Chain with other methods
561+
fmt.Println(str.ReplaceAll("World", "Universe").ToUpper()) // HELLO UNIVERSE UNIVERSE
562+
```
563+
564+
565+
566+
## Running the tests
401567
``` bash
402568
$ go test
403569
```

0 commit comments

Comments
 (0)