Skip to content

Commit dd5c1b2

Browse files
committed
adds extract, pad, others, fixes fxns acting on vecs that shd be strs
1 parent b372d99 commit dd5c1b2

File tree

8 files changed

+329
-141
lines changed

8 files changed

+329
-141
lines changed

README.md

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,37 +36,15 @@ Pkg.add(url = "https://github.com/TidierOrg/TidierStrings.jl.git")
3636

3737
TidierStrings.jl currently supports:
3838

39-
- `str_detect()`
40-
- `str_replace()`
41-
- `str_replace_all()`
42-
- `str_replace_missing()`
43-
- `str_removal_all()`
44-
- `str_remove()`
45-
- `str_count()`
46-
- `str_squish()`
47-
- `str_equal()`
48-
- `str_to_upper()`
49-
- `str_to_lower()`
50-
- `str_to_title()`
51-
- `str_to_sentence()`
52-
- `str_c`
53-
- `str_dup()`
54-
- `str_length()`
55-
- `str_width()`
56-
- `str_trim()`
57-
- `str_subset()`
58-
- `str_unique()`
59-
- `str_starts()`
60-
- `str_ends()`
61-
- `str_which()`
62-
- `str_flatten()`
63-
- `str_flatten_comma()`
64-
- `str_locate()`
65-
- `str_locate_all()`
66-
- `str_conv`
67-
- `str_like`
68-
- `str_wrap`
69-
- `word()`
39+
| **Category** | **Function** |
40+
|-------------------|----------------------------------------------------------------------------------------------------|
41+
| **Matching** | `str_count`, `str_detect`, `str_locate`, `str_locate_all`, `str_replace`, `str_replace_all`, |
42+
| | `str_remove`, `str_remove_all`, `str_split`, `str_starts`, `str_ends`, `str_subset`, `str_which` |
43+
| **Concatenation** | `str_c`, `str_flatten`, `str_flatten_comma` |
44+
| **Characters** | `str_dup`, `str_length`, `str_width`, `str_trim`, `str_squish`, `str_wrap`, `str_pad` |
45+
| **Locale** | `str_equal`, `str_to_upper`, `str_to_lower`, `str_to_title`, `str_to_sentence`, `str_unique` |
46+
| **Other** | `str_conv`, `str_like`, `str_replace_missing`, `word` |
47+
7048

7149
## Examples
7250

docs/src/index.md

Lines changed: 8 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,11 @@ The goal of this package is to replicate the beauty of stringr from R in Julia i
66

77
This package includes:
88

9-
10-
- `str_detect()`
11-
- `str_replace()`
12-
- `str_replace_all()`
13-
- `str_replace_missing()`
14-
- `str_removal_all()`
15-
- `str_remove()`
16-
- `str_count()`
17-
- `str_squish()`
18-
- `str_equal()`
19-
- `str_to_upper()`
20-
- `str_to_lower()`
21-
- `str_to_title()`
22-
- `str_to_sentence()`
23-
- `str_c`
24-
- `str_dup()`
25-
- `str_length()`
26-
- `str_width()`
27-
- `str_trim()`
28-
- `str_subset()`
29-
- `str_unique()`
30-
- `str_starts()`
31-
- `str_ends()`
32-
- `str_which()`
33-
- `str_flatten()`
34-
- `str_flatten_comma()`
35-
- `str_locate()`
36-
- `str_locate_all()`
37-
- `str_conv`
38-
- `str_like`
39-
- `str_wrap`
40-
- `word()`
9+
| **Category** | **Function** |
10+
|-------------------|----------------------------------------------------------------------------------------------------|
11+
| **Matching** | `str_count`, `str_detect`, `str_locate`, `str_locate_all`, `str_replace`, `str_replace_all`, |
12+
| | `str_remove`, `str_remove_all`, `str_split`, `str_starts`, `str_ends`, `str_subset`, `str_which` |
13+
| **Concatenation** | `str_c`, `str_flatten`, `str_flatten_comma` |
14+
| **Characters** | `str_dup`, `str_length`, `str_width`, `str_trim`, `str_squish`, `str_wrap`, `str_pad` |
15+
| **Locale** | `str_equal`, `str_to_upper`, `str_to_lower`, `str_to_title`, `str_to_sentence`, `str_unique` |
16+
| **Other** | `str_conv`, `str_like`, `str_replace_missing`, `word` |

src/TidierStrings.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,15 @@ export
66
# matching
77
str_count, str_detect, str_locate, str_locate_all, str_replace, str_replace_all,
88
str_remove, str_remove_all, str_split, str_starts, str_ends, str_subset, str_which,
9+
str_extract, str_extract_all, str_subset,
910
# concatenation
1011
str_c, str_flatten, str_flatten_comma,
1112
# characters
12-
str_dup, str_length, str_width, str_trim, str_squish, str_wrap,
13+
str_dup, str_length, str_width, str_trim, str_squish, str_wrap, str_pad,
1314
# locale
1415
str_equal, str_to_upper, str_to_lower, str_to_title, str_to_sentence, str_unique,
1516
# other
16-
str_conv, str_like, str_replace_missing, word
17+
str_conv, str_like, str_replace_missing, word, str_trunc
1718

1819
include("docstrings.jl")
1920
include("matching.jl")

src/characters.jl

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,35 @@ function str_wrap(
8686
Base.push!(lines, current_line)
8787

8888
return Base.join(lines, "\n")
89-
end
89+
end
90+
91+
"""
92+
$docstring_str_pad
93+
"""
94+
function str_pad(string::AbstractString, width::Integer;
95+
side::String = "right", pad::AbstractString = " ", use_width::Bool = true)
96+
if use_width
97+
len = length(string)
98+
else
99+
len = ncodeunits(string)
100+
end
101+
102+
if width <= len
103+
return string
104+
end
105+
106+
padding_length = width - len
107+
padding = repeat(pad, cld(padding_length, length(pad)))
108+
109+
if side == "right"
110+
return string * first(padding, padding_length)
111+
elseif side == "left"
112+
return first(padding, padding_length) * string
113+
elseif side == "both"
114+
left_pad = div(padding_length, 2)
115+
right_pad = padding_length - left_pad
116+
return first(padding, left_pad) * string * first(padding, right_pad)
117+
else
118+
throw(ArgumentError("side must be one of 'left', 'right', or 'both'"))
119+
end
120+
end

src/concatenation.jl

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
11
"""
22
$docstring_str_c
33
"""
4-
function str_c(strings::AbstractVector; sep::AbstractString="")
5-
strings = filter(!ismissing, strings)
6-
7-
return join(strings, sep)
4+
function str_c(strings::AbstractVector...; sep::AbstractString = "", collapse::AbstractString = "")
5+
# Remove any `missing` values from each vector
6+
filtered_strings = [filter(!ismissing, vec) for vec in strings]
7+
8+
if length(strings) == 1
9+
# If only one vector is provided, join its elements without a separator
10+
return join(filtered_strings[1])
11+
else
12+
# Zip the vectors together and concatenate corresponding elements with `sep`
13+
concatenated = [join(pair, sep) for pair in zip(filtered_strings...)]
14+
15+
# Join the concatenated elements with `collapse`
16+
return collapse == "" ? concatenated : join(concatenated, collapse)
17+
end
818
end
919

1020
"""

0 commit comments

Comments
 (0)