aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorinoas <mail@inoas.com>2022-04-11 15:33:33 +0200
committerLouis Pilfold <louis@lpil.uk>2022-04-12 23:00:26 +0100
commit97b12bb5ec023b0b1077e4559b1ba0f4932d8cb3 (patch)
tree8f3857145f69984f9eca203ffb2d3ba1044b00fd
parent12a0f53eb5c95e774fc537398a27ed42f3873ab6 (diff)
downloadgleam_stdlib-97b12bb5ec023b0b1077e4559b1ba0f4932d8cb3.tar.gz
gleam_stdlib-97b12bb5ec023b0b1077e4559b1ba0f4932d8cb3.zip
JavaScript implementation of the module's receives fallback code for NodeJS 14 compatibility
-rw-r--r--CHANGELOG.md1
-rw-r--r--src/gleam_stdlib.mjs14
2 files changed, 14 insertions, 1 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index c16bea4..e0867b3 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,7 @@
for empty strings on older JavaScript runtimes.
- The `bool` module gains the `to_string` function.
- The `function` module gains the `tap` function.
+- The JavaScript implementation of the `string` module's `string_replace` received fallback code for NodeJS 14 compatibility.
## v0.20.0 - 2022-02-22
diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs
index 280cd95..4dd4aa7 100644
--- a/src/gleam_stdlib.mjs
+++ b/src/gleam_stdlib.mjs
@@ -58,7 +58,19 @@ export function int_to_base_string(int, base) {
}
export function string_replace(string, target, substitute) {
- return string.replaceAll(target, substitute);
+ if (typeof string.replaceAll !== "undefined") {
+ return string.replaceAll(target, substitute);
+ }
+ // Fallback for older Node.js versions:
+ // 1. <https://stackoverflow.com/a/1144788>
+ // 2. <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions#escaping>
+ // TODO: This fallback could be remove once Node.js 14 is EOL
+ // aka <https://nodejs.org/en/about/releases/> on or after 2024-04-30
+ return string.replace(
+ // $& means the whole matched string
+ new RegExp(target.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'g'),
+ substitute
+ );
}
export function string_reverse(string) {