From 838904656a8b38d32a1ab897b93ab35f8b688118 Mon Sep 17 00:00:00 2001 From: Louis Pilfold Date: Wed, 28 Jul 2021 19:00:08 +0100 Subject: Bit string JS functions --- src/gleam_stdlib.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/gleam_stdlib.js') diff --git a/src/gleam_stdlib.js b/src/gleam_stdlib.js index 89908de..d18cd5e 100644 --- a/src/gleam_stdlib.js +++ b/src/gleam_stdlib.js @@ -107,7 +107,9 @@ export function join(xs) { } export function byte_size(data) { - if (typeof Blob === "function") { + if (data instanceof ArrayBuffer) { + return data.byteLength; + } else if (typeof Blob === "function") { return new Blob([data]).size; } else if (typeof Buffer === "function") { return Buffer.byteLength(data); @@ -158,3 +160,18 @@ export function trim_left(string) { export function trim_right(string) { return string.trimRight(); } + +export function bit_string_from_string(string) { + return new TextEncoder().encode(string).buffer; +} + +export function bit_string_append(first, second) { + let array = new Uint8Array(first.byteLength + second.byteLength); + array.set(new Uint8Array(first), 0); + array.set(new Uint8Array(second), first.byteLength); + return array.buffer; +} + +export function log(term) { + console.log(term); +} -- cgit v1.2.3