Skip to content

Commit d889e56

Browse files
committed
Fix string.trim
1 parent f4f3b89 commit d889e56

File tree

5 files changed

+16
-11
lines changed

5 files changed

+16
-11
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
JavaScript target.
77
- Improved the performance of `string.drop_start`.
88
- Improved the performance of `list.strict_zip`.
9+
- Fixed a bug where `string.trim` could fail on JavaScript.
10+
- Fixed a bug where `string.trim` wouldn't trim multi-line string on JavaScript.
911

1012
## v0.45.0 - 2024-11-28
1113

gleam.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "gleam_stdlib"
2-
version = "0.45.0"
2+
version = "0.46.0"
33
gleam = ">= 0.32.0"
44
licences = ["Apache-2.0"]
55
description = "A standard library for the Gleam programming language"

src/gleam/string.gleam

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,9 +608,8 @@ fn padding(size: Int, pad_string: String) -> String {
608608
/// // -> "hats"
609609
/// ```
610610
///
611-
@external(javascript, "../gleam_stdlib.mjs", "trim")
612611
pub fn trim(string: String) -> String {
613-
erl_trim(string, Both)
612+
string |> trim_start |> trim_end
614613
}
615614

616615
@external(erlang, "string", "trim")
@@ -619,7 +618,6 @@ fn erl_trim(a: String, b: Direction) -> String
619618
type Direction {
620619
Leading
621620
Trailing
622-
Both
623621
}
624622

625623
/// Removes whitespace on the left of a `String`.

src/gleam_stdlib.mjs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,6 @@ const unicode_whitespaces = [
307307

308308
const trim_start_regex = new RegExp(`^[${unicode_whitespaces}]*`);
309309
const trim_end_regex = new RegExp(`[${unicode_whitespaces}]*$`);
310-
const trim_regex = new RegExp(
311-
`^[${unicode_whitespaces}]*(.*?)[${unicode_whitespaces}]*$`,
312-
);
313-
314-
export function trim(string) {
315-
return string.match(trim_regex)[1];
316-
}
317310

318311
export function trim_start(string) {
319312
return string.replace(trim_start_regex, "");

test/gleam/string_test.gleam

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,18 @@ pub fn trim_test() {
167167
|> should.equal("hats")
168168
}
169169

170+
pub fn trim2_test() {
171+
"k\r1=v2"
172+
|> string.trim
173+
|> should.equal("k\r1=v2")
174+
}
175+
176+
pub fn trim3_test() {
177+
" \nhello\nworld\n "
178+
|> string.trim
179+
|> should.equal("hello\nworld")
180+
}
181+
170182
pub fn trim_start_test() {
171183
" hats \n"
172184
|> string.trim_start

0 commit comments

Comments
 (0)