aboutsummaryrefslogtreecommitdiff
path: root/src/gleam_stdlib.js
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2021-07-28 19:46:11 +0100
committerLouis Pilfold <louis@lpil.uk>2021-07-28 19:46:11 +0100
commitf37b2a7389cb7946d8a21381eb134cd3d579abc0 (patch)
tree77f87a92898e1215162ebfb4e619e38b3e0bbf2a /src/gleam_stdlib.js
parent838904656a8b38d32a1ab897b93ab35f8b688118 (diff)
downloadgleam_stdlib-f37b2a7389cb7946d8a21381eb134cd3d579abc0.tar.gz
gleam_stdlib-f37b2a7389cb7946d8a21381eb134cd3d579abc0.zip
JS should
Diffstat (limited to 'src/gleam_stdlib.js')
-rw-r--r--src/gleam_stdlib.js30
1 files changed, 22 insertions, 8 deletions
diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js
index d18cd5e..085bbe6 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 ok(x) {
return { type: "Ok", 0: x };
}
-function Error(x) {
+function 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 ok(Number(value));
} else {
- return Error(Nil);
+ return 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 ok([first, string.slice(first.length)]);
} else {
- return Error(Nil);
+ return 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 ok([before, after]);
} else {
- return Error(Nil);
+ return error(Nil);
}
}
@@ -175,3 +175,17 @@ export function bit_string_append(first, second) {
export function log(term) {
console.log(term);
}
+
+export function stringify(data) {
+ let replacer = (_key, value) =>
+ typeof value === "bigint" ? value.toString() + "n" : value;
+ try {
+ return JSON.stringify(data, replacer);
+ } catch (_error) {
+ return "//reference-cycle";
+ }
+}
+
+export function crash(message) {
+ throw new Error(message);
+}