@keysdown/form-wrapper 0.0.14 → 1.0.5

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
@@ -1,5 +1,5 @@
1
1
  <h1 align="center">
2
- Form Wrapper (Beta)
2
+ Form Wrapper
3
3
  </h1>
4
4
 
5
5
  <p align="center">
@@ -11,8 +11,9 @@
11
11
 
12
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.
13
13
 
14
- > ![NOTE]
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.
14
+ <p align="center">
15
+ <strong>Bundle size (minified + gzip):</strong> ~1.4 kB
16
+ </p>
16
17
 
17
18
  ## Installation
18
19
 
@@ -271,6 +272,44 @@ form.reset()
271
272
  console.log(form.values()) // {username: null}
272
273
  ```
273
274
 
275
+ ### wasChanged(field)
276
+
277
+ Method used to check if one or more fields have been changed from their original values. When an array is provided, returns `true` if **any** of the fields were changed.
278
+
279
+ ```js
280
+ const form = createForm({
281
+ name: null,
282
+ username: null
283
+ })
284
+
285
+ console.log(form.wasChanged('username')) // false
286
+
287
+ form.username = 'keysdown'
288
+
289
+ console.log(form.wasChanged('username')) // true
290
+
291
+ console.log(form.wasChanged(['name', 'username'])) // true (any changed)
292
+ ```
293
+
294
+ ### filled(field)
295
+
296
+ Method used to check if one or more fields are filled (not `null`, not `undefined`, not empty string `''`). When an array is provided, returns `true` only if **all** fields are filled.
297
+
298
+ ```js
299
+ const form = createForm({
300
+ name: null,
301
+ username: null
302
+ })
303
+
304
+ form.username = 'keysdown'
305
+
306
+ console.log(form.filled('username')) // true
307
+
308
+ console.log(form.filled('name')) // false
309
+
310
+ console.log(form.filled(['name', 'username'])) // false (name is not filled)
311
+ ```
312
+
274
313
  ### setAwaiting(awaiting = true)
275
314
 
276
315
  Method used to change the form loading state.
@@ -551,10 +590,11 @@ form.rules.push('username', 'required')
551
590
 
552
591
  #### has(key)
553
592
 
554
- Checking if the collection has an item with the key.
593
+ Checking if the collection has an item with the key. Accepts a single key or an array of keys. When an array is provided, returns `true` if **any** of the keys exist.
555
594
 
556
595
  ```js
557
596
  form.errors.has('username')
597
+ form.errors.has(['username', 'email'])
558
598
  form.messages.has('username')
559
599
  form.rules.has('username')
