Comparing version 6.6.0 to 6.7.0
@@ -0,1 +1,15 @@ | ||
## **6.7.0** | ||
- [New] `stringify`/`parse`: add `comma` as an `arrayFormat` option (#276, #219) | ||
- [Fix] correctly parse nested arrays (#212) | ||
- [Fix] `utils.merge`: avoid a crash with a null target and a truthy non-array source, also with an array source | ||
- [Robustness] `stringify`: cache `Object.prototype.hasOwnProperty` | ||
- [Refactor] `utils`: `isBuffer`: small tweak; add tests | ||
- [Refactor] use cached `Array.isArray` | ||
- [Refactor] `parse`/`stringify`: make a function to normalize the options | ||
- [Refactor] `utils`: reduce observable [[Get]]s | ||
- [Refactor] `stringify`/`utils`: cache `Array.isArray` | ||
- [Tests] always use `String(x)` over `x.toString()` | ||
- [Tests] fix Buffer tests to work in node < 4.5 and node < 5.10 | ||
- [Tests] temporarily allow coverage to fail | ||
## **6.6.0** | ||
@@ -14,3 +28,3 @@ - [New] Add support for iso-8859-1, utf8 "sentinel" and numeric entities (#268) | ||
- [Refactor] `utils`: `compactQueue`: make it explicitly side-effecting | ||
- [Dev Deps] update `browserify, `eslint`, `@ljharb/eslint-config`, `iconv-lite`, `safe-publish-latest`, `tape` | ||
- [Dev Deps] update `browserify`, `eslint`, `@ljharb/eslint-config`, `iconv-lite`, `safe-publish-latest`, `tape` | ||
- [Tests] up to `node` `v10.10`, `v9.11`, `v8.12`, `v6.14`, `v4.9`; pin included builds to LTS | ||
@@ -17,0 +31,0 @@ |
189
dist/qs.js
@@ -47,2 +47,3 @@ (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(){function r(e,n,t){function o(i,f){if(!n[i]){if(!e[i]){var c="function"==typeof require&&require;if(!f&&c)return c(i,!0);if(u)return u(i,!0);var a=new Error("Cannot find module '"+i+"'");throw a.code="MODULE_NOT_FOUND",a}var p=n[i]={exports:{}};e[i][0].call(p.exports,function(r){var n=e[i][1][r];return o(n||r)},p,p.exports,r,e,n,t)}return n[i].exports}for(var u="function"==typeof require&&require,i=0;i<t.length;i++)o(t[i]);return o}return r})()({1:[function(require,module,exports){ | ||
charsetSentinel: false, | ||
comma: false, | ||
decoder: utils.decode, | ||
@@ -119,2 +120,7 @@ delimiter: '&', | ||
} | ||
if (val && options.comma && val.indexOf(',') > -1) { | ||
val = val.split(','); | ||
} | ||
if (has.call(obj, key)) { | ||
@@ -219,28 +225,38 @@ obj[key] = utils.combine(obj[key], val); | ||
module.exports = function (str, opts) { | ||
var options = opts ? utils.assign({}, opts) : {}; | ||
var normalizeParseOptions = function normalizeParseOptions(opts) { | ||
if (!opts) { | ||
return defaults; | ||
} | ||
if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') { | ||
if (opts.decoder !== null && opts.decoder !== undefined && typeof opts.decoder !== 'function') { | ||
throw new TypeError('Decoder has to be a function.'); | ||
} | ||
options.ignoreQueryPrefix = options.ignoreQueryPrefix === true; | ||
options.delimiter = typeof options.delimiter === 'string' || utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter; | ||
options.depth = typeof options.depth === 'number' ? options.depth : defaults.depth; | ||
options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : defaults.arrayLimit; | ||
options.parseArrays = options.parseArrays !== false; | ||
options.decoder = typeof options.decoder === 'function' ? options.decoder : defaults.decoder; | ||
options.allowDots = typeof options.allowDots === 'undefined' ? defaults.allowDots : !!options.allowDots; | ||
options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : defaults.plainObjects; | ||
options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : defaults.allowPrototypes; | ||
options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : defaults.parameterLimit; | ||
options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling; | ||
if (typeof options.charset !== 'undefined' && options.charset !== 'utf-8' && options.charset !== 'iso-8859-1') { | ||
if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') { | ||
throw new Error('The charset option must be either utf-8, iso-8859-1, or undefined'); | ||
} | ||
if (typeof options.charset === 'undefined') { | ||
options.charset = defaults.charset; | ||
} | ||
var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset; | ||
return { | ||
allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots, | ||
allowPrototypes: typeof opts.allowPrototypes === 'boolean' ? opts.allowPrototypes : defaults.allowPrototypes, | ||
arrayLimit: typeof opts.arrayLimit === 'number' ? opts.arrayLimit : defaults.arrayLimit, | ||
charset: charset, | ||
charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel, | ||
comma: typeof opts.comma === 'boolean' ? opts.comma : defaults.comma, | ||
decoder: typeof opts.decoder === 'function' ? opts.decoder : defaults.decoder, | ||
delimiter: typeof opts.delimiter === 'string' || utils.isRegExp(opts.delimiter) ? opts.delimiter : defaults.delimiter, | ||
depth: typeof opts.depth === 'number' ? opts.depth : defaults.depth, | ||
ignoreQueryPrefix: opts.ignoreQueryPrefix === true, | ||
interpretNumericEntities: typeof opts.interpretNumericEntities === 'boolean' ? opts.interpretNumericEntities : defaults.interpretNumericEntities, | ||
parameterLimit: typeof opts.parameterLimit === 'number' ? opts.parameterLimit : defaults.parameterLimit, | ||
parseArrays: opts.parseArrays !== false, | ||
plainObjects: typeof opts.plainObjects === 'boolean' ? opts.plainObjects : defaults.plainObjects, | ||
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling | ||
}; | ||
}; | ||
module.exports = function (str, opts) { | ||
var options = normalizeParseOptions(opts); | ||
if (str === '' || str === null || typeof str === 'undefined') { | ||
@@ -270,2 +286,3 @@ return options.plainObjects ? Object.create(null) : {}; | ||
var formats = require('./formats'); | ||
var has = Object.prototype.hasOwnProperty; | ||
@@ -276,2 +293,3 @@ var arrayPrefixGenerators = { | ||
}, | ||
comma: 'comma', | ||
indices: function indices(prefix, key) { // eslint-disable-line func-name-matching | ||
@@ -302,2 +320,3 @@ return prefix + '[' + key + ']'; | ||
encodeValuesOnly: false, | ||
formatter: formats.formatters[formats['default']], | ||
// deprecated | ||
@@ -332,2 +351,4 @@ indices: false, | ||
obj = serializeDate(obj); | ||
} else if (generateArrayPrefix === 'comma' && isArray(obj)) { | ||
obj = obj.join(','); | ||
} | ||
@@ -358,3 +379,3 @@ | ||
var objKeys; | ||
if (Array.isArray(filter)) { | ||
if (isArray(filter)) { | ||
objKeys = filter; | ||
@@ -373,6 +394,6 @@ } else { | ||
if (Array.isArray(obj)) { | ||
if (isArray(obj)) { | ||
pushToArray(values, stringify( | ||
obj[key], | ||
generateArrayPrefix(prefix, key), | ||
typeof generateArrayPrefix === 'function' ? generateArrayPrefix(prefix, key) : prefix, | ||
generateArrayPrefix, | ||
@@ -412,30 +433,52 @@ strictNullHandling, | ||
module.exports = function (object, opts) { | ||
var obj = object; | ||
var options = opts ? utils.assign({}, opts) : {}; | ||
var normalizeStringifyOptions = function normalizeStringifyOptions(opts) { | ||
if (!opts) { | ||
return defaults; | ||
} | ||
if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') { | ||
if (opts.encoder !== null && opts.encoder !== undefined && typeof opts.encoder !== 'function') { | ||
throw new TypeError('Encoder has to be a function.'); | ||
} | ||
var delimiter = typeof options.delimiter === 'undefined' ? defaults.delimiter : options.delimiter; | ||
var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling; | ||
var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : defaults.skipNulls; | ||
var encode = typeof options.encode === 'boolean' ? options.encode : defaults.encode; | ||
var encoder = typeof options.encoder === 'function' ? options.encoder : defaults.encoder; | ||
var sort = typeof options.sort === 'function' ? options.sort : null; | ||
var allowDots = typeof options.allowDots === 'undefined' ? defaults.allowDots : !!options.allowDots; | ||
var serializeDate = typeof options.serializeDate === 'function' ? options.serializeDate : defaults.serializeDate; | ||
var encodeValuesOnly = typeof options.encodeValuesOnly === 'boolean' ? options.encodeValuesOnly : defaults.encodeValuesOnly; | ||
var charset = options.charset || defaults.charset; | ||
if (typeof options.charset !== 'undefined' && options.charset !== 'utf-8' && options.charset !== 'iso-8859-1') { | ||
throw new Error('The charset option must be either utf-8, iso-8859-1, or undefined'); | ||
var charset = opts.charset || defaults.charset; | ||
if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') { | ||
throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'); | ||
} | ||
if (typeof options.format === 'undefined') { | ||
options.format = formats['default']; | ||
} else if (!Object.prototype.hasOwnProperty.call(formats.formatters, options.format)) { | ||
throw new TypeError('Unknown format option provided.'); | ||
var format = formats['default']; | ||
if (typeof opts.format !== 'undefined') { | ||
if (!has.call(formats.formatters, opts.format)) { | ||
throw new TypeError('Unknown format option provided.'); | ||
} | ||
format = opts.format; | ||
} | ||
var formatter = formats.formatters[options.format]; | ||
var formatter = formats.formatters[format]; | ||
var filter = defaults.filter; | ||
if (typeof opts.filter === 'function' || isArray(opts.filter)) { | ||
filter = opts.filter; | ||
} | ||
return { | ||
addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix, | ||
allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots, | ||
charset: charset, | ||
charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel, | ||
delimiter: typeof opts.delimiter === 'undefined' ? defaults.delimiter : opts.delimiter, | ||
encode: typeof opts.encode === 'boolean' ? opts.encode : defaults.encode, | ||
encoder: typeof opts.encoder === 'function' ? opts.encoder : defaults.encoder, | ||
encodeValuesOnly: typeof opts.encodeValuesOnly === 'boolean' ? opts.encodeValuesOnly : defaults.encodeValuesOnly, | ||
filter: filter, | ||
formatter: formatter, | ||
serializeDate: typeof opts.serializeDate === 'function' ? opts.serializeDate : defaults.serializeDate, | ||
skipNulls: typeof opts.skipNulls === 'boolean' ? opts.skipNulls : defaults.skipNulls, | ||
sort: typeof opts.sort === 'function' ? opts.sort : null, | ||
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling | ||
}; | ||
}; | ||
module.exports = function (object, opts) { | ||
var obj = object; | ||
var options = normalizeStringifyOptions(opts); | ||
var objKeys; | ||
@@ -447,3 +490,3 @@ var filter; | ||
obj = filter('', obj); | ||
} else if (Array.isArray(options.filter)) { | ||
} else if (isArray(options.filter)) { | ||
filter = options.filter; | ||
@@ -460,6 +503,6 @@ objKeys = filter; | ||
var arrayFormat; | ||
if (options.arrayFormat in arrayPrefixGenerators) { | ||
arrayFormat = options.arrayFormat; | ||
} else if ('indices' in options) { | ||
arrayFormat = options.indices ? 'indices' : 'repeat'; | ||
if (opts && opts.arrayFormat in arrayPrefixGenerators) { | ||
arrayFormat = opts.arrayFormat; | ||
} else if (opts && 'indices' in opts) { | ||
arrayFormat = opts.indices ? 'indices' : 'repeat'; | ||
} else { | ||
@@ -475,4 +518,4 @@ arrayFormat = 'indices'; | ||
if (sort) { | ||
objKeys.sort(sort); | ||
if (options.sort) { | ||
objKeys.sort(options.sort); | ||
} | ||
@@ -483,3 +526,3 @@ | ||
if (skipNulls && obj[key] === null) { | ||
if (options.skipNulls && obj[key] === null) { | ||
continue; | ||
@@ -491,20 +534,20 @@ } | ||
generateArrayPrefix, | ||
strictNullHandling, | ||
skipNulls, | ||
encode ? encoder : null, | ||
filter, | ||
sort, | ||
allowDots, | ||
serializeDate, | ||
formatter, | ||
encodeValuesOnly, | ||
charset | ||
options.strictNullHandling, | ||
options.skipNulls, | ||
options.encode ? options.encoder : null, | ||
options.filter, | ||
options.sort, | ||
options.allowDots, | ||
options.serializeDate, | ||
options.formatter, | ||
options.encodeValuesOnly, | ||
options.charset | ||
)); | ||
} | ||
var joined = keys.join(delimiter); | ||
var joined = keys.join(options.delimiter); | ||
var prefix = options.addQueryPrefix === true ? '?' : ''; | ||
if (options.charsetSentinel) { | ||
if (charset === 'iso-8859-1') { | ||
if (options.charset === 'iso-8859-1') { | ||
// encodeURIComponent('✓'), the "numeric entity" representation of a checkmark | ||
@@ -525,2 +568,3 @@ prefix += 'utf8=%26%2310003%3B&'; | ||
var has = Object.prototype.hasOwnProperty; | ||
var isArray = Array.isArray; | ||
@@ -541,3 +585,3 @@ var hexTable = (function () { | ||
if (Array.isArray(obj)) { | ||
if (isArray(obj)) { | ||
var compacted = []; | ||
@@ -573,5 +617,5 @@ | ||
if (typeof source !== 'object') { | ||
if (Array.isArray(target)) { | ||
if (isArray(target)) { | ||
target.push(source); | ||
} else if (typeof target === 'object') { | ||
} else if (target && typeof target === 'object') { | ||
if ((options && (options.plainObjects || options.allowPrototypes)) || !has.call(Object.prototype, source)) { | ||
@@ -587,3 +631,3 @@ target[source] = true; | ||
if (typeof target !== 'object') { | ||
if (!target || typeof target !== 'object') { | ||
return [target].concat(source); | ||
@@ -593,11 +637,12 @@ } | ||
var mergeTarget = target; | ||
if (Array.isArray(target) && !Array.isArray(source)) { | ||
if (isArray(target) && !isArray(source)) { | ||
mergeTarget = arrayToObject(target, options); | ||
} | ||
if (Array.isArray(target) && Array.isArray(source)) { | ||
if (isArray(target) && isArray(source)) { | ||
source.forEach(function (item, i) { | ||
if (has.call(target, i)) { | ||
if (target[i] && typeof target[i] === 'object') { | ||
target[i] = merge(target[i], item, options); | ||
var targetItem = target[i]; | ||
if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') { | ||
target[i] = merge(targetItem, item, options); | ||
} else { | ||
@@ -733,3 +778,3 @@ target.push(item); | ||
var isBuffer = function isBuffer(obj) { | ||
if (obj === null || typeof obj === 'undefined') { | ||
if (!obj || typeof obj !== 'object') { | ||
return false; | ||
@@ -736,0 +781,0 @@ } |
@@ -13,2 +13,3 @@ 'use strict'; | ||
charsetSentinel: false, | ||
comma: false, | ||
decoder: utils.decode, | ||
@@ -85,2 +86,7 @@ delimiter: '&', | ||
} | ||
if (val && options.comma && val.indexOf(',') > -1) { | ||
val = val.split(','); | ||
} | ||
if (has.call(obj, key)) { | ||
@@ -185,28 +191,38 @@ obj[key] = utils.combine(obj[key], val); | ||
module.exports = function (str, opts) { | ||
var options = opts ? utils.assign({}, opts) : {}; | ||
var normalizeParseOptions = function normalizeParseOptions(opts) { | ||
if (!opts) { | ||
return defaults; | ||
} | ||
if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') { | ||
if (opts.decoder !== null && opts.decoder !== undefined && typeof opts.decoder !== 'function') { | ||
throw new TypeError('Decoder has to be a function.'); | ||
} | ||
options.ignoreQueryPrefix = options.ignoreQueryPrefix === true; | ||
options.delimiter = typeof options.delimiter === 'string' || utils.isRegExp(options.delimiter) ? options.delimiter : defaults.delimiter; | ||
options.depth = typeof options.depth === 'number' ? options.depth : defaults.depth; | ||
options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : defaults.arrayLimit; | ||
options.parseArrays = options.parseArrays !== false; | ||
options.decoder = typeof options.decoder === 'function' ? options.decoder : defaults.decoder; | ||
options.allowDots = typeof options.allowDots === 'undefined' ? defaults.allowDots : !!options.allowDots; | ||
options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : defaults.plainObjects; | ||
options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : defaults.allowPrototypes; | ||
options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : defaults.parameterLimit; | ||
options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling; | ||
if (typeof options.charset !== 'undefined' && options.charset !== 'utf-8' && options.charset !== 'iso-8859-1') { | ||
if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') { | ||
throw new Error('The charset option must be either utf-8, iso-8859-1, or undefined'); | ||
} | ||
if (typeof options.charset === 'undefined') { | ||
options.charset = defaults.charset; | ||
} | ||
var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset; | ||
return { | ||
allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots, | ||
allowPrototypes: typeof opts.allowPrototypes === 'boolean' ? opts.allowPrototypes : defaults.allowPrototypes, | ||
arrayLimit: typeof opts.arrayLimit === 'number' ? opts.arrayLimit : defaults.arrayLimit, | ||
charset: charset, | ||
charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel, | ||
comma: typeof opts.comma === 'boolean' ? opts.comma : defaults.comma, | ||
decoder: typeof opts.decoder === 'function' ? opts.decoder : defaults.decoder, | ||
delimiter: typeof opts.delimiter === 'string' || utils.isRegExp(opts.delimiter) ? opts.delimiter : defaults.delimiter, | ||
depth: typeof opts.depth === 'number' ? opts.depth : defaults.depth, | ||
ignoreQueryPrefix: opts.ignoreQueryPrefix === true, | ||
interpretNumericEntities: typeof opts.interpretNumericEntities === 'boolean' ? opts.interpretNumericEntities : defaults.interpretNumericEntities, | ||
parameterLimit: typeof opts.parameterLimit === 'number' ? opts.parameterLimit : defaults.parameterLimit, | ||
parseArrays: opts.parseArrays !== false, | ||
plainObjects: typeof opts.plainObjects === 'boolean' ? opts.plainObjects : defaults.plainObjects, | ||
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling | ||
}; | ||
}; | ||
module.exports = function (str, opts) { | ||
var options = normalizeParseOptions(opts); | ||
if (str === '' || str === null || typeof str === 'undefined') { | ||
@@ -213,0 +229,0 @@ return options.plainObjects ? Object.create(null) : {}; |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var formats = require('./formats'); | ||
var has = Object.prototype.hasOwnProperty; | ||
@@ -11,2 +12,3 @@ var arrayPrefixGenerators = { | ||
}, | ||
comma: 'comma', | ||
indices: function indices(prefix, key) { // eslint-disable-line func-name-matching | ||
@@ -37,2 +39,3 @@ return prefix + '[' + key + ']'; | ||
encodeValuesOnly: false, | ||
formatter: formats.formatters[formats['default']], | ||
// deprecated | ||
@@ -67,2 +70,4 @@ indices: false, | ||
obj = serializeDate(obj); | ||
} else if (generateArrayPrefix === 'comma' && isArray(obj)) { | ||
obj = obj.join(','); | ||
} | ||
@@ -93,3 +98,3 @@ | ||
var objKeys; | ||
if (Array.isArray(filter)) { | ||
if (isArray(filter)) { | ||
objKeys = filter; | ||
@@ -108,6 +113,6 @@ } else { | ||
if (Array.isArray(obj)) { | ||
if (isArray(obj)) { | ||
pushToArray(values, stringify( | ||
obj[key], | ||
generateArrayPrefix(prefix, key), | ||
typeof generateArrayPrefix === 'function' ? generateArrayPrefix(prefix, key) : prefix, | ||
generateArrayPrefix, | ||
@@ -147,30 +152,52 @@ strictNullHandling, | ||
module.exports = function (object, opts) { | ||
var obj = object; | ||
var options = opts ? utils.assign({}, opts) : {}; | ||
var normalizeStringifyOptions = function normalizeStringifyOptions(opts) { | ||
if (!opts) { | ||
return defaults; | ||
} | ||
if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') { | ||
if (opts.encoder !== null && opts.encoder !== undefined && typeof opts.encoder !== 'function') { | ||
throw new TypeError('Encoder has to be a function.'); | ||
} | ||
var delimiter = typeof options.delimiter === 'undefined' ? defaults.delimiter : options.delimiter; | ||
var strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : defaults.strictNullHandling; | ||
var skipNulls = typeof options.skipNulls === 'boolean' ? options.skipNulls : defaults.skipNulls; | ||
var encode = typeof options.encode === 'boolean' ? options.encode : defaults.encode; | ||
var encoder = typeof options.encoder === 'function' ? options.encoder : defaults.encoder; | ||
var sort = typeof options.sort === 'function' ? options.sort : null; | ||
var allowDots = typeof options.allowDots === 'undefined' ? defaults.allowDots : !!options.allowDots; | ||
var serializeDate = typeof options.serializeDate === 'function' ? options.serializeDate : defaults.serializeDate; | ||
var encodeValuesOnly = typeof options.encodeValuesOnly === 'boolean' ? options.encodeValuesOnly : defaults.encodeValuesOnly; | ||
var charset = options.charset || defaults.charset; | ||
if (typeof options.charset !== 'undefined' && options.charset !== 'utf-8' && options.charset !== 'iso-8859-1') { | ||
throw new Error('The charset option must be either utf-8, iso-8859-1, or undefined'); | ||
var charset = opts.charset || defaults.charset; | ||
if (typeof opts.charset !== 'undefined' && opts.charset !== 'utf-8' && opts.charset !== 'iso-8859-1') { | ||
throw new TypeError('The charset option must be either utf-8, iso-8859-1, or undefined'); | ||
} | ||
if (typeof options.format === 'undefined') { | ||
options.format = formats['default']; | ||
} else if (!Object.prototype.hasOwnProperty.call(formats.formatters, options.format)) { | ||
throw new TypeError('Unknown format option provided.'); | ||
var format = formats['default']; | ||
if (typeof opts.format !== 'undefined') { | ||
if (!has.call(formats.formatters, opts.format)) { | ||
throw new TypeError('Unknown format option provided.'); | ||
} | ||
format = opts.format; | ||
} | ||
var formatter = formats.formatters[options.format]; | ||
var formatter = formats.formatters[format]; | ||
var filter = defaults.filter; | ||
if (typeof opts.filter === 'function' || isArray(opts.filter)) { | ||
filter = opts.filter; | ||
} | ||
return { | ||
addQueryPrefix: typeof opts.addQueryPrefix === 'boolean' ? opts.addQueryPrefix : defaults.addQueryPrefix, | ||
allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots, | ||
charset: charset, | ||
charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel, | ||
delimiter: typeof opts.delimiter === 'undefined' ? defaults.delimiter : opts.delimiter, | ||
encode: typeof opts.encode === 'boolean' ? opts.encode : defaults.encode, | ||
encoder: typeof opts.encoder === 'function' ? opts.encoder : defaults.encoder, | ||
encodeValuesOnly: typeof opts.encodeValuesOnly === 'boolean' ? opts.encodeValuesOnly : defaults.encodeValuesOnly, | ||
filter: filter, | ||
formatter: formatter, | ||
serializeDate: typeof opts.serializeDate === 'function' ? opts.serializeDate : defaults.serializeDate, | ||
skipNulls: typeof opts.skipNulls === 'boolean' ? opts.skipNulls : defaults.skipNulls, | ||
sort: typeof opts.sort === 'function' ? opts.sort : null, | ||
strictNullHandling: typeof opts.strictNullHandling === 'boolean' ? opts.strictNullHandling : defaults.strictNullHandling | ||
}; | ||
}; | ||
module.exports = function (object, opts) { | ||
var obj = object; | ||
var options = normalizeStringifyOptions(opts); | ||
var objKeys; | ||
@@ -182,3 +209,3 @@ var filter; | ||
obj = filter('', obj); | ||
} else if (Array.isArray(options.filter)) { | ||
} else if (isArray(options.filter)) { | ||
filter = options.filter; | ||
@@ -195,6 +222,6 @@ objKeys = filter; | ||
var arrayFormat; | ||
if (options.arrayFormat in arrayPrefixGenerators) { | ||
arrayFormat = options.arrayFormat; | ||
} else if ('indices' in options) { | ||
arrayFormat = options.indices ? 'indices' : 'repeat'; | ||
if (opts && opts.arrayFormat in arrayPrefixGenerators) { | ||
arrayFormat = opts.arrayFormat; | ||
} else if (opts && 'indices' in opts) { | ||
arrayFormat = opts.indices ? 'indices' : 'repeat'; | ||
} else { | ||
@@ -210,4 +237,4 @@ arrayFormat = 'indices'; | ||
if (sort) { | ||
objKeys.sort(sort); | ||
if (options.sort) { | ||
objKeys.sort(options.sort); | ||
} | ||
@@ -218,3 +245,3 @@ | ||
if (skipNulls && obj[key] === null) { | ||
if (options.skipNulls && obj[key] === null) { | ||
continue; | ||
@@ -226,20 +253,20 @@ } | ||
generateArrayPrefix, | ||
strictNullHandling, | ||
skipNulls, | ||
encode ? encoder : null, | ||
filter, | ||
sort, | ||
allowDots, | ||
serializeDate, | ||
formatter, | ||
encodeValuesOnly, | ||
charset | ||
options.strictNullHandling, | ||
options.skipNulls, | ||
options.encode ? options.encoder : null, | ||
options.filter, | ||
options.sort, | ||
options.allowDots, | ||
options.serializeDate, | ||
options.formatter, | ||
options.encodeValuesOnly, | ||
options.charset | ||
)); | ||
} | ||
var joined = keys.join(delimiter); | ||
var joined = keys.join(options.delimiter); | ||
var prefix = options.addQueryPrefix === true ? '?' : ''; | ||
if (options.charsetSentinel) { | ||
if (charset === 'iso-8859-1') { | ||
if (options.charset === 'iso-8859-1') { | ||
// encodeURIComponent('✓'), the "numeric entity" representation of a checkmark | ||
@@ -246,0 +273,0 @@ prefix += 'utf8=%26%2310003%3B&'; |
'use strict'; | ||
var has = Object.prototype.hasOwnProperty; | ||
var isArray = Array.isArray; | ||
@@ -19,3 +20,3 @@ var hexTable = (function () { | ||
if (Array.isArray(obj)) { | ||
if (isArray(obj)) { | ||
var compacted = []; | ||
@@ -51,5 +52,5 @@ | ||
if (typeof source !== 'object') { | ||
if (Array.isArray(target)) { | ||
if (isArray(target)) { | ||
target.push(source); | ||
} else if (typeof target === 'object') { | ||
} else if (target && typeof target === 'object') { | ||
if ((options && (options.plainObjects || options.allowPrototypes)) || !has.call(Object.prototype, source)) { | ||
@@ -65,3 +66,3 @@ target[source] = true; | ||
if (typeof target !== 'object') { | ||
if (!target || typeof target !== 'object') { | ||
return [target].concat(source); | ||
@@ -71,11 +72,12 @@ } | ||
var mergeTarget = target; | ||
if (Array.isArray(target) && !Array.isArray(source)) { | ||
if (isArray(target) && !isArray(source)) { | ||
mergeTarget = arrayToObject(target, options); | ||
} | ||
if (Array.isArray(target) && Array.isArray(source)) { | ||
if (isArray(target) && isArray(source)) { | ||
source.forEach(function (item, i) { | ||
if (has.call(target, i)) { | ||
if (target[i] && typeof target[i] === 'object') { | ||
target[i] = merge(target[i], item, options); | ||
var targetItem = target[i]; | ||
if (targetItem && typeof targetItem === 'object' && item && typeof item === 'object') { | ||
target[i] = merge(targetItem, item, options); | ||
} else { | ||
@@ -211,3 +213,3 @@ target.push(item); | ||
var isBuffer = function isBuffer(obj) { | ||
if (obj === null || typeof obj === 'undefined') { | ||
if (!obj || typeof obj !== 'object') { | ||
return false; | ||
@@ -214,0 +216,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/ljharb/qs", | ||
"version": "6.6.0", | ||
"version": "6.7.0", | ||
"repository": { | ||
@@ -21,3 +21,7 @@ "type": "git", | ||
"querystring", | ||
"qs" | ||
"qs", | ||
"query", | ||
"url", | ||
"parse", | ||
"stringify" | ||
], | ||
@@ -29,14 +33,16 @@ "engines": { | ||
"devDependencies": { | ||
"@ljharb/eslint-config": "^13.0.0", | ||
"@ljharb/eslint-config": "^13.1.1", | ||
"browserify": "^16.2.3", | ||
"covert": "^1.1.0", | ||
"covert": "^1.1.1", | ||
"editorconfig-tools": "^0.1.1", | ||
"eslint": "^5.9.0", | ||
"eslint": "^5.15.3", | ||
"evalmd": "^0.0.17", | ||
"for-each": "^0.3.3", | ||
"iconv-lite": "^0.4.24", | ||
"mkdirp": "^0.5.1", | ||
"object-inspect": "^1.6.0", | ||
"qs-iconv": "^1.0.4", | ||
"safe-publish-latest": "^1.1.2", | ||
"safer-buffer": "^2.1.2", | ||
"tape": "^4.9.1" | ||
"tape": "^4.10.1" | ||
}, | ||
@@ -43,0 +49,0 @@ "scripts": { |
@@ -241,3 +241,3 @@ # qs <sup>[![Version Badge][2]][1]</sup> | ||
**qs** will also limit specifying indices in an array to a maximum index of `20`. Any array members with an index of greater than `20` will | ||
instead be converted to an object with the index as the key: | ||
instead be converted to an object with the index as the key. This is needed to handle cases when someone sent, for example, `a[999999999]` and it will take significant time to iterate over this huge array. | ||
@@ -277,2 +277,9 @@ ```javascript | ||
Some people use comma to join array, **qs** can parse it: | ||
```javascript | ||
var arraysOfObjects = qs.parse('a=b,c', { comma: true }) | ||
assert.deepEqual(arraysOfObjects, { a: ['b', 'c'] }) | ||
``` | ||
(_this cannot convert nested objects, such as `a={b:1},{c:d}`_) | ||
### Stringifying | ||
@@ -353,2 +360,4 @@ | ||
// 'a=b&a=c' | ||
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'comma' }) | ||
// 'a=b,c' | ||
``` | ||
@@ -355,0 +364,0 @@ |
@@ -240,2 +240,10 @@ 'use strict'; | ||
t.test('parses jquery-param strings', function (st) { | ||
// readable = 'filter[0][]=int1&filter[0][]==&filter[0][]=77&filter[]=and&filter[2][]=int2&filter[2][]==&filter[2][]=8' | ||
var encoded = 'filter%5B0%5D%5B%5D=int1&filter%5B0%5D%5B%5D=%3D&filter%5B0%5D%5B%5D=77&filter%5B%5D=and&filter%5B2%5D%5B%5D=int2&filter%5B2%5D%5B%5D=%3D&filter%5B2%5D%5B%5D=8'; | ||
var expected = { filter: [['int1', '=', '77'], 'and', ['int2', '=', '8']] }; | ||
st.deepEqual(qs.parse(encoded), expected); | ||
st.end(); | ||
}); | ||
t.test('continues parsing when no parent is found', function (st) { | ||
@@ -343,2 +351,11 @@ st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' }); | ||
t.test('parses string with comma as array divider', function (st) { | ||
st.deepEqual(qs.parse('foo=bar,tee', { comma: true }), { foo: ['bar', 'tee'] }); | ||
st.deepEqual(qs.parse('foo[bar]=coffee,tee', { comma: true }), { foo: { bar: ['coffee', 'tee'] } }); | ||
st.deepEqual(qs.parse('foo=', { comma: true }), { foo: '' }); | ||
st.deepEqual(qs.parse('foo', { comma: true }), { foo: '' }); | ||
st.deepEqual(qs.parse('foo', { comma: true, strictNullHandling: true }), { foo: null }); | ||
st.end(); | ||
}); | ||
t.test('parses an object in dot notation', function (st) { | ||
@@ -552,3 +569,3 @@ var input = { | ||
} | ||
return iconv.decode(SaferBuffer.from(result), 'shift_jis').toString(); | ||
return String(iconv.decode(SaferBuffer.from(result), 'shift_jis')); | ||
} | ||
@@ -555,0 +572,0 @@ }), { 県: '大阪府' }); |
@@ -22,2 +22,11 @@ 'use strict'; | ||
t.test('stringifies falsy values', function (st) { | ||
st.equal(qs.stringify(undefined), ''); | ||
st.equal(qs.stringify(null), ''); | ||
st.equal(qs.stringify(null, { strictNullHandling: true }), ''); | ||
st.equal(qs.stringify(false), ''); | ||
st.equal(qs.stringify(0), ''); | ||
st.end(); | ||
}); | ||
t.test('adds query prefix', function (st) { | ||
@@ -33,2 +42,9 @@ st.equal(qs.stringify({ a: 'b' }, { addQueryPrefix: true }), '?a=b'); | ||
t.test('stringifies nested falsy values', function (st) { | ||
st.equal(qs.stringify({ a: { b: { c: null } } }), 'a%5Bb%5D%5Bc%5D='); | ||
st.equal(qs.stringify({ a: { b: { c: null } } }, { strictNullHandling: true }), 'a%5Bb%5D%5Bc%5D'); | ||
st.equal(qs.stringify({ a: { b: { c: false } } }), 'a%5Bb%5D%5Bc%5D=false'); | ||
st.end(); | ||
}); | ||
t.test('stringifies a nested object', function (st) { | ||
@@ -58,2 +74,7 @@ st.equal(qs.stringify({ a: { b: 'c' } }), 'a%5Bb%5D=c'); | ||
st.equal( | ||
qs.stringify({ a: ['b', 'c', 'd'] }, { arrayFormat: 'comma' }), | ||
'a=b%2Cc%2Cd', | ||
'comma => comma' | ||
); | ||
st.equal( | ||
qs.stringify({ a: ['b', 'c', 'd'] }), | ||
@@ -84,2 +105,3 @@ 'a%5B0%5D=b&a%5B1%5D=c&a%5B2%5D=d', | ||
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'brackets' }), 'a%5Bb%5D%5B%5D=c&a%5Bb%5D%5B%5D=d'); | ||
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { arrayFormat: 'comma' }), 'a%5Bb%5D=c%2Cd'); // a[b]=c,d | ||
st.equal(qs.stringify({ a: { b: ['c', 'd'] } }), 'a%5Bb%5D%5B0%5D=c&a%5Bb%5D%5B1%5D=d'); | ||
@@ -109,2 +131,10 @@ st.end(); | ||
{ a: { b: ['c', 'd'] } }, | ||
{ allowDots: true, encode: false, arrayFormat: 'comma' } | ||
), | ||
'a.b=c,d', | ||
'comma: stringifies with dots + comma' | ||
); | ||
st.equal( | ||
qs.stringify( | ||
{ a: { b: ['c', 'd'] } }, | ||
{ allowDots: true, encode: false } | ||
@@ -121,3 +151,3 @@ ), | ||
qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'indices' }), | ||
'a%5B0%5D%5Bb%5D=c', | ||
'a%5B0%5D%5Bb%5D=c', // a[0][b]=c | ||
'indices => brackets' | ||
@@ -127,3 +157,3 @@ ); | ||
qs.stringify({ a: [{ b: 'c' }] }, { arrayFormat: 'brackets' }), | ||
'a%5B%5D%5Bb%5D=c', | ||
'a%5B%5D%5Bb%5D=c', // a[][b]=c | ||
'brackets => brackets' | ||
@@ -130,0 +160,0 @@ ); |
'use strict'; | ||
var test = require('tape'); | ||
var inspect = require('object-inspect'); | ||
var SaferBuffer = require('safer-buffer').Buffer; | ||
var forEach = require('for-each'); | ||
var utils = require('../lib/utils'); | ||
test('merge()', function (t) { | ||
t.deepEqual(utils.merge(null, true), [null, true], 'merges true into null'); | ||
t.deepEqual(utils.merge(null, [42]), [null, 42], 'merges null into an array'); | ||
t.deepEqual(utils.merge({ a: 'b' }, { a: 'c' }), { a: ['b', 'c'] }, 'merges two objects with the same key'); | ||
@@ -24,2 +31,26 @@ | ||
t.test( | ||
'avoids invoking array setters unnecessarily', | ||
{ skip: typeof Object.defineProperty !== 'function' }, | ||
function (st) { | ||
var setCount = 0; | ||
var getCount = 0; | ||
var observed = []; | ||
Object.defineProperty(observed, 0, { | ||
get: function () { | ||
getCount += 1; | ||
return { bar: 'baz' }; | ||
}, | ||
set: function () { setCount += 1; } | ||
}); | ||
utils.merge(observed, [null]); | ||
st.equal(setCount, 0); | ||
st.equal(getCount, 1); | ||
observed[0] = observed[0]; // eslint-disable-line no-self-assign | ||
st.equal(setCount, 1); | ||
st.equal(getCount, 2); | ||
st.end(); | ||
} | ||
); | ||
t.end(); | ||
@@ -91,1 +122,17 @@ }); | ||
}); | ||
test('isBuffer()', function (t) { | ||
forEach([null, undefined, true, false, '', 'abc', 42, 0, NaN, {}, [], function () {}, /a/g], function (x) { | ||
t.equal(utils.isBuffer(x), false, inspect(x) + ' is not a buffer'); | ||
}); | ||
var fakeBuffer = { constructor: Buffer }; | ||
t.equal(utils.isBuffer(fakeBuffer), false, 'fake buffer is not a buffer'); | ||
var saferBuffer = SaferBuffer.from('abc'); | ||
t.equal(utils.isBuffer(saferBuffer), true, 'SaferBuffer instance is a buffer'); | ||
var buffer = Buffer.from ? Buffer.from('abc') : new Buffer('abc'); | ||
t.equal(utils.isBuffer(buffer), true, 'real Buffer instance is a buffer'); | ||
t.end(); | ||
}); |
Sorry, the diff of this file is not supported yet
142918
2576
571
14