Skip to content

Commit

Permalink
Merge pull request #13 from cecoeco/main
Browse files Browse the repository at this point in the history
  • Loading branch information
drizk1 committed May 11, 2024
2 parents 2176dca + 1c279c3 commit d239a58
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 3 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ TidierStrings.jl currently supports:
- `str_equal()`
- `str_to_upper()`
- `str_to_lower()`
- `str_to_title()`
- `str_to_sentence()`
- `str_to_dup()`
- `str_to_length()`
- `str_to_width()`
- `str_to_trim()`
- `str_subset()`

## Examples
Expand Down
6 changes: 6 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,10 @@ This package includes:
- `str_equal()`
- `str_to_upper()`
- `str_to_lower()`
- `str_to_title()`
- `str_to_sentence()`
- `str_to_dup()`
- `str_to_length()`
- `str_to_width()`
- `str_to_trim()`
- `str_subset()`
Binary file removed src/.DS_Store
Binary file not shown.
84 changes: 83 additions & 1 deletion src/TidierStrings.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module TidierStrings

export str_detect, str_replace, str_replace_all, str_remove_all, str_remove, str_count, str_squish, str_equal, str_to_upper, str_to_lower, str_split, str_subset
export str_detect, str_replace, str_replace_all, str_remove_all, str_remove, str_count, str_squish, str_equal, str_to_upper, str_to_lower, str_split, str_subset,
str_to_title, str_to_sentence, str_dup, str_length, str_width, str_trim

include("strings_docstrings.jl")

Expand Down Expand Up @@ -239,6 +240,87 @@ end
#end


"""
$docstring_str_to_title
"""
function str_to_title(s::AbstractString)
if ismissing(s)
return(s)
else
return titlecase(s)
end
end


"""
$docstring_str_to_sentence
"""
function str_to_sentence(s::AbstractString)
if ismissing(s)
return(s)
else
sentences = split(s, r"(?<=[.!?])\s+")
capitalized_sentences = [uppercase(first(sentence)) * lowercase(sentence[2:end]) for sentence in sentences]
return join(capitalized_sentences, " ")
end
end


"""
$docstring_str_dup
"""
function str_dup(s::AbstractString, times::Integer)
if ismissing(s)
return(s)
end

return repeat(s, times)
end


"""
$docstring_str_length
"""
function str_length(s::AbstractString)
if ismissing(s)
return(s)
end

return length(s)
end


"""
$docstring_str_width
"""
function str_width(s::AbstractString)
if ismissing(s)
return(s)
end

return textwidth(s)
end


"""
$docstring_str_trim
"""
function str_trim(s::AbstractString, side::String="both")
if ismissing(s)
return(s)
end

if side == "both"
return strip(s)
elseif side == "left"
return lstrip(s)
elseif side == "right"
return rstrip(s)
else
throw(ArgumentError("side must be one of 'both', 'left', or 'right'"))
end
end


"""
$docstring_str_subset
Expand Down
127 changes: 125 additions & 2 deletions src/strings_docstrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ true

const docstring_str_replace =
"""
str_replace(column::String, pattern::Union{String, Regex}, replacement::String)
str_replace(column::String, pattern::Union{String, Regex}, replacement::String)
Replace the first occurrence of a pattern in a string with a specified string.
Expand Down Expand Up @@ -236,7 +236,7 @@ julia> str_remove_all(column, " strings")

const docstring_str_remove =
"""
str_remove(column::String, pattern::Union{String, Regex})
str_remove(column::String, pattern::Union{String, Regex})
Remove the first occurrence of the pattern in the string.
Expand All @@ -257,3 +257,126 @@ julia> str_remove(column, " strings")
```
"""

const docstring_str_to_title =
"""
str_to_title(s::AbstractString)
Convert the first character of each word in a string to upper case.
Arguments
- `s`: Input string.
Returns
A string with the first character of each word converted to upper case.
Examples
```jldoctest
julia> str_to_title("hello world!")
"Hello World!"
julia> str_to_title("This is a title")
"This Is A Title"
```
"""

const docstring_str_to_sentence =
"""
str_to_sentence(s::AbstractString)
Convert the first character of each sentence in a string to upper case.
Arguments
- `s`: Input string.
Returns
A string with the first character of each sentence converted to upper case.
Examples
```jldoctest
julia> str_to_sentence("hello world!")
"Hello world!"
julia> str_to_sentence("a sentence mUst starT With A capital letter.")
"A sentence must start With a capital letter."
"""

const docstring_str_dup =
"""
str_dup(s::AbstractString, times::Int)
Duplicate the string `s` `times` times.
Arguments
- `s`: Input string.
- `times`: Number of times to duplicate the string.
Returns
A string with the string `s` duplicated `times` times.
Examples
```jldoctest
julia> str_dup("hello", 3)
"hellohellohello"
"""

const docstring_str_length =
"""
str_length(s::AbstractString)
Return the length of the string `s`.
Arguments
- `s`: Input string.
Returns
The length of the string `s`.
Examples
```jldoctest
julia> str_length("hello world! 😊")
14
julia> str_length("😊")
1
```
"""

const docstring_str_width =
"""
str_width(s::AbstractString)
Return the width of the string `s`.
Arguments
- `s`: Input string.
Returns
The width of the string `s`.
Examples
```jldoctest
julia> str_width("hello world! 😊")
15
julia> str_width("😊")
2
"""

const docstring_str_trim =
"""
str_trim(s::AbstractString, side::String="both")
Removes all whitespace from the string `s` on the left and right side, or on both sides if `side` is "both".
Arguments
- `s`: Input string.
- `side`: The side(s) from which to remove whitespace. Can be "left", "right", or "both".
Returns
The string `s` with all whitespace removed on the left and right side, or on both sides if `side` is "both".
Examples
```jldoctest
julia> str_trim(" hello world! 😊 ")
"hello world! 😊"
"""
Binary file removed test/.DS_Store
Binary file not shown.

0 comments on commit d239a58

Please sign in to comment.