Skip to content
Toggle navigation
Toggle navigation
This project
Loading...
Sign in
John McEleney
/
casperjs
Go to a project
Toggle navigation
Toggle navigation pinning
Projects
Groups
Snippets
Help
Project
Activity
Repository
Pipelines
Graphs
Issues
0
Merge Requests
0
Wiki
Network
Create a new issue
Builds
Commits
Issue Boards
Files
Commits
Network
Compare
Branches
Tags
Commit
0bacd9b2
...
0bacd9b29234f8391c95e844a586fd2c0ae06b43
authored
2013-01-09 20:36:28 +0100
by
Nicolas Perriault
Browse Files
Options
Browse Files
Tag
Download
Email Patches
Plain Diff
updated querystring module
1 parent
ce066ab8
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
58 additions
and
31 deletions
modules/querystring.js
modules/querystring.js
View file @
0bacd9b
...
...
@@ -22,7 +22,6 @@
// Query String Utilities
var
QueryString
=
exports
;
//var urlDecode = process.binding('http_parser').urlDecode; // phantomjs incompatible
// If obj.hasOwnProperty has been overridden, then calling
...
...
@@ -134,33 +133,31 @@ var stringifyPrimitive = function(v) {
QueryString
.
stringify
=
QueryString
.
encode
=
function
(
obj
,
sep
,
eq
,
name
)
{
sep
=
sep
||
'&'
;
eq
=
eq
||
'='
;
obj
=
(
obj
===
null
)
?
undefined
:
obj
;
switch
(
typeof
obj
)
{
case
'object'
:
return
Object
.
keys
(
obj
).
map
(
function
(
k
)
{
if
(
Array
.
isArray
(
obj
[
k
]))
{
return
obj
[
k
].
map
(
function
(
v
)
{
return
QueryString
.
escape
(
stringifyPrimitive
(
k
))
+
eq
+
QueryString
.
escape
(
stringifyPrimitive
(
v
));
}).
join
(
sep
);
}
else
{
return
QueryString
.
escape
(
stringifyPrimitive
(
k
))
+
eq
+
QueryString
.
escape
(
stringifyPrimitive
(
obj
[
k
]));
}
}).
join
(
sep
);
if
(
obj
===
null
)
{
obj
=
undefined
;
}
if
(
typeof
obj
===
'object'
)
{
return
Object
.
keys
(
obj
).
map
(
function
(
k
)
{
var
ks
=
QueryString
.
escape
(
stringifyPrimitive
(
k
))
+
eq
;
if
(
Array
.
isArray
(
obj
[
k
]))
{
return
obj
[
k
].
map
(
function
(
v
)
{
return
ks
+
QueryString
.
escape
(
stringifyPrimitive
(
v
));
}).
join
(
sep
);
}
else
{
return
ks
+
QueryString
.
escape
(
stringifyPrimitive
(
obj
[
k
]));
}
}).
join
(
sep
);
default
:
if
(
!
name
)
return
''
;
return
QueryString
.
escape
(
stringifyPrimitive
(
name
))
+
eq
+
QueryString
.
escape
(
stringifyPrimitive
(
obj
));
}
if
(
!
name
)
return
''
;
return
QueryString
.
escape
(
stringifyPrimitive
(
name
))
+
eq
+
QueryString
.
escape
(
stringifyPrimitive
(
obj
));
};
// Parse a key=val string.
QueryString
.
parse
=
QueryString
.
decode
=
function
(
qs
,
sep
,
eq
)
{
QueryString
.
parse
=
QueryString
.
decode
=
function
(
qs
,
sep
,
eq
,
options
)
{
sep
=
sep
||
'&'
;
eq
=
eq
||
'='
;
var
obj
=
{};
...
...
@@ -169,19 +166,49 @@ QueryString.parse = QueryString.decode = function(qs, sep, eq) {
return
obj
;
}
qs
.
split
(
sep
).
forEach
(
function
(
kvp
)
{
var
x
=
kvp
.
split
(
eq
);
var
k
=
QueryString
.
unescape
(
x
[
0
],
true
);
var
v
=
QueryString
.
unescape
(
x
.
slice
(
1
).
join
(
eq
),
true
);
var
regexp
=
/
\+
/g
;
qs
=
qs
.
split
(
sep
);
var
maxKeys
=
1000
;
if
(
options
&&
typeof
options
.
maxKeys
===
'number'
)
{
maxKeys
=
options
.
maxKeys
;
}
var
len
=
qs
.
length
;
// maxKeys <= 0 means that we should not limit keys count
if
(
maxKeys
>
0
&&
len
>
maxKeys
)
{
len
=
maxKeys
;
}
for
(
var
i
=
0
;
i
<
len
;
++
i
)
{
var
x
=
qs
[
i
].
replace
(
regexp
,
'%20'
),
idx
=
x
.
indexOf
(
eq
),
kstr
,
vstr
,
k
,
v
;
if
(
idx
>=
0
)
{
kstr
=
x
.
substr
(
0
,
idx
);
vstr
=
x
.
substr
(
idx
+
1
);
}
else
{
kstr
=
x
;
vstr
=
''
;
}
try
{
k
=
decodeURIComponent
(
kstr
);
v
=
decodeURIComponent
(
vstr
);
}
catch
(
e
)
{
k
=
QueryString
.
unescape
(
kstr
,
true
);
v
=
QueryString
.
unescape
(
vstr
,
true
);
}
if
(
!
hasOwnProperty
(
obj
,
k
))
{
obj
[
k
]
=
v
;
}
else
if
(
!
Array
.
isArray
(
obj
[
k
]))
{
obj
[
k
]
=
[
obj
[
k
],
v
];
}
else
{
}
else
if
(
Array
.
isArray
(
obj
[
k
]))
{
obj
[
k
].
push
(
v
);
}
else
{
obj
[
k
]
=
[
obj
[
k
],
v
];
}
}
);
}
return
obj
;
};
...
...
Please
register
or
sign in
to post a comment