All files / app/util format.ts

79.41% Statements 135/170
74% Branches 111/150
69.44% Functions 25/36
89.07% Lines 106/119

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199172x       172x 172x 172x   172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x 172x     99x       11x 11x 44x 11x 33x 11x 22x   22x 11x 11x 11x     11x       42x 32x 31x       8x 40x   6x       14x 14x 14x 4x   3x   7x 6x 5x 4x       11x       19x 16x 14x 12x 11x 8x 5x 5x 5x       4x             4x     172x   1x               1x     1x       2x               22x 13x 22x 22x 10x 9x 8x       5x 5x 5x 5x 4x 4x 3x 2x 1x 1x       4x 2x 4x 4x 2x 2x 2x 2x 2x       2x 2x 2x               2x 2x 2x 2x         8x 1x 1x       5x 3x 2x       5x 2x 1x       6x   6x    
import * as he from 'he';
import { filter, sortBy, uniq } from 'lodash-es';
import { Ref } from '../model/ref';
import { Config, ModType } from '../model/tag';
import { reverseOrigin } from '../mods/mailbox';
import { config } from '../service/config.service';
import { hasPrefix, hasTag, publicTag } from './tag';
 
export const URI_REGEX = /^[^\s:\/?#]+:(\/\/([^\/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?$/;
export const TAG_REGEX = /^[_+]?[a-z0-9]+([./][a-z0-9]+)*$/;
export const TAG_SUFFIX_REGEX = /^[+_]|([_+]?[a-z0-9]+([./][a-z0-9]+)*)?$/;
export const TAGS_REGEX = /^[,\s]*-?[_+]?[a-z0-9]+([.\/][a-z0-9]+)*([,\s]+-?[_+]?[a-z0-9]+([.\/][a-z0-9]+)*)*[,\s]*$/;
export const USER_REGEX = /^[_+]user(\/[a-z0-9]+([./][a-z0-9]+)*)?$/;
export const QUALIFIED_USER_REGEX = /^[_+]user(\/[a-z0-9]+([./][a-z0-9]+)*)?(@[a-z0-9]+(\.[a-z0-9]+)*)?$/;
export const PLUGIN_REGEX = /^[_+]?plugin\/[a-z0-9]+([./][a-z0-9]+)*$/;
export const ORIGIN_NOT_BLANK_REGEX = /^@[a-z0-9]+(\.[a-z0-9]+)*$/;
export const ORIGIN_REGEX = /^(@[a-z0-9]+(\.[a-z0-9]+)*)?$/;
export const QUALIFIED_TAG_REGEX = /^[_+]?[a-z0-9]+([./][a-z0-9]+)*(@[a-z0-9]+(\.[a-z0-9]+)*)?$/;
export const QUALIFIED_TAGS_REGEX = /^[,\s]*[_+]?[a-z0-9]+([./][a-z0-9]+)*(@[a-z0-9]+(\.[a-z0-9]+)*)?([,\s]+[_+]?[a-z0-9]+([./][a-z0-9]+)*(@[a-z0-9]+(\.[a-z0-9]+)*)?)*[,\s]*$/;
export const SELECTOR_REGEX = /^!?([_+]?[a-z0-9]+([./][a-z0-9]+)*|([_+]?[a-z0-9]+([./][a-z0-9]+)*)?(@[a-z0-9]+(\.[a-z0-9]+)*|@\*|@|\*))$/;
export const QUERY_REGEX = /^(!?([_+]?[a-z0-9]+([./][a-z0-9]+)*|([_+]?[a-z0-9]+([./][a-z0-9]+)*)?(@[a-z0-9]+(\.[a-z0-9]+)*|@\*|@|\*))|\(!?([_+]?[a-z0-9]+([./][a-z0-9]+)*|([_+]?[a-z0-9]+([./][a-z0-9]+)*)?(@[a-z0-9]+(\.[a-z0-9])*|@\*|@|\*))([ |]!?([_+]?[a-z0-9]+([./][a-z0-9]+)*|([_+]?[a-z0-9]+([./][a-z0-9]+)*)?(@[a-z0-9]+(\.[a-z0-9])*|@\*|@|\*)))*\))([ |:&](!?([_+]?[a-z0-9]+([./][a-z0-9]+)*|([_+]?[a-z0-9]+([./][a-z0-9]+)*)?(@[a-z0-9]+(\.[a-z0-9])*|@\*|@|\*))|\(!?([_+]?[a-z0-9]+([./][a-z0-9]+)*|([_+]?[a-z0-9]+([./][a-z0-9]+)*)?(@[a-z0-9]+(\.[a-z0-9])*|@\*|@|\*))([ |]!?([_+]?[a-z0-9]+([./][a-z0-9]+)*|([_+]?[a-z0-9]+([./][a-z0-9]+)*)?(@[a-z0-9]+(\.[a-z0-9])*|@\*|@|\*)))*\)))*$/;
 
export function templates(tags?: string[], template?: string) {
  return filter(tags, t => hasPrefix(t, template));
}
 
export function authors(ref: Ref, prefixes = ['+user', '_user', 'plugin/from', 'plugin/alias']) {
  const authors = [];
  for (const p of prefixes) {
    if (p === '+user') {
      authors.push(...templates(ref.tags || [], '+user').map(t => t + (ref.origin || '')));
    } else if (p === '_user') {
      authors.push(...templates(ref.tags || [], '_user').map(t => t + (ref.origin || '')));
    I} else if (p === '+plugin') {
      authors.push(...templates(ref.tags || [], '+plugin').map(t => t + (ref.origin || '')));
    } else if (p === 'plugin/from') {
      authors.push(...templates(ref.tags || [], 'plugin/from').map(t => reverseOrigin(t.substring('plugin/from/'.length), ref.origin || '')));
    E} else if (p === 'plugin/alias') {
      authors.push(...templates(ref.tags || [], 'plugin/alias').map(t => t.substring('plugin/alias/'.length) + (ref.origin || '')));
    }
  }
  return uniq(authors);
}
 
export function userAuthors(ref: Ref) {
  return uniq(templates(ref.tags || [], 'user')
    .filter(t => !publicTag(t))
    .map(t => t + (ref.origin || '')));
}
 
export function clickableLink(url: string) {
  for (const v of config().allowedSchemes) {
    if (url.startsWith(v)) return true;
  }
  return false;
}
 
export function urlSummary(url: string) {
  try {
    const parsed = new URL(url);
    if (parsed.protocol === 'http:' || parsed.protocol === 'https:') {
      return parsed.host;
    }
    return parsed.protocol.substring(0, parsed.protocol.length - 1);
  } catch (e) {}
  if (url.startsWith('lnbc')) return 'lightning';
  if (url.startsWith('bc1')) return 'bitcoin';
  if (url.startsWith('0x')) return 'ethereum';
  return 'unknown';
}
 
export function interestingTags(tags?: string[]): string[] {
  return filter(tags, interestingTag);
}
 
export function interestingTag(tag: string) {
  if (tag === 'public') return false;
  if (tag === 'locked') return false;
  if (tag === 'internal') return false;
  if (tag === '_moderated') return false;
  if (hasPrefix(tag, 'plugin')) return false;
  if (hasPrefix(tag, 'user')) return false;
  Iif (tag === '+user') return false;
  Iif (tag === '_user') return false;
  return true;
}
 
export function formatAuthor(tag: string) {
  return tag
    .replace('+user@', '@')
    .replace('+', '')
    .replace('user/', '');
}
 
export function isTextPost(ref: Ref) {
  return ref.url.startsWith('comment:') && !hasTag('internal', ref);
}
 
export const DEFAULT_TYPE = 'feature';
export function configGroups(def: Record<string, Config>): Record<ModType, [string, Config][]> {
  let result = Object.entries(def).reduce((result, item) => {
    const mod = modId(item[1]);
    const type: [string, Config][] = result[item[1].config?.type || DEFAULT_TYPE] ||= [];
    if (!type.find(i => mod === modId(i[1]))) {
      type.push([item[0].replace(/[.]/g, '-'), item[1]]);
    }
    return result;
  }, {} as Record<ModType, [string, Config][]>)
  for (const k of Object.keys(result) as ModType[]) {
    result[k] = sortBy(result[k], [e => e[1]!.tag.match(/^[+_]/) ? e[1]!.tag.substring(1) : e[1]!.tag]);
  }
  return result;
}
 
export function formSafeNames(map: any) {
  return Object.fromEntries(Object.entries(map).map(e => [e[0].replace(/[.]/g, '-'), e[1]]));
}
 
export function modId(c?: Config) {
  return c?.config?.mod || c?.name || c?.tag || '';
}
 
export function getTitle(ref: Ref | undefined): string {
  if (!ref) return '';
  const title = (ref.title || '').trim();
  const comment = (ref.comment || '').trim();
  if (title) return title;
  if (comment) return trimCommentForTitle(comment);
  if (ref.url?.startsWith('tag:/')) return '#' + ref.url.substring('tag:/'.length);
  return ref.url;
}
 
export function getNiceTitle(ref: Ref | undefined): string {
  Iif (!ref) return '';
  const title = (ref.title || '').trim();
  const comment = (ref.comment || '').trim();
  if (title) return title;
  Iif (comment) return trimCommentForTitle(comment);
  if (ref.url.startsWith('cache:')) return '';
  if (ref.url.startsWith('comment:')) return '';
  if (ref.url.startsWith('internal:')) return '';
  Iif (ref.url.startsWith('tag:/')) return '#' + ref.url.substring('tag:/'.length);
  return ref.url;
}
 
export function getPageTitle(ref: Ref | undefined, top?: Ref): string {
  if (!ref) return '';
  const title = (ref.title || '').trim();
  const comment = (ref.comment || '').trim();
  Iif (title) return title;
  Iif (top?.title) return $localize`Re: ` + getTitle(top);
  Iif (comment) return trimCommentForTitle(comment);
  Iif (top?.comment) return $localize`Re: ` + getTitle(top);
  Iif (ref.url.startsWith('tag:/')) return '#' + ref.url.substring('tag:/'.length);
  return ref.url;
}
 
function trimCommentForTitle(comment: string): string {
  Iif (!comment) return '';
  comment = he.decode(comment.replace( /<[^>]+>/g, ''));
  Eif (!comment.includes('\n')) return trimTextForTitle(comment)
  const lines = comment.split('\n').map(t => t.trim()).filter(t => t.length);
  const newText = lines.filter(l => !l.startsWith('>'));
  if (newText.length) return trimTextForTitle(newText[0]);
  return trimTextForTitle(lines[0]);
}
 
function trimTextForTitle(comment: string) {
  Iif (!comment) return '';
  comment = comment.replace(/!\[+?]\(([^)]|\\\))*\)/, $localize` Image `);
  comment = comment.replace(/\[([^\]]+)]\(([^)]|\\\))*\)/, ' $1 ');
  Eif (comment.length <= 140) return comment;
  return comment.substring(0, 140) + '...';
}
 
export function hasComment(comment?: string) {
  if (!comment) return false;
  comment = he.decode(comment.replace( /<[^>]+>/g, ''));
  return trimCommentForTitle(comment) !== comment;
}
 
export function getRe(title?: string) {
  if (!title) return '';
  if (title.startsWith($localize`Re: `)) return title;
  return $localize`Re: ` + title;
}
 
export function tagLink(tag: string, origin?: string, local?: string) {
  if (local === origin || !local && !origin) return tag;
  if (local && !origin) return tag + '@';
  return tag + (origin || '');
}
 
export function readableBytes(size: number): string  {
  const i = !size ? 0 : Math.floor(Math.log(size) / Math.log(1024));
  // @ts-ignore
  return (size / Math.pow(1024, i)).toFixed(2) * 1 + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i];
}