560
600
  ```
@@ -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);if(e){const r=Array.isArray(e)?e[0]:e;if(r)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 u{}class j extends u{}class w 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 y{constructor(){a(this,"errors",new w);a(this,"messages",new f);a(this,"rules",new j)}}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 y);this.addFields(t)}addField(t,e){if(e!==null&&typeof e=="object"&&"value"in e&&!(e instanceof File)){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(t){const e={};return Object.keys(this.originalValues).forEach(r=>{(!t||t.includes(r))&&(e[r]=this[r])}),e}valuesAsFormData(t){return h(this.values(t))}}const V=s=>new d(s);exports.createForm=V;exports.default=d;
1
+ Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var e=e=>typeof e==`string`?e.split(`|`):e,t=t=>({validation:{rules:t.validation?.rules?e(t.validation.rules):[],messages:t.validation?.messages??{}},value:t.value??null}),n=class{collection={};all(){return this.collection}first(e){let t=this.get(e);if(t){let e=Array.isArray(t)?t[0]:t;if(e)return String(e)}return null}any(){return Object.keys(this.collection).length>0}fill(e){return this.collection=e,this}push(e,t){return this.collection={...this.collection,[e]:t},this}has(e){return Array.isArray(e)?e.some(e=>this.collection.hasOwnProperty(e)):this.collection.hasOwnProperty(e)}get(e,t=null){return this.has(e)?this.collection[e]:t}unset(e){return this.has(e)&&delete this.collection[e],this}clear(){return this.collection={},this}},r=class extends n{},i=class extends n{},a=class extends n{push(e,t){let n=this.get(e)||[];return this.collection={...this.collection,[e]:[...n,t]},this}get(e){return super.get(e,[])}},o=class{errors=new a;messages=new r;rules=new i},s={required:e=>new Promise((t,n)=>{if(e==null)return n();String(e).replace(/\s/g,``).length>0?t(e):n()})},c=e=>typeof e==`object`&&!!e&&!Array.isArray(e),l=(e,t,n=null)=>{let r=t||new FormData;return Object.keys(e).forEach(t=>{let i=e[t];if(t=n?`${n}[${t}]`:t,!([void 0,null].indexOf(i)>-1)){if(c(i)&&!(i instanceof File)||Array.isArray(i)){l(i,r,t);return}r.append(t,i)}}),r},u=class{awaiting=!1;originalValues={};validation=new o;constructor(e){this.addFields(e)}addField(e,n){if(typeof n==`object`&&n&&Object.prototype.toString.call(n)===`[object Object]`&&`value`in n){let r=t(n);this[e]=r.value,this.originalValues[e]=r.value,this.validation.messages.push(e,r.validation.messages),this.validation.rules.push(e,r.validation.rules)}else this[e]=n,this.originalValues[e]=n;return this}addFields(e){return Object.keys(e).forEach(t=>{this.addField(t,e[t])}),this}get errors(){return this.validation.errors}fill(e,t=!1){return Object.keys(e).forEach(n=>{let r=e[n];t&&(this.originalValues[n]=r),this[n]=r}),this}get messages(){return this.validation.messages}removeField(e){return delete this[e],delete this.originalValues[e],this.validation.errors.unset(e),this.validation.messages.unset(e),this.validation.rules.unset(e),this}removeFields(e){return e.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(e=>{this[e]=this.originalValues[e]}),this}wasChanged(e){return Array.isArray(e)?e.some(e=>this[e]!==this.originalValues[e]):this[e]!==this.originalValues[e]}filled(e){return Array.isArray(e)?e.every(e=>this[e]!==null&&this[e]!==void 0&&this[e]!==``):this[e]!==null&&this[e]!==void 0&&this[e]!==``}get rules(){return this.validation.rules}setAwaiting(e=!0){return this.awaiting=e,this}validate(e=null){return e?this.validateField(e):this.validateForm()}validateField(e){this.validation.errors.unset(e);let t=this.validation.rules.get(e);if(t&&t.length>0){let n=t.map(t=>{let n=t.split(`:`),r=n[0],i=n.length===2?n[1].split(`,`):[];return r in s?s[r](this[e],i).catch(t=>{let n=this.validation.messages.get(e);return n&&r in n&&this.validation.errors.push(e,n[r]),Promise.reject(t)}):Promise.reject(Error(`There is no validation rule called "${r}"`))});return Promise.all(n).then(()=>{})}return Promise.resolve()}validateForm(){let e=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(e).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(e){let t={};return Object.keys(this.originalValues).forEach(n=>{(!e||e.includes(n))&&(t[n]=this[n])}),t}valuesAsFormData(e){return l(this.values(e))}},d=e=>new u(e),f=u;exports.createForm=d,exports.default=f;
@@ -1,6 +1,6 @@
1
- export * from './main'
1
+ export * from './src/main'
2
2
  export {}
3
- import FormWrapper from './main'
3
+ import FormWrapper from './src/main'
4
4
  export default FormWrapper
5
- export * from './main'
5
+ export * from './src/main'
6
6
  export {}
@@ -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);if(e){const r=Array.isArray(e)?e[0]:e;if(r)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 f 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 b{constructor(){o(this,"errors",new f);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()})},j=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(j(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 b);this.addFields(t)}addField(t,e){if(e!==null&&typeof e=="object"&&"value"in e&&!(e instanceof File)){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],y=a.length===2?a[1].split(","):[];return c in g?g[c](this[t],y).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(t){const e={};return Object.keys(this.originalValues).forEach(r=>{(!t||t.includes(r))&&(e[r]=this[r])}),e}valuesAsFormData(t){return v(this.values(t))}}const w=s=>new m(s);return n.createForm=w,n.default=m,Object.defineProperties(n,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}}),n}({});
1
+ var FormWrapper=(function(e){Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var t=e=>typeof e==`string`?e.split(`|`):e,n=e=>({validation:{rules:e.validation?.rules?t(e.validation.rules):[],messages:e.validation?.messages??{}},value:e.value??null}),r=class{collection={};all(){return this.collection}first(e){let t=this.get(e);if(t){let e=Array.isArray(t)?t[0]:t;if(e)return String(e)}return null}any(){return Object.keys(this.collection).length>0}fill(e){return this.collection=e,this}push(e,t){return this.collection={...this.collection,[e]:t},this}has(e){return Array.isArray(e)?e.some(e=>this.collection.hasOwnProperty(e)):this.collection.hasOwnProperty(e)}get(e,t=null){return this.has(e)?this.collection[e]:t}unset(e){return this.has(e)&&delete this.collection[e],this}clear(){return this.collection={},this}},i=class extends r{},a=class extends r{},o=class extends r{push(e,t){let n=this.get(e)||[];return this.collection={...this.collection,[e]:[...n,t]},this}get(e){return super.get(e,[])}},s=class{errors=new o;messages=new i;rules=new a},c={required:e=>new Promise((t,n)=>{if(e==null)return n();String(e).replace(/\s/g,``).length>0?t(e):n()})},l=e=>typeof e==`object`&&!!e&&!Array.isArray(e),u=(e,t,n=null)=>{let r=t||new FormData;return Object.keys(e).forEach(t=>{let i=e[t];if(t=n?`${n}[${t}]`:t,!([void 0,null].indexOf(i)>-1)){if(l(i)&&!(i instanceof File)||Array.isArray(i)){u(i,r,t);return}r.append(t,i)}}),r},d=class{awaiting=!1;originalValues={};validation=new s;constructor(e){this.addFields(e)}addField(e,t){if(typeof t==`object`&&t&&Object.prototype.toString.call(t)===`[object Object]`&&`value`in t){let r=n(t);this[e]=r.value,this.originalValues[e]=r.value,this.validation.messages.push(e,r.validation.messages),this.validation.rules.push(e,r.validation.rules)}else this[e]=t,this.originalValues[e]=t;return this}addFields(e){return Object.keys(e).forEach(t=>{this.addField(t,e[t])}),this}get errors(){return this.validation.errors}fill(e,t=!1){return Object.keys(e).forEach(n=>{let r=e[n];t&&(this.originalValues[n]=r),this[n]=r}),this}get messages(){return this.validation.messages}removeField(e){return delete this[e],delete this.originalValues[e],this.validation.errors.unset(e),this.validation.messages.unset(e),this.validation.rules.unset(e),this}removeFields(e){return e.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(e=>{this[e]=this.originalValues[e]}),this}wasChanged(e){return Array.isArray(e)?e.some(e=>this[e]!==this.originalValues[e]):this[e]!==this.originalValues[e]}filled(e){return Array.isArray(e)?e.every(e=>this[e]!==null&&this[e]!==void 0&&this[e]!==``):this[e]!==null&&this[e]!==void 0&&this[e]!==``}get rules(){return this.validation.rules}setAwaiting(e=!0){return this.awaiting=e,this}validate(e=null){return e?this.validateField(e):this.validateForm()}validateField(e){this.validation.errors.unset(e);let t=this.validation.rules.get(e);if(t&&t.length>0){let n=t.map(t=>{let n=t.split(`:`),r=n[0],i=n.length===2?n[1].split(`,`):[];return r in c?c[r](this[e],i).catch(t=>{let n=this.validation.messages.get(e);return n&&r in n&&this.validation.errors.push(e,n[r]),Promise.reject(t)}):Promise.reject(Error(`There is no validation rule called "${r}"`))});return Promise.all(n).then(()=>{})}return Promise.resolve()}validateForm(){let e=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(e).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(e){let t={};return Object.keys(this.originalValues).forEach(n=>{(!e||e.includes(n))&&(t[n]=this[n])}),t}valuesAsFormData(e){return u(this.values(e))}},f=e=>new d(e),p=d;return e.createForm=f,e.default=p,e})({});
@@ -1,187 +1,164 @@
1
- var v = Object.defineProperty;
2
- var m = (e, t, s) => t in e ? v(e, t, { enumerable: !0, configurable: !0, writable: !0, value: s }) : e[t] = s;
3
- var a = (e, t, s) => m(e, typeof t != "symbol" ? t + "" : t, s);
4
- const F = (e) => typeof e == "string" ? e.split("|") : e, p = (e) => {
5
- var t, s;
6
- return {
7
- validation: {
8
- rules: (t = e.validation) != null && t.rules ? F(e.validation.rules) : [],
9
- messages: ((s = e.validation) == null ? void 0 : s.messages) || {}
10
- },
11
- value: e.value || null
12
- };
13
- };
14
- class h {
15
- constructor() {
16
- a(this, "collection", {});
17
- }
18
- all() {
19
- return this.collection;
20
- }
21
- first(t) {
22
- const s = this.get(t);
23
- if (s) {
24
- const i = Array.isArray(s) ? s[0] : s;
25
- if (i)
26
- return String(i);
27
- }
28
- return null;
29
- }
30
- any() {
31
- return Object.keys(this.collection).length > 0;
32
- }
33
- fill(t) {
34
- return this.collection = t, this;
35
- }
36
- push(t, s) {
37
- return this.collection = {
38
- ...this.collection,
39
- [t]: s
40
- }, this;
41
- }
42
- has(t) {
43
- return this.collection.hasOwnProperty(t);
44
- }
45
- get(t, s = null) {
46
- return this.has(t) ? this.collection[t] : s;
47
- }
48
- unset(t) {
49
- return this.has(t) && delete this.collection[t], this;
50
- }
51
- clear() {
52
- return this.collection = {}, this;
53
- }
54
- }
55
- class f extends h {
56
- }
57
- class j extends h {
58
- }
59
- class b extends h {
60
- push(t, s) {
61
- const i = this.get(t) || [];
62
- return this.collection = {
63
- ...this.collection,
64
- [t]: [...i, s]
65
- }, this;
66
- }
67
- get(t) {
68
- return super.get(t, []);
69
- }
70
- }
71
- class w {
72
- constructor() {
73
- a(this, "errors", new b());
74
- a(this, "messages", new f());
75
- a(this, "rules", new j());
76
- }
77
- }
78
- const y = (e) => new Promise((t, s) => {
79
- e == null && s(), String(e).replace(/\s/g, "").length > 0 ? t(e) : s();
80
- }), c = {
81
- required: y
82
- }, V = (e) => e !== null && typeof e == "object" && !Array.isArray(e), u = (e, t, s = null) => {
83
- const i = t || new FormData();
84
- return Object.keys(e).forEach((r) => {
85
- const n = e[r];
86
- if (r = s ? `${s}[${r}]` : r, !([void 0, null].indexOf(n) > -1)) {
87
- if (V(n) && !(n instanceof File) || Array.isArray(n)) {
88
- u(n, i, r);
89
- return;
90
- }
91
- i.append(r, n);
92
- }
93
- }), i;
94
- };
95
- class O {
96
- constructor(t) {
97
- a(this, "awaiting", !1);
98
- a(this, "originalValues", {});
99
- a(this, "validation", new w());
100
- this.addFields(t);
101
- }
102
- addField(t, s) {
103
- if (s !== null && typeof s == "object" && "value" in s && !(s instanceof File)) {
104
- const i = p(s);
105
- 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);
106
- } else
107
- this[t] = s, this.originalValues[t] = s;
108
- return this;
109
- }
110
- addFields(t) {
111
- return Object.keys(t).forEach((s) => {
112
- this.addField(s, t[s]);
113
- }), this;
114
- }
115
- get errors() {
116
- return this.validation.errors;
117
- }
118
- fill(t, s = !1) {
119
- return Object.keys(t).forEach((i) => {
120
- let r = t[i];
121
- s && (this.originalValues[i] = r), this[i] = r;
122
- }), this;
123
- }
124
- get messages() {
125
- return this.validation.messages;
126
- }
127
- removeField(t) {
128
- return delete this[t], delete this.originalValues[t], this.validation.messages.unset(t), this.validation.rules.unset(t), this;
129
- }
130
- removeFields(t) {
131
- return t.forEach((s) => {
132
- this.removeField(s);
133
- }), this;
134
- }
135
- reset() {
136
- return this.validation.errors.clear(), Object.keys(this.originalValues).forEach((t) => {
137
- this[t] = this.originalValues[t];
138
- }), this;
139
- }
140
- get rules() {
141
- return this.validation.rules;
142
- }
143
- setAwaiting(t = !0) {
144
- return this.awaiting = t, this;
145
- }
146
- validate(t = null) {
147
- return t ? this.validateField(t) : this.validateForm();
148
- }
149
- validateField(t) {
150
- this.validation.errors.unset(t);
151
- const s = this.validation.rules.get(t);
152
- if (s && s.length > 0) {
153
- const i = s.map(
154
- (r) => {
155
- const n = r.split(":"), l = n[0], d = n.length === 2 ? n[1].split(",") : [];
156
- return l in c ? c[l](this[t], d).catch((g) => {
157
- const o = this.validation.messages.get(t);
158
- return o && l in o && this.validation.errors.push(t, o[l]), Promise.reject(g);
159
- }) : Promise.reject(new Error(`There is no validation rule called "${r}"`));
160
- }
161
- );
162
- return Promise.all(i).then(() => {
163
- });
164
- }
165
- return Promise.resolve();
166
- }
167
- validateForm() {
168
- const t = Object.keys(this.originalValues).map(
169
- (s) => this.validateField(s)
170
- );
171
- return Promise.all(t).then(() => Promise.resolve(this)).catch(() => Promise.reject(this));
172
- }
173
- values(t) {
174
- const s = {};
175
- return Object.keys(this.originalValues).forEach((i) => {
176
- (!t || t.includes(i)) && (s[i] = this[i]);
177
- }), s;
178
- }
179
- valuesAsFormData(t) {
180
- return u(this.values(t));
181
- }
182
- }
183
- const A = (e) => new O(e);
184
- export {
185
- A as createForm,
186
- O as default
187
- };
1
+ //#region src/utils/fields.ts
2
+ var e = (e) => typeof e == "string" ? e.split("|") : e, t = (t) => ({
3
+ validation: {
4
+ rules: t.validation?.rules ? e(t.validation.rules) : [],
5
+ messages: t.validation?.messages ?? {}
6
+ },
7
+ value: t.value ?? null
8
+ }), n = class {
9
+ collection = {};
10
+ all() {
11
+ return this.collection;
12
+ }
13
+ first(e) {
14
+ let t = this.get(e);
15
+ if (t) {
16
+ let e = Array.isArray(t) ? t[0] : t;
17
+ if (e) return String(e);
18
+ }
19
+ return null;
20
+ }
21
+ any() {
22
+ return Object.keys(this.collection).length > 0;
23
+ }
24
+ fill(e) {
25
+ return this.collection = e, this;
26
+ }
27
+ push(e, t) {
28
+ return this.collection = {
29
+ ...this.collection,
30
+ [e]: t
31
+ }, this;
32
+ }
33
+ has(e) {
34
+ return Array.isArray(e) ? e.some((e) => this.collection.hasOwnProperty(e)) : this.collection.hasOwnProperty(e);
35
+ }
36
+ get(e, t = null) {
37
+ return this.has(e) ? this.collection[e] : t;
38
+ }
39
+ unset(e) {
40
+ return this.has(e) && delete this.collection[e], this;
41
+ }
42
+ clear() {
43
+ return this.collection = {}, this;
44
+ }
45
+ }, r = class extends n {}, i = class extends n {}, a = class extends n {
46
+ push(e, t) {
47
+ let n = this.get(e) || [];
48
+ return this.collection = {
49
+ ...this.collection,
50
+ [e]: [...n, t]
51
+ }, this;
52
+ }
53
+ get(e) {
54
+ return super.get(e, []);
55
+ }
56
+ }, o = class {
57
+ errors = new a();
58
+ messages = new r();
59
+ rules = new i();
60
+ }, s = { required: (e) => new Promise((t, n) => {
61
+ if (e == null) return n();
62
+ String(e).replace(/\s/g, "").length > 0 ? t(e) : n();
63
+ }) }, c = (e) => typeof e == "object" && !!e && !Array.isArray(e), l = (e, t, n = null) => {
64
+ let r = t || new FormData();
65
+ return Object.keys(e).forEach((t) => {
66
+ let i = e[t];
67
+ if (t = n ? `${n}[${t}]` : t, !([void 0, null].indexOf(i) > -1)) {
68
+ if (c(i) && !(i instanceof File) || Array.isArray(i)) {
69
+ l(i, r, t);
70
+ return;
71
+ }
72
+ r.append(t, i);
73
+ }
74
+ }), r;
75
+ }, u = class {
76
+ awaiting = !1;
77
+ originalValues = {};
78
+ validation = new o();
79
+ constructor(e) {
80
+ this.addFields(e);
81
+ }
82
+ addField(e, n) {
83
+ if (typeof n == "object" && n && Object.prototype.toString.call(n) === "[object Object]" && "value" in n) {
84
+ let r = t(n);
85
+ this[e] = r.value, this.originalValues[e] = r.value, this.validation.messages.push(e, r.validation.messages), this.validation.rules.push(e, r.validation.rules);
86
+ } else this[e] = n, this.originalValues[e] = n;
87
+ return this;
88
+ }
89
+ addFields(e) {
90
+ return Object.keys(e).forEach((t) => {
91
+ this.addField(t, e[t]);
92
+ }), this;
93
+ }
94
+ get errors() {
95
+ return this.validation.errors;
96
+ }
97
+ fill(e, t = !1) {
98
+ return Object.keys(e).forEach((n) => {
99
+ let r = e[n];
100
+ t && (this.originalValues[n] = r), this[n] = r;
101
+ }), this;
102
+ }
103
+ get messages() {
104
+ return this.validation.messages;
105
+ }
106
+ removeField(e) {
107
+ return delete this[e], delete this.originalValues[e], this.validation.errors.unset(e), this.validation.messages.unset(e), this.validation.rules.unset(e), this;
108
+ }
109
+ removeFields(e) {
110
+ return e.forEach((e) => {
111
+ this.removeField(e);
112
+ }), this;
113
+ }
114
+ reset() {
115
+ return this.validation.errors.clear(), Object.keys(this.originalValues).forEach((e) => {
116
+ this[e] = this.originalValues[e];
117
+ }), this;
118
+ }
119
+ wasChanged(e) {
120
+ return Array.isArray(e) ? e.some((e) => this[e] !== this.originalValues[e]) : this[e] !== this.originalValues[e];
121
+ }
122
+ filled(e) {
123
+ return Array.isArray(e) ? e.every((e) => this[e] !== null && this[e] !== void 0 && this[e] !== "") : this[e] !== null && this[e] !== void 0 && this[e] !== "";
124
+ }
125
+ get rules() {
126
+ return this.validation.rules;
127
+ }
128
+ setAwaiting(e = !0) {
129
+ return this.awaiting = e, this;
130
+ }
131
+ validate(e = null) {
132
+ return e ? this.validateField(e) : this.validateForm();
133
+ }
134
+ validateField(e) {
135
+ this.validation.errors.unset(e);
136
+ let t = this.validation.rules.get(e);
137
+ if (t && t.length > 0) {
138
+ let n = t.map((t) => {
139
+ let n = t.split(":"), r = n[0], i = n.length === 2 ? n[1].split(",") : [];
140
+ return r in s ? s[r](this[e], i).catch((t) => {
141
+ let n = this.validation.messages.get(e);
142
+ return n && r in n && this.validation.errors.push(e, n[r]), Promise.reject(t);
143
+ }) : Promise.reject(/* @__PURE__ */ Error(`There is no validation rule called "${r}"`));
144
+ });
145
+ return Promise.all(n).then(() => {});
146
+ }
147
+ return Promise.resolve();
148
+ }
149
+ validateForm() {
150
+ let e = Object.keys(this.originalValues).map((e) => this.validateField(e));
151
+ return Promise.all(e).then(() => Promise.resolve(this)).catch(() => Promise.reject(this));
152
+ }
153
+ values(e) {
154
+ let t = {};
155
+ return Object.keys(this.originalValues).forEach((n) => {
156
+ (!e || e.includes(n)) && (t[n] = this[n]);
157
+ }), t;
158
+ }
159
+ valuesAsFormData(e) {
160
+ return l(this.values(e));
161
+ }
162
+ }, d = (e) => new u(e), f = u;
163
+ //#endregion
164
+ export { d as createForm, f as default };
@@ -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 l=(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 h{constructor(){l(this,"collection",{})}all(){return this.collection}first(t){const e=this.get(t);if(e){const i=Array.isArray(e)?e[0]:e;if(i)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 f extends h{}class p extends h{}class F extends h{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(){l(this,"errors",new F);l(this,"messages",new f);l(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()})},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 o=s[a];if(a=e?`${e}[${a}]`:a,!([void 0,null].indexOf(o)>-1)){if(j(o)&&!(o instanceof File)||Array.isArray(o)){m(o,i,a);return}i.append(a,o)}}),i};class v{constructor(t){l(this,"awaiting",!1);l(this,"originalValues",{});l(this,"validation",new b);this.addFields(t)}addField(t,e){if(e!==null&&typeof e=="object"&&"value"in e&&!(e instanceof File)){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 o=a.split(":"),c=o[0],w=o.length===2?o[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 "${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(t){const e={};return Object.keys(this.originalValues).forEach(i=>{(!t||t.includes(i))&&(e[i]=this[i])}),e}valuesAsFormData(t){return m(this.values(t))}}const y=s=>new v(s);r.createForm=y,r.default=v,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
1
+ (function(e,t){typeof exports==`object`&&typeof module<`u`?t(exports):typeof define==`function`&&define.amd?define([`exports`],t):(e=typeof globalThis<`u`?globalThis:e||self,t(e.FormWrapper={}))})(this,function(e){Object.defineProperties(e,{__esModule:{value:!0},[Symbol.toStringTag]:{value:`Module`}});var t=e=>typeof e==`string`?e.split(`|`):e,n=e=>({validation:{rules:e.validation?.rules?t(e.validation.rules):[],messages:e.validation?.messages??{}},value:e.value??null}),r=class{collection={};all(){return this.collection}first(e){let t=this.get(e);if(t){let e=Array.isArray(t)?t[0]:t;if(e)return String(e)}return null}any(){return Object.keys(this.collection).length>0}fill(e){return this.collection=e,this}push(e,t){return this.collection={...this.collection,[e]:t},this}has(e){return Array.isArray(e)?e.some(e=>this.collection.hasOwnProperty(e)):this.collection.hasOwnProperty(e)}get(e,t=null){return this.has(e)?this.collection[e]:t}unset(e){return this.has(e)&&delete this.collection[e],this}clear(){return this.collection={},this}},i=class extends r{},a=class extends r{},o=class extends r{push(e,t){let n=this.get(e)||[];return this.collection={...this.collection,[e]:[...n,t]},this}get(e){return super.get(e,[])}},s=class{errors=new o;messages=new i;rules=new a},c={required:e=>new Promise((t,n)=>{if(e==null)return n();String(e).replace(/\s/g,``).length>0?t(e):n()})},l=e=>typeof e==`object`&&!!e&&!Array.isArray(e),u=(e,t,n=null)=>{let r=t||new FormData;return Object.keys(e).forEach(t=>{let i=e[t];if(t=n?`${n}[${t}]`:t,!([void 0,null].indexOf(i)>-1)){if(l(i)&&!(i instanceof File)||Array.isArray(i)){u(i,r,t);return}r.append(t,i)}}),r},d=class{awaiting=!1;originalValues={};validation=new s;constructor(e){this.addFields(e)}addField(e,t){if(typeof t==`object`&&t&&Object.prototype.toString.call(t)===`[object Object]`&&`value`in t){let r=n(t);this[e]=r.value,this.originalValues[e]=r.value,this.validation.messages.push(e,r.validation.messages),this.validation.rules.push(e,r.validation.rules)}else this[e]=t,this.originalValues[e]=t;return this}addFields(e){return Object.keys(e).forEach(t=>{this.addField(t,e[t])}),this}get errors(){return this.validation.errors}fill(e,t=!1){return Object.keys(e).forEach(n=>{let r=e[n];t&&(this.originalValues[n]=r),this[n]=r}),this}get messages(){return this.validation.messages}removeField(e){return delete this[e],delete this.originalValues[e],this.validation.errors.unset(e),this.validation.messages.unset(e),this.validation.rules.unset(e),this}removeFields(e){return e.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(e=>{this[e]=this.originalValues[e]}),this}wasChanged(e){return Array.isArray(e)?e.some(e=>this[e]!==this.originalValues[e]):this[e]!==this.originalValues[e]}filled(e){return Array.isArray(e)?e.every(e=>this[e]!==null&&this[e]!==void 0&&this[e]!==``):this[e]!==null&&this[e]!==void 0&&this[e]!==``}get rules(){return this.validation.rules}setAwaiting(e=!0){return this.awaiting=e,this}validate(e=null){return e?this.validateField(e):this.validateForm()}validateField(e){this.validation.errors.unset(e);let t=this.validation.rules.get(e);if(t&&t.length>0){let n=t.map(t=>{let n=t.split(`:`),r=n[0],i=n.length===2?n[1].split(`,`):[];return r in c?c[r](this[e],i).catch(t=>{let n=this.validation.messages.get(e);return n&&r in n&&this.validation.errors.push(e,n[r]),Promise.reject(t)}):Promise.reject(Error(`There is no validation rule called "${r}"`))});return Promise.all(n).then(()=>{})}return Promise.resolve()}validateForm(){let e=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(e).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(e){let t={};return Object.keys(this.originalValues).forEach(n=>{(!e||e.includes(n))&&(t[n]=this[n])}),t}valuesAsFormData(e){return u(this.values(e))}},f=e=>new d(e),p=d;e.createForm=f,e.default=p});
@@ -17,6 +17,8 @@ export declare class Form {
17
17
  removeField(field: string): this;
18
18
  removeFields(fields: string[]): this;
19
19
  reset(): this;
20
+ wasChanged(field: string | string[]): boolean;
21
+ filled(field: string | string[]): boolean;
20
22
  get rules(): import('./Rules').Rules;
21
23
  setAwaiting(awaiting?: boolean): this;
22
24
  validate(field?: string | null): Promise<this | void>;
@@ -6,7 +6,7 @@ export declare class Collection<T> {
6
6
  any(): boolean;
7
7
  fill(items: Items<T>): this;
8
8
  push(key: string, data: any): this;
9
- has(key: string): boolean;
9
+ has(key: string | string[]): boolean;
10
10
  get(item: string, defaultValue?: T | null): T | null;
11
11
  unset(key: string): this;
12
12
  clear(): this;
@@ -1,3 +1,3 @@
1
1
  import { Rules } from '../types/rules';
2
- export declare const required: (value: string) => Promise<any>;
2
+ export declare const required: (value: any) => Promise<any>;
3
3
  export declare const Rule: Rules;
package/package.json CHANGED
@@ -1,6 +1,14 @@
1
1
  {
2
2
  "name": "@keysdown/form-wrapper",
3
- "version": "0.0.14",
3
+ "version": "1.0.5",
4
+ "repository": {
5
+ "type": "git",
6
+ "url": "https://github.com/keysdown/form-wrapper.git"
7
+ },
8
+ "publishConfig": {
9
+ "access": "public",
10
+ "registry": "https://registry.npmjs.org"
11
+ },
4
12
  "type": "module",
5
13
  "files": [
6
14
  "dist/*"
@@ -18,12 +26,17 @@
18
26
  "scripts": {
19
27
  "dev": "vite",
20
28
  "build": "tsc && vite build",
21
- "preview": "vite preview"
29
+ "preview": "vite preview",
30
+ "test": "vitest run",
31
+ "test:watch": "vitest",
32
+ "test:coverage": "vitest run --coverage"
22
33
  },
23
34
  "devDependencies": {
24
- "@types/node": "^22.7.4",
25
- "typescript": "^5.5.3",
26
- "vite": "^5.4.1",
27
- "vite-plugin-dts": "^4.2.3"
35
+ "@types/node": "^25.7.0",
36
+ "@vitest/coverage-v8": "^4.1.6",
37
+ "typescript": "^6.0.3",
38
+ "vite": "^8.0.12",
39
+ "vite-plugin-dts": "^5.0.0",
40
+ "vitest": "^4.1.6"
28
41
  }
29
42
  }
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes