Comparing version 6.1.0 to 6.2.0
@@ -1,2 +0,7 @@ | ||
## [**6.1.0**](https://github.com/ljharb/qs/issues?milestone=34&state=closed) | ||
## [**6.2.0**](https://github.com/ljharb/qs/issues?milestone=36&state=closed) | ||
- [New] pass Buffers to the encoder/decoder directly (#161) | ||
- [New] add "encoder" and "decoder" options, for custom param encoding/decoding (#160) | ||
- [Fix] fix compacting of nested sparse arrays (#150) | ||
## [**6.1.0**](https://github.com/ljharb/qs/issues?milestone=35&state=closed) | ||
- [New] allowDots option for `stringify` (#151) | ||
@@ -3,0 +8,0 @@ - [Fix] "sort" option should work at a depth of 3 or more (#151) |
135
dist/qs.js
@@ -17,3 +17,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 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){ | ||
var internals = { | ||
var defaults = { | ||
delimiter: '&', | ||
@@ -26,6 +26,7 @@ depth: 5, | ||
allowPrototypes: false, | ||
allowDots: false | ||
allowDots: false, | ||
decoder: Utils.decode | ||
}; | ||
internals.parseValues = function (str, options) { | ||
var parseValues = function parseValues(str, options) { | ||
var obj = {}; | ||
@@ -39,10 +40,10 @@ var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit); | ||
if (pos === -1) { | ||
obj[Utils.decode(part)] = ''; | ||
obj[options.decoder(part)] = ''; | ||
if (options.strictNullHandling) { | ||
obj[Utils.decode(part)] = null; | ||
obj[options.decoder(part)] = null; | ||
} | ||
} else { | ||
var key = Utils.decode(part.slice(0, pos)); | ||
var val = Utils.decode(part.slice(pos + 1)); | ||
var key = options.decoder(part.slice(0, pos)); | ||
var val = options.decoder(part.slice(pos + 1)); | ||
@@ -60,3 +61,3 @@ if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
internals.parseObject = function (chain, val, options) { | ||
var parseObject = function parseObject(chain, val, options) { | ||
if (!chain.length) { | ||
@@ -71,3 +72,3 @@ return val; | ||
obj = []; | ||
obj = obj.concat(internals.parseObject(chain, val, options)); | ||
obj = obj.concat(parseObject(chain, val, options)); | ||
} else { | ||
@@ -85,5 +86,5 @@ obj = options.plainObjects ? Object.create(null) : {}; | ||
obj = []; | ||
obj[index] = internals.parseObject(chain, val, options); | ||
obj[index] = parseObject(chain, val, options); | ||
} else { | ||
obj[cleanRoot] = internals.parseObject(chain, val, options); | ||
obj[cleanRoot] = parseObject(chain, val, options); | ||
} | ||
@@ -95,3 +96,3 @@ } | ||
internals.parseKeys = function (givenKey, val, options) { | ||
var parseKeys = function parseKeys(givenKey, val, options) { | ||
if (!givenKey) { | ||
@@ -147,3 +148,3 @@ return; | ||
return internals.parseObject(keys, val, options); | ||
return parseObject(keys, val, options); | ||
}; | ||
@@ -153,21 +154,23 @@ | ||
var options = opts || {}; | ||
options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter; | ||
options.depth = typeof options.depth === 'number' ? options.depth : internals.depth; | ||
options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : internals.arrayLimit; | ||
if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') { | ||
throw new TypeError('Decoder has to be a function.'); | ||
} | ||
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.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : internals.allowDots; | ||
options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : internals.plainObjects; | ||
options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : internals.allowPrototypes; | ||
options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit; | ||
options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling; | ||
options.decoder = typeof options.decoder === 'function' ? options.decoder : defaults.decoder; | ||
options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : defaults.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 ( | ||
str === '' || | ||
str === null || | ||
typeof str === 'undefined' | ||
) { | ||
if (str === '' || str === null || typeof str === 'undefined') { | ||
return options.plainObjects ? Object.create(null) : {}; | ||
} | ||
var tempObj = typeof str === 'string' ? internals.parseValues(str, options) : str; | ||
var tempObj = typeof str === 'string' ? parseValues(str, options) : str; | ||
var obj = options.plainObjects ? Object.create(null) : {}; | ||
@@ -180,3 +183,3 @@ | ||
var key = keys[i]; | ||
var newObj = internals.parseKeys(key, tempObj[key], options); | ||
var newObj = parseKeys(key, tempObj[key], options); | ||
obj = Utils.merge(obj, newObj, options); | ||
@@ -193,26 +196,26 @@ } | ||
var internals = { | ||
var arrayPrefixGenerators = { | ||
brackets: function brackets(prefix) { | ||
return prefix + '[]'; | ||
}, | ||
indices: function indices(prefix, key) { | ||
return prefix + '[' + key + ']'; | ||
}, | ||
repeat: function repeat(prefix) { | ||
return prefix; | ||
} | ||
}; | ||
var defaults = { | ||
delimiter: '&', | ||
arrayPrefixGenerators: { | ||
brackets: function (prefix) { | ||
return prefix + '[]'; | ||
}, | ||
indices: function (prefix, key) { | ||
return prefix + '[' + key + ']'; | ||
}, | ||
repeat: function (prefix) { | ||
return prefix; | ||
} | ||
}, | ||
strictNullHandling: false, | ||
skipNulls: false, | ||
encode: true | ||
encode: true, | ||
encoder: Utils.encode | ||
}; | ||
internals.stringify = function (object, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort, allowDots) { | ||
var stringify = function stringify(object, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots) { | ||
var obj = object; | ||
if (typeof filter === 'function') { | ||
obj = filter(prefix, obj); | ||
} else if (Utils.isBuffer(obj)) { | ||
obj = String(obj); | ||
} else if (obj instanceof Date) { | ||
@@ -222,3 +225,3 @@ obj = obj.toISOString(); | ||
if (strictNullHandling) { | ||
return encode ? Utils.encode(prefix) : prefix; | ||
return encoder ? encoder(prefix) : prefix; | ||
} | ||
@@ -229,7 +232,7 @@ | ||
if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean') { | ||
if (encode) { | ||
return [Utils.encode(prefix) + '=' + Utils.encode(obj)]; | ||
if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || Utils.isBuffer(obj)) { | ||
if (encoder) { | ||
return [encoder(prefix) + '=' + encoder(obj)]; | ||
} | ||
return [prefix + '=' + obj]; | ||
return [prefix + '=' + String(obj)]; | ||
} | ||
@@ -259,5 +262,5 @@ | ||
if (Array.isArray(obj)) { | ||
values = values.concat(internals.stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort, allowDots)); | ||
values = values.concat(stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots)); | ||
} else { | ||
values = values.concat(internals.stringify(obj[key], prefix + (allowDots ? '.' + key : '[' + key + ']'), generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort, allowDots)); | ||
values = values.concat(stringify(obj[key], prefix + (allowDots ? '.' + key : '[' + key + ']'), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots)); | ||
} | ||
@@ -272,6 +275,7 @@ } | ||
var options = opts || {}; | ||
var delimiter = typeof options.delimiter === 'undefined' ? internals.delimiter : options.delimiter; | ||
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 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 = encode ? (typeof options.encoder === 'function' ? options.encoder : defaults.encoder) : null; | ||
var sort = typeof options.sort === 'function' ? options.sort : null; | ||
@@ -281,2 +285,7 @@ var allowDots = typeof options.allowDots === 'undefined' ? false : options.allowDots; | ||
var filter; | ||
if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') { | ||
throw new TypeError('Encoder has to be a function.'); | ||
} | ||
if (typeof options.filter === 'function') { | ||
@@ -296,3 +305,3 @@ filter = options.filter; | ||
var arrayFormat; | ||
if (options.arrayFormat in internals.arrayPrefixGenerators) { | ||
if (options.arrayFormat in arrayPrefixGenerators) { | ||
arrayFormat = options.arrayFormat; | ||
@@ -305,3 +314,3 @@ } else if ('indices' in options) { | ||
var generateArrayPrefix = internals.arrayPrefixGenerators[arrayFormat]; | ||
var generateArrayPrefix = arrayPrefixGenerators[arrayFormat]; | ||
@@ -323,3 +332,3 @@ if (!objKeys) { | ||
keys = keys.concat(internals.stringify(obj[key], key, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort, allowDots)); | ||
keys = keys.concat(stringify(obj[key], key, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots)); | ||
} | ||
@@ -379,3 +388,3 @@ | ||
return Object.keys(source).reduce(function (acc, key) { | ||
return Object.keys(source).reduce(function (acc, key) { | ||
var value = source[key]; | ||
@@ -388,3 +397,3 @@ | ||
} | ||
return acc; | ||
return acc; | ||
}, mergeTarget); | ||
@@ -444,3 +453,3 @@ }; | ||
c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF)); | ||
out += (hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]); | ||
out += hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]; | ||
} | ||
@@ -468,3 +477,5 @@ | ||
for (var i = 0; i < obj.length; ++i) { | ||
if (typeof obj[i] !== 'undefined') { | ||
if (obj[i] && typeof obj[i] === 'object') { | ||
compacted.push(exports.compact(obj[i], refs)); | ||
} else if (typeof obj[i] !== 'undefined') { | ||
compacted.push(obj[i]); | ||
@@ -471,0 +482,0 @@ } |
@@ -5,3 +5,3 @@ 'use strict'; | ||
var internals = { | ||
var defaults = { | ||
delimiter: '&', | ||
@@ -14,6 +14,7 @@ depth: 5, | ||
allowPrototypes: false, | ||
allowDots: false | ||
allowDots: false, | ||
decoder: Utils.decode | ||
}; | ||
internals.parseValues = function (str, options) { | ||
var parseValues = function parseValues(str, options) { | ||
var obj = {}; | ||
@@ -27,10 +28,10 @@ var parts = str.split(options.delimiter, options.parameterLimit === Infinity ? undefined : options.parameterLimit); | ||
if (pos === -1) { | ||
obj[Utils.decode(part)] = ''; | ||
obj[options.decoder(part)] = ''; | ||
if (options.strictNullHandling) { | ||
obj[Utils.decode(part)] = null; | ||
obj[options.decoder(part)] = null; | ||
} | ||
} else { | ||
var key = Utils.decode(part.slice(0, pos)); | ||
var val = Utils.decode(part.slice(pos + 1)); | ||
var key = options.decoder(part.slice(0, pos)); | ||
var val = options.decoder(part.slice(pos + 1)); | ||
@@ -48,3 +49,3 @@ if (Object.prototype.hasOwnProperty.call(obj, key)) { | ||
internals.parseObject = function (chain, val, options) { | ||
var parseObject = function parseObject(chain, val, options) { | ||
if (!chain.length) { | ||
@@ -59,3 +60,3 @@ return val; | ||
obj = []; | ||
obj = obj.concat(internals.parseObject(chain, val, options)); | ||
obj = obj.concat(parseObject(chain, val, options)); | ||
} else { | ||
@@ -73,5 +74,5 @@ obj = options.plainObjects ? Object.create(null) : {}; | ||
obj = []; | ||
obj[index] = internals.parseObject(chain, val, options); | ||
obj[index] = parseObject(chain, val, options); | ||
} else { | ||
obj[cleanRoot] = internals.parseObject(chain, val, options); | ||
obj[cleanRoot] = parseObject(chain, val, options); | ||
} | ||
@@ -83,3 +84,3 @@ } | ||
internals.parseKeys = function (givenKey, val, options) { | ||
var parseKeys = function parseKeys(givenKey, val, options) { | ||
if (!givenKey) { | ||
@@ -135,3 +136,3 @@ return; | ||
return internals.parseObject(keys, val, options); | ||
return parseObject(keys, val, options); | ||
}; | ||
@@ -141,21 +142,23 @@ | ||
var options = opts || {}; | ||
options.delimiter = typeof options.delimiter === 'string' || Utils.isRegExp(options.delimiter) ? options.delimiter : internals.delimiter; | ||
options.depth = typeof options.depth === 'number' ? options.depth : internals.depth; | ||
options.arrayLimit = typeof options.arrayLimit === 'number' ? options.arrayLimit : internals.arrayLimit; | ||
if (options.decoder !== null && options.decoder !== undefined && typeof options.decoder !== 'function') { | ||
throw new TypeError('Decoder has to be a function.'); | ||
} | ||
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.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : internals.allowDots; | ||
options.plainObjects = typeof options.plainObjects === 'boolean' ? options.plainObjects : internals.plainObjects; | ||
options.allowPrototypes = typeof options.allowPrototypes === 'boolean' ? options.allowPrototypes : internals.allowPrototypes; | ||
options.parameterLimit = typeof options.parameterLimit === 'number' ? options.parameterLimit : internals.parameterLimit; | ||
options.strictNullHandling = typeof options.strictNullHandling === 'boolean' ? options.strictNullHandling : internals.strictNullHandling; | ||
options.decoder = typeof options.decoder === 'function' ? options.decoder : defaults.decoder; | ||
options.allowDots = typeof options.allowDots === 'boolean' ? options.allowDots : defaults.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 ( | ||
str === '' || | ||
str === null || | ||
typeof str === 'undefined' | ||
) { | ||
if (str === '' || str === null || typeof str === 'undefined') { | ||
return options.plainObjects ? Object.create(null) : {}; | ||
} | ||
var tempObj = typeof str === 'string' ? internals.parseValues(str, options) : str; | ||
var tempObj = typeof str === 'string' ? parseValues(str, options) : str; | ||
var obj = options.plainObjects ? Object.create(null) : {}; | ||
@@ -168,3 +171,3 @@ | ||
var key = keys[i]; | ||
var newObj = internals.parseKeys(key, tempObj[key], options); | ||
var newObj = parseKeys(key, tempObj[key], options); | ||
obj = Utils.merge(obj, newObj, options); | ||
@@ -171,0 +174,0 @@ } |
@@ -5,26 +5,26 @@ 'use strict'; | ||
var internals = { | ||
var arrayPrefixGenerators = { | ||
brackets: function brackets(prefix) { | ||
return prefix + '[]'; | ||
}, | ||
indices: function indices(prefix, key) { | ||
return prefix + '[' + key + ']'; | ||
}, | ||
repeat: function repeat(prefix) { | ||
return prefix; | ||
} | ||
}; | ||
var defaults = { | ||
delimiter: '&', | ||
arrayPrefixGenerators: { | ||
brackets: function (prefix) { | ||
return prefix + '[]'; | ||
}, | ||
indices: function (prefix, key) { | ||
return prefix + '[' + key + ']'; | ||
}, | ||
repeat: function (prefix) { | ||
return prefix; | ||
} | ||
}, | ||
strictNullHandling: false, | ||
skipNulls: false, | ||
encode: true | ||
encode: true, | ||
encoder: Utils.encode | ||
}; | ||
internals.stringify = function (object, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort, allowDots) { | ||
var stringify = function stringify(object, prefix, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots) { | ||
var obj = object; | ||
if (typeof filter === 'function') { | ||
obj = filter(prefix, obj); | ||
} else if (Utils.isBuffer(obj)) { | ||
obj = String(obj); | ||
} else if (obj instanceof Date) { | ||
@@ -34,3 +34,3 @@ obj = obj.toISOString(); | ||
if (strictNullHandling) { | ||
return encode ? Utils.encode(prefix) : prefix; | ||
return encoder ? encoder(prefix) : prefix; | ||
} | ||
@@ -41,7 +41,7 @@ | ||
if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean') { | ||
if (encode) { | ||
return [Utils.encode(prefix) + '=' + Utils.encode(obj)]; | ||
if (typeof obj === 'string' || typeof obj === 'number' || typeof obj === 'boolean' || Utils.isBuffer(obj)) { | ||
if (encoder) { | ||
return [encoder(prefix) + '=' + encoder(obj)]; | ||
} | ||
return [prefix + '=' + obj]; | ||
return [prefix + '=' + String(obj)]; | ||
} | ||
@@ -71,5 +71,5 @@ | ||
if (Array.isArray(obj)) { | ||
values = values.concat(internals.stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort, allowDots)); | ||
values = values.concat(stringify(obj[key], generateArrayPrefix(prefix, key), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots)); | ||
} else { | ||
values = values.concat(internals.stringify(obj[key], prefix + (allowDots ? '.' + key : '[' + key + ']'), generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort, allowDots)); | ||
values = values.concat(stringify(obj[key], prefix + (allowDots ? '.' + key : '[' + key + ']'), generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots)); | ||
} | ||
@@ -84,6 +84,7 @@ } | ||
var options = opts || {}; | ||
var delimiter = typeof options.delimiter === 'undefined' ? internals.delimiter : options.delimiter; | ||
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 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 = encode ? (typeof options.encoder === 'function' ? options.encoder : defaults.encoder) : null; | ||
var sort = typeof options.sort === 'function' ? options.sort : null; | ||
@@ -93,2 +94,7 @@ var allowDots = typeof options.allowDots === 'undefined' ? false : options.allowDots; | ||
var filter; | ||
if (options.encoder !== null && options.encoder !== undefined && typeof options.encoder !== 'function') { | ||
throw new TypeError('Encoder has to be a function.'); | ||
} | ||
if (typeof options.filter === 'function') { | ||
@@ -108,3 +114,3 @@ filter = options.filter; | ||
var arrayFormat; | ||
if (options.arrayFormat in internals.arrayPrefixGenerators) { | ||
if (options.arrayFormat in arrayPrefixGenerators) { | ||
arrayFormat = options.arrayFormat; | ||
@@ -117,3 +123,3 @@ } else if ('indices' in options) { | ||
var generateArrayPrefix = internals.arrayPrefixGenerators[arrayFormat]; | ||
var generateArrayPrefix = arrayPrefixGenerators[arrayFormat]; | ||
@@ -135,3 +141,3 @@ if (!objKeys) { | ||
keys = keys.concat(internals.stringify(obj[key], key, generateArrayPrefix, strictNullHandling, skipNulls, encode, filter, sort, allowDots)); | ||
keys = keys.concat(stringify(obj[key], key, generateArrayPrefix, strictNullHandling, skipNulls, encoder, filter, sort, allowDots)); | ||
} | ||
@@ -138,0 +144,0 @@ |
@@ -49,3 +49,3 @@ 'use strict'; | ||
return Object.keys(source).reduce(function (acc, key) { | ||
return Object.keys(source).reduce(function (acc, key) { | ||
var value = source[key]; | ||
@@ -58,3 +58,3 @@ | ||
} | ||
return acc; | ||
return acc; | ||
}, mergeTarget); | ||
@@ -114,3 +114,3 @@ }; | ||
c = 0x10000 + (((c & 0x3FF) << 10) | (string.charCodeAt(i) & 0x3FF)); | ||
out += (hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]); | ||
out += hexTable[0xF0 | (c >> 18)] + hexTable[0x80 | ((c >> 12) & 0x3F)] + hexTable[0x80 | ((c >> 6) & 0x3F)] + hexTable[0x80 | (c & 0x3F)]; | ||
} | ||
@@ -138,3 +138,5 @@ | ||
for (var i = 0; i < obj.length; ++i) { | ||
if (typeof obj[i] !== 'undefined') { | ||
if (obj[i] && typeof obj[i] === 'object') { | ||
compacted.push(exports.compact(obj[i], refs)); | ||
} else if (typeof obj[i] !== 'undefined') { | ||
compacted.push(obj[i]); | ||
@@ -141,0 +143,0 @@ } |
@@ -5,3 +5,3 @@ { | ||
"homepage": "https://github.com/ljharb/qs", | ||
"version": "6.1.0", | ||
"version": "6.2.0", | ||
"repository": { | ||
@@ -28,13 +28,15 @@ "type": "git", | ||
"devDependencies": { | ||
"browserify": "^12.0.1", | ||
"tape": "^4.3.0", | ||
"browserify": "^13.0.1", | ||
"tape": "^4.5.1", | ||
"covert": "^1.1.0", | ||
"mkdirp": "^0.5.1", | ||
"eslint": "^1.10.3", | ||
"@ljharb/eslint-config": "^1.6.1", | ||
"eslint": "^2.9.0", | ||
"@ljharb/eslint-config": "^4.0.0", | ||
"parallelshell": "^2.0.0", | ||
"evalmd": "^0.0.16" | ||
"iconv-lite": "^0.4.13", | ||
"evalmd": "^0.0.17" | ||
}, | ||
"scripts": { | ||
"test": "parallelshell 'npm run readme' 'npm run lint' 'npm run coverage'", | ||
"pretest": "parallelshell 'npm run --silent readme' 'npm run --silent lint'", | ||
"test": "npm run --silent coverage", | ||
"tests-only": "node test", | ||
@@ -41,0 +43,0 @@ "readme": "evalmd README.md", |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var qs = require('../'); | ||
var iconv = require('iconv-lite'); | ||
@@ -185,2 +186,5 @@ test('parse()', function (t) { | ||
st.deepEqual(qs.parse('a[10]=1&a[2]=2'), { a: ['2', '1'] }); | ||
st.deepEqual(qs.parse('a[1][b][2][c]=1'), { a: [{ b: [{ c: '1' }] }] }); | ||
st.deepEqual(qs.parse('a[1][2][3][c]=1'), { a: [[[{ c: '1' }]]] }); | ||
st.deepEqual(qs.parse('a[1][2][3][c][1]=1'), { a: [[[{ c: ['1'] }]]] }); | ||
st.end(); | ||
@@ -395,2 +399,28 @@ }); | ||
}); | ||
t.test('can parse with custom encoding', function (st) { | ||
st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', { | ||
decoder: function (str) { | ||
var reg = /\%([0-9A-F]{2})/ig; | ||
var result = []; | ||
var parts; | ||
var last = 0; | ||
while (parts = reg.exec(str)) { | ||
result.push(parseInt(parts[1], 16)); | ||
last = parts.index + parts[0].length; | ||
} | ||
return iconv.decode(new Buffer(result), 'shift_jis').toString(); | ||
} | ||
}), { 県: '大阪府' }); | ||
st.end(); | ||
}); | ||
t.test('throws error with wrong decoder', function (st) { | ||
st.throws(function () { | ||
qs.parse({}, { | ||
decoder: 'string' | ||
}); | ||
}, new TypeError('Decoder has to be a function.')); | ||
st.end(); | ||
}); | ||
}); |
@@ -5,2 +5,3 @@ 'use strict'; | ||
var qs = require('../'); | ||
var iconv = require('iconv-lite'); | ||
@@ -25,3 +26,3 @@ test('stringify()', function (t) { | ||
}); | ||
t.test('stringifies a nested object with dots notation', function (st) { | ||
@@ -58,3 +59,3 @@ st.equal(qs.stringify({ a: { b: 'c' } }, { allowDots: true }), 'a.b=c'); | ||
}); | ||
t.test('stringifies a nested array value with dots notation', function (st) { | ||
@@ -70,3 +71,8 @@ st.equal(qs.stringify({ a: { b: ['c', 'd'] } }, { allowDots: true, encode: false }), 'a.b[0]=c&a.b[1]=d'); | ||
}); | ||
t.test('stringifies an array with mixed objects and primitives', function (st) { | ||
st.equal(qs.stringify({ a: [{ b: 1 }, 2, 3] }, { encode: false }), 'a[0][b]=1&a[1]=2&a[2]=3'); | ||
st.end(); | ||
}); | ||
t.test('stringifies an object inside an array with dots notation', function (st) { | ||
@@ -256,3 +262,3 @@ st.equal(qs.stringify({ a: [{ b: 'c' }] }, { allowDots: true, encode: false }), 'a[0].b=c'); | ||
}); | ||
t.test('can sort the keys at depth 3 or more too', function (st) { | ||
@@ -264,2 +270,42 @@ var sort = function (a, b) { return a.localeCompare(b); }; | ||
}); | ||
t.test('can stringify with custom encoding', function (st) { | ||
st.equal(qs.stringify({ 県: '大阪府', '': ''}, { | ||
encoder: function (str) { | ||
if (str.length === 0) { | ||
return ''; | ||
} | ||
var buf = iconv.encode(str, 'shiftjis'); | ||
var result = []; | ||
for (var i=0; i < buf.length; ++i) { | ||
result.push(buf.readUInt8(i).toString(16)); | ||
} | ||
return '%' + result.join('%'); | ||
} | ||
}), '%8c%a7=%91%e5%8d%e3%95%7b&='); | ||
st.end(); | ||
}); | ||
t.test('throws error with wrong encoder', function (st) { | ||
st.throws(function () { | ||
qs.stringify({}, { | ||
encoder: 'string' | ||
}); | ||
}, new TypeError('Encoder has to be a function.')); | ||
st.end(); | ||
}); | ||
t.test('can use custom encoder for a buffer object', { | ||
skip: typeof Buffer === 'undefined' | ||
}, function (st) { | ||
st.equal(qs.stringify({ a: new Buffer([1]) }, { | ||
encoder: function (buffer) { | ||
if (typeof buffer === 'string') { | ||
return buffer; | ||
} | ||
return String.fromCharCode(buffer.readUInt8(0) + 97); | ||
} | ||
}), 'a=b'); | ||
st.end(); | ||
}); | ||
}); |
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
No README
QualityPackage does not have a README. This may indicate a failed publish or a low quality package.
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
1513
74057
9
16
1
0