elk/composables/misc.ts

41 lines
1.2 KiB
TypeScript
Raw Permalink Normal View History

2023-01-08 06:21:09 +00:00
import type { mastodon } from 'masto'
2022-11-23 03:48:01 +00:00
export const UserLinkRE = /^https?:\/\/([^/]+)\/@([^/]+)$/
export const TagLinkRE = /^https?:\/\/([^/]+)\/tags\/([^/]+)$/
export const HTMLTagRE = /<[^>]+>/g
2022-11-23 03:48:01 +00:00
2022-11-21 13:21:53 +00:00
export function getDataUrlFromArr(arr: Uint8ClampedArray, w: number, h: number) {
if (typeof w === 'undefined' || typeof h === 'undefined')
w = h = Math.sqrt(arr.length / 4)
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')!
canvas.width = w
canvas.height = h
const imgData = ctx.createImageData(w, h)
imgData.data.set(arr)
ctx.putImageData(imgData, 0, 0)
return canvas.toDataURL()
}
2023-01-08 06:21:09 +00:00
export function emojisArrayToObject(emojis: mastodon.v1.CustomEmoji[]) {
return Object.fromEntries(emojis.map(i => [i.shortcode, i]))
}
2022-11-24 03:42:03 +00:00
export function noop() {}
export const useIsMac = () => {
const headers = useRequestHeaders(['user-agent'])
return computed(() => headers['user-agent']?.includes('Macintosh')
2022-12-02 08:52:00 +00:00
?? navigator?.platform?.includes('Mac') ?? false)
}
export const isEmptyObject = (object: Object) => Object.keys(object).length === 0
export function removeHTMLTags(str: string) {
return str.replaceAll(HTMLTagRE, '')
}