// Copyright (C) 2012-2023 Zammad Foundation, https://zammad-foundation.org/ import linkifyStr from 'linkify-string' export { htmlCleanup } from './htmlCleanup' type Falsy = false | 0 | '' | null | undefined type IsTruthy = T extends Falsy ? never : T export const truthy = (value: Maybe): value is IsTruthy => { return !!value } export const edgesToArray = ( object?: Maybe<{ edges?: { node: T }[] }>, ): T[] => { return object?.edges?.map((edge) => edge.node) || [] } export const normalizeEdges = ( object?: Maybe<{ edges?: { node: T }[]; totalCount?: number }>, ): { array: T[]; totalCount: number } => { const array = edgesToArray(object) return { array, totalCount: object?.totalCount ?? array.length, } } export const mergeArray = (a: T, b: T) => { return [...new Set([...a, ...b])] } export const waitForAnimationFrame = () => { return new Promise((resolve) => requestAnimationFrame(resolve)) } export const textCleanup = (ascii: string) => { if (!ascii) return '' return ascii .trim() .replace(/(\r\n|\n\r)/g, '\n') // cleanup .replace(/\r/g, '\n') // cleanup .replace(/[ ]\n/g, '\n') // remove tailing spaces .replace(/\n{3,20}/g, '\n\n') // remove multiple empty lines } // taken from App.Utils.text2html for consistency export const textToHtml = (text: string) => { text = textCleanup(text) text = linkifyStr(text) text = text.replace(/(\n\r|\r\n|\r)/g, '\n') text = text.replace(/ {2}/g, '  ') text = `
${text.replace(/\n/g, '
')}
` return text.replace(/
<\/div>/g, '

') } export const humanizeFileSize = (size: number) => { if (size > 1024 * 1024) { return `${Math.round((size * 10) / (1024 * 1024)) / 10} MB` } if (size > 1024) { return `${Math.round(size / 1024)} KB` } return `${size} Bytes` }