-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
193 lines (172 loc) · 5.16 KB
/
utils.js
File metadata and controls
193 lines (172 loc) · 5.16 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { writable } from 'svelte/store'
import * as v from 'valibot'
export function normalize_path(value) {
return '/' + (value || '').replace(/^\/|\/$/g, '')
}
export function scroll_to_hash(hash) {
let id = hash.slice(1)
if (!id) return false
try {
id = decodeURIComponent(id)
} catch {}
const el = document.getElementById(id) || document.querySelector(`[name="${CSS.escape(id)}"]`)
el?.scrollIntoView()
return !!el
}
export function try_parse_json(value) {
if (typeof value !== 'string') return undefined
try {
return JSON.parse(value)
} catch {
return undefined
}
}
export function normalize_array_style(value) {
if (typeof value === 'string') return { default: value }
if (value && typeof value === 'object' && !Array.isArray(value))
return { default: value.default || 'repeat', ...value }
return { default: 'repeat' }
}
export function merge_search_opts(base = {}, next = {}) {
const out = { ...base, ...next }
const a = normalize_array_style(base.array_style)
const b = next.array_style
out.array_style =
b === undefined
? a
: typeof b === 'string'
? { default: b }
: { ...a, ...b, default: b.default || a.default }
return out
}
export function coerce_search_value(value, def, style, schema) {
if (value == null) return undefined
const last = Array.isArray(value) ? value.at(-1) : value
if (Array.isArray(def) || schema?.type === 'array' || schema?.wrapped?.type === 'array') {
const arr = Array.isArray(value) ? value : [value]
if (style !== 'json') {
const item = (schema?.type === 'array' ? schema : schema?.wrapped)?.item
const t = (item?.wrapped || item)?.type
const def = t === 'number' ? 0 : t === 'boolean' ? false : t === 'object' ? {} : null
return def == null ? arr : arr.map(v => coerce_search_value(v, def, style))
}
const parsed = try_parse_json(last)
return Array.isArray(parsed) ? parsed : arr
}
if (typeof def === 'number' && typeof last === 'string') {
const s = last.trim()
if (!s) return undefined
const n = Number(s)
return Number.isNaN(n) ? undefined : n
}
if (typeof def === 'boolean' && typeof last === 'string') {
if (last === 'true') return true
if (last === 'false') return false
return undefined
}
if (def && typeof def === 'object' && typeof last === 'string') {
const parsed = try_parse_json(last)
return parsed && typeof parsed === 'object' ? parsed : last
}
return last
}
export function read_search(url, schema, opts) {
const sp = url?.searchParams
if (!sp || !schema?.entries) return {}
const out = {}
const as = opts?.array_style
for (const k in schema.entries) {
const all = sp.getAll(k)
if (!all.length) continue
const style = as?.[k] || as?.default || 'repeat'
if (style === 'json') out[k] = all.at(-1)
else if (style === 'csv') out[k] = all.flatMap(v => (v ? v.split(',').filter(Boolean) : []))
else out[k] = all.length > 1 ? all : all[0]
}
return out
}
export function validate_search(raw, schema, defaults = {}, opts) {
if (!schema?.entries) return {}
const input = {}
const as = opts?.array_style
for (const k in schema.entries) {
if (!raw || !(k in raw)) continue
const style = as?.[k] || as?.default || 'repeat'
input[k] = coerce_search_value(raw[k], defaults?.[k], style, schema.entries[k])
}
const whole = v.safeParse(schema, input)
if (whole.success) return whole.output
const out = { ...defaults }
for (const k in input) {
const res = v.safeParse(schema.entries[k], input[k])
if (res.success) out[k] = res.output
}
return out
}
function safe_json(value) {
try {
return JSON.stringify(value)
} catch {
return null
}
}
export function build_search_string(cur, values, keys, defaults, opts, same_value) {
let sp = new URLSearchParams(cur.search)
const as = opts?.array_style
for (const k of keys) {
sp.delete(k)
const val = values?.[k]
if (val == null) continue
if (!opts?.show_defaults && same_value?.(val, defaults?.[k])) continue
if (Array.isArray(val)) {
if (!val.length) continue
const style = as?.[k] || as?.default || 'repeat'
if (style === 'csv') {
sp.set(k, val.map(String).join(','))
continue
}
if (style === 'json') {
sp.set(k, safe_json(val) ?? String(val))
continue
}
for (const x of val) if (x != null) sp.append(k, String(x))
continue
}
if (val && typeof val === 'object') {
sp.set(k, safe_json(val) ?? String(val))
continue
}
sp.set(k, String(val))
}
if (opts?.sort) {
sp = new URLSearchParams([...sp.entries()].sort(([a], [b]) => a.localeCompare(b)))
}
return sp.toString()
}
export function build_search_url(cur, values, keys, defaults, opts, same_value) {
const next = new URL(cur.href)
const s = build_search_string(cur, values, keys, defaults, opts, same_value)
next.search = s ? `?${s}` : ''
return next.href === cur.href ? null : next
}
export function create_search_store(stringify = () => '') {
let current = {}
const store = writable(current)
const api = {
subscribe: store.subscribe,
set(value) {
current = value && typeof value === 'object' ? value : {}
store.set(current)
},
update(fn) {
api.set(fn(current))
},
toString() {
return stringify(current)
},
set_stringifier(fn) {
stringify = fn || (() => '')
},
}
return api
}