fully-react
Version:
React Server for Vite
55 lines (45 loc) • 1.45 kB
text/typescript
import { decode, encode } from './qss'
import { AnySearchSchema } from './route'
export const defaultParseSearch = parseSearchWith(JSON.parse)
export const defaultStringifySearch = stringifySearchWith(JSON.stringify)
export function parseSearchWith(parser: (str: string) => any) {
return (searchStr: string): AnySearchSchema => {
if (searchStr.substring(0, 1) === '?') {
searchStr = searchStr.substring(1)
}
let query: Record<string, unknown> = decode(searchStr)
// Try to parse any query params that might be json
for (let key in query) {
const value = query[key]
if (typeof value === 'string') {
try {
query[key] = parser(value)
} catch (err) {
//
}
}
}
return query
}
}
export function stringifySearchWith(stringify: (search: any) => string) {
return (search: Record<string, any>) => {
search = { ...search }
if (search) {
Object.keys(search).forEach((key) => {
const val = search[key]
if (typeof val === 'undefined' || val === undefined) {
delete search[key]
} else if (val && typeof val === 'object' && val !== null) {
try {
search[key] = stringify(val)
} catch (err) {
// silent
}
}
})
}
const searchStr = encode(search as Record<string, string>).toString()
return searchStr ? `?${searchStr}` : ''
}
}