forked from marioizquierdo/jquery.serializeJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.serializejson.min.js
10 lines (9 loc) · 4.19 KB
/
jquery.serializejson.min.js
1
2
3
4
5
6
7
8
9
10
/*!
SerializeJSON jQuery plugin.
https://github.com/marioizquierdo/jquery.serializeJSON
version 3.1.0 (Jan, 2018)
Copyright (c) 2012-2018 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){if("function"==typeof define&&define.amd)define(["jquery"],e);else if("object"==typeof exports){var n=require("jquery");module.exports=e(n)}else e(window.jQuery||window.Zepto||window.$)}(function(e){"use strict";e.fn.serializeJSON=function(n){var r=e.serializeJSON,t=this,a=r.setupOpts(n),i=e.extend({},a.defaultTypes,a.customTypes),u=t.serializeArray();r.readCheckboxUncheckedValues(u,a,t);var s={};return e.each(u,function(e,n){var u=n.name,l=n.value,o=u,p=r.attrFromInputWithName(t,u,"data-value-type");if(!p&&!a.disableColonTypes){var f=r.splitType(u);o=f[0],p=f[1]}if("skip"!==p){p||(p=a.defaultType);var d=r.applyTypeFunc(u,l,p,i);if(d||!r.shouldSkipFalsy(t,u,o,p,a)){var c=r.splitInputNameIntoKeysArray(o);r.deepSet(s,c,d,a)}}}),s},e.serializeJSON={defaultOptions:{},defaultBaseOptions:{checkboxUncheckedValue:void 0,useIntKeysAsArrayIndex:!1,skipFalsyValuesForTypes:[],skipFalsyValuesForFields:[],disableColonTypes:!1,customTypes:{},defaultTypes:{string:function(e){return String(e)},number:function(e){return Number(e)},boolean:function(e){return-1===["false","null","undefined","","0"].indexOf(e)},null:function(e){return-1===["false","null","undefined","","0"].indexOf(e)?e:null},array:function(e){return JSON.parse(e)},object:function(e){return JSON.parse(e)},skip:null},defaultType:"string"},setupOpts:function(n){null==n&&(n={});var r=e.serializeJSON,t=["checkboxUncheckedValue","useIntKeysAsArrayIndex","skipFalsyValuesForTypes","skipFalsyValuesForFields","disableColonTypes","customTypes","defaultTypes","defaultType"];for(var a in n)if(-1===t.indexOf(a))throw new Error("serializeJSON ERROR: invalid option '"+a+"'. Please use one of "+t.join(", "));return e.extend({},r.defaultBaseOptions,r.defaultOptions,n)},applyTypeFunc:function(e,r,t,a){var i=a[t];if(!i)throw new Error("serializeJSON ERROR: Invalid type "+t+" found in input name '"+e+"', please use one of "+n(a).join(", "));return i(r)},readCheckboxUncheckedValues:function(n,r,t){null==r&&(r={});var a="input[type=checkbox][name]:not(:checked):not([disabled])";t.find(a).add(t.filter(a)).each(function(t,a){var i=e(a).attr("data-unchecked-value");if(null==i&&(i=r.checkboxUncheckedValue),null!=i){if(a.name&&-1!==a.name.indexOf("[]["))throw new Error("serializeJSON ERROR: checkbox unchecked values are not supported on nested arrays of objects like '"+a.name+"'. See https://github.com/marioizquierdo/jquery.serializeJSON/issues/67");n.push({name:a.name,value:i})}})},splitType:function(e){var n=e.split(":");if(n.length>1){var r=n.pop();return[n.join(":"),r]}return[e,""]},shouldSkipFalsy:function(n,r,t,a,i){var u=e.serializeJSON.attrFromInputWithName(n,r,"data-skip-falsy");if(null!=u)return"false"!==u;var s=i.skipFalsyValuesForFields;if(s&&(-1!==s.indexOf(t)||-1!==s.indexOf(r)))return!0;var l=i.skipFalsyValuesForTypes;return null==a&&(a="string"),!(!l||-1===l.indexOf(a))},attrFromInputWithName:function(e,n,r){var t='[name="'+n.replace(/(:|\.|\[|\]|\s)/g,"\\$1")+'"]';return e.find(t).add(e.filter(t)).attr(r)},splitInputNameIntoKeysArray:function(n){var r=n.split("[");return""===(r=e.map(r,function(e){return e.replace(/\]/g,"")}))[0]&&r.shift(),r},deepSet:function(n,i,u,s){null==s&&(s={});var l=e.serializeJSON;if(t(n))throw new Error("ArgumentError: param 'o' expected to be an object or array, found undefined");if(!i||0===i.length)throw new Error("ArgumentError: param 'keys' expected to be an array with least one element");var o=i[0];if(1===i.length)""===o?n.push(u):n[o]=u;else{var p=i[1];if(""===o){var f=n.length-1,d=n[f];o=r(d)&&(t(d[p])||i.length>2)?f:f+1}""===p?!t(n[o])&&e.isArray(n[o])||(n[o]=[]):s.useIntKeysAsArrayIndex&&a(p)?!t(n[o])&&e.isArray(n[o])||(n[o]=[]):!t(n[o])&&r(n[o])||(n[o]={});var c=i.slice(1);l.deepSet(n[o],c,u,s)}}};var n=function(e){if(Object.keys)return Object.keys(e);var n,r=[];for(n in e)r.push(n);return r},r=function(e){return e===Object(e)},t=function(e){return void 0===e},a=function(e){return/^[0-9]+$/.test(String(e))}});