@@ -1,39 +1,33 @@
11import { compose, strSplice } from 'lib/lib';
22
3+const TICKET_MAX_BYTE_LEN = 32; // tickets can be as large as 32 bytes
4+
35export const buildFormatter = (formatters = []) =>
46 compose(...formatters.reverse());
57
6−export const prependSig = s => {
7− if (!s || s.length === 0) {
8− return s || '';
9− }
10−
11− return s.charAt(0) === '~' ? s : `~${s}`;
12−};
13−
148/**
15− * inserts a - every 7 characters
16− * (must be preceeded by prependSig to work correctly)
17− * @param {number} maxByteLength the maximum number of patq blocks to have in a string
9+ * inserts a ~ or - every 7 characters
1810 */
19−export const ensurePatQDashes = maxByteLength => s => {
11+export const ensurePatFormat = s => {
2012 if (!s || s.length === 0) {
2113 return s || '';
2214 }
2315
2416 // ensure there's a dash every 6 + 1 characters
2517 const dashAt = i => i * (6 + 1);
26− const maxBlocks = maxByteLength / 2;
18+ const maxBlocks = TICKET_MAX_BYTE_LEN / 2;
2719
28− for (let i = 1; i !== maxBlocks; i++) {
20+ for (let i = 0; i !== maxBlocks; i++) {
2921 const dashIndex = dashAt(i);
3022
3123 if (s.length - 1 < dashIndex) {
3224 return s;
3325 }
3426
35− if (s.charAt(dashIndex) !== '-') {
36− s = strSplice(s, dashIndex, '-');
27+ const sep = i === 0 ? '~' : '-';
28+
29+ if (s.charAt(dashIndex) !== sep) {
30+ s = strSplice(s, dashIndex, sep);
3731 }
3832 }
3933