aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2024-12-08 13:40:24 +0000
committerLouis Pilfold <louis@lpil.uk>2024-12-08 13:40:24 +0000
commitd889e565edeb6014d24d7749866faa3250e785c4 (patch)
treeb05e1c2580c4356cf1421d95d968ef6a52429014
parentf4f3b89f1819a0d55448ef2e9997fd06d349e392 (diff)
downloadgleam_stdlib-d889e565edeb6014d24d7749866faa3250e785c4.tar.gz
gleam_stdlib-d889e565edeb6014d24d7749866faa3250e785c4.zip
Fix string.trim
-rw-r--r--CHANGELOG.md2
-rw-r--r--gleam.toml2
-rw-r--r--src/gleam/string.gleam4
-rw-r--r--src/gleam_stdlib.mjs7
-rw-r--r--test/gleam/string_test.gleam12
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
diff --git a/gleam.toml b/gleam.toml
index d89603e..985ca7c 100644
--- a/gleam.toml
+++ b/gleam.toml
@@ -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