on master help sign in
chore: cleanup/comment ensurePatFormat function
commitd5c7a6f97d206f6523bab31e729f4c71981337f7 authorMatt Condon (shrugs) <mattgcondon@gmail.com> dateTue 13 Aug 2019, 09:27 (7 years ago) parentd72cd54
1 file changed+7−3
src/form/formatters.js+7−3
src/form/formatters.js+7−3
@@ -1,31 +1,35 @@
11import { compose, strSplice } from 'lib/lib';
22
33const TICKET_MAX_BYTE_LEN = 32; // tickets can be as large as 32 bytes
4+const PAT_BLOCK_CHAR_LENGTH = 6; // pat{p,q} blocks are 6 characters long
45
56export const buildFormatter = (formatters = []) =>
67 compose(...formatters.reverse());
78
89/**
9− * inserts a ~ or - every 7 characters
10+ * inserts a ~ or - before every pat block
1011 */
1112export const ensurePatFormat = s => {
13+ // bail if falsy or empty string, nothing to do
1214 if (!s || s.length === 0) {
1315 return s || '';
1416 }
1517
1618 // ensure there's a dash every 6 + 1 characters
17− const dashAt = i => i * (6 + 1);
19+ const dashAt = i => i * (PAT_BLOCK_CHAR_LENGTH + 1);
1820 const maxBlocks = TICKET_MAX_BYTE_LEN / 2;
1921
22+ // for every index that may need a separator
2023 for (let i = 0; i !== maxBlocks; i++) {
2124 const dashIndex = dashAt(i);
2225
26+ // if the string is too short to have a separator here, we're done
2327 if (s.length - 1 < dashIndex) {
2428 return s;
2529 }
2630
31+ // insert the separator at the correct index if not already there
2732 const sep = i === 0 ? '~' : '-';
28−
2933 if (s.charAt(dashIndex) !== sep) {
3034 s = strSplice(s, dashIndex, sep);
3135 }