aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRichard Viney <richard.viney@gmail.com>2024-11-12 11:49:30 +1300
committerLouis Pilfold <louis@lpil.uk>2024-11-26 13:37:23 +0000
commitb0afb8f5522ab96b54580dfe94ec045da0f758aa (patch)
treeaf1a49d6f78c6aa035e8b9746e42091335d1035e /src
parent8349133498dbd595b82c783d8e11d530689eeb35 (diff)
downloadgleam_stdlib-b0afb8f5522ab96b54580dfe94ec045da0f758aa.tar.gz
gleam_stdlib-b0afb8f5522ab96b54580dfe94ec045da0f758aa.zip
Optimise string trimming on JavaScript
Diffstat (limited to 'src')
-rw-r--r--src/gleam_stdlib.mjs13
1 files changed, 8 insertions, 5 deletions
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs
index 46bc72a..06302bf 100644
--- a/src/gleam_stdlib.mjs
+++ b/src/gleam_stdlib.mjs
@@ -302,19 +302,22 @@ 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 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 trim_start(trim_end(string));
+ return string.match(trim_regex)[1];
}
export function trim_start(string) {
- return string.replace(left_trim_regex, "");
+ return string.replace(trim_start_regex, "");
}
export function trim_end(string) {
- return string.replace(right_trim_regex, "");
+ return string.replace(trim_end_regex, "");
}
export function bit_array_from_string(string) {