12
12
*/
13
13
14
14
15
- var sys = require ( "sys" ) ;
16
- var util = require ( "./util" ) ;
15
+ var sys = require ( "sys" ) ,
16
+ util = require ( "./util" ) ;
17
+
18
+ exports . parse = querystring_parse ;
17
19
18
20
/**
19
21
* <p>The querystring module adds support for serializing JavaScript objects into
20
22
* query strings and parsing JavaScript objects from query strings format.</p>
21
23
*
22
- * <p>The querystring namespace is added to your YUI instance including static methods
23
- * querystring.parse(..) and querystring.stringify(..).</p>
24
- *
25
24
* <p>The <code>querystring</code> module is a rollup of <code>querystring-parse</code> and
26
25
* <code>querystring-stringify</code>.</p>
27
26
*
@@ -43,19 +42,13 @@ var util = require("./util");
43
42
* @for querystring
44
43
* @static
45
44
*/
46
- var parse = function ( qs , sep , eq ) {
47
- // wouldn't Array(qs.split()).map(pieceParser(eq)).reduce(mergeParams) be prettier?
48
- return util . reduce (
49
- util . map (
50
- qs . split ( sep || "&" ) ,
51
- pieceParser ( eq || "=" )
52
- ) ,
53
- { } ,
54
- mergeParams
55
- ) ;
45
+ function querystring_parse ( qs , sep , eq , unesc ) {
46
+ return qs . split ( sep || "&" )
47
+ . map ( pieceParser ( eq || "=" , unesc || unescape ) )
48
+ . reduce ( mergeParams , { } ) ;
56
49
} ;
57
50
58
- var unescape = function ( s ) {
51
+ function unescape ( s ) {
59
52
return decodeURIComponent ( s . replace ( / \+ / g, ' ' ) ) ;
60
53
} ;
61
54
@@ -69,76 +62,72 @@ var unescape = function (s) {
69
62
// return parse(foo[bar], [{bla:"baz"}])
70
63
// return parse(foo, {bar:[{bla:"baz"}]})
71
64
// return {foo:{bar:[{bla:"baz"}] }}
72
- var pieceParser = function ( eq ) {
73
- return function parsePiece ( key , val ) {
74
- if ( arguments . length !== 2 ) {
75
- // key=val, called from the map/reduce
76
- key = key . split ( eq ) ;
77
- return parsePiece (
78
- unescape ( key . shift ( ) ) ,
79
- unescape ( key . join ( eq ) )
80
- ) ;
81
- }
82
- key = key . replace ( / ^ \s + | \s + $ / g, '' ) ;
83
- if ( util . isString ( val ) ) {
84
- val = val . replace ( / ^ \s + | \s + $ / g, '' ) ;
85
- // convert numerals to numbers
86
- if ( ! isNaN ( val ) ) {
87
- var numVal = + val ;
88
- if ( val === numVal . toString ( 10 ) ) val = numVal ;
89
- }
90
- }
91
- var sliced = / ( .* ) \[ ( [ ^ \] ] * ) \] $ / . exec ( key ) ;
92
- if ( ! sliced ) {
93
- var ret = { } ;
94
- if ( key ) ret [ key ] = val ;
95
- return ret ;
96
- }
97
- // ["foo[][bar][][baz]", "foo[][bar][]", "baz"]
98
- var tail = sliced [ 2 ] , head = sliced [ 1 ] ;
99
-
100
- // array: key[]=val
101
- if ( ! tail ) return parsePiece ( head , [ val ] ) ;
102
-
103
- // obj: key[subkey]=val
104
- var ret = { } ;
105
- ret [ tail ] = val ;
106
- return parsePiece ( head , ret ) ;
107
- } ;
65
+ function pieceParser ( eq , unesc ) {
66
+ return function parsePiece ( key , val ) {
67
+
68
+ if ( arguments . length !== 2 ) {
69
+ // key=val, called from the map/reduce
70
+ key = key . split ( eq ) ;
71
+ return parsePiece (
72
+ unesc ( key . shift ( ) ) ,
73
+ unesc ( key . join ( eq ) )
74
+ ) ;
75
+ }
76
+ key = key . replace ( / ^ \s + | \s + $ / g, '' ) ;
77
+ if ( util . isString ( val ) ) {
78
+ val = val . replace ( / ^ \s + | \s + $ / g, '' ) ;
79
+ // convert numerals to numbers
80
+ if ( ! isNaN ( val ) ) {
81
+ var numVal = + val ;
82
+ if ( val === numVal . toString ( 10 ) ) val = numVal ;
83
+ }
84
+ }
85
+ var sliced = / ( .* ) \[ ( [ ^ \] ] * ) \] $ / . exec ( key ) ;
86
+ if ( ! sliced ) {
87
+ var ret = { } ;
88
+ if ( key ) ret [ key ] = val ;
89
+ return ret ;
90
+ }
91
+ // ["foo[][bar][][baz]", "foo[][bar][]", "baz"]
92
+ var tail = sliced [ 2 ] ,
93
+ head = sliced [ 1 ] ;
94
+
95
+ // array: key[]=val
96
+ if ( ! tail ) return parsePiece ( head , [ val ] ) ;
97
+
98
+ // obj: key[subkey]=val
99
+ var ret = { } ;
100
+ ret [ tail ] = val ;
101
+ return parsePiece ( head , ret ) ;
102
+ } ;
108
103
} ;
109
104
110
105
// the reducer function that merges each query piece together into one set of params
111
106
function mergeParams ( params , addition ) {
112
- var ret ;
113
-
114
- if ( ! params ) {
115
- // if it's uncontested, then just return the addition.
116
- ret = addition ;
117
- } else if ( util . isArray ( params ) ) {
118
- // if the existing value is an array, then concat it.
119
- ret = params . concat ( addition ) ;
120
- } else if ( ! util . isObject ( params ) || ! util . isObject ( addition ) ) {
121
- // if the existing value is not an array, and either are not objects, arrayify it.
122
- ret = [ params ] . concat ( addition ) ;
123
- } else {
124
- // else merge them as objects, which is a little more complex
125
- ret = mergeObjects ( params , addition ) ;
126
- }
127
- return ret ;
107
+ var ret ;
108
+
109
+ if ( ! params ) {
110
+ // if it's uncontested, then just return the addition.
111
+ ret = addition ;
112
+ } else if ( util . isArray ( params ) ) {
113
+ // if the existing value is an array, then concat it.
114
+ ret = params . concat ( addition ) ;
115
+ } else if ( ! util . isObject ( params ) || ! util . isObject ( addition ) ) {
116
+ // if the existing value is not an array, and either are not objects, arrayify it.
117
+ ret = [ params ] . concat ( addition ) ;
118
+ } else {
119
+ // else merge them as objects, which is a little more complex
120
+ ret = mergeObjects ( params , addition ) ;
121
+ }
122
+ return ret ;
128
123
} ;
129
124
130
125
131
126
// Merge two *objects* together. If this is called, we've already ruled
132
127
// out the simple cases, and need to do the for-in business.
133
128
function mergeObjects ( params , addition ) {
134
- for ( var i in addition ) if ( i && addition . hasOwnProperty ( i ) ) {
135
- params [ i ] = mergeParams ( params [ i ] , addition [ i ] ) ;
136
- }
137
- return params ;
129
+ for ( var i in addition ) if ( i && addition . hasOwnProperty ( i ) ) {
130
+ params [ i ] = mergeParams ( params [ i ] , addition [ i ] ) ;
131
+ }
132
+ return params ;
138
133
} ;
139
-
140
-
141
-
142
-
143
- /* exports */
144
- exports . parse = parse ;
0 commit comments