diff --git a/dist/jquery.serialize-object.min.js b/dist/jquery.serialize-object.min.js index b7ebaf7..731b872 100644 --- a/dist/jquery.serialize-object.min.js +++ b/dist/jquery.serialize-object.min.js @@ -5,4 +5,4 @@ * @license BSD * @version 2.4.3 */ -!function(e,r){if("function"==typeof define&&define.amd)define(["exports","jquery"],function(e,i){return r(e,i)});else if("undefined"!=typeof exports){var i=require("jquery");r(exports,i)}else r(e,e.jQuery||e.Zepto||e.ender||e.$)}(this,function(e,r){function i(e,i){function n(e,r,i){return e[r]=i,e}function a(e,r){for(var i,a=e.match(t.key);void 0!==(i=a.pop());)if(t.push.test(i)){var o=s(e.replace(/\[\]$/,""));r=n([],o,r)}else t.fixed.test(i)?r=n([],i,r):t.named.test(i)&&(r=n({},i,r));return r}function s(e){return void 0===h[e]&&(h[e]=0),h[e]++}function o(e){switch(r('[name="'+e.name+'"]',i).attr("type")){case"checkbox":return"on"===e.value?!0:e.value;default:return e.value}}function u(r){if(!t.validate.test(r.name))return this;var i=a(r.name,o(r));return c=e.extend(!0,c,i),this}function f(r){if(!e.isArray(r))throw new Error("formSerializer.addPairs expects an Array");for(var i=0,t=r.length;t>i;i++)this.addPair(r[i]);return this}function d(){return c}function l(){return JSON.stringify(d())}var c={},h={};this.addPair=u,this.addPairs=f,this.serialize=d,this.serializeJSON=l}var t={validate:/^[a-z_][a-z0-9_]*(?:\[(?:\d*|[a-z0-9_]+)\])*$/i,key:/[a-z0-9_]+|(?=\[\])/gi,push:/^$/,fixed:/^\d+$/,named:/^[a-z0-9_]+$/i};return i.patterns=t,i.serializeObject=function(){return this.length>1?new Error("jquery-serialize-object can only serialize one form at a time"):new i(r,this).addPairs(this.serializeArray()).serialize()},i.serializeJSON=function(){return this.length>1?new Error("jquery-serialize-object can only serialize one form at a time"):new i(r,this).addPairs(this.serializeArray()).serializeJSON()},"undefined"!=typeof r.fn&&(r.fn.serializeObject=i.serializeObject,r.fn.serializeJSON=i.serializeJSON),e.FormSerializer=i,i}); \ No newline at end of file +!function(e,i){if("function"==typeof define&&define.amd)define(["exports","jquery"],function(e,r){return i(e,r)});else if("undefined"!=typeof exports){var r=require("jquery");i(exports,r)}else i(e,e.jQuery||e.Zepto||e.ender||e.$)}(this,function(e,i){function r(e,r){function n(e,i,r){return e[i]=r,e}function a(e,i){for(var r,a=e.match(t.key),o="";void 0!==(r=a.pop());)if(t.push.test(r)){var f=s(a.join("."),o);i=n([],f,i),o+="["+f+"]"}else t.fixed.test(r)?(i=n([],r,i),o+="["+r+"]"):t.named.test(r)&&(i=n({},r,i),o+=r);return i}function s(e,i){if(z[e]=z[e]||{size:0,keys_set:{}},z[e].keys_set[i]){z[e].size++,z[e].keys_set={};for(var r in z)r!=e&&0==r.indexOf(e)&&(z[r]=void 0)}return z[e].keys_set[i]=!0,z[e].size}function o(e){switch(i('[name="'+e.name+'"]',r).attr("type")){case"checkbox":return"on"===e.value?!0:e.value;default:return e.value}}function f(i){if(!t.validate.test(i.name))return this;var r=a(i.name,o(i));return c=e.extend(!0,c,r),this}function u(i){if(!e.isArray(i))throw new Error("formSerializer.addPairs expects an Array");for(var r=0,t=i.length;t>r;r++)this.addPair(i[r]);return this}function d(){return c}function l(){return JSON.stringify(d())}var c={},z={};this.addPair=f,this.addPairs=u,this.serialize=d,this.serializeJSON=l}var t={validate:/^[a-z_][a-z0-9_]*(?:\[(?:\d*|[a-z0-9_]+)\])*$/i,key:/[a-z0-9_]+|(?=\[\])/gi,push:/^$/,fixed:/^\d+$/,named:/^[a-z0-9_]+$/i};return r.patterns=t,r.serializeObject=function(){return this.length>1?new Error("jquery-serialize-object can only serialize one form at a time"):new r(i,this).addPairs(this.serializeArray()).serialize()},r.serializeJSON=function(){return this.length>1?new Error("jquery-serialize-object can only serialize one form at a time"):new r(i,this).addPairs(this.serializeArray()).serializeJSON()},"undefined"!=typeof i.fn&&(i.fn.serializeObject=r.serializeObject,i.fn.serializeJSON=r.serializeJSON),e.FormSerializer=r,r}); \ No newline at end of file diff --git a/jquery.serialize-object.js b/jquery.serialize-object.js index 85c09e6..f790d49 100644 --- a/jquery.serialize-object.js +++ b/jquery.serialize-object.js @@ -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) { diff --git a/test/integration/encode-test.js b/test/integration/encode-test.js index a306dff..1834abd 100644 --- a/test/integration/encode-test.js +++ b/test/integration/encode-test.js @@ -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 = $('
'); + assert.deepEqual($form.serializeObject(), {a: ["c"] }); + }); + + it("checkbox inputs as nested arrays don't break", function() { + var $form = $('
'); + assert.deepEqual($form.serializeObject(), {a: [ { b: ["c"]}]}); + }); + + it("crazy nested arrays don't break", function() { + var $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 = $('
'); + assert.deepEqual($form.serializeObject(), {a: [ { b: ["c"], d: 'e' } ]}); + }); + it("checkbox inputs as booleans if value is 'on'", function() { var $form = $('
'); assert.deepEqual($form.serializeObject(), {a: true});