request/lib/helper.js

33 lines
647 B
JavaScript
Raw Permalink Normal View History

2024-07-16 17:45:52 +08:00
/**
* {}
* @author yutent<yutent.io@gmail.com>
* @date 2024/07/16 17:01:43
*/
import { parse } from 'node:querystring'
export function querystring(str) {
let query = parse(str)
for (let k of Object.keys(query)) {
let val = query[k]
if (k.endsWith('[]')) {
let _k = k.slice(0, -2)
query[_k] = val
delete query[k]
} else if (k.endsWith(']')) {
let idx = k.lastIndexOf('[')
let _pk = k.slice(0, idx)
let _k = k.slice(idx + 1, -1)
if (query[_pk]) {
query[_pk][_k] = val
} else {
query[_pk] = { [_k]: val }
}
delete query[k]
}
}
return query
}