diff options
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | gleam.toml | 2 | ||||
-rw-r--r-- | src/gleam/string.gleam | 4 | ||||
-rw-r--r-- | src/gleam_stdlib.mjs | 7 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 12 |
5 files changed, 16 insertions, 11 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 8379e72..22190d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,8 @@ JavaScript target. - Improved the performance of `string.drop_start`. - Improved the performance of `list.strict_zip`. +- Fixed a bug where `string.trim` could fail on JavaScript. +- Fixed a bug where `string.trim` wouldn't trim multi-line string on JavaScript. ## v0.45.0 - 2024-11-28 @@ -1,5 +1,5 @@ name = "gleam_stdlib" -version = "0.45.0" +version = "0.46.0" gleam = ">= 0.32.0" licences = ["Apache-2.0"] description = "A standard library for the Gleam programming language" diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index 00d58ad..a87b343 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -608,9 +608,8 @@ fn padding(size: Int, pad_string: String) -> String { /// // -> "hats" /// ``` /// -@external(javascript, "../gleam_stdlib.mjs", "trim") pub fn trim(string: String) -> String { - erl_trim(string, Both) + string |> trim_start |> trim_end } @external(erlang, "string", "trim") @@ -619,7 +618,6 @@ fn erl_trim(a: String, b: Direction) -> String type Direction { Leading Trailing - Both } /// Removes whitespace on the left of a `String`. diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index dc57c9c..4f804f7 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -307,13 +307,6 @@ const unicode_whitespaces = [ const trim_start_regex = new RegExp(`^[${unicode_whitespaces}]*`); const trim_end_regex = new RegExp(`[${unicode_whitespaces}]*$`); -const trim_regex = new RegExp( - `^[${unicode_whitespaces}]*(.*?)[${unicode_whitespaces}]*$`, -); - -export function trim(string) { - return string.match(trim_regex)[1]; -} export function trim_start(string) { return string.replace(trim_start_regex, ""); diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index 641d64d..8ff4f39 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -167,6 +167,18 @@ pub fn trim_test() { |> should.equal("hats") } +pub fn trim2_test() { + "k\r1=v2" + |> string.trim + |> should.equal("k\r1=v2") +} + +pub fn trim3_test() { + " \nhello\nworld\n " + |> string.trim + |> should.equal("hello\nworld") +} + pub fn trim_start_test() { " hats \n" |> string.trim_start |