Open richard-viney opened 2 months ago
Sounds good, but we'll need to think of some other name as the ones used in the code example don't fit with the convention used in this repo.
Changing left and right for start and end sounds good.
What names would you suggest?
Exposing the existing Direction
type would mean only one public function is added:
@target(erlang)
pub fn trim_codepoints(
string: String,
direction: Direction,
codepoints: String,
) -> String {
let codepoint_ints =
codepoints
|> to_utf_codepoints
|> list.map(utf_codepoint_to_int)
do_trim_codepoints(string, direction, codepoint_ints)
}
@target(erlang)
@external(erlang, "string", "trim")
pub fn do_trim_codepoints(
string: String,
direction: Direction,
codepoints: List(Int),
) -> String
@target(javascript)
pub fn do_trim_codepoints(
string: String,
direction: Direction,
codepoints: List(Int),
) -> String {
todo
}
We will not expose the direction type, it needs to be different functions.
maybe one of these
pub fn trim_codepoints(
string: String
) { todo }
// and any pair:
pub fn trim_start_codepoints(
string: String,
by: String,
) { todo }
pub fn trim_end_codepoints(
string: String,
by: String,
) { todo }
pub fn trim_head_codepoints(
string: String,
by: String,
) { todo }
pub fn trim_tail_codepoints(
string: String,
by: String,
) { todo }
pub fn trim_beginning_codepoints(
string: String,
by: String,
) { todo }
pub fn trim_ending_codepoints(
string: String,
by: String,
) { todo }
The existing
string.trim
,string.trim_left
andstring.trim_right
functions currently only trim whitespace.Could we add equivalent trimming functions that take the codepoints to be trimmed as an input, similar to Erlang's
string.trim/3
?E.g.
There are other ways to slice this, such as taking a
String
as the final parameter and using its codepoints as the ones to trim, rather than aList(UtfCodepoint)
.I haven't looked into what the JavaScript implementation would be, but can do so.
On a related note, my understanding is that the existing methods with the words "left" and "right" in them assume left-to-right reading order, which doesn't hold for all languages (e.g. Arabic, Hebrew). Could consider using "start" and "end" instead, or adding these as aliases.