@@ -1,6 +1,7 @@
1−import { compose } from 'lib/lib';
1+import { compose, strSplice } from 'lib/lib';
22
3−export const buildFormatter = (formatters = []) => compose(...formatters);
3+export const buildFormatter = (formatters = []) =>
4+ compose(...formatters.reverse());
45
56export const prependSig = s => {
67 if (!s || s.length === 0) {
@@ -10,6 +11,35 @@ export const prependSig = s => {
1011 return s.charAt(0) === '~' ? s : `~${s}`;
1112};
1213
14+/**
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
18+ */
19+export const ensurePatQDashes = maxByteLength => s => {
20+ if (!s || s.length === 0) {
21+ return s || '';
22+ }
23+
24+ // ensure there's a dash every 6 + 1 characters
25+ const dashAt = i => i * (6 + 1);
26+ const maxBlocks = maxByteLength / 2;
27+
28+ for (let i = 1; i !== maxBlocks; i++) {
29+ const dashIndex = dashAt(i);
30+
31+ if (s.length - 1 < dashIndex) {
32+ return s;
33+ }
34+
35+ if (s.charAt(dashIndex) !== '-') {
36+ s = strSplice(s, dashIndex, '-');
37+ }
38+ }
39+
40+ return s;
41+};
42+
1343export const convertToNumber = s => {
1444 try {
1545 return parseInt(s, 10);