-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.ts
More file actions
56 lines (46 loc) · 1.36 KB
/
Copy pathutils.ts
File metadata and controls
56 lines (46 loc) · 1.36 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
51
52
53
54
55
56
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))
}
/**
* Removes accents from letters and transforms to lowercase
*/
export function normalize_text(txt: string) {
return txt
.toLowerCase()
.normalize('NFD')
.replace(/\p{Diacritic}/gu, '')
}
export function get_comparison_score(value: string, query: string) {
if (!query) return -1
const v = normalize_text(value)
const q = normalize_text(query)
if (v === q) return 3
if (v.startsWith(q)) return 2
if (v.includes(q)) return 1
return 0
}
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)
}
}