@keysdown/form-wrapper 0.0.7 → 0.0.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -0
- package/dist/form-wrapper.cjs +1 -1
- package/dist/form-wrapper.iife.js +1 -1
- package/dist/form-wrapper.js +1 -1
- package/dist/form-wrapper.umd.cjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -2,8 +2,16 @@
|
|
|
2
2
|
Form Wrapper (Beta)
|
|
3
3
|
</h1>
|
|
4
4
|
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://www.npmjs.com/package/@keysdown/form-wrapper" target="_blank">
|
|
7
|
+
<img src="https://img.shields.io/npm/v/@keysdown/form-wrapper.svg?style=shield" alt="npm"/>
|
|
8
|
+
</a>
|
|
9
|
+
<img src="https://img.shields.io/github/license/keysdown/form-wrapper.svg" alt="MIT"/>
|
|
10
|
+
</p>
|
|
11
|
+
|
|
5
12
|
> A package that allows you to easily manage forms, with Form Wrapper it is possible to perform validations with error messages, in addition to managing the state of the forms.
|
|
6
13
|
|
|
14
|
+
> ![NOTE]
|
|
7
15
|
> This package is currently in Beta and is being tested in live applications, feel free to help in the development and maturation of the package.
|
|
8
16
|
|
|
9
17
|
## Installation
|
package/dist/form-wrapper.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var m=Object.defineProperty;var F=(s,t,e)=>t in s?m(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var a=(s,t,e)=>F(s,typeof t!="symbol"?t+"":t,e);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const p=s=>typeof s=="string"?s.split("|"):s,b=s=>{var t,e;return{validation:{rules:(t=s.validation)!=null&&t.rules?p(s.validation.rules):[],messages:((e=s.validation)==null?void 0:e.messages)||{}},value:s.value||null}};class u{constructor(){a(this,"collection",{})}all(){return this.collection}first(t){const e=this.get(t);return e?Array.isArray(e)?e[0]:e:null}any(){return Object.keys(this.collection).length>0}fill(t){return this.collection=t,this}push(t,e){return this.collection={...this.collection,[t]:e},this}has(t){return this.collection.hasOwnProperty(t)}get(t,e=null){return this.has(t)?this.collection[t]:e}unset(t){return this.has(t)&&delete this.collection[t],this}clear(){return this.collection={},this}}class j extends u{}class y extends u{}class f extends u{push(t,e){const r=this.get(t)||[];return this.collection={...this.collection,[t]:[...r,e]},this}get(t){return super.get(t,[])}}class w{constructor(){a(this,"errors",new f);a(this,"messages",new j);a(this,"rules",new y)}}const O=s=>new Promise((t,e)=>{s==null&&e(),String(s).replace(/\s/g,"").length>0?t(s):e()}),h={required:O},P=s=>s!==null&&typeof s=="object"&&!Array.isArray(s),c=(s,t,e=null)=>{const r=t||new FormData;return Object.keys(s).forEach(i=>{const n=s[i];if(i=e?`${e}[${i}]`:i,!([void 0,null].indexOf(n)>-1)){if(P(n)&&!(n instanceof File)||Array.isArray(n)){c(n,r,i);return}r.append(i,n)}}),r};class d{constructor(t){a(this,"awaiting",!1);a(this,"originalValues",{});a(this,"validation",new w);this.addFields(t)}addField(t,e){if(typeof e=="object"&&"value"in e){const r=b(e);this[t]=r.value,this.originalValues[t]=r.value,this.validation.messages.push(t,r.validation.messages),this.validation.rules.push(t,r.validation.rules)}else this[t]=e,this.originalValues[t]=e;return this}addFields(t){return Object.keys(t).forEach(e=>{this.addField(e,t[e])}),this}get errors(){return this.validation.errors}fill(t,e=!1){return Object.keys(t).forEach(r=>{let i=t[r];e&&(this.originalValues[r]=i),this[r]=i}),this}get messages(){return this.validation.messages}removeField(t){return delete this[t],delete this.originalValues[t],this.validation.messages.unset(t),this.validation.rules.unset(t),this}removeFields(t){return t.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(t=>{this[t]=this.originalValues[t]}),this}get rules(){return this.validation.rules}setAwaiting(t=!0){return this.awaiting=t,this}validate(t=null){return t?this.validateField(t):this.validateForm()}validateField(t){this.validation.errors.unset(t);const e=this.validation.rules.get(t);if(e&&e.length>0){const r=e.map(i=>{const n=i.split(":"),l=n[0],g=n.length===2?n[1].split(","):[];return l in h?h[l](this[t],g).catch(v=>{const o=this.validation.messages.get(t);return o&&l in o&&this.validation.errors.push(t,o[l]),Promise.reject(v)}):Promise.reject(new Error(`There is no validation rule called "${i}"`))});return Promise.all(r).then(()=>{})}return Promise.resolve()}validateForm(){const t=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(t).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(){const t={};return Object.keys(this.originalValues).forEach(e=>{t[e]=this[e]}),t}valuesAsFormData(){return c(this.values())}}const V=s=>new d(s);exports.createForm=V;exports.default=d;
|
|
1
|
+
"use strict";var m=Object.defineProperty;var F=(s,t,e)=>t in s?m(s,t,{enumerable:!0,configurable:!0,writable:!0,value:e}):s[t]=e;var a=(s,t,e)=>F(s,typeof t!="symbol"?t+"":t,e);Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const p=s=>typeof s=="string"?s.split("|"):s,b=s=>{var t,e;return{validation:{rules:(t=s.validation)!=null&&t.rules?p(s.validation.rules):[],messages:((e=s.validation)==null?void 0:e.messages)||{}},value:s.value||null}};class u{constructor(){a(this,"collection",{})}all(){return this.collection}first(t){const e=this.get(t);return e?Array.isArray(e)?e[0]:e:null}any(){return Object.keys(this.collection).length>0}fill(t){return this.collection=t,this}push(t,e){return this.collection={...this.collection,[t]:e},this}has(t){return this.collection.hasOwnProperty(t)}get(t,e=null){return this.has(t)?this.collection[t]:e}unset(t){return this.has(t)&&delete this.collection[t],this}clear(){return this.collection={},this}}class j extends u{}class y extends u{}class f extends u{push(t,e){const r=this.get(t)||[];return this.collection={...this.collection,[t]:[...r,e]},this}get(t){return super.get(t,[])}}class w{constructor(){a(this,"errors",new f);a(this,"messages",new j);a(this,"rules",new y)}}const O=s=>new Promise((t,e)=>{s==null&&e(),String(s).replace(/\s/g,"").length>0?t(s):e()}),h={required:O},P=s=>s!==null&&typeof s=="object"&&!Array.isArray(s),c=(s,t,e=null)=>{const r=t||new FormData;return Object.keys(s).forEach(i=>{const n=s[i];if(i=e?`${e}[${i}]`:i,!([void 0,null].indexOf(n)>-1)){if(P(n)&&!(n instanceof File)||Array.isArray(n)){c(n,r,i);return}r.append(i,n)}}),r};class d{constructor(t){a(this,"awaiting",!1);a(this,"originalValues",{});a(this,"validation",new w);this.addFields(t)}addField(t,e){if(e!==null&&typeof e=="object"&&"value"in e){const r=b(e);this[t]=r.value,this.originalValues[t]=r.value,this.validation.messages.push(t,r.validation.messages),this.validation.rules.push(t,r.validation.rules)}else this[t]=e,this.originalValues[t]=e;return this}addFields(t){return Object.keys(t).forEach(e=>{this.addField(e,t[e])}),this}get errors(){return this.validation.errors}fill(t,e=!1){return Object.keys(t).forEach(r=>{let i=t[r];e&&(this.originalValues[r]=i),this[r]=i}),this}get messages(){return this.validation.messages}removeField(t){return delete this[t],delete this.originalValues[t],this.validation.messages.unset(t),this.validation.rules.unset(t),this}removeFields(t){return t.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(t=>{this[t]=this.originalValues[t]}),this}get rules(){return this.validation.rules}setAwaiting(t=!0){return this.awaiting=t,this}validate(t=null){return t?this.validateField(t):this.validateForm()}validateField(t){this.validation.errors.unset(t);const e=this.validation.rules.get(t);if(e&&e.length>0){const r=e.map(i=>{const n=i.split(":"),l=n[0],g=n.length===2?n[1].split(","):[];return l in h?h[l](this[t],g).catch(v=>{const o=this.validation.messages.get(t);return o&&l in o&&this.validation.errors.push(t,o[l]),Promise.reject(v)}):Promise.reject(new Error(`There is no validation rule called "${i}"`))});return Promise.all(r).then(()=>{})}return Promise.resolve()}validateForm(){const t=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(t).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(){const t={};return Object.keys(this.originalValues).forEach(e=>{t[e]=this[e]}),t}valuesAsFormData(){return c(this.values())}}const V=s=>new d(s);exports.createForm=V;exports.default=d;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var FormWrapper=function(n){"use strict";var P=Object.defineProperty;var V=(n,l,u)=>l in n?P(n,l,{enumerable:!0,configurable:!0,writable:!0,value:u}):n[l]=u;var o=(n,l,u)=>V(n,typeof l!="symbol"?l+"":l,u);const l=s=>typeof s=="string"?s.split("|"):s,u=s=>{var t,e;return{validation:{rules:(t=s.validation)!=null&&t.rules?l(s.validation.rules):[],messages:((e=s.validation)==null?void 0:e.messages)||{}},value:s.value||null}};class h{constructor(){o(this,"collection",{})}all(){return this.collection}first(t){const e=this.get(t);return e?Array.isArray(e)?e[0]:e:null}any(){return Object.keys(this.collection).length>0}fill(t){return this.collection=t,this}push(t,e){return this.collection={...this.collection,[t]:e},this}has(t){return this.collection.hasOwnProperty(t)}get(t,e=null){return this.has(t)?this.collection[t]:e}unset(t){return this.has(t)&&delete this.collection[t],this}clear(){return this.collection={},this}}class F extends h{}class p extends h{}class b extends h{push(t,e){const r=this.get(t)||[];return this.collection={...this.collection,[t]:[...r,e]},this}get(t){return super.get(t,[])}}class j{constructor(){o(this,"errors",new b);o(this,"messages",new F);o(this,"rules",new p)}}const g={required:s=>new Promise((t,e)=>{s==null&&e(),String(s).replace(/\s/g,"").length>0?t(s):e()})},f=s=>s!==null&&typeof s=="object"&&!Array.isArray(s),v=(s,t,e=null)=>{const r=t||new FormData;return Object.keys(s).forEach(i=>{const a=s[i];if(i=e?`${e}[${i}]`:i,!([void 0,null].indexOf(a)>-1)){if(f(a)&&!(a instanceof File)||Array.isArray(a)){v(a,r,i);return}r.append(i,a)}}),r};class m{constructor(t){o(this,"awaiting",!1);o(this,"originalValues",{});o(this,"validation",new j);this.addFields(t)}addField(t,e){if(typeof e=="object"&&"value"in e){const r=u(e);this[t]=r.value,this.originalValues[t]=r.value,this.validation.messages.push(t,r.validation.messages),this.validation.rules.push(t,r.validation.rules)}else this[t]=e,this.originalValues[t]=e;return this}addFields(t){return Object.keys(t).forEach(e=>{this.addField(e,t[e])}),this}get errors(){return this.validation.errors}fill(t,e=!1){return Object.keys(t).forEach(r=>{let i=t[r];e&&(this.originalValues[r]=i),this[r]=i}),this}get messages(){return this.validation.messages}removeField(t){return delete this[t],delete this.originalValues[t],this.validation.messages.unset(t),this.validation.rules.unset(t),this}removeFields(t){return t.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(t=>{this[t]=this.originalValues[t]}),this}get rules(){return this.validation.rules}setAwaiting(t=!0){return this.awaiting=t,this}validate(t=null){return t?this.validateField(t):this.validateForm()}validateField(t){this.validation.errors.unset(t);const e=this.validation.rules.get(t);if(e&&e.length>0){const r=e.map(i=>{const a=i.split(":"),c=a[0],w=a.length===2?a[1].split(","):[];return c in g?g[c](this[t],w).catch(O=>{const d=this.validation.messages.get(t);return d&&c in d&&this.validation.errors.push(t,d[c]),Promise.reject(O)}):Promise.reject(new Error(`There is no validation rule called "${i}"`))});return Promise.all(r).then(()=>{})}return Promise.resolve()}validateForm(){const t=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(t).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(){const t={};return Object.keys(this.originalValues).forEach(e=>{t[e]=this[e]}),t}valuesAsFormData(){return v(this.values())}}const y=s=>new m(s);return n.createForm=y,n.default=m,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),n}({});
|
|
1
|
+
var FormWrapper=function(n){"use strict";var P=Object.defineProperty;var V=(n,l,u)=>l in n?P(n,l,{enumerable:!0,configurable:!0,writable:!0,value:u}):n[l]=u;var o=(n,l,u)=>V(n,typeof l!="symbol"?l+"":l,u);const l=s=>typeof s=="string"?s.split("|"):s,u=s=>{var t,e;return{validation:{rules:(t=s.validation)!=null&&t.rules?l(s.validation.rules):[],messages:((e=s.validation)==null?void 0:e.messages)||{}},value:s.value||null}};class h{constructor(){o(this,"collection",{})}all(){return this.collection}first(t){const e=this.get(t);return e?Array.isArray(e)?e[0]:e:null}any(){return Object.keys(this.collection).length>0}fill(t){return this.collection=t,this}push(t,e){return this.collection={...this.collection,[t]:e},this}has(t){return this.collection.hasOwnProperty(t)}get(t,e=null){return this.has(t)?this.collection[t]:e}unset(t){return this.has(t)&&delete this.collection[t],this}clear(){return this.collection={},this}}class F extends h{}class p extends h{}class b extends h{push(t,e){const r=this.get(t)||[];return this.collection={...this.collection,[t]:[...r,e]},this}get(t){return super.get(t,[])}}class j{constructor(){o(this,"errors",new b);o(this,"messages",new F);o(this,"rules",new p)}}const g={required:s=>new Promise((t,e)=>{s==null&&e(),String(s).replace(/\s/g,"").length>0?t(s):e()})},f=s=>s!==null&&typeof s=="object"&&!Array.isArray(s),v=(s,t,e=null)=>{const r=t||new FormData;return Object.keys(s).forEach(i=>{const a=s[i];if(i=e?`${e}[${i}]`:i,!([void 0,null].indexOf(a)>-1)){if(f(a)&&!(a instanceof File)||Array.isArray(a)){v(a,r,i);return}r.append(i,a)}}),r};class m{constructor(t){o(this,"awaiting",!1);o(this,"originalValues",{});o(this,"validation",new j);this.addFields(t)}addField(t,e){if(e!==null&&typeof e=="object"&&"value"in e){const r=u(e);this[t]=r.value,this.originalValues[t]=r.value,this.validation.messages.push(t,r.validation.messages),this.validation.rules.push(t,r.validation.rules)}else this[t]=e,this.originalValues[t]=e;return this}addFields(t){return Object.keys(t).forEach(e=>{this.addField(e,t[e])}),this}get errors(){return this.validation.errors}fill(t,e=!1){return Object.keys(t).forEach(r=>{let i=t[r];e&&(this.originalValues[r]=i),this[r]=i}),this}get messages(){return this.validation.messages}removeField(t){return delete this[t],delete this.originalValues[t],this.validation.messages.unset(t),this.validation.rules.unset(t),this}removeFields(t){return t.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(t=>{this[t]=this.originalValues[t]}),this}get rules(){return this.validation.rules}setAwaiting(t=!0){return this.awaiting=t,this}validate(t=null){return t?this.validateField(t):this.validateForm()}validateField(t){this.validation.errors.unset(t);const e=this.validation.rules.get(t);if(e&&e.length>0){const r=e.map(i=>{const a=i.split(":"),c=a[0],w=a.length===2?a[1].split(","):[];return c in g?g[c](this[t],w).catch(O=>{const d=this.validation.messages.get(t);return d&&c in d&&this.validation.errors.push(t,d[c]),Promise.reject(O)}):Promise.reject(new Error(`There is no validation rule called "${i}"`))});return Promise.all(r).then(()=>{})}return Promise.resolve()}validateForm(){const t=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(t).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(){const t={};return Object.keys(this.originalValues).forEach(e=>{t[e]=this[e]}),t}valuesAsFormData(){return v(this.values())}}const y=s=>new m(s);return n.createForm=y,n.default=m,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),n}({});
|
package/dist/form-wrapper.js
CHANGED
|
@@ -95,7 +95,7 @@ class O {
|
|
|
95
95
|
this.addFields(t);
|
|
96
96
|
}
|
|
97
97
|
addField(t, s) {
|
|
98
|
-
if (typeof s == "object" && "value" in s) {
|
|
98
|
+
if (s !== null && typeof s == "object" && "value" in s) {
|
|
99
99
|
const r = p(s);
|
|
100
100
|
this[t] = r.value, this.originalValues[t] = r.value, this.validation.messages.push(t, r.validation.messages), this.validation.rules.push(t, r.validation.rules);
|
|
101
101
|
} else
|
|
@@ -1 +1 @@
|
|
|
1
|
-
(function(r,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(r=typeof globalThis<"u"?globalThis:r||self,n(r.FormWrapper={}))})(this,function(r){"use strict";var P=Object.defineProperty;var V=(r,n,u)=>n in r?P(r,n,{enumerable:!0,configurable:!0,writable:!0,value:u}):r[n]=u;var
|
|
1
|
+
(function(r,n){typeof exports=="object"&&typeof module<"u"?n(exports):typeof define=="function"&&define.amd?define(["exports"],n):(r=typeof globalThis<"u"?globalThis:r||self,n(r.FormWrapper={}))})(this,function(r){"use strict";var P=Object.defineProperty;var V=(r,n,u)=>n in r?P(r,n,{enumerable:!0,configurable:!0,writable:!0,value:u}):r[n]=u;var o=(r,n,u)=>V(r,typeof n!="symbol"?n+"":n,u);const n=s=>typeof s=="string"?s.split("|"):s,u=s=>{var t,e;return{validation:{rules:(t=s.validation)!=null&&t.rules?n(s.validation.rules):[],messages:((e=s.validation)==null?void 0:e.messages)||{}},value:s.value||null}};class c{constructor(){o(this,"collection",{})}all(){return this.collection}first(t){const e=this.get(t);return e?Array.isArray(e)?e[0]:e:null}any(){return Object.keys(this.collection).length>0}fill(t){return this.collection=t,this}push(t,e){return this.collection={...this.collection,[t]:e},this}has(t){return this.collection.hasOwnProperty(t)}get(t,e=null){return this.has(t)?this.collection[t]:e}unset(t){return this.has(t)&&delete this.collection[t],this}clear(){return this.collection={},this}}class p extends c{}class f extends c{}class F extends c{push(t,e){const i=this.get(t)||[];return this.collection={...this.collection,[t]:[...i,e]},this}get(t){return super.get(t,[])}}class b{constructor(){o(this,"errors",new F);o(this,"messages",new p);o(this,"rules",new f)}}const g={required:s=>new Promise((t,e)=>{s==null&&e(),String(s).replace(/\s/g,"").length>0?t(s):e()})},j=s=>s!==null&&typeof s=="object"&&!Array.isArray(s),m=(s,t,e=null)=>{const i=t||new FormData;return Object.keys(s).forEach(a=>{const l=s[a];if(a=e?`${e}[${a}]`:a,!([void 0,null].indexOf(l)>-1)){if(j(l)&&!(l instanceof File)||Array.isArray(l)){m(l,i,a);return}i.append(a,l)}}),i};class v{constructor(t){o(this,"awaiting",!1);o(this,"originalValues",{});o(this,"validation",new b);this.addFields(t)}addField(t,e){if(e!==null&&typeof e=="object"&&"value"in e){const i=u(e);this[t]=i.value,this.originalValues[t]=i.value,this.validation.messages.push(t,i.validation.messages),this.validation.rules.push(t,i.validation.rules)}else this[t]=e,this.originalValues[t]=e;return this}addFields(t){return Object.keys(t).forEach(e=>{this.addField(e,t[e])}),this}get errors(){return this.validation.errors}fill(t,e=!1){return Object.keys(t).forEach(i=>{let a=t[i];e&&(this.originalValues[i]=a),this[i]=a}),this}get messages(){return this.validation.messages}removeField(t){return delete this[t],delete this.originalValues[t],this.validation.messages.unset(t),this.validation.rules.unset(t),this}removeFields(t){return t.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(t=>{this[t]=this.originalValues[t]}),this}get rules(){return this.validation.rules}setAwaiting(t=!0){return this.awaiting=t,this}validate(t=null){return t?this.validateField(t):this.validateForm()}validateField(t){this.validation.errors.unset(t);const e=this.validation.rules.get(t);if(e&&e.length>0){const i=e.map(a=>{const l=a.split(":"),h=l[0],w=l.length===2?l[1].split(","):[];return h in g?g[h](this[t],w).catch(O=>{const d=this.validation.messages.get(t);return d&&h in d&&this.validation.errors.push(t,d[h]),Promise.reject(O)}):Promise.reject(new Error(`There is no validation rule called "${a}"`))});return Promise.all(i).then(()=>{})}return Promise.resolve()}validateForm(){const t=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(t).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(){const t={};return Object.keys(this.originalValues).forEach(e=>{t[e]=this[e]}),t}valuesAsFormData(){return m(this.values())}}const y=s=>new v(s);r.createForm=y,r.default=v,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|