Skip to content

Commit

Permalink
Merge pull request #20 from TidierOrg/bug_fix_add_others
Browse files Browse the repository at this point in the history
adds extract, pad, others, fixes fxns acting on vecs that shd be strs
  • Loading branch information
drizk1 authored Aug 22, 2024
2 parents b372d99 + dd5c1b2 commit ef1faa2
Show file tree
Hide file tree
Showing 8 changed files with 329 additions and 141 deletions.
40 changes: 9 additions & 31 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,15 @@ Pkg.add(url = "https://github.com/TidierOrg/TidierStrings.jl.git")

TidierStrings.jl currently supports:

- `str_detect()`
- `str_replace()`
- `str_replace_all()`
- `str_replace_missing()`
- `str_removal_all()`
- `str_remove()`
- `str_count()`
- `str_squish()`
- `str_equal()`
- `str_to_upper()`
- `str_to_lower()`
- `str_to_title()`
- `str_to_sentence()`
- `str_c`
- `str_dup()`
- `str_length()`
- `str_width()`
- `str_trim()`
- `str_subset()`
- `str_unique()`
- `str_starts()`
- `str_ends()`
- `str_which()`
- `str_flatten()`
- `str_flatten_comma()`
- `str_locate()`
- `str_locate_all()`
- `str_conv`
- `str_like`
- `str_wrap`
- `word()`
| **Category** | **Function** |
|-------------------|----------------------------------------------------------------------------------------------------|
| **Matching** | `str_count`, `str_detect`, `str_locate`, `str_locate_all`, `str_replace`, `str_replace_all`, |
| | `str_remove`, `str_remove_all`, `str_split`, `str_starts`, `str_ends`, `str_subset`, `str_which` |
| **Concatenation** | `str_c`, `str_flatten`, `str_flatten_comma` |
| **Characters** | `str_dup`, `str_length`, `str_width`, `str_trim`, `str_squish`, `str_wrap`, `str_pad` |
| **Locale** | `str_equal`, `str_to_upper`, `str_to_lower`, `str_to_title`, `str_to_sentence`, `str_unique` |
| **Other** | `str_conv`, `str_like`, `str_replace_missing`, `word` |


## Examples

Expand Down
40 changes: 8 additions & 32 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,11 @@ The goal of this package is to replicate the beauty of stringr from R in Julia i

This package includes:


- `str_detect()`
- `str_replace()`
- `str_replace_all()`
- `str_replace_missing()`
- `str_removal_all()`
- `str_remove()`
- `str_count()`
- `str_squish()`
- `str_equal()`
- `str_to_upper()`
- `str_to_lower()`
- `str_to_title()`
- `str_to_sentence()`
- `str_c`
- `str_dup()`
- `str_length()`
- `str_width()`
- `str_trim()`
- `str_subset()`
- `str_unique()`
- `str_starts()`
- `str_ends()`
- `str_which()`
- `str_flatten()`
- `str_flatten_comma()`
- `str_locate()`
- `str_locate_all()`
- `str_conv`
- `str_like`
- `str_wrap`
- `word()`
| **Category** | **Function** |
|-------------------|----------------------------------------------------------------------------------------------------|
| **Matching** | `str_count`, `str_detect`, `str_locate`, `str_locate_all`, `str_replace`, `str_replace_all`, |
| | `str_remove`, `str_remove_all`, `str_split`, `str_starts`, `str_ends`, `str_subset`, `str_which` |
| **Concatenation** | `str_c`, `str_flatten`, `str_flatten_comma` |
| **Characters** | `str_dup`, `str_length`, `str_width`, `str_trim`, `str_squish`, `str_wrap`, `str_pad` |
| **Locale** | `str_equal`, `str_to_upper`, `str_to_lower`, `str_to_title`, `str_to_sentence`, `str_unique` |
| **Other** | `str_conv`, `str_like`, `str_replace_missing`, `word` |
5 changes: 3 additions & 2 deletions src/TidierStrings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ export
# matching
str_count, str_detect, str_locate, str_locate_all, str_replace, str_replace_all,
str_remove, str_remove_all, str_split, str_starts, str_ends, str_subset, str_which,
str_extract, str_extract_all, str_subset,
# concatenation
str_c, str_flatten, str_flatten_comma,
# characters
str_dup, str_length, str_width, str_trim, str_squish, str_wrap,
str_dup, str_length, str_width, str_trim, str_squish, str_wrap, str_pad,
# locale
str_equal, str_to_upper, str_to_lower, str_to_title, str_to_sentence, str_unique,
# other
str_conv, str_like, str_replace_missing, word
str_conv, str_like, str_replace_missing, word, str_trunc

include("docstrings.jl")
include("matching.jl")
Expand Down
33 changes: 32 additions & 1 deletion src/characters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,35 @@ function str_wrap(
Base.push!(lines, current_line)

return Base.join(lines, "\n")
end
end

"""
$docstring_str_pad
"""
function str_pad(string::AbstractString, width::Integer;
side::String = "right", pad::AbstractString = " ", use_width::Bool = true)
if use_width
len = length(string)
else
len = ncodeunits(string)
end

if width <= len
return string
end

padding_length = width - len
padding = repeat(pad, cld(padding_length, length(pad)))

if side == "right"
return string * first(padding, padding_length)
elseif side == "left"
return first(padding, padding_length) * string
elseif side == "both"
left_pad = div(padding_length, 2)
right_pad = padding_length - left_pad
return first(padding, left_pad) * string * first(padding, right_pad)
else
throw(ArgumentError("side must be one of 'left', 'right', or 'both'"))
end
end
18 changes: 14 additions & 4 deletions src/concatenation.jl
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
"""
$docstring_str_c
"""
function str_c(strings::AbstractVector; sep::AbstractString="")
strings = filter(!ismissing, strings)

return join(strings, sep)
function str_c(strings::AbstractVector...; sep::AbstractString = "", collapse::AbstractString = "")
# Remove any `missing` values from each vector
filtered_strings = [filter(!ismissing, vec) for vec in strings]

if length(strings) == 1
# If only one vector is provided, join its elements without a separator
return join(filtered_strings[1])
else
# Zip the vectors together and concatenate corresponding elements with `sep`
concatenated = [join(pair, sep) for pair in zip(filtered_strings...)]

# Join the concatenated elements with `collapse`
return collapse == "" ? concatenated : join(concatenated, collapse)
end
end

"""
Expand Down
Loading

0 comments on commit ef1faa2

Please sign in to comment.