-
Notifications
You must be signed in to change notification settings - Fork 6
/
index.js
89 lines (83 loc) · 2.39 KB
/
index.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
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
if (typeof window !=="undefined") {
var native_qs = require("querystring-browser");
} else {
var native_qs = require("querystring");
}
var _ = require('lodash');
module.exports = function(query) {
if( ! query ) return {};
if( typeof query == 'string' ) {
if( ! query.match(/\[\w*\]/) ) return native_qs.parse(query);
query = native_qs.parse(query);
}
return parse( query )
}
function parse( query ){
var dirty = false;
for( var key in query ) {
var value = query[key];
if( typeof value == 'string' && !isNaN(1*value) ) value = 1*value;
if( matches = key.match(/^(.+)(\[[^\]]*\])$/) ){
var parent = matches[1];
var child = matches[2];
if( child.match(/^\[\s*\]$/) ) {
if( value instanceof Array ) query[parent] = value
else query[parent] = [value]
} else {
if( !(query[parent] instanceof Object) ) query[parent] = {};
child = child.replace(/[\[\] ]/g, "");
query[parent][child] = value
}
delete query[key]
dirty = true
}
}
return dirty ? parse(query) : query
}
module.exports.parse = module.exports;
module.exports.stringify = function(obj) {
if (_.isObject(obj) && !_.isArray(obj)) {
var result = [];
_.each(obj, function(val, key){
if (_.isArray(val)) {
result.push(stringifyArray(key, val));
} else if (_.isObject(val)) {
result.push(stringifyObject(key, val));
} else if (val) {
result.push(key+'='+native_qs.escape(val));
}
});
result = result.filter(function(e){return e});
return result.join('&');
} else {
throw new Error('Invalid Object Format');
return ;
}
}
function stringifyObject (key, obj) {
var result = [];
_.each(obj, function(val, key2){
key2 = key+'['+key2+']';
if (_.isArray(val)) {
result.push(stringifyArray(key2, val));
} else if (_.isObject(val)) {
result.push(stringifyObject(key2, val));
} else if (val) {
result.push(key2+'='+val);
}
});
result = result.filter(function(e){return e});
return result.join('&');
}
function stringifyArray (key, obj) {
var result = [];
_.each(obj, function(val){
if (_.isObject(val)) {
// not allow any object in an array
throw new Error('Not Allow Any Object In An Array:'+key+'='+JSON.stringify(obj));
} else if (val) {
result.push(key+'[]=' + val);
}
});
return result.join('&');
}