aboutsummaryrefslogtreecommitdiffstats
path: root/web/utils.js
diff options
context:
space:
mode:
Diffstat (limited to 'web/utils.js')
-rw-r--r--web/utils.js34
1 files changed, 34 insertions, 0 deletions
diff --git a/web/utils.js b/web/utils.js
new file mode 100644
index 0000000..d7cf657
--- /dev/null
+++ b/web/utils.js
@@ -0,0 +1,34 @@
+// format as hex number
+export const hex = (n) => '0x' + n.toString(16);
+
+// concatenate Uint8Array
+export const concat = (a, b) => {
+ const arr = new Uint8Array(a.length + b.length);
+ arr.set(a);
+ arr.set(b, a.length);
+ return arr;
+};
+
+// like Array.reduce() but await each iteration
+export const areduce = async (array, fn, state=null) => {
+ for (const val of array) {
+ state = await fn(val, state);
+ }
+ return state;
+};
+
+// generate array with range [a, b[ or [0, a[
+export const range = (a, b=null) => {
+ if (b === null) {
+ b = a;
+ a = 0;
+ }
+
+ const res = [];
+ for (let i = a; i < b; i++)
+ res.push(i);
+ return res;
+};
+
+// return a Promise that resolves in n seconds
+export const sleep = (n) => new Promise(res => setTimeout(res, n*1000));