-
Notifications
You must be signed in to change notification settings - Fork 65
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
numeric keys as arrays #14
Comments
Ohh heh, here it is! |
Ooh! |
qs.parse('user[0]=tj&user[1]=TJ'); qs.parse('user[1]=tj&user[2]=TJ'); |
@Delapouite hmm tough call, that could be a sparse array as well |
It should come out as a sparse array, I'd think. |
yeah that's what I would expect |
This is related, not sure if you want a separate issue, but it's nice to drop the key all together when the ui allows the user to 'add another' functionality of nested form data. console.log(qs.parse('workout%5Bname%5D=Stud&exercises%5B%5D%5Bsets%5D=1&exercises%5B%5D%5Bname%5D=Push+Up&exercises%5B%5D%5Bsets%5D=3&exercises%5B%5D%5Bname%5D=Pull+Up&exercises%5B%5D%5Bsets%5D=3&exercises%5B%5D%5Bname%5D=Situp+Up'); gives you: { workout: { name: 'Stud' },
exercises: [ '1', 'Push Up', '3', 'Pull Up', '3', 'Situp Up' ] } where (especially peeps used to rails) expect: { workout: { name: 'Stud' },
exercises:
[ { sets: '1', name: 'Push Up' },
{ sets: '3', name: 'Pull Up' },
{ sets: '3', name: 'Situp Up' } ] } |
@troyk agreed, that looks like a bug to me |
@visionmedia I was playing with it last night and gave up, but I'll go check it out again and see if I can muster up a pull request. |
Ok, so after messing with the current implementation, I decided to take a crack at porting Rack's to javascript, as I'm sure the Rack implementation is the right feature set. But it's getting clobbered on these tests: qs.parse('a[>=]=23')
.should.eql({ a: { '>=': '23' }});
qs.parse('a[<=>]==23')
.should.eql({ a: { '<=>': '=23' }});
qs.parse('a[==]=23')
.should.eql({ a: { '==': '23' }}); and a few more. Shouldn't 'a[>=]=23' really be 'a%5B%3E%3D%5D=23' ? If so, I'll go add the encoding to the tests to and keep moving forward. If I'm missing something, just let me know. Below is where I'm at, let me know what you think about proceeding down this path. function normalizeParams(params, name, v) {
var matches = name.match(/^[\[\]]*([^\[\]]+)\]*/);
if (!matches) return;
if ('undefined' == typeof v) v = '';
var k = matches[1]
, after = name.substr(k.length);
if (after == '') {
params[k] = v;
} else if (after == '[]') {
if (!params[k]) params[k] = [];
params[k].push(v);
} else if (matches = after.match(/^\[\]\[([^\[\]]+)\]$/) || after.match(/^\[(\d{1,9})?\](.+)$/)) {
if (!params[k]) params[k] = [];
var child_key = matches[1]
, param_len = params[k].length
, last_val = params[k][param_len - 1];
if ('object' == typeof last_val && !Array.isArray(last_val) && !last_val[child_key]) {
normalizeParams(last_val, child_key, v);
} else if (child_key == String(param_len)) {
params[k].push(v);
} else {
params[k].push(normalizeParams({},child_key,v));
}
} else {
params[k] = normalizeParams(params[k] || {}, after, v);
}
return(params);
}
exports.parse = function(str) {
var params = {}
if (null == str || '' == str) return params;
String(str).split('&').forEach(function(pair){
pair = decodeURIComponent(pair.replace(/\+/g, ' ')).split('=',2);
console.log(pair);
normalizeParams(params,pair[0],pair[1]);
});
return params;
} |
IMO these should stay as objects with string keys, to match Rack/Rails behavior. We're running into an issue using Express to do some proxying of requests from client to Rails app. {"nodes": ["159", "39"]} Whereas Rack/Rails sees the same params as: {"nodes": {"500": "159", "501": "39"}} Not that "because the Ruby community does it" is a great reason, but why break from convention? |
I guess a (gross) workaround is to do something like this: <input type="hidden" name="nodes[_]" value=""> |
currently gives you:
The text was updated successfully, but these errors were encountered: