Skip to content

Commit

Permalink
properly handled nested array
Browse files Browse the repository at this point in the history
  • Loading branch information
layerssss committed Nov 27, 2014
1 parent efaebf6 commit 7480d4b
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dist/jquery.serialize-object.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 20 additions & 7 deletions jquery.serialize-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,36 +48,49 @@
}

function makeObject(root, value) {

var keys = root.match(patterns.key), k;
var keys = root.match(patterns.key), k, key_pushes = '';

// nest, nest, ..., nest
while ((k = keys.pop()) !== undefined) {
// foo[]
if (patterns.push.test(k)) {
var idx = incrementPush(root.replace(/\[\]$/, ''));
var idx = incrementPush(keys.join('.'), key_pushes);
value = build([], idx, value);
key_pushes += '[' + idx +']';
}

// foo[n]
else if (patterns.fixed.test(k)) {
value = build([], k, value);
key_pushes += '[' + k + ']';
}

// foo; foo[bar]
else if (patterns.named.test(k)) {
value = build({}, k, value);
key_pushes += k;
}
}

return value;
}

function incrementPush(key) {
if (pushes[key] === undefined) {
pushes[key] = 0;
function incrementPush(path, key) {
pushes[path] = pushes[path] || {
size: 0,
keys_set: {}
};
if (pushes[path].keys_set[key]) {
pushes[path].size ++;
pushes[path].keys_set = {}
for (var sub_path in pushes){
if (sub_path != path && 0 == sub_path.indexOf(path)){
pushes[sub_path] = undefined;
}
}
}
return pushes[key]++;
pushes[path].keys_set[key] = true
return pushes[path].size;
}

function encode(pair) {
Expand Down
20 changes: 20 additions & 0 deletions test/integration/encode-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,26 @@ describe("encode", function() {
assert.deepEqual($form.serializeObject(), {a: {b: {c: true}}});
});

it("checkbox inputs as array don't break", function() {
var $form = $('<form><input type="checkbox" name="a[]" value="c" checked></form>');
assert.deepEqual($form.serializeObject(), {a: ["c"] });
});

it("checkbox inputs as nested arrays don't break", function() {
var $form = $('<form><input type="checkbox" name="a[][b][]" value="c" checked><input type="checkbox" name="a[][b][]" value="d"></form>');
assert.deepEqual($form.serializeObject(), {a: [ { b: ["c"]}]});
});

it("crazy nested arrays don't break", function() {
var $form = $('<form><input name="a[][b]" value="1"><input name="a[][c][]" value="2"><input name="a[][c][]" value="3"><input name="a[][b]" value="4"><input name="a[][c][]" value="5"><input name="a[][c][]" value="6"></form>');
assert.deepEqual($form.serializeObject(), {a: [ { b: "1", c: ["2", "3"] }, { b: "4", c: ["5", "6"] } ]});
});

it("checkbox inputs as nested arrays with a sibling don't break", function() {
var $form = $('<form><input type="checkbox" name="a[][b][]" value="c" checked><input name="a[][d]" value="e" checked></form>');
assert.deepEqual($form.serializeObject(), {a: [ { b: ["c"], d: 'e' } ]});
});

it("checkbox inputs as booleans if value is 'on'", function() {
var $form = $('<form><input type="checkbox" name="a" value="on" checked></form>');
assert.deepEqual($form.serializeObject(), {a: true});
Expand Down

0 comments on commit 7480d4b

Please sign in to comment.