-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhtmlFormObjectParserForGoogleAppsScript_js.min.js
1 lines (1 loc) · 2.58 KB
/
htmlFormObjectParserForGoogleAppsScript_js.min.js
1
function ParseFormObjectForGAS(formObj,fields,excludeTypes,includeOrder){return new HtmlFormObjectParserForGoogleAppsScript(formObj,fields,excludeTypes,includeOrder).Do()}!function(r){var HtmlFormObjectParserForGoogleAppsScript;HtmlFormObjectParserForGoogleAppsScript=function(){class HtmlFormObjectParserForGoogleAppsScript{constructor(obj_,fieldsValue_,excludeTypes_,includeOrder_){if("[object HTMLFormElement]"!==obj_.toString())throw new Error("Invalid HTMLFormElement.");if(this.formObj=Array.from(obj_),0===this.formObj.length)throw new Error("No properties in the inputted form object.");if(fieldsValue_&&""!==fieldsValue_){if("string"!=typeof fieldsValue_)throw new Error('Invalid fields. Please set it like "name,type,value,files,checked"')}else fieldsValue_="name,type,value,files,checked";if(excludeTypes_&&""!==excludeTypes_){if("-"===excludeTypes_)excludeTypes_="";else if("string"!=typeof excludeTypes_)throw new Error('Invalid exclude types. Please set it like "submit"')}else excludeTypes_="submit";this.fields=fieldsValue_.split(",").map(g=>g.trim()),this.excludeTypes=excludeTypes_.split(",").map(g=>g.trim()),this.includeOrder=includeOrder_||!1}async Do(){var ar,e,initObj,orderOfFormObject;try{return ar=await Promise.all(this.formObj.map((obj,i)=>new Promise(async(res,rej)=>{var files,htmlObj,temp;for(i in htmlObj={},obj)"function"!=typeof obj[i]&&null!==obj[i]&&(htmlObj[i]=obj[i]);return temp={[obj.name||`noName${i+1}`]:htmlObj},"file"===obj.type&&(files=obj.files,temp[obj.name].files={},temp[obj.name].files=await(async files=>await Promise.all([...files].map(file=>new Promise((resolve,reject)=>{var fr;return(fr=new FileReader).onload=f=>resolve({filename:file.name,mimeType:file.type,bytes:[...new Int8Array(f.target.result)]}),fr.onerror=err=>reject(err),fr.readAsArrayBuffer(file)}))))(files).catch(err=>rej(err))),res(temp)}))),initObj={},this.includeOrder&&(initObj={orderOfFormObject:orderOfFormObject=ar.flatMap(oo=>{var key;return key=Object.keys(oo)[0],this.excludeTypes.includes(oo[key].type)?[]:[key]})}),ar.reduce((oo,ee)=>{var k,v;return[[k,v]]=Object.entries(ee),this.fields.length>0&&Object.keys(v).forEach(ff=>{if(!this.fields.includes(ff)&&!this.fields.includes("*"))return delete v[ff]}),this.excludeTypes.length>0&&this.excludeTypes.some(ff=>v.type===ff)?oo:Object.assign(oo,{[k]:oo[k]?oo[k].concat(v):[v]})},initObj)}catch(error){throw new Error(e=error)}}}return HtmlFormObjectParserForGoogleAppsScript.name="HtmlFormObjectParserForGoogleAppsScript_js",HtmlFormObjectParserForGoogleAppsScript}.call(this),r.HtmlFormObjectParserForGoogleAppsScript=HtmlFormObjectParserForGoogleAppsScript}(this);