FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node-querystring/lib/querystring.js at master · netconstructor/node-querystring · GitHub
netconstructor
/
node-querystring
Public
forked from
simplegeo/node-querystring
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Pull requests
0
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
node-querystring
/
lib
/
querystring.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
123 lines (107 loc) · 2.67 KB
Breadcrumbs
node-querystring
/
lib
/
querystring.js
Copy path
File metadata and controls
123 lines (107 loc) · 2.67 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*!
* querystring
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Library version.
*/
exports
.
version
=
'0.1.0'
;
/**
* Parse the given query `str`, returning an object.
*
*
@param
{
String
} str
*
@return
{
Object
}
*
@api
public
*/
exports
.
parse
=
function
(
str
)
{
if
(
str
==
undefined
||
str
==
''
)
return
{
}
;
return
String
(
str
)
.
split
(
'&'
)
.
reduce
(
function
(
ret
,
pair
)
{
var
pair
=
decodeURIComponent
(
pair
.
replace
(
/
\+
/
g
,
' '
)
)
,
eql
=
pair
.
indexOf
(
'='
)
,
brace
=
lastBraceInKey
(
pair
)
,
key
=
pair
.
substr
(
0
,
brace
||
eql
)
,
val
=
pair
.
substr
(
brace
||
eql
,
pair
.
length
)
,
val
=
val
.
substr
(
val
.
indexOf
(
'='
)
+
1
,
val
.
length
)
,
obj
=
ret
;
// ?foo
if
(
''
==
key
)
key
=
pair
,
val
=
''
;
// nested
if
(
~
key
.
indexOf
(
']'
)
)
{
var
parts
=
key
.
split
(
'['
)
,
len
=
parts
.
length
,
last
=
len
-
1
;
function
parse
(
obj
,
parts
,
parent
,
key
)
{
var
part
=
parts
.
shift
(
)
;
// end
if
(
!
part
)
{
if
(
Array
.
isArray
(
parent
[
key
]
)
)
{
parent
[
key
]
.
push
(
val
)
;
}
else
if
(
'object'
==
typeof
parent
[
key
]
)
{
parent
[
key
]
=
val
;
}
else
{
parent
[
key
]
=
[
parent
[
key
]
,
val
]
;
}
// array
}
else
if
(
']'
==
part
)
{
obj
=
parent
[
key
]
=
Array
.
isArray
(
parent
[
key
]
)
?
parent
[
key
]
:
[
]
;
if
(
''
!=
val
)
obj
.
push
(
val
)
;
// prop
}
else
if
(
~
part
.
indexOf
(
']'
)
)
{
part
=
part
.
substr
(
0
,
part
.
length
-
1
)
;
parse
(
obj
[
part
]
=
obj
[
part
]
||
{
}
,
parts
,
obj
,
part
)
;
// key
}
else
{
parse
(
obj
[
part
]
=
obj
[
part
]
||
{
}
,
parts
,
obj
,
part
)
;
}
}
parse
(
obj
,
parts
)
;
// optimize
}
else
{
set
(
obj
,
key
,
val
)
;
}
return
ret
;
}
,
{
}
)
;
}
;
/**
* Set `obj`'s `key` to `val` respecting
* the weird and wonderful syntax of a qs,
* where "foo=bar&foo=baz" becomes an array.
*
*
@param
{
Object
} obj
*
@param
{
String
} key
*
@param
{
String
} val
*
@api
private
*/
function
set
(
obj
,
key
,
val
)
{
var
v
=
obj
[
key
]
;
if
(
undefined
===
v
)
{
obj
[
key
]
=
val
;
}
else
if
(
Array
.
isArray
(
v
)
)
{
v
.
push
(
val
)
;
}
else
{
obj
[
key
]
=
[
v
,
val
]
;
}
}
/**
* Locate last brace in `str` within the key.
*
*
@param
{
String
} str
*
@return
{
Number
}
*
@api
private
*/
function
lastBraceInKey
(
str
)
{
var
len
=
str
.
length
,
brace
,
c
;
for
(
var
i
=
0
;
i
<
len
;
++
i
)
{
c
=
str
[
i
]
;
if
(
']'
==
c
)
brace
=
false
;
if
(
'['
==
c
)
brace
=
true
;
if
(
'='
==
c
&&
!
brace
)
return
i
;
}
}
Back
|
FazBrowse Home
|
New Git URL