Comparing version 5.1.0 to 5.2.0
@@ -231,7 +231,9 @@ (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.Qs = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
}, | ||
strictNullHandling: false | ||
strictNullHandling: false, | ||
skipNulls: false, | ||
encode: true | ||
}; | ||
internals.stringify = function (obj, prefix, generateArrayPrefix, strictNullHandling, filter) { | ||
internals.stringify = function (obj, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter) { | ||
@@ -249,3 +251,3 @@ if (typeof filter === 'function') { | ||
if (strictNullHandling) { | ||
return Utils.encode(prefix); | ||
return encode ? Utils.encode(prefix) : prefix; | ||
} | ||
@@ -260,3 +262,6 @@ | ||
return [Utils.encode(prefix) + '=' + Utils.encode(obj)]; | ||
if (encode) { | ||
return [Utils.encode(prefix) + '=' + Utils.encode(obj)]; | ||
} | ||
return [prefix + '=' + obj]; | ||
} | ||
@@ -274,7 +279,13 @@ | ||
if (skipNulls && | ||
obj[key] === null) { | ||
continue; | ||
} | ||
if (Array.isArray(obj)) { | ||
values = values.concat(internals.stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, filter)); | ||
values = values.concat(internals.stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, skipNulls, encode, filter)); | ||
} | ||
else { | ||
values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']', generateArrayPrefix, strictNullHandling, filter)); | ||
values = values.concat(internals.stringify(obj[key], prefix + '[' + key + ']', generateArrayPrefix, strictNullHandling, skipNulls, encode, filter)); | ||
} | ||
@@ -292,2 +303,4 @@ } | ||
var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling; | ||
var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : internals.skipNulls; | ||
var encode = typeof options.encode === 'boolean' ? options.encode : internals.encode; | ||
var objKeys; | ||
@@ -327,5 +340,13 @@ var filter; | ||
} | ||
for (var i = 0, il = objKeys.length; i < il; ++i) { | ||
var key = objKeys[i]; | ||
keys = keys.concat(internals.stringify(obj[key], key, generateArrayPrefix, strictNullHandling, filter)); | ||
if (skipNulls && | ||
obj[key] === null) { | ||
continue; | ||
} | ||
keys = keys.concat(internals.stringify(obj[key], key, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter)); | ||
} | ||
@@ -332,0 +353,0 @@ |
@@ -30,3 +30,3 @@ // Load modules | ||
internals.stringify = function (obj, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter) { | ||
internals.stringify = function (obj, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort) { | ||
@@ -66,3 +66,10 @@ if (typeof filter === 'function') { | ||
var objKeys = Array.isArray(filter) ? filter : Object.keys(obj); | ||
var objKeys; | ||
if (Array.isArray(filter)) { | ||
objKeys = filter; | ||
} else { | ||
var keys = Object.keys(obj); | ||
objKeys = sort ? keys.sort(sort) : keys; | ||
} | ||
for (var i = 0, il = objKeys.length; i < il; ++i) { | ||
@@ -96,2 +103,3 @@ var key = objKeys[i]; | ||
var encode = typeof options.encode === 'boolean' ? options.encode : internals.encode; | ||
var sort = typeof options.sort === 'function' ? options.sort : null; | ||
var objKeys; | ||
@@ -132,2 +140,6 @@ var filter; | ||
if (sort) { | ||
objKeys.sort(sort); | ||
} | ||
for (var i = 0, il = objKeys.length; i < il; ++i) { | ||
@@ -142,3 +154,3 @@ var key = objKeys[i]; | ||
keys = keys.concat(internals.stringify(obj[key], key, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter)); | ||
keys = keys.concat(internals.stringify(obj[key], key, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort)); | ||
} | ||
@@ -145,0 +157,0 @@ |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/hapijs/qs", | ||
"version": "5.1.0", | ||
"version": "5.2.0", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -204,3 +204,3 @@ # qs | ||
When stringifying, **qs** always URI encodes output. Objects are stringified as you would expect: | ||
When stringifying, **qs** by default URI encodes output. Objects are stringified as you would expect: | ||
@@ -214,2 +214,9 @@ ```javascript | ||
This encoding can be disabled by setting the `encode` option to `false`: | ||
```javascript | ||
Qs.stringify({ a: { b: 'c' } }, { encode: false }); | ||
// 'a[b]=c' | ||
``` | ||
Examples beyond this point will be shown as though the output is not URI encoded for clarity. Please note that the return values in these cases *will* be URI encoded during real usage. | ||
@@ -216,0 +223,0 @@ |
@@ -281,2 +281,14 @@ /* eslint no-extend-native:0 */ | ||
}); | ||
it('can sort the keys', function (done) { | ||
var sort = function alphabeticalSort (a, b) { | ||
return a.localeCompare(b); | ||
}; | ||
expect(Qs.stringify({ a: 'c', z: 'y', b : 'f' }, { sort : sort })).to.equal('a=c&b=f&z=y'); | ||
expect(Qs.stringify({ a: 'c', z: { j: 'a', i:'b' }, b : 'f' }, { sort : sort })).to.equal('a=c&b=f&z%5Bi%5D=b&z%5Bj%5D=a'); | ||
done(); | ||
}); | ||
}); |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
74359
1424
332