Skip to content

Commit

Permalink
Rename left/right variants to start/end
Browse files Browse the repository at this point in the history
  • Loading branch information
ollien committed Oct 6, 2024
1 parent fb84c30 commit 45d1a2c
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 47 deletions.
52 changes: 32 additions & 20 deletions src/gleam/string.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -588,73 +588,85 @@ type Direction {
Both
}

/// Removes whitespace on the left of a `String`.
/// Removes whitespace at the start of a `String`.
///
/// ## Examples
///
/// ```gleam
/// trim_left(" hats \n")
/// trim_start(" hats \n")
/// // -> "hats \n"
/// ```
///
pub fn trim_start(string: String) -> String {
do_trim_start(string)
}

/// An alias for trim_start
@deprecated("Use trim_start. There is no behavior change")
pub fn trim_left(string: String) -> String {
do_trim_left(string)
trim_start(string)
}

@external(javascript, "../gleam_stdlib.mjs", "trim_left")
fn do_trim_left(string: String) -> String {
@external(javascript, "../gleam_stdlib.mjs", "trim_start")
fn do_trim_start(string: String) -> String {
erl_trim(string, Leading)
}

/// Removes whitespace on the right of a `String`.
/// Removes whitespace at the end of a `String`.
///
/// ## Examples
///
/// ```gleam
/// trim_right(" hats \n")
/// trim_end(" hats \n")
/// // -> " hats"
/// ```
///
pub fn trim_end(string: String) -> String {
do_trim_end(string)
}

/// An alias for trim_end
@deprecated("Use trim_end. There is no behavior change")
pub fn trim_right(string: String) -> String {
do_trim_right(string)
trim_end(string)
}

@external(javascript, "../gleam_stdlib.mjs", "trim_right")
fn do_trim_right(string: String) -> String {
@external(javascript, "../gleam_stdlib.mjs", "trim_end")
fn do_trim_end(string: String) -> String {
erl_trim(string, Trailing)
}

/// Like `trim_left`, but removes the specified chars on the left of a `String`
/// Like `trim_start`, but removes the specified chars at the start of a `String`
///
/// ## Examples
///
/// ```gleam
/// trim_chars_left("..,hats,..", ".,")
/// // -> "hats,.."
/// ```
pub fn trim_chars_left(string: String, charset: String) -> String {
do_trim_chars_left(string, charset)
pub fn trim_chars_start(string: String, charset: String) -> String {
do_trim_chars_start(string, charset)
}

@external(javascript, "../gleam_stdlib.mjs", "trim_chars_left")
fn do_trim_chars_left(string: String, charset: String) -> String {
@external(javascript, "../gleam_stdlib.mjs", "trim_chars_start")
fn do_trim_chars_start(string: String, charset: String) -> String {
erl_trim_chars(string, Leading, erl_to_graphemes(charset))
}

/// Like `trim_right`, but removes the specified chars on the right of a `String`
/// Like `trim_end`, but removes the specified chars at the end of a `String`
///
/// ## Examples
///
/// ```gleam
/// trim_chars_right("..,hats,..", ".,")
/// // -> "..,hats"
/// ```
pub fn trim_chars_right(string: String, charset: String) -> String {
do_trim_chars_right(string, charset)
pub fn trim_chars_end(string: String, charset: String) -> String {
do_trim_chars_end(string, charset)
}

@external(javascript, "../gleam_stdlib.mjs", "trim_chars_right")
fn do_trim_chars_right(string: String, charset: String) -> String {
@external(javascript, "../gleam_stdlib.mjs", "trim_chars_end")
fn do_trim_chars_end(string: String, charset: String) -> String {
erl_trim_chars(string, Trailing, erl_to_graphemes(charset))
}

Expand Down
22 changes: 11 additions & 11 deletions src/gleam_stdlib.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -262,33 +262,33 @@ const unicode_whitespaces = [
"\u2029", // Paragraph separator
].join("");

const left_trim_regex = new_left_trim_regexp(unicode_whitespaces);
const start_trim_regex = new_start_trim_regexp(unicode_whitespaces);
const right_trim_regex = new_right_trim_regexp(unicode_whitespaces);

export function trim(string) {
return trim_left(trim_right(string));
return trim_start(trim_end(string));
}

export function trim_left(string) {
return string.replace(left_trim_regex, "");
export function trim_start(string) {
return string.replace(start_trim_regex, "");
}

export function trim_right(string) {
export function trim_end(string) {
return string.replace(right_trim_regex, "");
}

export function trim_chars(string, charset) {
const trimmed_right = trim_chars_right(string, charset);
return trim_chars_left(trimmed_right, charset);
const trimmed_right = trim_chars_end(string, charset);
return trim_chars_start(trimmed_right, charset);
}

export function trim_chars_left(string, charset) {
const trim_regexp = new_left_trim_regexp(charset);
export function trim_chars_start(string, charset) {
const trim_regexp = new_start_trim_regexp(charset);

return string.replace(trim_regexp, "")
}

export function trim_chars_right(string, charset) {
export function trim_chars_end(string, charset) {
const trim_regexp = new_right_trim_regexp(charset);

return string.replace(trim_regexp, "")
Expand Down Expand Up @@ -974,7 +974,7 @@ export function bit_array_compare(first, second) {
return new Lt(); // second has more items
}

function new_left_trim_regexp(charset) {
function new_start_trim_regexp(charset) {
return new RegExp(`^([${charset}]*)`, "g");
}

Expand Down
32 changes: 16 additions & 16 deletions test/gleam/string_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -164,51 +164,51 @@ pub fn trim_test() {
|> should.equal("hats")
}

pub fn trim_left_test() {
pub fn trim_start_test() {
" hats \n"
|> string.trim_left
|> string.trim_start
|> should.equal("hats \n")
}

pub fn trim_left_rtl_test() {
pub fn trim_start_rtl_test() {
" עברית "
|> string.trim_left
|> string.trim_start
|> should.equal("עברית ")
}

pub fn trim_right_rtl_test() {
pub fn trim_end_rtl_test() {
" עברית "
|> string.trim_right
|> string.trim_end
|> should.equal(" עברית")
}

pub fn trim_right_test() {
pub fn trim_end_test() {
" hats \n"
|> string.trim_right
|> string.trim_end
|> should.equal(" hats")
}

pub fn trim_chars_left_test() {
pub fn trim_chars_start_test() {
",..hats..,"
|> string.trim_chars_left(",.")
|> string.trim_chars_start(",.")
|> should.equal("hats..,")
}

pub fn trim_chars_left_rtl_test() {
pub fn trim_chars_start_rtl_test() {
"שמש"
|> string.trim_chars_left("ש")
|> string.trim_chars_start("ש")
|> should.equal("מש")
}

pub fn trim_chars_right_test() {
pub fn trim_chars_end_test() {
",..hats..,"
|> string.trim_chars_right(",.")
|> string.trim_chars_end(",.")
|> should.equal(",..hats")
}

pub fn trim_chars_right_rtl_test() {
pub fn trim_chars_end_rtl_test() {
"שמש"
|> string.trim_chars_right("ש")
|> string.trim_chars_end("ש")
|> should.equal("שמ")
}

Expand Down

0 comments on commit 45d1a2c

Please sign in to comment.