@keysdown/form-wrapper 0.0.8 → 0.0.10
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
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);
|
|
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);if(e){const r=Array.isArray(e)?e[0]:e;return String(r)}return 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()}),c={required:O},P=s=>s!==null&&typeof s=="object"&&!Array.isArray(s),h=(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)){h(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 c?c[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 h(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);
|
|
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);if(e){const r=Array.isArray(e)?e[0]:e;return String(r)}return 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
|
@@ -20,7 +20,11 @@ class h {
|
|
|
20
20
|
}
|
|
21
21
|
first(t) {
|
|
22
22
|
const s = this.get(t);
|
|
23
|
-
|
|
23
|
+
if (s) {
|
|
24
|
+
const r = Array.isArray(s) ? s[0] : s;
|
|
25
|
+
return String(r);
|
|
26
|
+
}
|
|
27
|
+
return null;
|
|
24
28
|
}
|
|
25
29
|
any() {
|
|
26
30
|
return Object.keys(this.collection).length > 0;
|
|
@@ -95,7 +99,7 @@ class O {
|
|
|
95
99
|
this.addFields(t);
|
|
96
100
|
}
|
|
97
101
|
addField(t, s) {
|
|
98
|
-
if (typeof s == "object" && "value" in s) {
|
|
102
|
+
if (s !== null && typeof s == "object" && "value" in s) {
|
|
99
103
|
const r = p(s);
|
|
100
104
|
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
105
|
} 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);if(e){const i=Array.isArray(e)?e[0]:e;return String(i)}return 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),v=(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)){v(l,i,a);return}i.append(a,l)}}),i};class m{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 v(this.values())}}const y=s=>new m(s);r.createForm=y,r.default=m,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
|
|
@@ -2,7 +2,7 @@ import { Items } from '../types/collections';
|
|
|
2
2
|
export declare class Collection<T> {
|
|
3
3
|
collection: Items<T>;
|
|
4
4
|
all(): Items<T>;
|
|
5
|
-
first(key: string):
|
|
5
|
+
first(key: string): string | null;
|
|
6
6
|
any(): boolean;
|
|
7
7
|
fill(items: Items<T>): this;
|
|
8
8
|
push(key: string, data: any): this;
|