UNPKG

miniprogram-formdata

Version:
1 lines 1.75 kB
import FilePolyfill from"miniprogram-file";let FormDataPolyfill;if("undefined"==typeof FormData){const t=FilePolyfill,e={blob:t=>"[object Blob]"===Object.prototype.toString.call(t),file:t=>"[object File]"===Object.prototype.toString.call(t),obj:t=>null!==t&&"object"==typeof t},o=function(t,e){if(t.length<e)throw new TypeError(`${e} argument required, but only ${t.length} present.`)},r=function(o,r,l){return e.blob(r)?r=new t([r],l=l||"blob"):e.file(r)&&(l=l||r.name),[o,r]},l=new WeakMap;class i{constructor(t){if(void 0!==t)throw new Error("Failed to construct 'FormData': the 'form' option is unsupported.");l.set(this,[])}append(t,e,i){o(arguments,2),l.get(this).push(r(t,e,i))}set(t,e,i){o(arguments,2);const n=[],s=r(t,e,i);let a=!0;l.get(this).forEach((e=>{e[0]===t?a&&(a=!n.push(s)):n.push(e)})),a&&n.push(s),l.set(this,n)}delete(t){o(arguments,1),t=String(t),l.set(this,l.get(this).filter((e=>e[0]!==t)))}get(t){o(arguments,1);const e=l.get(this);t=String(t);for(let o=0;o<e.length;o++)if(e[o][0]===t)return e[o][1];return null}getAll(t){return o(arguments,1),t=String(t),l.get(this).reduce(((e,o)=>(o[0]===t&&e.push(o[1]),e)),[])}has(t){o(arguments,1),t=String(t);for(let e=0;e<l.get(this).length;e++)if(l.get(this)[e][0]===t)return!0;return!1}*entries(){for(let t=0;t<l.get(this).length;t++)yield l.get(this)[t]}[Symbol.iterator](){return this.entries()}forEach(t,e){o(arguments,1);for(const[o,r]of this)t.call(e,r,o,this)}*keys(){for(const[t]of this)yield t}*values(){for(const[,t]of this)yield t}toString(){return"[object FormData]"}}"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(i.prototype,Symbol.toStringTag,{value:"FormData"}),FormDataPolyfill=i}else FormDataPolyfill=FormData;export default FormDataPolyfill;