@metronlabs/rx-form-data
Advanced tools
Comparing version 0.0.4 to 0.0.5
@@ -1,1 +0,1 @@ | ||
"use strict";const e=e=>"string"==typeof e,t=t=>Boolean(e(t)&&t.trim().length),n=e=>"[object RegExp]"===Object.prototype.toString.call(e),r={_is:"None"};function a(e){return{_is:"Some",value:e}}function c(e){return"Some"===e._is}function i(e){return"None"===e._is}function s(e,t){return n=>i(n)?e():t(n.value)}function o(e,t,n){return i(e)&&i(t)?r:c(e)&&c(t)?n?a(n(e.value,t.value)):t:c(e)&&i(t)?e:i(e)&&c(t)?t:r}function u(e){return null===e||void 0===e?r:a(e)}function l(e){return isNaN(Number(e))?r:a(Number(e))}function f(n,c){return e(n)?c?t(n)?a(n):r:a(n):r}const m="input:text",d="input:number",E="input:email",v="input:password",p="input:checkbox",R="input:radio",b="input:color",S="input:date",y="input:datetime-local",h="input:file",A="input:hidden",L="input:month",w="input:range",g="input:search",O="input:tel",T="input:time",I="input:url",j="input:week",F="textarea:textarea",_="select:select-one",N="select:select-multiple",D={FOCUS:"focus",INPUT:"input",CHANGE:"change",BLUR:"blur",SUBMIT:"submit",RESET:"reset"},U="REGISTER_ALL",k="UNREGISTER_ALL",M="REGISTER",z="UNREGISTER",$="UPSERT_FIELD",G="DELETE_FIELD",x="CLEAR_FIELDS",C="RESET",H={EMIT_FORM_VALUES:"EMIT_FORM_VALUES"},P="ADD",B="DELETE",V="CLEAR",Y={REGISTER:"REGISTER_FIELDS",REGISTER_ALL:"REGISTER_ALL_FIELDS",UNREGISTER:"UNREGISTER_FIELDS",UNREGISTER_ALL:"UNREGISTER_ALL_FIELDS",DESTROY:"DESTROY_PROGRAM"};function q(e){return e instanceof HTMLFormElement}function J(e){return e instanceof HTMLInputElement}function K(e){return e instanceof HTMLTextAreaElement}function Q(e){return e instanceof HTMLSelectElement}function W(e){return J(e)||Q(e)||K(e)}function X(e){return W(e)?J(e)?function(e){if(!J(e))return r;const n=`${e.tagName.toLowerCase()}:${e.type}`;switch(n){case m:case g:case E:case b:case A:return a(e.value.trim());case v:return a(e.value);case d:case w:return l(Number(e.value.trim()));case I:try{return a(new URL(e.value.trim()).href.trim())}catch(t){return console.warn("[RxFormData] Failed to decode url input field value into URL object",e.value,t),r}case O:return a(e.value.trim().replace(/\D/g,""));case h:return s(()=>r,e=>a(Object.freeze(Array.from(Array(e.length).keys()).reduce((t,n)=>{const r=e.item(n);return r?t.concat(r):t},[]))))(u(e.files));case S:case y:case T:case j:return l(e.valueAsNumber);case L:try{return a(new Date(e.value+"-1").getTime())}catch(e){return console.error("[RxFormData] Failed to decode month input field value into number",e),r}case R:case p:{const c=n.split(":")[1],s=u(e.form);if(i(s))return r;const o=s.value.querySelectorAll(`input[type='${c}'][name='${e.name}']:checked`);return a(Object.freeze(Array.from(o).map((n,r)=>J(n)&&t(n.value)?n.value.trim():`${e.name}[${r}]`)))}default:return r}}(e):Q(e)?function(e){if(!Q(e))return r;switch(`${e.tagName.toLowerCase()}:${e.type}`){case _:case N:return a(Object.freeze(Array.from(e.selectedOptions).map((n,r)=>t(n.value)?n.value.trim():`${e.name}[${r}]`)));default:return r}}(e):K(e)?function(e){return K(e)?a(e.value.trim()):r}(e):r:r}function Z(e){if(e instanceof Error)throw e;const t=new Error("[RxFormData] ERROR!");throw t.details=JSON.stringify(e),t}function ee(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)&&"Object"===e.constructor.name}const te=Object.freeze({$:r,tag:r,name:r,value:r,validity:r,touched:!1,modified:!1,visited:!1});function ne(e){return ee(e)}function re(e,t){const n=s(()=>t,e=>e)(e.$),r=s(()=>t,e=>e)(e.tag),a=s(()=>t,e=>e)(e.name),c=s(()=>t,e=>e)(e.validity);return Object.freeze({$:n,tag:r,name:a,value:(()=>{if(r===t)return t;const n=s(()=>t,e=>e)(e.value);if(n===t)return n;switch(r){case m:case g:case E:case b:case A:case F:case I:case O:case v:return String(n);case d:case w:case S:case y:case T:case j:case L:return Number(n);case h:return Array.isArray(n)?Object.freeze(n.map(e=>Object.freeze(e))):[];case R:case p:case _:case N:return Array.isArray(n)?Object.freeze(n.map(String)):[];default:return t}})(),validity:c,touched:e.touched,modified:e.modified,visited:e.visited})}function ae(e,t){if(i(t.name))return e;const n=t.name.value,r=u(e.get(n)),a=s(()=>t,e=>{return n=e,r=t,Object.freeze({$:o(n.$,r.$),tag:o(n.tag,r.tag),name:o(n.name,r.name),value:o(n.value,r.value),validity:o(n.validity,r.validity,(e,t)=>({...e,...t})),touched:n.touched||r.touched,modified:n.modified||r.modified,visited:n.visited||r.visited});var n,r})(r);return e.set(n,a)}function ce(e,t){return e.clear(),t?new Map(t.entries()):e}function ie(e,n,r){for(let e=0,t=r.length;e<t;e++)n.delete(r[e]);for(const n of e.keys()){r.some(e=>e instanceof RegExp?e.test(n):!!t(e)&&e.trim()===n.trim())&&e.delete(n)}return[n,e]}function se(e,t){const n=new CustomEvent(H.EMIT_FORM_VALUES,{detail:Object.freeze(new Map(t.entries()))});e.dispatchEvent(n)}function oe(r){const a=new Map;if(i(r))return void Z("[RxFormData] invalid form element for form field storage initialization");const c=r.value,o=new Map;o.set(a,new Map(a.entries()));const u=()=>{const e=o.get(a);return e?new Map(e.entries()):new Map},l=new Set,m=(new Map).set(l,l),d=()=>{const e=m.get(l);return e?new Set(e.values()):new Set};return{storage:u,action:(r,E)=>{switch(r){case $:if(ne(E)){const e=s(()=>"",e=>e.trim())(E.name);[...d().keys()].some(r=>t(r)?r.trim()===e.trim():!!n(r)&&r.test(e))&&(o.set(a,ae(u(),E)),se(c,u()))}break;case x:o.set(a,ce(u())),m.set(l,new Set),se(c,u());break;case U:{const e=Array.from(c.elements).reduce((e,t)=>W(t)?e.concat(t.name):e,[]);m.set(l,new Set(e));break}case k:m.set(l,new Set);break;case G:{const n=Array.isArray(E)?E.filter(e=>t(e)||ne(e)):[];n.length&&(o.set(a,function(n,r){return r.reduce((n,r)=>{const a=e(r)?f(r,!0):r.name;return i(a)?n:t(a.value)?(n.delete(a.value.trim()),n):n},n)}(u(),n)),se(c,u()));break}case C:o.set(a,ce(u(),a)),se(c,u());break;case M:{const e=Array.isArray(E)?E.filter(e=>t(e)||n(e)):[];e.length&&m.set(l,function(e,t){for(let n=0,r=t.length;n<r;n++)e.add(t[n]);return e}(d(),e));break}case z:if(E&&ee(E)){const e=Array.isArray(E.use)?E.use.filter(e=>t(e)||n(e)):[];if(e.length){const t=ie(u(),d(),e);m.set(l,t[0]),E.keepvalues||(o.set(a,t[1]),se(c,u()))}}else{const e=Array.isArray(E)?E.filter(e=>t(e)||n(e)):[];if(e.length){const t=ie(u(),d(),e);m.set(l,t[0])}}}}}}function ue(e,t){t($,function(e){const t=u(e.target);if(i(t))return te;if(!W(t.value))return te;const n=t.value,r=f(n.name,!0),c=a(`${n.tagName}:${n.type}`.toLowerCase()),s=X(n),o=[D.CHANGE,D.BLUR].includes(e.type),l=[D.CHANGE].includes(e.type),m=[D.FOCUS,D.BLUR].includes(e.type);return Object.freeze({$:a(n),name:r,tag:c,value:s,validity:a(Object.freeze(n.validity)),touched:o,modified:l,visited:m})}(e))}function le(e,t,n,r){return function(a){if(a instanceof CustomEvent)switch(a.type){case H.EMIT_FORM_VALUES:{const e=r.subscribers(),t=null,a=[...n.storage().entries()].reduce((e,[n,r])=>(e[n]=re(r,t),e),{});for(const t of e)t(a);break}}else switch(a.type){case D.SUBMIT:a.preventDefault(),async function(e,t,n){const r=new FormData(e);new Promise((a,c)=>{Promise.resolve().then(()=>{const e=[...t.entries()].reduce((e,[t,n])=>(e[t]=re(n,null),e),{});return Promise.resolve(n(Object.freeze(e),r))}).then(()=>{console.info(`[RxFormData #${e.id}] form submission handler success`),a()}).catch(t=>{console.error(`[RxFormData #${e.id}] form submission handler error(s)`,t),c(t)})})}(e,n.storage(),t);break;case D.FOCUS:case D.INPUT:case D.CHANGE:case D.BLUR:ue(a,n.action);break;case D.RESET:n.action(C)}}}module.exports=function(e,c){const s=function(e){if(q(e))return a(e);if(i(f(e,!0)))return r;const t=u(document.querySelector("form#"+e.trim()));return i(t)?r:q(t.value)?a(t.value):r}(e);if(i(s))return void Z("Invalid form element. Form id provided did not match any form element in the DOM.");const o=oe(s)||null,l=o?function(e,t,n){const r=new Set;if(i(e))return void Z("[RxFormData] invalid form element for events initialization");const a=new Map;a.set(r,new Set(r.values()));const c=()=>{const e=a.get(r);return e||new Set},s=(e,t)=>{switch(e){case P:t instanceof Function&&a.set(r,new Set(c().values()).add(t));break;case B:if(t instanceof Function){const e=new Set(c().values());e.delete(t),a.set(r,e)}break;case V:a.set(r,new Set)}},o={subscribers:c,action:s},u=e.value,l=le(u,t,n,o);return[...Object.values(D),...Object.values(H)].forEach(e=>{u.addEventListener(e,l,!0)}),{...o,cleanup:()=>{[...Object.values(D),...Object.values(H)].forEach(e=>{u.removeEventListener(e,l,!0)}),s(V),n.action(x)}}}(s,c,o):null;return o&&l?Object.freeze({ACTION_TYPE:Object.freeze(Y),register:Object.freeze(e=>(o.action(M,e),Object.freeze(()=>{o.action(z,e)}))),subscribe:Object.freeze(e=>(l.action(P,e),Object.freeze(()=>{l.action(B,e)}))),dispatch:Object.freeze((r,a)=>{switch(r){case Y.REGISTER_ALL:o.action(U);break;case Y.REGISTER:{const e=Array.isArray(a)?a.reduce((e,r)=>t(r)||n(r)?e.concat(r):e,[]):[];e.length&&o.action(M,e);break}case Y.UNREGISTER_ALL:o.action(k),!(!1!==a)&&o.action(C);break;case Y.UNREGISTER:{const e=Array.isArray(a)?a.reduce((e,r)=>t(r)||n(r)?e.concat(r):e,[]):[];e.length&&o.action(z,e);break}case Y.DESTROY:o.action(C),o.action(k),l.cleanup&&l.cleanup();break;default:return void console.debug(`[RxFormData: #${e}] uknown action dispatched...`,a)}})}):void 0}; | ||
"use strict";const e=e=>"string"==typeof e,t=t=>Boolean(e(t)&&t.trim().length),n=e=>"[object RegExp]"===Object.prototype.toString.call(e),r={_is:"None"};function a(e){return{_is:"Some",value:e}}function c(e){return"Some"===e._is}function i(e){return"None"===e._is}function s(e,t){return n=>i(n)?e():t(n.value)}function o(e,t,n){return i(e)&&i(t)?r:c(e)&&c(t)?n?a(n(e.value,t.value)):t:c(e)&&i(t)?e:i(e)&&c(t)?t:r}function u(e){return null===e||void 0===e?r:a(e)}function l(e){return isNaN(Number(e))?r:a(Number(e))}function E(n,c){return e(n)?c?t(n)?a(n):r:a(n):r}const f={INPUT_TEXT:"input:text",INPUT_NUMBER:"input:number",INPUT_EMAIL:"input:email",INPUT_PASSWORD:"input:password",INPUT_CHECKBOX:"input:checkbox",INPUT_RADIO:"input:radio",INPUT_COLOR:"input:color",INPUT_DATE:"input:date",INPUT_DATETIME_LOCAL:"input:datetime-local",INPUT_FILE:"input:file",INPUT_HIDDEN:"input:hidden",INPUT_MONTH:"input:month",INPUT_RANGE:"input:range",INPUT_SEARCH:"input:search",INPUT_TEL:"input:tel",INPUT_TIME:"input:time",INPUT_URL:"input:url",INPUT_WEEK:"input:week",TEXTAREA:"textarea:textarea",SELECT_SINGLE:"select:select-one",SELECT_MULTIPLE:"select:select-multiple"},T={FOCUS:"focus",INPUT:"input",CHANGE:"change",BLUR:"blur",SUBMIT:"submit",RESET:"reset"},m="REGISTER_ALL",d="UNREGISTER_ALL",I="REGISTER",N="UNREGISTER",_="UPSERT_FIELD",R="DELETE_FIELD",U="CLEAR_FIELDS",v="RESET",L={EMIT_FORM_VALUES:"EMIT_FORM_VALUES"},A="ADD",S="DELETE",b="CLEAR",p={REGISTER:"REGISTER_FIELDS",REGISTER_ALL:"REGISTER_ALL_FIELDS",UNREGISTER:"UNREGISTER_FIELDS",UNREGISTER_ALL:"UNREGISTER_ALL_FIELDS",DESTROY:"DESTROY_PROGRAM"};function P(e){return e instanceof HTMLFormElement}function O(e){return e instanceof HTMLInputElement}function y(e){return e instanceof HTMLTextAreaElement}function h(e){return e instanceof HTMLSelectElement}function w(e){return O(e)||h(e)||y(e)}function D(e){return w(e)?O(e)?function(e){if(!O(e))return r;const n=`${e.tagName.toLowerCase()}:${e.type}`;switch(n){case f.INPUT_TEXT:case f.INPUT_SEARCH:case f.INPUT_EMAIL:case f.INPUT_COLOR:case f.INPUT_HIDDEN:return a(e.value.trim());case f.INPUT_PASSWORD:return a(e.value);case f.INPUT_NUMBER:case f.INPUT_RANGE:return l(Number(e.value.trim()));case f.INPUT_URL:try{return a(new URL(e.value.trim()).href.trim())}catch(t){return console.warn("[RxFormData] Failed to decode url input field value into URL object",e.value,t),r}case f.INPUT_TEL:return a(e.value.trim().replace(/\D/g,""));case f.INPUT_FILE:return s(()=>r,e=>a(Object.freeze(Array.from(Array(e.length).keys()).reduce((t,n)=>{const r=e.item(n);return r?t.concat(r):t},[]))))(u(e.files));case f.INPUT_DATE:case f.INPUT_DATETIME_LOCAL:case f.INPUT_TIME:case f.INPUT_WEEK:return l(e.valueAsNumber);case f.INPUT_MONTH:try{return a(new Date(e.value+"-1").getTime())}catch(e){return console.error("[RxFormData] Failed to decode month input field value into number",e),r}case f.INPUT_RADIO:case f.INPUT_CHECKBOX:{const c=n.split(":")[1],s=u(e.form);if(i(s))return r;const o=s.value.querySelectorAll(`input[type='${c}'][name='${e.name}']:checked`);return a(Object.freeze(Array.from(o).map((n,r)=>O(n)&&t(n.value)?n.value.trim():`${e.name}[${r}]`)))}default:return r}}(e):h(e)?function(e){if(!h(e))return r;switch(`${e.tagName.toLowerCase()}:${e.type}`){case f.SELECT_SINGLE:case f.SELECT_MULTIPLE:return a(Object.freeze(Array.from(e.selectedOptions).map((n,r)=>t(n.value)?n.value.trim():`${e.name}[${r}]`)));default:return r}}(e):y(e)?function(e){return y(e)?a(e.value.trim()):r}(e):r:r}function g(t){return!!e(t)||("number"==typeof t||!(!Array.isArray(t)||!t.every(e)&&!t.every(e=>e instanceof File)))}function M(e){return t(e)&&Object.keys(f).includes(e)}function C(e){if(e instanceof Error)throw e;const t=new Error("[RxFormData] ERROR!");throw t.details=JSON.stringify(e),t}function j(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)&&"Object"===e.constructor.name}const F=Object.freeze({$:r,tag:r,name:r,value:r,validity:r,touched:!1,modified:!1,visited:!1});function k(e){if(!j(e))return!1;const n=Object.keys(F);if(!Object.keys(e).every(e=>n.includes(e)))return!1;const r=(e,n)=>j(e)&&t(e._is)&&["none","some"].includes(e._is)&&("some"!==e._is||n(e.value));return Object.entries(e).reduce((e,[n,a])=>{switch(n){case"$":return e&&r(a,w);case"tag":return e&&r(a,M);case"name":return e&&r(a,t);case"value":return e&&r(a,g);case"validity":return e&&r(a,e=>e instanceof ValidityState);case"touched":case"visited":case"modified":return"boolean"==typeof a;default:return!1}},!0)}function G(e,t){const n=s(()=>t,e=>e)(e.$),r=s(()=>t,e=>e)(e.tag),a=s(()=>t,e=>e)(e.name),c=s(()=>t,e=>e)(e.validity);return Object.freeze({$:n,tag:r,name:a,value:(()=>{if(r===t)return t;const n=s(()=>t,e=>e)(e.value);if(n===t)return n;switch(r){case f.INPUT_TEXT:case f.INPUT_SEARCH:case f.INPUT_EMAIL:case f.INPUT_COLOR:case f.INPUT_HIDDEN:case f.TEXTAREA:case f.INPUT_URL:case f.INPUT_TEL:case f.INPUT_PASSWORD:return String(n);case f.INPUT_NUMBER:case f.INPUT_RANGE:case f.INPUT_DATE:case f.INPUT_DATETIME_LOCAL:case f.INPUT_TIME:case f.INPUT_WEEK:case f.INPUT_MONTH:return Number(n);case f.INPUT_FILE:return Array.isArray(n)?Object.freeze(n.map(e=>Object.freeze(e))):[];case f.INPUT_RADIO:case f.INPUT_CHECKBOX:case f.SELECT_SINGLE:case f.SELECT_MULTIPLE:return Array.isArray(n)?Object.freeze(n.map(String)):[];default:return t}})(),validity:c,touched:e.touched,modified:e.modified,visited:e.visited})}function $(e,t){if(i(t.name))return e;const n=t.name.value,r=u(e.get(n)),a=s(()=>t,e=>{return n=e,r=t,Object.freeze({$:o(n.$,r.$),tag:o(n.tag,r.tag),name:o(n.name,r.name),value:o(n.value,r.value),validity:o(n.validity,r.validity,(e,t)=>({...e,...t})),touched:n.touched||r.touched,modified:n.modified||r.modified,visited:n.visited||r.visited});var n,r})(r);return e.set(n,a)}function z(e,t){return e.clear(),t?new Map(t.entries()):e}function H(e,n,r){for(let e=0,t=r.length;e<t;e++)n.delete(r[e]);for(const n of e.keys()){r.some(e=>e instanceof RegExp?e.test(n):!!t(e)&&e.trim()===n.trim())&&e.delete(n)}return[n,e]}function x(e,t){const n=new CustomEvent(L.EMIT_FORM_VALUES,{detail:Object.freeze(new Map(t.entries()))});e.dispatchEvent(n)}function B(r){const a=new Map;if(i(r))return void C("[RxFormData] invalid form element for form field storage initialization");const c=r.value,o=new Map;o.set(a,new Map(a.entries()));const u=()=>{const e=o.get(a);return e?new Map(e.entries()):new Map},l=new Set,f=(new Map).set(l,l),T=()=>{const e=f.get(l);return e?new Set(e.values()):new Set};return{storage:u,action:(r,L)=>{switch(r){case _:if(k(L)){const e=s(()=>"",e=>e.trim())(L.name);[...T().keys()].some(r=>t(r)?r.trim()===e.trim():!!n(r)&&r.test(e))&&(o.set(a,$(u(),L)),x(c,u()))}break;case U:o.set(a,z(u())),f.set(l,new Set),x(c,u());break;case m:{const e=Array.from(c.elements).reduce((e,t)=>w(t)?e.concat(t.name):e,[]);f.set(l,new Set(e));break}case d:f.set(l,new Set);break;case R:{const n=Array.isArray(L)?L.filter(e=>t(e)||k(e)):[];n.length&&(o.set(a,function(n,r){return r.reduce((n,r)=>{const a=e(r)?E(r,!0):r.name;return i(a)?n:t(a.value)?(n.delete(a.value.trim()),n):n},n)}(u(),n)),x(c,u()));break}case v:o.set(a,z(u(),a)),x(c,u());break;case I:{const e=Array.isArray(L)?L.filter(e=>t(e)||n(e)):[];e.length&&f.set(l,function(e,t){for(let n=0,r=t.length;n<r;n++)e.add(t[n]);return e}(T(),e));break}case N:if(L&&j(L)){const e=Array.isArray(L.use)?L.use.filter(e=>t(e)||n(e)):[];if(e.length){const t=H(u(),T(),e);f.set(l,t[0]),L.keepvalues||(o.set(a,t[1]),x(c,u()))}}else{const e=Array.isArray(L)?L.filter(e=>t(e)||n(e)):[];if(e.length){const t=H(u(),T(),e);f.set(l,t[0])}}}}}}function X(e,t){t(_,function(e){const t=u(e.target);if(i(t))return F;if(!w(t.value))return F;const n=t.value,r=E(n.name,!0),c=a(`${n.tagName}:${n.type}`.toLowerCase()),s=D(n),o=[T.CHANGE,T.BLUR].includes(e.type),l=[T.CHANGE].includes(e.type),f=[T.FOCUS,T.BLUR].includes(e.type);return Object.freeze({$:a(n),name:r,tag:c,value:s,validity:a(Object.freeze(n.validity)),touched:o,modified:l,visited:f})}(e))}function K(e,t,n,r){return function(a){if(a instanceof CustomEvent)switch(a.type){case L.EMIT_FORM_VALUES:{const e=r.subscribers(),t=null,a=[...n.storage().entries()].reduce((e,[n,r])=>(e[n]=G(r,t),e),{});for(const t of e)t(a);break}}else switch(a.type){case T.SUBMIT:a.preventDefault(),async function(e,t,n){const r=new FormData(e);new Promise((a,c)=>{Promise.resolve().then(()=>{const e=[...t.entries()].reduce((e,[t,n])=>(e[t]=G(n,null),e),{});return Promise.resolve(n(Object.freeze(e),r))}).then(()=>{console.info(`[RxFormData #${e.id}] form submission handler success`),a()}).catch(t=>{console.error(`[RxFormData #${e.id}] form submission handler error(s)`,t),c(t)})})}(e,n.storage(),t);break;case T.FOCUS:case T.INPUT:case T.CHANGE:case T.BLUR:X(a,n.action);break;case T.RESET:n.action(v)}}}module.exports=function(e,c){const s=function(e){if(P(e))return a(e);if(i(E(e,!0)))return r;const t=u(document.querySelector("form#"+e.trim()));return i(t)?r:P(t.value)?a(t.value):r}(e);if(i(s))return void C("Invalid form element. Form id provided did not match any form element in the DOM.");const o=B(s)||null,l=o?function(e,t,n){const r=new Set;if(i(e))return void C("[RxFormData] invalid form element for events initialization");const a=new Map;a.set(r,new Set(r.values()));const c=()=>{const e=a.get(r);return e||new Set},s=(e,t)=>{switch(e){case A:t instanceof Function&&a.set(r,new Set(c().values()).add(t));break;case S:if(t instanceof Function){const e=new Set(c().values());e.delete(t),a.set(r,e)}break;case b:a.set(r,new Set)}},o={subscribers:c,action:s},u=e.value,l=K(u,t,n,o);return[...Object.values(T),...Object.values(L)].forEach(e=>{u.addEventListener(e,l,!0)}),{...o,cleanup:()=>{[...Object.values(T),...Object.values(L)].forEach(e=>{u.removeEventListener(e,l,!0)}),s(b),n.action(U)}}}(s,c,o):null;return o&&l?Object.freeze({ACTION_TYPE:Object.freeze(p),register:Object.freeze(e=>(o.action(I,e),Object.freeze(()=>{o.action(N,e)}))),subscribe:Object.freeze(e=>(l.action(A,e),Object.freeze(()=>{l.action(S,e)}))),dispatch:Object.freeze((r,a)=>{switch(r){case p.REGISTER_ALL:o.action(m);break;case p.REGISTER:{const e=Array.isArray(a)?a.reduce((e,r)=>t(r)||n(r)?e.concat(r):e,[]):[];e.length&&o.action(I,e);break}case p.UNREGISTER_ALL:o.action(d),!(!1!==a)&&o.action(v);break;case p.UNREGISTER:{const e=Array.isArray(a)?a.reduce((e,r)=>t(r)||n(r)?e.concat(r):e,[]):[];e.length&&o.action(N,e);break}case p.DESTROY:o.action(v),o.action(d),l.cleanup&&l.cleanup();break;default:return void console.debug(`[RxFormData: #${e}] uknown action dispatched...`,a)}})}):void 0}; |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).RxFormData=t()}(this,(function(){"use strict";const e=e=>"string"==typeof e,t=t=>Boolean(e(t)&&t.trim().length),n=e=>"[object RegExp]"===Object.prototype.toString.call(e),r={_is:"None"};function a(e){return{_is:"Some",value:e}}function c(e){return"Some"===e._is}function i(e){return"None"===e._is}function s(e,t){return n=>i(n)?e():t(n.value)}function o(e,t,n){return i(e)&&i(t)?r:c(e)&&c(t)?n?a(n(e.value,t.value)):t:c(e)&&i(t)?e:i(e)&&c(t)?t:r}function u(e){return null===e||void 0===e?r:a(e)}function l(e){return isNaN(Number(e))?r:a(Number(e))}function f(n,c){return e(n)?c?t(n)?a(n):r:a(n):r}const m="input:text",d="input:number",E="input:email",v="input:password",p="input:checkbox",R="input:radio",b="input:color",S="input:date",y="input:datetime-local",h="input:file",A="input:hidden",L="input:month",w="input:range",g="input:search",O="input:tel",T="input:time",I="input:url",j="input:week",F="textarea:textarea",_="select:select-one",D="select:select-multiple",N={FOCUS:"focus",INPUT:"input",CHANGE:"change",BLUR:"blur",SUBMIT:"submit",RESET:"reset"},U="REGISTER_ALL",k="UNREGISTER_ALL",M="REGISTER",z="UNREGISTER",$="UPSERT_FIELD",G="DELETE_FIELD",x="CLEAR_FIELDS",C="RESET",H={EMIT_FORM_VALUES:"EMIT_FORM_VALUES"},P="ADD",B="DELETE",V="CLEAR",Y={REGISTER:"REGISTER_FIELDS",REGISTER_ALL:"REGISTER_ALL_FIELDS",UNREGISTER:"UNREGISTER_FIELDS",UNREGISTER_ALL:"UNREGISTER_ALL_FIELDS",DESTROY:"DESTROY_PROGRAM"};function q(e){return e instanceof HTMLFormElement}function J(e){return e instanceof HTMLInputElement}function K(e){return e instanceof HTMLTextAreaElement}function Q(e){return e instanceof HTMLSelectElement}function W(e){return J(e)||Q(e)||K(e)}function X(e){return W(e)?J(e)?function(e){if(!J(e))return r;const n=`${e.tagName.toLowerCase()}:${e.type}`;switch(n){case m:case g:case E:case b:case A:return a(e.value.trim());case v:return a(e.value);case d:case w:return l(Number(e.value.trim()));case I:try{return a(new URL(e.value.trim()).href.trim())}catch(t){return console.warn("[RxFormData] Failed to decode url input field value into URL object",e.value,t),r}case O:return a(e.value.trim().replace(/\D/g,""));case h:return s(()=>r,e=>a(Object.freeze(Array.from(Array(e.length).keys()).reduce((t,n)=>{const r=e.item(n);return r?t.concat(r):t},[]))))(u(e.files));case S:case y:case T:case j:return l(e.valueAsNumber);case L:try{return a(new Date(e.value+"-1").getTime())}catch(e){return console.error("[RxFormData] Failed to decode month input field value into number",e),r}case R:case p:{const c=n.split(":")[1],s=u(e.form);if(i(s))return r;const o=s.value.querySelectorAll(`input[type='${c}'][name='${e.name}']:checked`);return a(Object.freeze(Array.from(o).map((n,r)=>J(n)&&t(n.value)?n.value.trim():`${e.name}[${r}]`)))}default:return r}}(e):Q(e)?function(e){if(!Q(e))return r;switch(`${e.tagName.toLowerCase()}:${e.type}`){case _:case D:return a(Object.freeze(Array.from(e.selectedOptions).map((n,r)=>t(n.value)?n.value.trim():`${e.name}[${r}]`)));default:return r}}(e):K(e)?function(e){return K(e)?a(e.value.trim()):r}(e):r:r}function Z(e){if(e instanceof Error)throw e;const t=new Error("[RxFormData] ERROR!");throw t.details=JSON.stringify(e),t}function ee(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)&&"Object"===e.constructor.name}const te=Object.freeze({$:r,tag:r,name:r,value:r,validity:r,touched:!1,modified:!1,visited:!1});function ne(e){return ee(e)}function re(e,t){const n=s(()=>t,e=>e)(e.$),r=s(()=>t,e=>e)(e.tag),a=s(()=>t,e=>e)(e.name),c=s(()=>t,e=>e)(e.validity);return Object.freeze({$:n,tag:r,name:a,value:(()=>{if(r===t)return t;const n=s(()=>t,e=>e)(e.value);if(n===t)return n;switch(r){case m:case g:case E:case b:case A:case F:case I:case O:case v:return String(n);case d:case w:case S:case y:case T:case j:case L:return Number(n);case h:return Array.isArray(n)?Object.freeze(n.map(e=>Object.freeze(e))):[];case R:case p:case _:case D:return Array.isArray(n)?Object.freeze(n.map(String)):[];default:return t}})(),validity:c,touched:e.touched,modified:e.modified,visited:e.visited})}function ae(e,t){if(i(t.name))return e;const n=t.name.value,r=u(e.get(n)),a=s(()=>t,e=>{return n=e,r=t,Object.freeze({$:o(n.$,r.$),tag:o(n.tag,r.tag),name:o(n.name,r.name),value:o(n.value,r.value),validity:o(n.validity,r.validity,(e,t)=>({...e,...t})),touched:n.touched||r.touched,modified:n.modified||r.modified,visited:n.visited||r.visited});var n,r})(r);return e.set(n,a)}function ce(e,t){return e.clear(),t?new Map(t.entries()):e}function ie(e,n,r){for(let e=0,t=r.length;e<t;e++)n.delete(r[e]);for(const n of e.keys()){r.some(e=>e instanceof RegExp?e.test(n):!!t(e)&&e.trim()===n.trim())&&e.delete(n)}return[n,e]}function se(e,t){const n=new CustomEvent(H.EMIT_FORM_VALUES,{detail:Object.freeze(new Map(t.entries()))});e.dispatchEvent(n)}function oe(r){const a=new Map;if(i(r))return void Z("[RxFormData] invalid form element for form field storage initialization");const c=r.value,o=new Map;o.set(a,new Map(a.entries()));const u=()=>{const e=o.get(a);return e?new Map(e.entries()):new Map},l=new Set,m=(new Map).set(l,l),d=()=>{const e=m.get(l);return e?new Set(e.values()):new Set};return{storage:u,action:(r,E)=>{switch(r){case $:if(ne(E)){const e=s(()=>"",e=>e.trim())(E.name);[...d().keys()].some(r=>t(r)?r.trim()===e.trim():!!n(r)&&r.test(e))&&(o.set(a,ae(u(),E)),se(c,u()))}break;case x:o.set(a,ce(u())),m.set(l,new Set),se(c,u());break;case U:{const e=Array.from(c.elements).reduce((e,t)=>W(t)?e.concat(t.name):e,[]);m.set(l,new Set(e));break}case k:m.set(l,new Set);break;case G:{const n=Array.isArray(E)?E.filter(e=>t(e)||ne(e)):[];n.length&&(o.set(a,function(n,r){return r.reduce((n,r)=>{const a=e(r)?f(r,!0):r.name;return i(a)?n:t(a.value)?(n.delete(a.value.trim()),n):n},n)}(u(),n)),se(c,u()));break}case C:o.set(a,ce(u(),a)),se(c,u());break;case M:{const e=Array.isArray(E)?E.filter(e=>t(e)||n(e)):[];e.length&&m.set(l,function(e,t){for(let n=0,r=t.length;n<r;n++)e.add(t[n]);return e}(d(),e));break}case z:if(E&&ee(E)){const e=Array.isArray(E.use)?E.use.filter(e=>t(e)||n(e)):[];if(e.length){const t=ie(u(),d(),e);m.set(l,t[0]),E.keepvalues||(o.set(a,t[1]),se(c,u()))}}else{const e=Array.isArray(E)?E.filter(e=>t(e)||n(e)):[];if(e.length){const t=ie(u(),d(),e);m.set(l,t[0])}}}}}}function ue(e,t){t($,function(e){const t=u(e.target);if(i(t))return te;if(!W(t.value))return te;const n=t.value,r=f(n.name,!0),c=a(`${n.tagName}:${n.type}`.toLowerCase()),s=X(n),o=[N.CHANGE,N.BLUR].includes(e.type),l=[N.CHANGE].includes(e.type),m=[N.FOCUS,N.BLUR].includes(e.type);return Object.freeze({$:a(n),name:r,tag:c,value:s,validity:a(Object.freeze(n.validity)),touched:o,modified:l,visited:m})}(e))}function le(e,t,n,r){return function(a){if(a instanceof CustomEvent)switch(a.type){case H.EMIT_FORM_VALUES:{const e=r.subscribers(),t=null,a=[...n.storage().entries()].reduce((e,[n,r])=>(e[n]=re(r,t),e),{});for(const t of e)t(a);break}}else switch(a.type){case N.SUBMIT:a.preventDefault(),async function(e,t,n){const r=new FormData(e);new Promise((a,c)=>{Promise.resolve().then(()=>{const e=[...t.entries()].reduce((e,[t,n])=>(e[t]=re(n,null),e),{});return Promise.resolve(n(Object.freeze(e),r))}).then(()=>{console.info(`[RxFormData #${e.id}] form submission handler success`),a()}).catch(t=>{console.error(`[RxFormData #${e.id}] form submission handler error(s)`,t),c(t)})})}(e,n.storage(),t);break;case N.FOCUS:case N.INPUT:case N.CHANGE:case N.BLUR:ue(a,n.action);break;case N.RESET:n.action(C)}}}return function(e,c){const s=function(e){if(q(e))return a(e);if(i(f(e,!0)))return r;const t=u(document.querySelector("form#"+e.trim()));return i(t)?r:q(t.value)?a(t.value):r}(e);if(i(s))return void Z("Invalid form element. Form id provided did not match any form element in the DOM.");const o=oe(s)||null,l=o?function(e,t,n){const r=new Set;if(i(e))return void Z("[RxFormData] invalid form element for events initialization");const a=new Map;a.set(r,new Set(r.values()));const c=()=>{const e=a.get(r);return e||new Set},s=(e,t)=>{switch(e){case P:t instanceof Function&&a.set(r,new Set(c().values()).add(t));break;case B:if(t instanceof Function){const e=new Set(c().values());e.delete(t),a.set(r,e)}break;case V:a.set(r,new Set)}},o={subscribers:c,action:s},u=e.value,l=le(u,t,n,o);return[...Object.values(N),...Object.values(H)].forEach(e=>{u.addEventListener(e,l,!0)}),{...o,cleanup:()=>{[...Object.values(N),...Object.values(H)].forEach(e=>{u.removeEventListener(e,l,!0)}),s(V),n.action(x)}}}(s,c,o):null;return o&&l?Object.freeze({ACTION_TYPE:Object.freeze(Y),register:Object.freeze(e=>(o.action(M,e),Object.freeze(()=>{o.action(z,e)}))),subscribe:Object.freeze(e=>(l.action(P,e),Object.freeze(()=>{l.action(B,e)}))),dispatch:Object.freeze((r,a)=>{switch(r){case Y.REGISTER_ALL:o.action(U);break;case Y.REGISTER:{const e=Array.isArray(a)?a.reduce((e,r)=>t(r)||n(r)?e.concat(r):e,[]):[];e.length&&o.action(M,e);break}case Y.UNREGISTER_ALL:o.action(k),!(!1!==a)&&o.action(C);break;case Y.UNREGISTER:{const e=Array.isArray(a)?a.reduce((e,r)=>t(r)||n(r)?e.concat(r):e,[]):[];e.length&&o.action(z,e);break}case Y.DESTROY:o.action(C),o.action(k),l.cleanup&&l.cleanup();break;default:return void console.debug(`[RxFormData: #${e}] uknown action dispatched...`,a)}})}):void 0}})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?module.exports=t():"function"==typeof define&&define.amd?define(t):(e=e||self).RxFormData=t()}(this,(function(){"use strict";const e=e=>"string"==typeof e,t=t=>Boolean(e(t)&&t.trim().length),n=e=>"[object RegExp]"===Object.prototype.toString.call(e),r={_is:"None"};function a(e){return{_is:"Some",value:e}}function c(e){return"Some"===e._is}function i(e){return"None"===e._is}function s(e,t){return n=>i(n)?e():t(n.value)}function o(e,t,n){return i(e)&&i(t)?r:c(e)&&c(t)?n?a(n(e.value,t.value)):t:c(e)&&i(t)?e:i(e)&&c(t)?t:r}function u(e){return null===e||void 0===e?r:a(e)}function l(e){return isNaN(Number(e))?r:a(Number(e))}function f(n,c){return e(n)?c?t(n)?a(n):r:a(n):r}const E={INPUT_TEXT:"input:text",INPUT_NUMBER:"input:number",INPUT_EMAIL:"input:email",INPUT_PASSWORD:"input:password",INPUT_CHECKBOX:"input:checkbox",INPUT_RADIO:"input:radio",INPUT_COLOR:"input:color",INPUT_DATE:"input:date",INPUT_DATETIME_LOCAL:"input:datetime-local",INPUT_FILE:"input:file",INPUT_HIDDEN:"input:hidden",INPUT_MONTH:"input:month",INPUT_RANGE:"input:range",INPUT_SEARCH:"input:search",INPUT_TEL:"input:tel",INPUT_TIME:"input:time",INPUT_URL:"input:url",INPUT_WEEK:"input:week",TEXTAREA:"textarea:textarea",SELECT_SINGLE:"select:select-one",SELECT_MULTIPLE:"select:select-multiple"},T={FOCUS:"focus",INPUT:"input",CHANGE:"change",BLUR:"blur",SUBMIT:"submit",RESET:"reset"},d="REGISTER_ALL",m="UNREGISTER_ALL",I="REGISTER",N="UNREGISTER",_="UPSERT_FIELD",R="DELETE_FIELD",U="CLEAR_FIELDS",v="RESET",L={EMIT_FORM_VALUES:"EMIT_FORM_VALUES"},A="ADD",S="DELETE",b="CLEAR",p={REGISTER:"REGISTER_FIELDS",REGISTER_ALL:"REGISTER_ALL_FIELDS",UNREGISTER:"UNREGISTER_FIELDS",UNREGISTER_ALL:"UNREGISTER_ALL_FIELDS",DESTROY:"DESTROY_PROGRAM"};function y(e){return e instanceof HTMLFormElement}function P(e){return e instanceof HTMLInputElement}function O(e){return e instanceof HTMLTextAreaElement}function h(e){return e instanceof HTMLSelectElement}function D(e){return P(e)||h(e)||O(e)}function w(e){return D(e)?P(e)?function(e){if(!P(e))return r;const n=`${e.tagName.toLowerCase()}:${e.type}`;switch(n){case E.INPUT_TEXT:case E.INPUT_SEARCH:case E.INPUT_EMAIL:case E.INPUT_COLOR:case E.INPUT_HIDDEN:return a(e.value.trim());case E.INPUT_PASSWORD:return a(e.value);case E.INPUT_NUMBER:case E.INPUT_RANGE:return l(Number(e.value.trim()));case E.INPUT_URL:try{return a(new URL(e.value.trim()).href.trim())}catch(t){return console.warn("[RxFormData] Failed to decode url input field value into URL object",e.value,t),r}case E.INPUT_TEL:return a(e.value.trim().replace(/\D/g,""));case E.INPUT_FILE:return s(()=>r,e=>a(Object.freeze(Array.from(Array(e.length).keys()).reduce((t,n)=>{const r=e.item(n);return r?t.concat(r):t},[]))))(u(e.files));case E.INPUT_DATE:case E.INPUT_DATETIME_LOCAL:case E.INPUT_TIME:case E.INPUT_WEEK:return l(e.valueAsNumber);case E.INPUT_MONTH:try{return a(new Date(e.value+"-1").getTime())}catch(e){return console.error("[RxFormData] Failed to decode month input field value into number",e),r}case E.INPUT_RADIO:case E.INPUT_CHECKBOX:{const c=n.split(":")[1],s=u(e.form);if(i(s))return r;const o=s.value.querySelectorAll(`input[type='${c}'][name='${e.name}']:checked`);return a(Object.freeze(Array.from(o).map((n,r)=>P(n)&&t(n.value)?n.value.trim():`${e.name}[${r}]`)))}default:return r}}(e):h(e)?function(e){if(!h(e))return r;switch(`${e.tagName.toLowerCase()}:${e.type}`){case E.SELECT_SINGLE:case E.SELECT_MULTIPLE:return a(Object.freeze(Array.from(e.selectedOptions).map((n,r)=>t(n.value)?n.value.trim():`${e.name}[${r}]`)));default:return r}}(e):O(e)?function(e){return O(e)?a(e.value.trim()):r}(e):r:r}function g(t){return!!e(t)||("number"==typeof t||!(!Array.isArray(t)||!t.every(e)&&!t.every(e=>e instanceof File)))}function M(e){return t(e)&&Object.keys(E).includes(e)}function C(e){if(e instanceof Error)throw e;const t=new Error("[RxFormData] ERROR!");throw t.details=JSON.stringify(e),t}function j(e){return"object"==typeof e&&null!==e&&!Array.isArray(e)&&"Object"===e.constructor.name}const F=Object.freeze({$:r,tag:r,name:r,value:r,validity:r,touched:!1,modified:!1,visited:!1});function k(e){if(!j(e))return!1;const n=Object.keys(F);if(!Object.keys(e).every(e=>n.includes(e)))return!1;const r=(e,n)=>j(e)&&t(e._is)&&["none","some"].includes(e._is)&&("some"!==e._is||n(e.value));return Object.entries(e).reduce((e,[n,a])=>{switch(n){case"$":return e&&r(a,D);case"tag":return e&&r(a,M);case"name":return e&&r(a,t);case"value":return e&&r(a,g);case"validity":return e&&r(a,e=>e instanceof ValidityState);case"touched":case"visited":case"modified":return"boolean"==typeof a;default:return!1}},!0)}function G(e,t){const n=s(()=>t,e=>e)(e.$),r=s(()=>t,e=>e)(e.tag),a=s(()=>t,e=>e)(e.name),c=s(()=>t,e=>e)(e.validity);return Object.freeze({$:n,tag:r,name:a,value:(()=>{if(r===t)return t;const n=s(()=>t,e=>e)(e.value);if(n===t)return n;switch(r){case E.INPUT_TEXT:case E.INPUT_SEARCH:case E.INPUT_EMAIL:case E.INPUT_COLOR:case E.INPUT_HIDDEN:case E.TEXTAREA:case E.INPUT_URL:case E.INPUT_TEL:case E.INPUT_PASSWORD:return String(n);case E.INPUT_NUMBER:case E.INPUT_RANGE:case E.INPUT_DATE:case E.INPUT_DATETIME_LOCAL:case E.INPUT_TIME:case E.INPUT_WEEK:case E.INPUT_MONTH:return Number(n);case E.INPUT_FILE:return Array.isArray(n)?Object.freeze(n.map(e=>Object.freeze(e))):[];case E.INPUT_RADIO:case E.INPUT_CHECKBOX:case E.SELECT_SINGLE:case E.SELECT_MULTIPLE:return Array.isArray(n)?Object.freeze(n.map(String)):[];default:return t}})(),validity:c,touched:e.touched,modified:e.modified,visited:e.visited})}function $(e,t){if(i(t.name))return e;const n=t.name.value,r=u(e.get(n)),a=s(()=>t,e=>{return n=e,r=t,Object.freeze({$:o(n.$,r.$),tag:o(n.tag,r.tag),name:o(n.name,r.name),value:o(n.value,r.value),validity:o(n.validity,r.validity,(e,t)=>({...e,...t})),touched:n.touched||r.touched,modified:n.modified||r.modified,visited:n.visited||r.visited});var n,r})(r);return e.set(n,a)}function z(e,t){return e.clear(),t?new Map(t.entries()):e}function H(e,n,r){for(let e=0,t=r.length;e<t;e++)n.delete(r[e]);for(const n of e.keys()){r.some(e=>e instanceof RegExp?e.test(n):!!t(e)&&e.trim()===n.trim())&&e.delete(n)}return[n,e]}function x(e,t){const n=new CustomEvent(L.EMIT_FORM_VALUES,{detail:Object.freeze(new Map(t.entries()))});e.dispatchEvent(n)}function B(r){const a=new Map;if(i(r))return void C("[RxFormData] invalid form element for form field storage initialization");const c=r.value,o=new Map;o.set(a,new Map(a.entries()));const u=()=>{const e=o.get(a);return e?new Map(e.entries()):new Map},l=new Set,E=(new Map).set(l,l),T=()=>{const e=E.get(l);return e?new Set(e.values()):new Set};return{storage:u,action:(r,L)=>{switch(r){case _:if(k(L)){const e=s(()=>"",e=>e.trim())(L.name);[...T().keys()].some(r=>t(r)?r.trim()===e.trim():!!n(r)&&r.test(e))&&(o.set(a,$(u(),L)),x(c,u()))}break;case U:o.set(a,z(u())),E.set(l,new Set),x(c,u());break;case d:{const e=Array.from(c.elements).reduce((e,t)=>D(t)?e.concat(t.name):e,[]);E.set(l,new Set(e));break}case m:E.set(l,new Set);break;case R:{const n=Array.isArray(L)?L.filter(e=>t(e)||k(e)):[];n.length&&(o.set(a,function(n,r){return r.reduce((n,r)=>{const a=e(r)?f(r,!0):r.name;return i(a)?n:t(a.value)?(n.delete(a.value.trim()),n):n},n)}(u(),n)),x(c,u()));break}case v:o.set(a,z(u(),a)),x(c,u());break;case I:{const e=Array.isArray(L)?L.filter(e=>t(e)||n(e)):[];e.length&&E.set(l,function(e,t){for(let n=0,r=t.length;n<r;n++)e.add(t[n]);return e}(T(),e));break}case N:if(L&&j(L)){const e=Array.isArray(L.use)?L.use.filter(e=>t(e)||n(e)):[];if(e.length){const t=H(u(),T(),e);E.set(l,t[0]),L.keepvalues||(o.set(a,t[1]),x(c,u()))}}else{const e=Array.isArray(L)?L.filter(e=>t(e)||n(e)):[];if(e.length){const t=H(u(),T(),e);E.set(l,t[0])}}}}}}function X(e,t){t(_,function(e){const t=u(e.target);if(i(t))return F;if(!D(t.value))return F;const n=t.value,r=f(n.name,!0),c=a(`${n.tagName}:${n.type}`.toLowerCase()),s=w(n),o=[T.CHANGE,T.BLUR].includes(e.type),l=[T.CHANGE].includes(e.type),E=[T.FOCUS,T.BLUR].includes(e.type);return Object.freeze({$:a(n),name:r,tag:c,value:s,validity:a(Object.freeze(n.validity)),touched:o,modified:l,visited:E})}(e))}function K(e,t,n,r){return function(a){if(a instanceof CustomEvent)switch(a.type){case L.EMIT_FORM_VALUES:{const e=r.subscribers(),t=null,a=[...n.storage().entries()].reduce((e,[n,r])=>(e[n]=G(r,t),e),{});for(const t of e)t(a);break}}else switch(a.type){case T.SUBMIT:a.preventDefault(),async function(e,t,n){const r=new FormData(e);new Promise((a,c)=>{Promise.resolve().then(()=>{const e=[...t.entries()].reduce((e,[t,n])=>(e[t]=G(n,null),e),{});return Promise.resolve(n(Object.freeze(e),r))}).then(()=>{console.info(`[RxFormData #${e.id}] form submission handler success`),a()}).catch(t=>{console.error(`[RxFormData #${e.id}] form submission handler error(s)`,t),c(t)})})}(e,n.storage(),t);break;case T.FOCUS:case T.INPUT:case T.CHANGE:case T.BLUR:X(a,n.action);break;case T.RESET:n.action(v)}}}return function(e,c){const s=function(e){if(y(e))return a(e);if(i(f(e,!0)))return r;const t=u(document.querySelector("form#"+e.trim()));return i(t)?r:y(t.value)?a(t.value):r}(e);if(i(s))return void C("Invalid form element. Form id provided did not match any form element in the DOM.");const o=B(s)||null,l=o?function(e,t,n){const r=new Set;if(i(e))return void C("[RxFormData] invalid form element for events initialization");const a=new Map;a.set(r,new Set(r.values()));const c=()=>{const e=a.get(r);return e||new Set},s=(e,t)=>{switch(e){case A:t instanceof Function&&a.set(r,new Set(c().values()).add(t));break;case S:if(t instanceof Function){const e=new Set(c().values());e.delete(t),a.set(r,e)}break;case b:a.set(r,new Set)}},o={subscribers:c,action:s},u=e.value,l=K(u,t,n,o);return[...Object.values(T),...Object.values(L)].forEach(e=>{u.addEventListener(e,l,!0)}),{...o,cleanup:()=>{[...Object.values(T),...Object.values(L)].forEach(e=>{u.removeEventListener(e,l,!0)}),s(b),n.action(U)}}}(s,c,o):null;return o&&l?Object.freeze({ACTION_TYPE:Object.freeze(p),register:Object.freeze(e=>(o.action(I,e),Object.freeze(()=>{o.action(N,e)}))),subscribe:Object.freeze(e=>(l.action(A,e),Object.freeze(()=>{l.action(S,e)}))),dispatch:Object.freeze((r,a)=>{switch(r){case p.REGISTER_ALL:o.action(d);break;case p.REGISTER:{const e=Array.isArray(a)?a.reduce((e,r)=>t(r)||n(r)?e.concat(r):e,[]):[];e.length&&o.action(I,e);break}case p.UNREGISTER_ALL:o.action(m),!(!1!==a)&&o.action(v);break;case p.UNREGISTER:{const e=Array.isArray(a)?a.reduce((e,r)=>t(r)||n(r)?e.concat(r):e,[]):[];e.length&&o.action(N,e);break}case p.DESTROY:o.action(v),o.action(m),l.cleanup&&l.cleanup();break;default:return void console.debug(`[RxFormData: #${e}] uknown action dispatched...`,a)}})}):void 0}})); |
import { Option } from "../datatypes/Option"; | ||
import { HTMLFormFieldElement, HTMLFormFieldValue } from "../datatypes/base"; | ||
import { HTMLFormFieldElement, HTMLFormFieldValue, HTMLFormFieldTag } from "../datatypes/base"; | ||
export declare function isFormElement(x: unknown): x is HTMLFormElement; | ||
@@ -15,1 +15,3 @@ export declare function isInputFieldElement(x: unknown): x is HTMLInputElement; | ||
export declare function isFormFieldElementActive($target: unknown): boolean; | ||
export declare function isFormFieldValue(x: unknown): x is HTMLFormFieldValue; | ||
export declare function isFormFieldInternalTag(x: unknown): x is HTMLFormFieldTag; |
{ | ||
"name": "@metronlabs/rx-form-data", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Framework agnostic reactive form data streaming", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
40449
279
0