diff --git a/README.md b/README.md index 28f5737..38bdcb5 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,14 @@ $.extend(FormSerializer.patterns, { //=> {foo: {bar: "a", bof: "b"}, hello: "world"} ``` +*Parameters* + +* `numkeys` — allow to use number keys instead of fixed numeric array indexes. False by default. + + ```js + $("form").serializeObject({'numkeys':true}); + ``` + Tests ----- @@ -203,4 +211,4 @@ $ npm run-script build [legacy]: https://github.com/macek/jquery-serialize-object/releases/tag/1.0.0 [dash-notation]: https://github.com/macek/jquery-serialize-object/issues/6 [dot-notation]: https://github.com/macek/jquery-serialize-object/issues/4 -[boolean]: https://github.com/macek/jquery-serialize-object/blob/master/test/integration/encode-test.js \ No newline at end of file +[boolean]: https://github.com/macek/jquery-serialize-object/blob/master/test/integration/encode-test.js diff --git a/dist/jquery.serialize-object.min.js b/dist/jquery.serialize-object.min.js index 6153c3b..d8b9800 100644 --- a/dist/jquery.serialize-object.min.js +++ b/dist/jquery.serialize-object.min.js @@ -5,4 +5,4 @@ * @license BSD * @version 2.5.0 */ -!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);void 0!==(r=a.pop());)if(t.push.test(r)){var u=s(e.replace(/\[\]$/,""));i=n([],u,i)}else t.fixed.test(r)?i=n([],r,i):t.named.test(r)&&(i=n({},r,i));return i}function s(e){return void 0===h[e]&&(h[e]=0),h[e]++}function u(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,u(i));return l=e.extend(!0,l,r),this}function d(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 o(){return l}function c(){return JSON.stringify(o())}var l={},h={};this.addPair=f,this.addPairs=d,this.serialize=o,this.serializeJSON=c}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 new r(i,this).addPairs(this.serializeArray()).serialize()},r.serializeJSON=function(){return 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}); +!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 t(e,i,r){return e[i]=r,e}function s(e,i){for(var r,s=e.match(n.key);void 0!==(r=s.pop());)if(n.push.test(r)){var f=u(e.replace(/\[\]$/,""));i=t([],f,i)}else if(n.fixed.test(r)){var o=[];a&&(o={}),i=t(o,r,i)}else n.named.test(r)&&(i=t({},r,i));return i}function u(e){return void 0===z[e]&&(z[e]=0),z[e]++}function f(e){switch(i('[name="'+e.name+'"]',r).attr("type")){case"checkbox":return"on"===e.value?!0:e.value;default:return e.value}}function o(i){if(!n.validate.test(i.name))return this;var r=s(i.name,f(i));return h=e.extend(!0,h,r),this}function d(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 c(){return h}function l(){return JSON.stringify(c())}var h={},z={};this.addPair=o,this.addPairs=d,this.serialize=c,this.serializeJSON=l}function t(e){return e?void(e.numkeys&&e.numkeys===!0&&(a=!0)):!0}var n={validate:/^[a-z_][a-z0-9_]*(?:\[(?:\d*|[a-z0-9_]+)\])*$/i,key:/[a-z0-9_]+|(?=\[\])/gi,push:/^$/,fixed:/^\d+$/,named:/^[a-z0-9_]+$/i},a=!1;return r.patterns=n,r.serializeObject=function(e){return t(e),new r(i,this).addPairs(this.serializeArray()).serialize()},r.serializeJSON=function(e){return t(e),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 6b33f37..37ffca4 100644 --- a/jquery.serialize-object.js +++ b/jquery.serialize-object.js @@ -35,6 +35,9 @@ named: /^[a-z0-9_]+$/i }; + // allow to use number keys instead of fixed numeric array indexes + var numkeys = false; + function FormSerializer(helper, $form) { // private variables @@ -61,7 +64,11 @@ // foo[n] else if (patterns.fixed.test(k)) { - value = build([], k, value); + var base = []; + if (numkeys) { + base = {}; + } + value = build(base, k, value); } // foo; foo[bar] @@ -121,15 +128,24 @@ this.serializeJSON = serializeJSON; } + function parseOpt(opt) { + if (!opt) return true; + if (opt.numkeys && opt.numkeys === true) { + numkeys = true; + } + } + FormSerializer.patterns = patterns; - FormSerializer.serializeObject = function serializeObject() { + FormSerializer.serializeObject = function serializeObject(opt) { + parseOpt(opt); return new FormSerializer($, this). addPairs(this.serializeArray()). serialize(); }; - FormSerializer.serializeJSON = function serializeJSON() { + FormSerializer.serializeJSON = function serializeJSON(opt) { + parseOpt(opt); return new FormSerializer($, this). addPairs(this.serializeArray()). serializeJSON();