aboutsummaryrefslogtreecommitdiff
path: root/src/gleam_stdlib.js
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2021-07-30 20:35:57 +0100
committerLouis Pilfold <louis@lpil.uk>2021-07-30 20:35:57 +0100
commit12d91899bf20c95c491f1c7323ec9e198208ebac (patch)
treefe591676915769aeec2d1c80ac2a7a7ff43110b6 /src/gleam_stdlib.js
parentf37b2a7389cb7946d8a21381eb134cd3d579abc0 (diff)
downloadgleam_stdlib-12d91899bf20c95c491f1c7323ec9e198208ebac.tar.gz
gleam_stdlib-12d91899bf20c95c491f1c7323ec9e198208ebac.zip
Bit string JS funtions
Diffstat (limited to 'src/gleam_stdlib.js')
-rw-r--r--src/gleam_stdlib.js26
1 files changed, 18 insertions, 8 deletions
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js
index 085bbe6..ff0921d 100644
--- a/src/gleam_stdlib.js
+++ b/src/gleam_stdlib.js
@@ -8,11 +8,11 @@ function to_list(array) {
return list;
}
-function ok(x) {
+function gleam_ok(x) {
return { type: "Ok", 0: x };
}
-function error(x) {
+function gleam_error(x) {
return { type: "Error", 0: x };
}
@@ -22,9 +22,9 @@ export function identity(x) {
export function parse_int(value) {
if (/^[-+]?(\d+)$/.test(value)) {
- return ok(Number(value));
+ return gleam_ok(Number(value));
} else {
- return error(Nil);
+ return gleam_error(Nil);
}
}
@@ -72,9 +72,9 @@ export function pop_grapheme(string) {
first = string.match(/./u)?.[0];
}
if (first) {
- return ok([first, string.slice(first.length)]);
+ return gleam_ok([first, string.slice(first.length)]);
} else {
- return error(Nil);
+ return gleam_error(Nil);
}
}
@@ -143,9 +143,9 @@ export function split_once(haystack, needle) {
if (index >= 0) {
let before = haystack.slice(0, index);
let after = haystack.slice(index + needle.length);
- return ok([before, after]);
+ return gleam_ok([before, after]);
} else {
- return error(Nil);
+ return gleam_error(Nil);
}
}
@@ -189,3 +189,13 @@ export function stringify(data) {
export function crash(message) {
throw new Error(message);
}
+
+export function bit_string_to_string(bit_string) {
+ try {
+ return gleam_ok(
+ new TextDecoder("utf-8", { fatal: true }).decode(bit_string)
+ );
+ } catch (_error) {
+ return gleam_error(undefined);
+ }
+}