From 0903554ec50c51a10ff932611c02888d646048de Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 6 Oct 2024 12:46:57 -0400 Subject: [PATCH 1/5] Add trim_chars, trim_chars_left, and trim_chars_right --- src/gleam/string.gleam | 60 +++++++++++++++++++++++ src/gleam_stdlib.mjs | 92 ++++++++++++++++++++++++++++++++++-- test/gleam/string_test.gleam | 42 ++++++++++++++++ 3 files changed, 191 insertions(+), 3 deletions(-) diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index e530c8fa..200c4ab5 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -553,9 +553,35 @@ fn do_trim(string: String) -> String { erl_trim(string, Both) } +/// Like `trim`, but removes the specified chars on both sides of a `String` +/// +/// ## Examples +/// +/// ```gleam +/// trim_chars_left("..,hats,..", ".,") +/// // -> "hats" +/// ``` +pub fn trim_chars(string: String, charset: String) -> String { + do_trim_chars(string, charset) +} + +@external(javascript, "../gleam_stdlib.mjs", "trim_chars") +fn do_trim_chars(string: String, charset: String) -> String { + erl_trim_chars(string, Both, erl_to_graphemes(charset)) +} + @external(erlang, "string", "trim") fn erl_trim(a: String, b: Direction) -> String +@external(erlang, "string", "trim") +fn erl_trim_chars(a: String, b: Direction, c: ErlGraphemes) -> String + +@external(erlang, "string", "to_graphemes") +fn erl_to_graphemes(a: String) -> ErlGraphemes + +// erlang's string:to_graphemes returns char() | [char()], which cannot be directly represented +type ErlGraphemes + type Direction { Leading Trailing @@ -598,6 +624,40 @@ fn do_trim_right(string: String) -> String { erl_trim(string, Trailing) } +/// Like `trim_left`, but removes the specified chars on the left 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) +} + +@external(javascript, "../gleam_stdlib.mjs", "trim_chars_left") +fn do_trim_chars_left(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` +/// +/// ## Examples +/// +/// ```gleam +/// trim_chars_right("..,hats,..", ".,") +/// // -> "..,hats" +/// ``` +pub fn trim_chars_right(string: String, charset: String) -> String { + do_trim_chars_right(string, charset) +} + +@external(javascript, "../gleam_stdlib.mjs", "trim_chars_right") +fn do_trim_chars_right(string: String, charset: String) -> String { + erl_trim_chars(string, Trailing, erl_to_graphemes(charset)) +} + /// Splits a non-empty `String` into its first element (head) and rest (tail). /// This lets you pattern match on `String`s exactly as you would with lists. /// diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 50ebb46f..cbeaf843 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -21,6 +21,9 @@ import Dict from "./dict.mjs"; const Nil = undefined; const NOT_FOUND = {}; +// See license note in escape_regexp_chars +const reRegExpChar = /[\\^$.*+?()[\]{}|]/g; +const reHasRegExpChar = RegExp(reRegExpChar.source); export function identity(x) { return x; @@ -259,8 +262,8 @@ const unicode_whitespaces = [ "\u2029", // Paragraph separator ].join(""); -const left_trim_regex = new RegExp(`^([${unicode_whitespaces}]*)`, "g"); -const right_trim_regex = new RegExp(`([${unicode_whitespaces}]*)$`, "g"); +const left_trim_regex = new_left_trim_regexp(unicode_whitespaces); +const right_trim_regex = new_right_trim_regexp(unicode_whitespaces); export function trim(string) { return trim_left(trim_right(string)); @@ -274,6 +277,23 @@ export function trim_right(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); +} + +export function trim_chars_left(string, charset) { + const trim_regexp = new_left_trim_regexp(charset); + + return string.replace(trim_regexp, "") +} + +export function trim_chars_right(string, charset) { + const trim_regexp = new_right_trim_regexp(charset); + + return string.replace(trim_regexp, "") +} + export function bit_array_from_string(string) { return toBitArray([stringBits(string)]); } @@ -296,7 +316,7 @@ export function crash(message) { export function bit_array_to_string(bit_array) { try { - const decoder = new TextDecoder("utf-8", { fatal: true }); + const decoder = new TextDecoder("utf-8", { fatarl: true }); return new Ok(decoder.decode(bit_array.buffer)); } catch { return new Error(Nil); @@ -953,3 +973,69 @@ export function bit_array_compare(first, second) { } return new Lt(); // second has more items } + +function new_left_trim_regexp(charset) { + return new RegExp(`^([${charset}]*)`, "g"); +} + +function new_right_trim_regexp(charset) { + const escaped_charset = escape_regexp_chars(charset); + return new RegExp(`([${escaped_charset}]*)$`, "g"); +} + +function escape_regexp_chars(string) { + /* + * The MIT License + + * Copyright JS Foundation and other contributors + * + * Based on Underscore.js, copyright Jeremy Ashkenas, + * DocumentCloud and Investigative Reporters & Editors + * + * This software consists of voluntary contributions made by many + * individuals. For exact contribution history, see the revision history + * available at https://github.com/lodash/lodash + * + * The following license applies to all parts of this software except as + * documented below: + * + * ==== + * + * Permission is hereby granted, free of charge, to any person obtaining + * a copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sublicense, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice shall be + * included in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, + * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE + * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION + * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION + * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + * + * ==== + * + * Copyright and related rights for sample code are waived via CC0. Sample + * code is defined as all source code displayed within the prose of the + * documentation. + * + * CC0: http://creativecommons.org/publicdomain/zero/1.0/ + * + * ==== + * + * Files located in the node_modules and vendor directories are externally + * maintained libraries used by this software which have their own + * licenses; we recommend you read them, as their terms may differ from the + * terms above. + */ + return string && reHasRegExpChar.test(string) + ? string.replace(reRegExpChar, '\\$&') + : string || ''; +} diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index 6d3031e4..a0ad4036 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -176,6 +176,18 @@ pub fn trim_right_test() { |> should.equal(" hats") } +pub fn trim_chars_left_test() { + ",..hats..," + |> string.trim_chars_left(",.") + |> should.equal("hats..,") +} + +pub fn trim_chars_right_test() { + ",..hats..," + |> string.trim_chars_right(",.") + |> should.equal(",..hats") +} + // unicode whitespaces pub fn trim_horizontal_tab_test() { "hats\u{0009}" @@ -364,6 +376,36 @@ pub fn trim_comma_test() { |> should.equal("hats,") } +pub fn trim_chars_test() { + ",,hats," + |> string.trim_chars(",") + |> should.equal("hats") +} + +pub fn trim_chars_commas_and_periods_test() { + ",,hats,..." + |> string.trim_chars(",.") + |> should.equal("hats") +} + +pub fn trim_chars_keeps_whitespace_not_in_charset_test() { + ",,hats ,..." + |> string.trim_chars(",.") + |> should.equal("hats ") +} + +pub fn trim_chars_does_not_trim_from_middle_of_string_test() { + ",,hats,hats,hats,..." + |> string.trim_chars(",.") + |> should.equal("hats,hats,hats") +} + +pub fn trim_chars_trims_complex_graphemes_test() { + "hats๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘" + |> string.trim_chars("๐Ÿ‘") + |> should.equal("hats") +} + pub fn starts_with_test() { "theory" |> string.starts_with("") From 0edcff5cfdccd0481748173af72e76c5b234a286 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 6 Oct 2024 13:28:14 -0400 Subject: [PATCH 2/5] Add RTL tests --- test/gleam/string_test.gleam | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index a0ad4036..12d364fe 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -170,6 +170,18 @@ pub fn trim_left_test() { |> should.equal("hats \n") } +pub fn trim_left_rtl_test() { + " ืขื‘ืจื™ืช " + |> string.trim_left + |> should.equal("ืขื‘ืจื™ืช ") +} + +pub fn trim_right_rtl_test() { + " ืขื‘ืจื™ืช " + |> string.trim_right + |> should.equal(" ืขื‘ืจื™ืช") +} + pub fn trim_right_test() { " hats \n" |> string.trim_right @@ -182,12 +194,24 @@ pub fn trim_chars_left_test() { |> should.equal("hats..,") } +pub fn trim_chars_left_rtl_test() { + "ืฉืžืฉ" + |> string.trim_chars_left("ืฉ") + |> should.equal("ืžืฉ") +} + pub fn trim_chars_right_test() { ",..hats..," |> string.trim_chars_right(",.") |> should.equal(",..hats") } +pub fn trim_chars_right_rtl_test() { + "ืฉืžืฉ" + |> string.trim_chars_right("ืฉ") + |> should.equal("ืฉืž") +} + // unicode whitespaces pub fn trim_horizontal_tab_test() { "hats\u{0009}" From db3d5c575d71dad7e7b8259421f7f07b586e6c75 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 6 Oct 2024 13:38:53 -0400 Subject: [PATCH 3/5] Rename left/right variants to start/end --- src/gleam/string.gleam | 58 ++++++++++++++++++++++-------------- src/gleam_stdlib.mjs | 22 +++++++------- test/gleam/string_test.gleam | 32 ++++++++++---------- 3 files changed, 62 insertions(+), 50 deletions(-) diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index 200c4ab5..de1883a4 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -558,7 +558,7 @@ fn do_trim(string: String) -> String { /// ## Examples /// /// ```gleam -/// trim_chars_left("..,hats,..", ".,") +/// trim_chars("..,hats,..", ".,") /// // -> "hats" /// ``` pub fn trim_chars(string: String, charset: String) -> String { @@ -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,..", ".,") +/// trim_chars_start("..,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,..", ".,") +/// trim_chars_end("..,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)) } diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index cbeaf843..557f7096 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -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, "") @@ -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"); } diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index 12d364fe..aea3f2c3 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -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("ืฉืž") } From cc0bdb0e9ebd14750d0011080ce82d09d0e1ee4e Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 6 Oct 2024 19:33:28 -0400 Subject: [PATCH 4/5] Fix fatal typo --- src/gleam_stdlib.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 557f7096..de6a4f15 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -316,7 +316,7 @@ export function crash(message) { export function bit_array_to_string(bit_array) { try { - const decoder = new TextDecoder("utf-8", { fatarl: true }); + const decoder = new TextDecoder("utf-8", { fatal: true }); return new Ok(decoder.decode(bit_array.buffer)); } catch { return new Error(Nil); From 8048b9e1ebda37787139a819740cd4861edc6ce7 Mon Sep 17 00:00:00 2001 From: Nick Krichevsky Date: Sun, 6 Oct 2024 19:38:59 -0400 Subject: [PATCH 5/5] trim_chars_* -> trim_*_with --- src/gleam/string.gleam | 38 ++++++++++++++++++------------------ src/gleam_stdlib.mjs | 10 +++++----- test/gleam/string_test.gleam | 36 +++++++++++++++++----------------- 3 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index de1883a4..c065d988 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -558,23 +558,23 @@ fn do_trim(string: String) -> String { /// ## Examples /// /// ```gleam -/// trim_chars("..,hats,..", ".,") +/// trim_with("..,hats,..", ".,") /// // -> "hats" /// ``` -pub fn trim_chars(string: String, charset: String) -> String { - do_trim_chars(string, charset) +pub fn trim_with(string: String, charset: String) -> String { + do_trim_with(string, charset) } -@external(javascript, "../gleam_stdlib.mjs", "trim_chars") -fn do_trim_chars(string: String, charset: String) -> String { - erl_trim_chars(string, Both, erl_to_graphemes(charset)) +@external(javascript, "../gleam_stdlib.mjs", "trim_with") +fn do_trim_with(string: String, charset: String) -> String { + erl_trim_with(string, Both, erl_to_graphemes(charset)) } @external(erlang, "string", "trim") fn erl_trim(a: String, b: Direction) -> String @external(erlang, "string", "trim") -fn erl_trim_chars(a: String, b: Direction, c: ErlGraphemes) -> String +fn erl_trim_with(a: String, b: Direction, c: ErlGraphemes) -> String @external(erlang, "string", "to_graphemes") fn erl_to_graphemes(a: String) -> ErlGraphemes @@ -641,16 +641,16 @@ fn do_trim_end(string: String) -> String { /// ## Examples /// /// ```gleam -/// trim_chars_start("..,hats,..", ".,") +/// trim_start_with("..,hats,..", ".,") /// // -> "hats,.." /// ``` -pub fn trim_chars_start(string: String, charset: String) -> String { - do_trim_chars_start(string, charset) +pub fn trim_start_with(string: String, charset: String) -> String { + do_trim_start_with(string, charset) } -@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)) +@external(javascript, "../gleam_stdlib.mjs", "trim_start_with") +fn do_trim_start_with(string: String, charset: String) -> String { + erl_trim_with(string, Leading, erl_to_graphemes(charset)) } /// Like `trim_end`, but removes the specified chars at the end of a `String` @@ -658,16 +658,16 @@ fn do_trim_chars_start(string: String, charset: String) -> String { /// ## Examples /// /// ```gleam -/// trim_chars_end("..,hats,..", ".,") +/// trim_end_with("..,hats,..", ".,") /// // -> "..,hats" /// ``` -pub fn trim_chars_end(string: String, charset: String) -> String { - do_trim_chars_end(string, charset) +pub fn trim_end_with(string: String, charset: String) -> String { + do_trim_end_with(string, charset) } -@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)) +@external(javascript, "../gleam_stdlib.mjs", "trim_end_with") +fn do_trim_end_with(string: String, charset: String) -> String { + erl_trim_with(string, Trailing, erl_to_graphemes(charset)) } /// Splits a non-empty `String` into its first element (head) and rest (tail). diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index de6a4f15..44b39701 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -277,18 +277,18 @@ export function trim_end(string) { return string.replace(right_trim_regex, ""); } -export function trim_chars(string, charset) { - const trimmed_right = trim_chars_end(string, charset); - return trim_chars_start(trimmed_right, charset); +export function trim_with(string, charset) { + const trimmed_right = trim_end_with(string, charset); + return trim_start_with(trimmed_right, charset); } -export function trim_chars_start(string, charset) { +export function trim_start_with(string, charset) { const trim_regexp = new_start_trim_regexp(charset); return string.replace(trim_regexp, "") } -export function trim_chars_end(string, charset) { +export function trim_end_with(string, charset) { const trim_regexp = new_right_trim_regexp(charset); return string.replace(trim_regexp, "") diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index aea3f2c3..0484d694 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -188,27 +188,27 @@ pub fn trim_end_test() { |> should.equal(" hats") } -pub fn trim_chars_start_test() { +pub fn trim_start_with_test() { ",..hats..," - |> string.trim_chars_start(",.") + |> string.trim_start_with(",.") |> should.equal("hats..,") } -pub fn trim_chars_start_rtl_test() { +pub fn trim_start_with_rtl_test() { "ืฉืžืฉ" - |> string.trim_chars_start("ืฉ") + |> string.trim_start_with("ืฉ") |> should.equal("ืžืฉ") } -pub fn trim_chars_end_test() { +pub fn trim_end_with_test() { ",..hats..," - |> string.trim_chars_end(",.") + |> string.trim_end_with(",.") |> should.equal(",..hats") } -pub fn trim_chars_end_rtl_test() { +pub fn trim_end_with_rtl_test() { "ืฉืžืฉ" - |> string.trim_chars_end("ืฉ") + |> string.trim_end_with("ืฉ") |> should.equal("ืฉืž") } @@ -400,33 +400,33 @@ pub fn trim_comma_test() { |> should.equal("hats,") } -pub fn trim_chars_test() { +pub fn trim_with_test() { ",,hats," - |> string.trim_chars(",") + |> string.trim_with(",") |> should.equal("hats") } -pub fn trim_chars_commas_and_periods_test() { +pub fn trim_with_commas_and_periods_test() { ",,hats,..." - |> string.trim_chars(",.") + |> string.trim_with(",.") |> should.equal("hats") } -pub fn trim_chars_keeps_whitespace_not_in_charset_test() { +pub fn trim_with_keeps_whitespace_not_in_charset_test() { ",,hats ,..." - |> string.trim_chars(",.") + |> string.trim_with(",.") |> should.equal("hats ") } -pub fn trim_chars_does_not_trim_from_middle_of_string_test() { +pub fn trim_with_does_not_trim_from_middle_of_string_test() { ",,hats,hats,hats,..." - |> string.trim_chars(",.") + |> string.trim_with(",.") |> should.equal("hats,hats,hats") } -pub fn trim_chars_trims_complex_graphemes_test() { +pub fn trim_with_trims_complex_graphemes_test() { "hats๐Ÿ‘๐Ÿ‘๐Ÿ‘๐Ÿ‘" - |> string.trim_chars("๐Ÿ‘") + |> string.trim_with("๐Ÿ‘") |> should.equal("hats") }