-
Notifications
You must be signed in to change notification settings - Fork 23
/
form-urlencoded.mjs
47 lines (38 loc) · 1.53 KB
/
form-urlencoded.mjs
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
export default (data, opts = {}) => {
const {
sorted, ignorenull, ignoreEmptyArray, useDot,
skipIndex, skipBracket, whitespace = '+'
} = opts
const encode = value => String(value)
.replace(/[^ !'()~*]/gu, encodeURIComponent)
.replace(/ /g, whitespace)
.replace(/[!'()~*]/g, ch =>
`%${ch.charCodeAt().toString(16).slice(-2).toUpperCase()}`)
const keys = (obj, keyarr = Object.keys(obj)) =>
sorted ? keyarr.sort() : keyarr
const filterjoin = arr => arr.filter(e => e).join('&')
const objnest = (name, obj) => filterjoin(keys(obj).map(key => useDot
? nest(`${name}.${key}`, obj[key])
: nest(`${name}[${key}]`, obj[key])))
const arrnest = (name, arr, brackets = skipBracket ? '' : '[]') => arr.length
? filterjoin(arr.map((elem, index) => skipIndex
? nest(name + brackets, elem)
: nest(name + '[' + index + ']', elem)))
: ignoreEmptyArray ? null : encode(name + brackets)
const setnest = (name, set) => filterjoin(
Array.from(set).map(elem => nest(name, elem)))
const nest = (name, value, type = typeof value, f = null) => {
if (value === f)
f = ignorenull ? f : encode(name) + '=' + f
else if (/string|number|boolean/.test(type))
f = encode(name) + '=' + encode(value)
else if (Array.isArray(value))
f = arrnest(name, value)
else if (value instanceof Set)
f = setnest(name, value)
else if (type === 'object')
f = objnest(name, value)
return f
}
return data && filterjoin(keys(data).map(key => nest(key, data[key])))
}