2626const {
2727 Array,
2828 ArrayIsArray,
29+ MathAbs,
2930 ObjectCreate,
3031 ObjectKeys,
3132} = primordials ;
@@ -162,6 +163,25 @@ function stringifyPrimitive(v) {
162163}
163164
164165
166+ function encodeStringified ( v , encode ) {
167+ if ( typeof v === 'string' )
168+ return ( v . length ? encode ( v ) : '' ) ;
169+ if ( typeof v === 'number' && isFinite ( v ) ) {
170+ // Values >= 1e21 automatically switch to scientific notation which requires
171+ // escaping due to the inclusion of a '+' in the output
172+ return ( MathAbs ( v ) < 1e21 ? '' + v : encode ( '' + v ) ) ;
173+ }
174+ if ( typeof v === 'boolean' )
175+ return v ? 'true' : 'false' ;
176+ return '' ;
177+ }
178+
179+
180+ function encodeStringifiedCustom ( v , encode ) {
181+ return encode ( stringifyPrimitive ( v ) ) ;
182+ }
183+
184+
165185function stringify ( obj , sep , eq , options ) {
166186 sep = sep || '&' ;
167187 eq = eq || '=' ;
@@ -170,6 +190,8 @@ function stringify(obj, sep, eq, options) {
170190 if ( options && typeof options . encodeURIComponent === 'function' ) {
171191 encode = options . encodeURIComponent ;
172192 }
193+ const convert =
194+ ( encode === qsEscape ? encodeStringified : encodeStringifiedCustom ) ;
173195
174196 if ( obj !== null && typeof obj === 'object' ) {
175197 const keys = ObjectKeys ( obj ) ;
@@ -179,7 +201,7 @@ function stringify(obj, sep, eq, options) {
179201 for ( let i = 0 ; i < len ; ++ i ) {
180202 const k = keys [ i ] ;
181203 const v = obj [ k ] ;
182- let ks = encode ( stringifyPrimitive ( k ) ) ;
204+ let ks = convert ( k , encode ) ;
183205 ks += eq ;
184206
185207 if ( ArrayIsArray ( v ) ) {
@@ -188,13 +210,13 @@ function stringify(obj, sep, eq, options) {
188210 const vlast = vlen - 1 ;
189211 for ( let j = 0 ; j < vlen ; ++ j ) {
190212 fields += ks ;
191- fields += encode ( stringifyPrimitive ( v [ j ] ) ) ;
213+ fields += convert ( v [ j ] , encode ) ;
192214 if ( j < vlast )
193215 fields += sep ;
194216 }
195217 } else {
196218 fields += ks ;
197- fields += encode ( stringifyPrimitive ( v ) ) ;
219+ fields += convert ( v , encode ) ;
198220 }
199221
200222 if ( i < flast )
0 commit comments