forked from iambumblehead/form-urlencoded
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathform-urlencoded.js
45 lines (36 loc) · 1.65 KB
/
form-urlencoded.js
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
// Filename: formurlencoded.js
// Timestamp: 2017.07.04-19:19:11 (last modified)
// Author(s): Bumblehead (www.bumblehead.com), JBlashill ([email protected]), Jumper423 ([email protected])
export default (data, opts = {}) => {
const sorted = Boolean(opts.sorted),
skipIndex = Boolean(opts.skipIndex),
ignorenull = Boolean(opts.ignorenull),
encode = value => String(value)
.replace(/[^ !'()~\*]/gu, encodeURIComponent)
.replace(/ /g, '+')
.replace(/[!'()~\*]/g, ch =>
'%' + ch.charCodeAt().toString(16).slice(-2).toUpperCase()),
keys = (obj, keyarr = Object.keys(obj)) =>
sorted ? keyarr.sort() : keyarr,
filterjoin = arr => arr.filter(e => e).join('&'),
objnest = (name, obj) =>
filterjoin(keys(obj).map(key =>
nest(name + '[' + key + ']', obj[key]))),
arrnest = (name, arr) => arr.length
? filterjoin(arr.map((elem, index) => skipIndex
? nest(name + '[]', elem)
: nest(name + '[' + index + ']', elem)))
: encode(name + '[]'),
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 (type === 'object')
f = objnest(name, value);
return f;
};
return data && filterjoin(keys(data).map(key => nest(key, data[key])));
};