-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.ts
More file actions
50 lines (39 loc) · 1.22 KB
/
utils.ts
File metadata and controls
50 lines (39 loc) · 1.22 KB
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
import { goto } from '$app/navigation'
import type { Attachment } from 'svelte/attachments'
export function get_device_type() {
const w = window.innerWidth
if (w < 640) return 'mobile'
if (w < 1024) return 'tablet'
return 'desktop'
}
export function filter_by_tag(tag: string) {
goto(`/categories/${tag}`)
}
export function pluralize(count: number, forms: { one: string; other: string }) {
const word = count === 1 ? forms.one : forms.other
return word.replace('{count}', String(count))
}
export function string_to_color(str: string): string {
let hash = 0
for (let i = 0; i < str.length; i++) {
hash = str.charCodeAt(i) + ((hash << 5) - hash)
}
const h = hash % 360
return `hsl(${h}, 80%, 50%)`
}
export const resize_textarea: Attachment = (textarea) => {
if (!(textarea instanceof HTMLTextAreaElement)) return
textarea.style.height = `${textarea.scrollHeight}px`
textarea.style.overflowY = 'hidden'
const adjust = () => {
textarea.style.height = 'auto'
textarea.style.height = `${textarea.scrollHeight}px`
}
textarea.addEventListener('input', adjust)
return () => {
textarea.removeEventListener('input', adjust)
}
}
export function strip_html_tags(input: string): string {
return input.replace(/<[^>]*>/g, '')
}