1import { compose, strSplice } from 'lib/lib';
2import { convertToInt } from 'lib/convertToInt';
4const HEX_PREFIX = '0x';
6const TICKET_MAX_BYTE_LEN = 32; // tickets can be as large as 32 bytes
7const PAT_BLOCK_CHAR_LENGTH = 6; // pat{p,q} blocks are 6 characters long
9export const buildFormatter = (formatters = []) =>
10 compose(...formatters.reverse());
13 * inserts a ~ or - before every pat block
15export const ensurePatFormat = s => {
16 // bail if falsy or empty string, nothing to do
17 if (!s || s.length === 0) {
21 // ensure there's a dash every 6 + 1 characters
22 const dashAt = i => i * (PAT_BLOCK_CHAR_LENGTH + 1);
23 const maxBlocks = TICKET_MAX_BYTE_LEN / 2;
25 // for every index that may need a separator
26 for (let i = 0; i !== maxBlocks; i++) {
27 const dashIndex = dashAt(i);
29 // if the string is too short to have a separator here, we're done
30 if (s.length - 1 < dashIndex) {
34 // insert the separator at the correct index if not already there
35 const sep = i === 0 ? '~' : '-';
36 if (s.charAt(dashIndex) !== sep) {
37 s = strSplice(s, dashIndex, sep);
44export const ensureHexPrefix = s => {
45 if (!s || s.length < HEX_PREFIX.length) {
49 return s.indexOf(HEX_PREFIX) !== 0 ? `${HEX_PREFIX}${s}` : s;
52export const convertToNumber = s => {
58 return convertToInt(s, 10);
64export const downcase = s => {
69 return s.toLowerCase();
72export const stripHexPrefix = s => {
77 return s.toLowerCase().indexOf(HEX_PREFIX) === 0
78 ? s.slice(HEX_PREFIX.length)
82export const ensureSigPrefix = s => {
83 if (!s || s.length < SIG_PREFIX.length) {
87 return s.indexOf(SIG_PREFIX) !== 0 ? `${SIG_PREFIX}${s}` : s;
90export const stripSigPrefix = s => {
95 return s.toLowerCase().indexOf(SIG_PREFIX) === 0
96 ? s.slice(SIG_PREFIX.length)
100export const ticketToSegments = (ticket: string): string[] => {
101 return stripSigPrefix(ticket).split('-');
104export const ticketFromSegments = (segments: string[]): string => {
105 return ensureSigPrefix(segments.join('-'));
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list