FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
node-querystring/lib/querystring.js at 0.3.2 · explodingbarrel/node-querystring · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
explodingbarrel
/
node-querystring
Public
forked from
tj/node-querystring
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
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
236 lines (207 loc) · 5.24 KB
Breadcrumbs
node-querystring
/
lib
/
querystring.js
Copy path
File metadata and controls
236 lines (207 loc) · 5.24 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*!
* querystring
* Copyright(c) 2010 TJ Holowaychuk <tj@vision-media.ca>
* MIT Licensed
*/
/**
* Library version.
*/
exports
.
version
=
'0.3.2'
;
/**
* Object#toString() ref for stringify().
*/
var
toString
=
Object
.
prototype
.
toString
;
/**
* Cache non-integer test regexp.
*/
var
notint
=
/
[
^
0
-
9
]
/
;
/**
* Parse the given query `str`, returning an object.
*
*
@param
{
String
} str
*
@return
{
Object
}
*
@api
public
*/
exports
.
parse
=
function
(
str
)
{
if
(
null
==
str
||
''
==
str
)
return
{
}
;
function
promote
(
parent
,
key
)
{
if
(
parent
[
key
]
.
length
==
0
)
return
parent
[
key
]
=
{
}
;
var
t
=
{
}
;
for
(
var
i
in
parent
[
key
]
)
t
[
i
]
=
parent
[
key
]
[
i
]
;
parent
[
key
]
=
t
;
return
t
;
}
return
String
(
str
)
.
split
(
'&'
)
.
reduce
(
function
(
ret
,
pair
)
{
try
{
pair
=
decodeURIComponent
(
pair
.
replace
(
/
\+
/
g
,
' '
)
)
;
}
catch
(
e
)
{
// ignore
}
var
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
)
,
parent
=
ret
;
// ?foo
if
(
''
==
key
)
key
=
pair
,
val
=
''
;
// nested
if
(
~
key
.
indexOf
(
']'
)
)
{
var
parts
=
key
.
split
(
'['
)
,
len
=
parts
.
length
,
last
=
len
-
1
;
function
parse
(
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
if
(
'undefined'
==
typeof
parent
[
key
]
)
{
parent
[
key
]
=
val
;
}
else
{
parent
[
key
]
=
[
parent
[
key
]
,
val
]
;
}
// array
}
else
{
var
obj
=
parent
[
key
]
=
parent
[
key
]
||
[
]
;
if
(
']'
==
part
)
{
if
(
Array
.
isArray
(
obj
)
)
{
if
(
''
!=
val
)
obj
.
push
(
val
)
;
}
else
if
(
'object'
==
typeof
obj
)
{
obj
[
Object
.
keys
(
obj
)
.
length
]
=
val
;
}
else
{
obj
=
parent
[
key
]
=
[
parent
[
key
]
,
val
]
;
}
// prop
}
else
if
(
~
part
.
indexOf
(
']'
)
)
{
part
=
part
.
substr
(
0
,
part
.
length
-
1
)
;
if
(
notint
.
test
(
part
)
&&
Array
.
isArray
(
obj
)
)
obj
=
promote
(
parent
,
key
)
;
parse
(
parts
,
obj
,
part
)
;
// key
}
else
{
if
(
notint
.
test
(
part
)
&&
Array
.
isArray
(
obj
)
)
obj
=
promote
(
parent
,
key
)
;
parse
(
parts
,
obj
,
part
)
;
}
}
}
parse
(
parts
,
parent
,
'base'
)
;
// optimize
}
else
{
if
(
notint
.
test
(
key
)
&&
Array
.
isArray
(
parent
.
base
)
)
{
var
t
=
{
}
;
for
(
var
k
in
parent
.
base
)
t
[
k
]
=
parent
.
base
[
k
]
;
parent
.
base
=
t
;
}
set
(
parent
.
base
,
key
,
val
)
;
}
return
ret
;
}
,
{
base
:
{
}
}
)
.
base
;
}
;
/**
* Turn the given `obj` into a query string
*
*
@param
{
Object
} obj
*
@return
{
String
}
*
@api
public
*/
var
stringify
=
exports
.
stringify
=
function
(
obj
,
prefix
)
{
if
(
Array
.
isArray
(
obj
)
)
{
return
stringifyArray
(
obj
,
prefix
)
;
}
else
if
(
'[object Object]'
==
toString
.
call
(
obj
)
)
{
return
stringifyObject
(
obj
,
prefix
)
;
}
else
if
(
'string'
==
typeof
obj
)
{
return
stringifyString
(
obj
,
prefix
)
;
}
else
{
return
prefix
;
}
}
;
/**
* Stringify the given `str`.
*
*
@param
{
String
} str
*
@param
{
String
} prefix
*
@return
{
String
}
*
@api
private
*/
function
stringifyString
(
str
,
prefix
)
{
if
(
!
prefix
)
throw
new
TypeError
(
'stringify expects an object'
)
;
return
prefix
+
'='
+
encodeURIComponent
(
str
)
;
}
/**
* Stringify the given `arr`.
*
*
@param
{
Array
} arr
*
@param
{
String
} prefix
*
@return
{
String
}
*
@api
private
*/
function
stringifyArray
(
arr
,
prefix
)
{
var
ret
=
[
]
;
if
(
!
prefix
)
throw
new
TypeError
(
'stringify expects an object'
)
;
for
(
var
i
=
0
;
i
<
arr
.
length
;
i
++
)
{
ret
.
push
(
stringify
(
arr
[
i
]
,
prefix
+
'[]'
)
)
;
}
return
ret
.
join
(
'&'
)
;
}
/**
* Stringify the given `obj`.
*
*
@param
{
Object
} obj
*
@param
{
String
} prefix
*
@return
{
String
}
*
@api
private
*/
function
stringifyObject
(
obj
,
prefix
)
{
var
ret
=
[
]
,
keys
=
Object
.
keys
(
obj
)
,
key
;
for
(
var
i
=
0
,
len
=
keys
.
length
;
i
<
len
;
++
i
)
{
key
=
keys
[
i
]
;
ret
.
push
(
stringify
(
obj
[
key
]
,
prefix
?
prefix
+
'['
+
encodeURIComponent
(
key
)
+
']'
:
encodeURIComponent
(
key
)
)
)
;
}
return
ret
.
join
(
'&'
)
;
}
/**
* 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