mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
at schema-errors 70 lines 1.9 kB view raw
1export function enforceLen( 2 str: string, 3 len: number, 4 ellipsis = false, 5 mode: 'end' | 'middle' = 'end', 6): string { 7 str = str || '' 8 if (str.length > len) { 9 if (ellipsis) { 10 if (mode === 'end') { 11 return str.slice(0, len) + '…' 12 } else if (mode === 'middle') { 13 const half = Math.floor(len / 2) 14 return str.slice(0, half) + '…' + str.slice(-half) 15 } else { 16 // fallback 17 return str.slice(0, len) 18 } 19 } else { 20 return str.slice(0, len) 21 } 22 } 23 return str 24} 25 26// https://stackoverflow.com/a/52171480 27export function toHashCode(str: string, seed = 0): number { 28 let h1 = 0xdeadbeef ^ seed, 29 h2 = 0x41c6ce57 ^ seed 30 for (let i = 0, ch; i < str.length; i++) { 31 ch = str.charCodeAt(i) 32 h1 = Math.imul(h1 ^ ch, 2654435761) 33 h2 = Math.imul(h2 ^ ch, 1597334677) 34 } 35 h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) 36 h1 ^= Math.imul(h2 ^ (h2 >>> 13), 3266489909) 37 h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) 38 h2 ^= Math.imul(h1 ^ (h1 >>> 13), 3266489909) 39 40 return 4294967296 * (2097151 & h2) + (h1 >>> 0) 41} 42 43export function countLines(str: string | undefined): number { 44 if (!str) return 0 45 return str.match(/\n/g)?.length ?? 0 46} 47 48// Augments search query with additional syntax like `from:me` 49export function augmentSearchQuery(query: string, {did}: {did?: string}) { 50 // Don't do anything if there's no DID 51 if (!did) { 52 return query 53 } 54 55 // We don't want to replace substrings that are being "quoted" because those 56 // are exact string matches, so what we'll do here is to split them apart 57 58 // Even-indexed strings are unquoted, odd-indexed strings are quoted 59 const splits = query.split(/("(?:[^"\\]|\\.)*")/g) 60 61 return splits 62 .map((str, idx) => { 63 if (idx % 2 === 0) { 64 return str.replaceAll(/(^|\s)from:me(\s|$)/g, `$1${did}$2`) 65 } 66 67 return str 68 }) 69 .join('') 70}