forked from marioizquierdo/jquery.serializeJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjquery.serializejson.min.js
More file actions
10 lines (9 loc) · 2.61 KB
/
jquery.serializejson.min.js
File metadata and controls
10 lines (9 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
/*!
SerializeJSON jQuery plugin.
https://github.com/marioizquierdo/jquery.serializeJSON
version 2.1.0 (May, 2014)
Copyright (c) 2014 Mario Izquierdo
Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php)
and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
*/
(function(e){"use strict";e.fn.serializeJSON=function(t){var n,r,i,s,o,u;o=e.serializeJSON;r=this.serializeArray();u=o.optsWithDefaults(t);n={};e.each(r,function(e,t){i=o.splitInputNameIntoKeysArray(t.name);s=o.parseValue(t.value,u);if(u.parseWithFunction)s=u.parseWithFunction(s);o.deepSet(n,i,s,u)});return n};e.serializeJSON={defaultOptions:{parseNumbers:false,parseBooleans:false,parseNulls:false,parseAll:false,parseWithFunction:null,useIntKeysAsArrayIndex:false},optsWithDefaults:function(t){var n,r;if(t==null)t={};n=e.serializeJSON;r=n.optWithDefaults("parseAll",t);return{parseNumbers:r||n.optWithDefaults("parseNumbers",t),parseBooleans:r||n.optWithDefaults("parseBooleans",t),parseNulls:r||n.optWithDefaults("parseNulls",t),parseWithFunction:n.optWithDefaults("parseWithFunction",t),useIntKeysAsArrayIndex:n.optWithDefaults("useIntKeysAsArrayIndex",t)}},optWithDefaults:function(t,n){return n[t]!==false&&(n[t]||e.serializeJSON.defaultOptions[t])},parseValue:function(t,n){var r,i;i=e.serializeJSON;if(n.parseNumbers&&i.isNumeric(t))return Number(t);if(n.parseBooleans&&(t==="true"||t==="false"))return t==="true";if(n.parseNulls&&t=="null")return null;return t},isObject:function(e){return e===Object(e)},isUndefined:function(e){return e===void 0},isValidArrayIndex:function(e){return/^[0-9]+$/.test(String(e))},isNumeric:function(e){return e-parseFloat(e)>=0},splitInputNameIntoKeysArray:function(t){var n,r,i;i=e.serializeJSON;if(i.isUndefined(t)){throw new Error("ArgumentError: param 'name' expected to be a string, found undefined")}n=e.map(t.split("["),function(e){r=e[e.length-1];return r==="]"?e.substring(0,e.length-1):e});if(n[0]===""){n.shift()}return n},deepSet:function(t,n,r,i){var s,o,u,a,f,l;if(i==null)i={};l=e.serializeJSON;if(l.isUndefined(t)){throw new Error("ArgumentError: param 'o' expected to be an object or array, found undefined")}if(!n||n.length===0){throw new Error("ArgumentError: param 'keys' expected to be an array with least one element")}s=n[0];if(n.length===1){if(s===""){t.push(r)}else{t[s]=r}}else{o=n[1];if(s===""){a=t.length-1;f=t[a];if(l.isObject(f)&&(l.isUndefined(f[o])||n.length>2)){s=a}else{s=a+1}}if(l.isUndefined(t[s])){if(o===""){t[s]=[]}else if(i.useIntKeysAsArrayIndex&&l.isValidArrayIndex(o)){t[s]=[]}else{t[s]={}}}u=n.slice(1);l.deepSet(t[s],u,r,i)}}}})(window.jQuery||window.Zepto||window.$)