on 261ea40 help sign in
bridge/src/lib/lib.js
53 lines 1.2 kB history raw
1import * as ob from 'urbit-ob';
3const compose = (...fs) =>
4 fs.reduceRight((pF, nF) => (...args) => nF(pF(...args)), v => v);
6const allFalse = (...args) => args.every(a => a === false);
8const isLast = (l, i) => i === l - 1;
10const isUndefined = v => v === undefined;
12const defaultTo = (v, d) => (isUndefined(v) ? d : v);
14const seq = n => Array.from(Array(n), (_, i) => i);
16const fill = (n, v) => Array.from(Array(n), () => v);
18const isValidGalaxy = name => {
19 let point;
20 try {
21 point = parseInt(ob.patp2dec(name), 10);
22 } catch (err) {
23 return false;
24 }
25 return point >= 0 && point < 256;
26};
28const randomPatq = len => {
29 let bytes = window.crypto.getRandomValues(new Uint8Array(len));
30 let hex = bytes.reduce(
31 (acc, byt) => acc + byt.toString(16).padStart(2, '0'),
32 ''
33 );
34 return ob.hex2patq(hex);
35};
37// TODO: is there a function for this calculation in urbit-ob or similar?
38// 3 characters per byte, plus a tilde and hypen prefix per pair of bytes
39const patpStringLength = byteLength =>
40 byteLength * 3 + Math.ceil(byteLength / 2);
42export {
43 compose,
44 allFalse,
45 isLast,
46 isUndefined,
47 defaultTo,
48 seq,
49 fill,
50 isValidGalaxy,
51 randomPatq,
52 patpStringLength,
53};