-
-
Notifications
You must be signed in to change notification settings - Fork 732
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom stringify arrays and objects #365
base: main
Are you sure you want to change the base?
Changes from all commits
e39c235
760a670
97154a6
4019179
7f216ee
df0cb44
dadf9db
670254b
698b683
f884e2d
1f35831
b9a032f
6151be3
7b36800
152b26c
fe6384c
76e4570
72dc89f
5af2bf8
eac5616
0625c49
eecd28d
911efab
ddc1ff9
37f6a6b
cd9a3cd
bf0ea91
8d1dea2
511e1c9
0932740
e25d9aa
ac71264
422d2c2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -139,10 +139,25 @@ var regexed = qs.parse('a=b;c=d,e=f', { delimiter: /[;,]/ }); | |||||
assert.deepEqual(regexed, { a: 'b', c: 'd', e: 'f' }); | ||||||
``` | ||||||
|
||||||
Option `allowDots` can be used to enable dot notation: | ||||||
You may set `objectFormat: 'dots'` to enable dot notation: | ||||||
|
||||||
```javascript | ||||||
var withDots = qs.parse('a.b=c', { allowDots: true }); | ||||||
var withDots = qs.parse('a.b=c', { objectFormat: 'dots' }); | ||||||
assert.deepEqual(withDots, { a: { b: 'c' } }); | ||||||
``` | ||||||
|
||||||
You may set `objectFormat: 'curly'` to enable curly brackets notation: | ||||||
|
||||||
```javascript | ||||||
var withDots = qs.parse('a{b}=c', { objectFormat: 'curly' }); | ||||||
assert.deepEqual(withDots, { a: { b: 'c' } }); | ||||||
``` | ||||||
|
||||||
You may use function in `objectFormat` to parse custom notation | ||||||
(function should replace your custom format with default brackets format): | ||||||
|
||||||
```javascript | ||||||
var withDots = qs.parse('a/b=c', { objectFormat: function (key) { return key.replace(/\/([^.[]+)/g, '[$1]'); } }); | ||||||
assert.deepEqual(withDots, { a: { b: 'c' } }); | ||||||
``` | ||||||
|
||||||
|
@@ -275,11 +290,18 @@ assert.deepEqual(arraysOfObjects, { a: [{ b: 'c' }] }); | |||||
|
||||||
Some people use comma to join array, **qs** can parse it: | ||||||
```javascript | ||||||
var arraysOfObjects = qs.parse('a=b,c', { comma: true }) | ||||||
var arraysOfObjects = qs.parse('a=b,c', { arrayFormat: 'comma' }) | ||||||
assert.deepEqual(arraysOfObjects, { a: ['b', 'c'] }) | ||||||
``` | ||||||
(_this cannot convert nested objects, such as `a={b:1},{c:d}`_) | ||||||
|
||||||
And you may write function for parse arrays with custom keys | ||||||
(function should replace your custom format with default brackets format): | ||||||
```javascript | ||||||
var arraysOfObjects = qs.parse('a-0=b&a-1=c', { arrayFormat: function (key) { return key.replace(/-([^-[]+)/g, '[$1]'); } }) | ||||||
assert.deepEqual(arraysOfObjects, { a: ['b', 'c'] }) | ||||||
``` | ||||||
|
||||||
### Stringifying | ||||||
|
||||||
[](#preventEval) | ||||||
|
@@ -381,6 +403,8 @@ qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' }) | |||||
// 'a=b&a=c' | ||||||
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'comma' }) | ||||||
// 'a=b,c' | ||||||
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: function customFormat(prefix, key) { return prefix + '-' + key; } }) | ||||||
// 'a-0=b&a-1=c' | ||||||
``` | ||||||
|
||||||
When objects are stringified, by default they use bracket notation: | ||||||
|
@@ -390,11 +414,17 @@ qs.stringify({ a: { b: { c: 'd', e: 'f' } } }); | |||||
// 'a[b][c]=d&a[b][e]=f' | ||||||
``` | ||||||
|
||||||
You may override this to use dot notation by setting the `allowDots` option to `true`: | ||||||
You may use the `objectFormat` option to specify the format of the output object: | ||||||
|
||||||
```javascript | ||||||
qs.stringify({ a: { b: { c: 'd', e: 'f' } } }, { allowDots: true }); | ||||||
qs.stringify({ a: { b: { c: 'd', e: 'f' } } }, { arrayFormat: 'dots' }); | ||||||
// 'a.b.c=d&a.b.e=f' | ||||||
qs.stringify({ a: { b: { c: 'd', e: 'f' } } }, { arrayFormat: 'brackets' }); | ||||||
// 'a[b][c]=d&a[b][e]=f' | ||||||
qs.stringify({ a: { b: { c: 'd', e: 'f' } } }, { arrayFormat: 'curly' }); | ||||||
// 'a{b}{c}=d&a{b}{e}=f' | ||||||
Comment on lines
+424
to
+425
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. similarly here
Suggested change
|
||||||
qs.stringify({ a: { b: { c: 'd', e: 'f' } } }, { arrayFormat: function customFormat(prefix, key) { return prefix + '/' + key; } }); | ||||||
// 'a/b/c=d&a/b/e=f' | ||||||
``` | ||||||
|
||||||
Empty strings and null values will omit the value, but the equals sign (=) remains in place: | ||||||
|
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -5,18 +5,40 @@ var utils = require('./utils'); | |||||||
var has = Object.prototype.hasOwnProperty; | ||||||||
var isArray = Array.isArray; | ||||||||
|
||||||||
var arrayKeyReplacer = { | ||||||||
brackets: function brackets(key) { | ||||||||
return key; | ||||||||
} | ||||||||
}; | ||||||||
|
||||||||
var objectKeyReplacer = { | ||||||||
brackets: function brackets(key) { | ||||||||
return key; | ||||||||
}, | ||||||||
curly: function curly(key) { | ||||||||
return key.replace(/\{([^{}[]+)\}/g, '[$1]'); | ||||||||
}, | ||||||||
Comment on lines
+18
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
dots: function dots(key) { | ||||||||
return key.replace(/\.([^.[]+)/g, '[$1]'); | ||||||||
} | ||||||||
}; | ||||||||
|
||||||||
var defaults = { | ||||||||
// deprecated | ||||||||
allowDots: false, | ||||||||
allowPrototypes: false, | ||||||||
arrayFormat: 'brackets', | ||||||||
arrayLimit: 20, | ||||||||
charset: 'utf-8', | ||||||||
charsetSentinel: false, | ||||||||
// deprecated | ||||||||
comma: false, | ||||||||
decoder: utils.decode, | ||||||||
delimiter: '&', | ||||||||
depth: 5, | ||||||||
ignoreQueryPrefix: false, | ||||||||
interpretNumericEntities: false, | ||||||||
objectFormat: 'brackets', | ||||||||
parameterLimit: 1000, | ||||||||
parseArrays: true, | ||||||||
plainObjects: false, | ||||||||
|
@@ -30,7 +52,7 @@ var interpretNumericEntities = function (str) { | |||||||
}; | ||||||||
|
||||||||
var parseArrayValue = function (val, options) { | ||||||||
if (val && typeof val === 'string' && options.comma && val.indexOf(',') > -1) { | ||||||||
if (val && typeof val === 'string' && options.arrayFormat === 'comma' && val.indexOf(',') > -1) { | ||||||||
return val.split(','); | ||||||||
} | ||||||||
|
||||||||
|
@@ -162,8 +184,18 @@ var parseKeys = function parseQueryStringKeys(givenKey, val, options, valuesPars | |||||||
return; | ||||||||
} | ||||||||
|
||||||||
// Transform dot notation to bracket notation | ||||||||
var key = options.allowDots ? givenKey.replace(/\.([^.[]+)/g, '[$1]') : givenKey; | ||||||||
// Transforms key to bracket notation | ||||||||
var key = givenKey; | ||||||||
if (typeof options.arrayFormat === 'function') { | ||||||||
key = options.arrayFormat(key); | ||||||||
} else if (options.arrayFormat in arrayKeyReplacer) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
key = arrayKeyReplacer[options.arrayFormat](key); | ||||||||
} | ||||||||
if (typeof options.objectFormat === 'function') { | ||||||||
key = options.objectFormat(key); | ||||||||
} else if (options.objectFormat in objectKeyReplacer) { | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
key = objectKeyReplacer[options.objectFormat](key); | ||||||||
} | ||||||||
|
||||||||
// The regex chunks | ||||||||
|
||||||||
|
@@ -225,9 +257,19 @@ var normalizeParseOptions = function normalizeParseOptions(opts) { | |||||||
} | ||||||||
var charset = typeof opts.charset === 'undefined' ? defaults.charset : opts.charset; | ||||||||
|
||||||||
var arrayFormat = opts.arrayFormat; | ||||||||
if (typeof arrayFormat === 'undefined' && opts.comma) { | ||||||||
arrayFormat = 'comma'; | ||||||||
} | ||||||||
var objectFormat = opts.objectFormat; | ||||||||
if (typeof objectFormat === 'undefined' && opts.allowDots) { | ||||||||
objectFormat = 'dots'; | ||||||||
} | ||||||||
|
||||||||
return { | ||||||||
allowDots: typeof opts.allowDots === 'undefined' ? defaults.allowDots : !!opts.allowDots, | ||||||||
allowPrototypes: typeof opts.allowPrototypes === 'boolean' ? opts.allowPrototypes : defaults.allowPrototypes, | ||||||||
arrayFormat: typeof arrayFormat === 'undefined' ? defaults.arrayFormat : arrayFormat, | ||||||||
arrayLimit: typeof opts.arrayLimit === 'number' ? opts.arrayLimit : defaults.arrayLimit, | ||||||||
charset: charset, | ||||||||
charsetSentinel: typeof opts.charsetSentinel === 'boolean' ? opts.charsetSentinel : defaults.charsetSentinel, | ||||||||
|
@@ -238,6 +280,7 @@ var normalizeParseOptions = function normalizeParseOptions(opts) { | |||||||
depth: (typeof opts.depth === 'number' || opts.depth === false) ? +opts.depth : defaults.depth, | ||||||||
ignoreQueryPrefix: opts.ignoreQueryPrefix === true, | ||||||||
interpretNumericEntities: typeof opts.interpretNumericEntities === 'boolean' ? opts.interpretNumericEntities : defaults.interpretNumericEntities, | ||||||||
objectFormat: typeof objectFormat === 'undefined' ? defaults.objectFormat : objectFormat, | ||||||||
parameterLimit: typeof opts.parameterLimit === 'number' ? opts.parameterLimit : defaults.parameterLimit, | ||||||||
parseArrays: opts.parseArrays !== false, | ||||||||
plainObjects: typeof opts.plainObjects === 'boolean' ? opts.plainObjects : defaults.plainObjects, | ||||||||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -17,6 +17,18 @@ var arrayPrefixGenerators = { | |||||
} | ||||||
}; | ||||||
|
||||||
var objectPrefixGenerators = { | ||||||
brackets: function brackets(prefix, key) { | ||||||
return prefix + '[' + key + ']'; | ||||||
}, | ||||||
curly: function curly(prefix, key) { | ||||||
return prefix + '{' + key + '}'; | ||||||
}, | ||||||
dots: function dots(prefix, key) { | ||||||
return prefix + '.' + key; | ||||||
} | ||||||
}; | ||||||
|
||||||
var isArray = Array.isArray; | ||||||
var push = Array.prototype.push; | ||||||
var pushToArray = function (arr, valueOrArray) { | ||||||
|
@@ -28,6 +40,7 @@ var toISO = Date.prototype.toISOString; | |||||
var defaultFormat = formats['default']; | ||||||
var defaults = { | ||||||
addQueryPrefix: false, | ||||||
// deprecated | ||||||
allowDots: false, | ||||||
charset: 'utf-8', | ||||||
charsetSentinel: false, | ||||||
|
@@ -58,12 +71,12 @@ var stringify = function stringify( | |||||
object, | ||||||
prefix, | ||||||
generateArrayPrefix, | ||||||
generateObjectPrefix, | ||||||
strictNullHandling, | ||||||
skipNulls, | ||||||
encoder, | ||||||
filter, | ||||||
sort, | ||||||
allowDots, | ||||||
serializeDate, | ||||||
formatter, | ||||||
encodeValuesOnly, | ||||||
|
@@ -120,12 +133,12 @@ var stringify = function stringify( | |||||
obj[key], | ||||||
typeof generateArrayPrefix === 'function' ? generateArrayPrefix(prefix, key) : prefix, | ||||||
generateArrayPrefix, | ||||||
generateObjectPrefix, | ||||||
strictNullHandling, | ||||||
skipNulls, | ||||||
encoder, | ||||||
filter, | ||||||
sort, | ||||||
allowDots, | ||||||
serializeDate, | ||||||
formatter, | ||||||
encodeValuesOnly, | ||||||
|
@@ -134,14 +147,14 @@ var stringify = function stringify( | |||||
} else { | ||||||
pushToArray(values, stringify( | ||||||
obj[key], | ||||||
prefix + (allowDots ? '.' + key : '[' + key + ']'), | ||||||
generateObjectPrefix(prefix, key), | ||||||
generateArrayPrefix, | ||||||
generateObjectPrefix, | ||||||
strictNullHandling, | ||||||
skipNulls, | ||||||
encoder, | ||||||
filter, | ||||||
sort, | ||||||
allowDots, | ||||||
serializeDate, | ||||||
formatter, | ||||||
encodeValuesOnly, | ||||||
|
@@ -220,16 +233,27 @@ module.exports = function (object, opts) { | |||||
return ''; | ||||||
} | ||||||
|
||||||
var arrayFormat; | ||||||
if (opts && opts.arrayFormat in arrayPrefixGenerators) { | ||||||
arrayFormat = opts.arrayFormat; | ||||||
var generateArrayPrefix; | ||||||
if (opts && typeof opts.arrayFormat === 'function') { | ||||||
generateArrayPrefix = opts.arrayFormat; | ||||||
} else if (opts && opts.arrayFormat in arrayPrefixGenerators) { | ||||||
generateArrayPrefix = arrayPrefixGenerators[opts.arrayFormat]; | ||||||
} else if (opts && 'indices' in opts) { | ||||||
arrayFormat = opts.indices ? 'indices' : 'repeat'; | ||||||
generateArrayPrefix = arrayPrefixGenerators[opts.indices ? 'indices' : 'repeat']; | ||||||
} else { | ||||||
arrayFormat = 'indices'; | ||||||
generateArrayPrefix = arrayPrefixGenerators.indices; | ||||||
} | ||||||
|
||||||
var generateArrayPrefix = arrayPrefixGenerators[arrayFormat]; | ||||||
var generateObjectPrefix; | ||||||
if (opts && typeof opts.objectFormat === 'function') { | ||||||
generateObjectPrefix = opts.objectFormat; | ||||||
} else if (opts && opts.objectFormat in objectPrefixGenerators) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
generateObjectPrefix = objectPrefixGenerators[opts.objectFormat]; | ||||||
} else if (opts && opts.allowDots) { | ||||||
generateObjectPrefix = objectPrefixGenerators.dots; | ||||||
} else { | ||||||
generateObjectPrefix = objectPrefixGenerators.brackets; | ||||||
} | ||||||
Comment on lines
+252
to
+256
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this seems like it should be handled by the option normalization and the default? |
||||||
|
||||||
if (!objKeys) { | ||||||
objKeys = Object.keys(obj); | ||||||
|
@@ -249,12 +273,12 @@ module.exports = function (object, opts) { | |||||
obj[key], | ||||||
key, | ||||||
generateArrayPrefix, | ||||||
generateObjectPrefix, | ||||||
options.strictNullHandling, | ||||||
options.skipNulls, | ||||||
options.encode ? options.encoder : null, | ||||||
options.filter, | ||||||
options.sort, | ||||||
options.allowDots, | ||||||
options.serializeDate, | ||||||
options.formatter, | ||||||
options.encodeValuesOnly, | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,36 +40,81 @@ test('parse()', function (t) { | |
st.end(); | ||
}); | ||
|
||
t.test('arrayFormat: indices allows only indexed arrays', function (st) { | ||
t.test('arrayFormat: indices allows indexed arrays', function (st) { | ||
st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'indices' }), { a: ['b', 'c'] }); | ||
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'indices' }), { a: ['b', 'c'] }); | ||
st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'indices' }), { a: 'b,c' }); | ||
st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'indices' }), { a: ['b', 'c'] }); | ||
st.end(); | ||
}); | ||
|
||
t.test('arrayFormat: comma allows only comma-separated arrays', function (st) { | ||
t.test('arrayFormat: comma allows comma-separated arrays', function (st) { | ||
st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] }); | ||
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] }); | ||
st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'comma' }), { a: 'b,c' }); | ||
st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'comma' }), { a: ['b', 'c'] }); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this shouldn't change, or else it's a breaking change |
||
st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] }); | ||
st.end(); | ||
}); | ||
|
||
t.test('arrayFormat: repeat allows only repeated values', function (st) { | ||
t.test('arrayFormat: repeat allows repeated values', function (st) { | ||
st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'repeat' }), { a: ['b', 'c'] }); | ||
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'repeat' }), { a: ['b', 'c'] }); | ||
st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'repeat' }), { a: 'b,c' }); | ||
st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'repeat' }), { a: ['b', 'c'] }); | ||
st.end(); | ||
}); | ||
|
||
t.test('arrayFormat: function(){} allows custom keys for arrays', function (st) { | ||
st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: function (key) { return key.replace(/-([^-[]+)/g, '[$1]'); } }), { a: ['b', 'c'] }); | ||
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: function (key) { return key.replace(/-([^-[]+)/g, '[$1]'); } }), { a: ['b', 'c'] }); | ||
st.deepEqual(qs.parse('a=b,c', { arrayFormat: function (key) { return key.replace(/-([^-[]+)/g, '[$1]'); } }), { a: 'b,c' }); | ||
st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: function (key) { return key.replace(/-([^-[]+)/g, '[$1]'); } }), { a: ['b', 'c'] }); | ||
st.deepEqual(qs.parse('a-0=b&a-1=c', { arrayFormat: function (key) { return key.replace(/-([^-[]+)/g, '[$1]'); } }), { a: ['b', 'c'] }); | ||
st.end(); | ||
}); | ||
|
||
t.test('allows enabling dot notation', function (st) { | ||
st.deepEqual(qs.parse('a.b=c'), { 'a.b': 'c' }); | ||
st.deepEqual(qs.parse('a.b=c', { allowDots: true }), { a: { b: 'c' } }); | ||
st.end(); | ||
}); | ||
|
||
t.test('objectFormat: brackets allows keys with brackets', function (st) { | ||
st.deepEqual(qs.parse('a[b]=c&a[d]=e'), { a: { b: 'c', d: 'e' } }); | ||
st.deepEqual(qs.parse('a[b]=c&a[d]=e', { objectFormat: 'brackets' }), { a: { b: 'c', d: 'e' } }); | ||
st.deepEqual(qs.parse('a.b=c&a.d=e', { objectFormat: 'brackets' }), { 'a.b': 'c', 'a.d': 'e' }); | ||
st.deepEqual(qs.parse('a{b}=c&a{d}=e', { objectFormat: 'brackets' }), { 'a{b}': 'c', 'a{d}': 'e' }); | ||
st.deepEqual(qs.parse('a/b=c&a/d=e', { objectFormat: 'brackets' }), { 'a/b': 'c', 'a/d': 'e' }); | ||
st.end(); | ||
}); | ||
|
||
t.test('objectFormat: dots allows keys with dots', function (st) { | ||
st.deepEqual(qs.parse('a.b=c&a.d=e'), { 'a.b': 'c', 'a.d': 'e' }); | ||
st.deepEqual(qs.parse('a[b]=c&a[d]=e', { objectFormat: 'dots' }), { a: { b: 'c', d: 'e' } }); | ||
st.deepEqual(qs.parse('a.b=c&a.d=e', { objectFormat: 'dots' }), { a: { b: 'c', d: 'e' } }); | ||
st.deepEqual(qs.parse('a{b}=c&a{d}=e', { objectFormat: 'dots' }), { 'a{b}': 'c', 'a{d}': 'e' }); | ||
st.deepEqual(qs.parse('a/b=c&a/d=e', { objectFormat: 'dots' }), { 'a/b': 'c', 'a/d': 'e' }); | ||
st.end(); | ||
}); | ||
|
||
t.test('objectFormat: curly allows keys with curly brackets', function (st) { | ||
st.deepEqual(qs.parse('a{b}=c&a{d}=e'), { 'a{b}': 'c', 'a{d}': 'e' }); | ||
st.deepEqual(qs.parse('a[b]=c&a[d]=e', { objectFormat: 'curly' }), { a: { b: 'c', d: 'e' } }); | ||
st.deepEqual(qs.parse('a.b=c&a.d=e', { objectFormat: 'curly' }), { 'a.b': 'c', 'a.d': 'e' }); | ||
st.deepEqual(qs.parse('a{b}=c&a{d}=e', { objectFormat: 'curly' }), { a: { b: 'c', d: 'e' } }); | ||
st.deepEqual(qs.parse('a/b=c&a/d=e', { objectFormat: 'curly' }), { 'a/b': 'c', 'a/d': 'e' }); | ||
st.end(); | ||
}); | ||
|
||
t.test('objectFormat: function allows keys with custom format', function (st) { | ||
st.deepEqual(qs.parse('a/b=c&a/d=e'), { 'a/b': 'c', 'a/d': 'e' }); | ||
st.deepEqual(qs.parse('a[b]=c&a[d]=e', { objectFormat: function (key) { return key.replace(/\/([^.[]+)/g, '[$1]'); } }), { a: { b: 'c', d: 'e' } }); | ||
st.deepEqual(qs.parse('a.b=c&a.d=e', { objectFormat: function (key) { return key.replace(/\/([^.[]+)/g, '[$1]'); } }), { 'a.b': 'c', 'a.d': 'e' }); | ||
st.deepEqual(qs.parse('a{b}=c&a{d}=e', { objectFormat: function (key) { return key.replace(/\/([^.[]+)/g, '[$1]'); } }), { 'a{b}': 'c', 'a{d}': 'e' }); | ||
st.deepEqual(qs.parse('a/b=c&a/d=e', { objectFormat: function (key) { return key.replace(/\/([^.[]+)/g, '[$1]'); } }), { a: { b: 'c', d: 'e' } }); | ||
st.end(); | ||
}); | ||
|
||
t.deepEqual(qs.parse('a[b]=c'), { a: { b: 'c' } }, 'parses a single nested string'); | ||
t.deepEqual(qs.parse('a[b][c]=d'), { a: { b: { c: 'd' } } }, 'parses a double nested string'); | ||
t.deepEqual( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is an extremely confusing and strange notation i've never seen anywhere else; i'm fine adding a function form so someone can do this themselves, but i'd need a lot of convincing that this format deserves first-class support.