on 64aee13 help sign in
bridge/src/form/formatters.js
63 lines 1.5 kB history raw
1import { compose, strSplice } from 'lib/lib';
3const TICKET_MAX_BYTE_LEN = 32; // tickets can be as large as 32 bytes
4const PAT_BLOCK_CHAR_LENGTH = 6; // pat{p,q} blocks are 6 characters long
6export const buildFormatter = (formatters = []) =>
7 compose(...formatters.reverse());
9/**
10 * inserts a ~ or - before every pat block
11 */
12export const ensurePatFormat = s => {
13 // bail if falsy or empty string, nothing to do
14 if (!s || s.length === 0) {
15 return s || '';
16 }
18 // ensure there's a dash every 6 + 1 characters
19 const dashAt = i => i * (PAT_BLOCK_CHAR_LENGTH + 1);
20 const maxBlocks = TICKET_MAX_BYTE_LEN / 2;
22 // for every index that may need a separator
23 for (let i = 0; i !== maxBlocks; i++) {
24 const dashIndex = dashAt(i);
26 // if the string is too short to have a separator here, we're done
27 if (s.length - 1 < dashIndex) {
28 return s;
29 }
31 // insert the separator at the correct index if not already there
32 const sep = i === 0 ? '~' : '-';
33 if (s.charAt(dashIndex) !== sep) {
34 s = strSplice(s, dashIndex, sep);
35 }
36 }
38 return s;
39};
41export const convertToNumber = s => {
42 try {
43 return parseInt(s, 10);
44 } catch {
45 return 0;
46 }
47};
49export const downcase = s => {
50 if (!s) {
51 return s;
52 }
54 return s.toLowerCase();
55};
57// const hideAllButLast = s => {
58// const ll = s[s.length -1];
59// const bs = fill(s.length - 1, '•')
60// return `${bs}${ll}`
61// };
62//
63// const hideAll = s => fill(s.length, '•');