-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inputtoarray.min.js
1 lines (1 loc) · 2.85 KB
/
inputtoarray.min.js
1
function(e){"use strict";function t(){this.set_defaults=function(){this.key=[],this.array=null,this.element=null,this.val_req=!1,this.elem_key=null},this.set_key=function(t){var n=this;""!==t&&"object"==typeof t&&e.each(t,function(e,t){n.key.push(t)})},this.render_array=function(){var t=this;return e.each(t.key,function(e,n){t.array=t.hook_array(e,n,t.array)}),t.array},this.element_array=function(){var e=this.element.attr(this.elem_key);if(void 0!==e||""!==e||!1!==e){var t=null;if(null!==(t=/\[]/g.exec(e))&&1===t.length&&"[]"===t[0])return!0}return!1},this.get_value=function(){var e=this,t=null;return t=e.element.is("input[type=checkbox]")||e.element.is("input[type=radio]")?!!e.element.is(":checked")&&e.element.val():e.element.is("textarea")?e.element.html():e.element.val(),!0===this.element_array()&&!1!==t?[t]:t},this.set_value=function(e,t,n,r){var i=this.get_value();return null===e[t]&&(e[t]={}),!1!==i&&(e[t][n]=this.key.length-1===r&&!0===this.val_req?i:null),e},this.hook_array=function(t,n,r){void 0===r&&(r=this.array);var i=this;return null===r?(r={})[n]=null:"object"!=typeof r&&"array"!=typeof r||e.each(r,function(e,a){null===a?r=i.set_value(r,e,n,t):"object"!=typeof a&&"array"!=typeof a||(r[e]=i.hook_array(t,n,r[e]))}),r},this.run_regex=function(e){for(var t=/\w+(?!\[)[\w&.\-]+\w+/g,n=null,r=this;null!==(n=t.exec(e));)n.index===t.lastIndex&&t.lastIndex++,r.set_key(n);return!0},this.get=function(e,t,n,r){var i=this;i.element=t,i.val_req=n,i.elem_key=r,this.run_regex(e);var a=this.render_array();return this.set_defaults(),a},this.get_key=function(e){return this.run_regex(e),void 0!==this.key[0]?this.key[0]:null},this.array_merge=function(){var e,t=Array.prototype.slice.call(arguments),n=t.length,r={},i="",a=0,_=0,s=0,f=0,o=Object.prototype.toString,l=!0;for(s=0;s<n;s++)if("[object Array]"!==o.call(t[s])){l=!1;break}if(l){for(l=[],s=0;s<n;s++)l=l.concat(t[s]);return l}for(s=0,f=0;s<n;s++)if(e=t[s],"[object Array]"===o.call(e))for(_=0,a=e.length;_<a;_++)r[f++]=e[_];else for(i in e)e.hasOwnProperty(i)&&(parseInt(i,10)+""===i?r[f++]=e[i]:r[i]=e[i]);return r},this.array_merge_recursive=function(e,t){var n="";if(e&&"[object Array]"===Object.prototype.toString.call(e)&&t&&"[object Array]"===Object.prototype.toString.call(t))for(n in t)e.push(t[n]);else if(e&&e instanceof Object&&t&&t instanceof Object)for(n in t)n in e?"object"==typeof e[n]&&"object"==typeof t?e[n]=this.array_merge_recursive(e[n],t[n]):"array"==typeof e[n]&&"array"==typeof t?e[n]=this.array_merge(e[n],t[n]):e[n]=t[n]:e[n]=t[n];return e},this.set_defaults()}e.fn.inputToArray=function(n){var r={},i=e.extend({key:"name",value:!0},n),a=new t;return this.each(function(){var t=e(this).attr(i.key);if(void 0!==t){var n=a.get(t,e(this),i.value,i.key);r=a.array_merge_recursive(n,r)}}),r},e.fn.inputArrayKey=function(n){return void 0===n&&(n="name"),void 0!==(n=e(this).attr(n))&&(new t).get_key(n)}}(jQuery)