@keysdown/form-wrapper 0.0.13 → 1.0.2

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,9 +11,6 @@
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.
16
-
17
14
  ## Installation
18
15
 
19
16
  ```shell
@@ -296,7 +293,7 @@ console.log(form.awaiting) // false
296
293
  Method used to validate the entire form or a specific field.
297
294
 
298
295
  ```js
299
- const form = createForm({
296
+ const form = createForm({
300
297
  username: {
301
298
  value: null,
302
299
  rules: ['required'],
@@ -330,7 +327,7 @@ form.validate()
330
327
  Method used to validate a specific field.
331
328
 
332
329
  ```js
333
- const form = createForm({
330
+ const form = createForm({
334
331
  username: {
335
332
  value: null,
336
333
  rules: ['required'],
@@ -364,7 +361,7 @@ form.validate('username')
364
361
  Method used to validate the entire form.
365
362
 
366
363
  ```js
367
- const form = createForm({
364
+ const form = createForm({
368
365
  username: {
369
366
  value: null,
370
367
  rules: ['required'],
@@ -400,7 +397,7 @@ form.validate()
400
397
  Method used to access all form values in json format.
401
398
 
402
399
  ```js
403
- const form = createForm({
400
+ const form = createForm({
404
401
  username: null
405
402
  })
406
403
 
@@ -409,12 +406,27 @@ form.username = 'keysdown'
409
406
  axios.post('some-api', form.values())
410
407
  ```
411
408
 
409
+ #### Filtering values
410
+
411
+ It is also possible to filter all accessed form values.
412
+
413
+ ```js
414
+ const form = createForm({
415
+ first_name: null,
416
+ last_name: null
417
+ })
418
+
419
+ form.first_name = 'keysdown'
420
+
421
+ axios.post('some-api', form.values(['first_name']))
422
+ ```
423
+
412
424
  ### valuesAsFormData()
413
425
 
414
426
  Method used to access all form values as form data.
415
427
 
416
428
  ```js
417
- const form = createForm({
429
+ const form = createForm({
418
430
  username: null
419
431
  })
420
432
 
@@ -427,6 +439,21 @@ axios.post('some-api', form.valuesAsFormData(), {
427
439
  })
428
440
  ```
429
441
 
442
+ #### Filtering values
443
+
444
+ It is also possible to filter all accessed form values.
445
+
446
+ ```js
447
+ const form = createForm({
448
+ first_name: null,
449
+ last_name: null
450
+ })
451
+
452
+ form.first_name = 'keysdown'
453
+
454
+ axios.post('some-api', form.valuesAsFormData(['first_name']))
455
+ ```
456
+
430
457
  ## Validation
431
458
 
432
459
  There is a property in the form object dedicated to validations, you can access everything related to validations through the `validation` property:
@@ -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 y 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 y);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 w);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(){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
+ 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 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}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],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(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 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}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,158 @@
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() {
174
- const t = {};
175
- return Object.keys(this.originalValues).forEach((s) => {
176
- t[s] = this[s];
177
- }), t;
178
- }
179
- valuesAsFormData() {
180
- return u(this.values());
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 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
+ get rules() {
120
+ return this.validation.rules;
121
+ }
122
+ setAwaiting(e = !0) {
123
+ return this.awaiting = e, this;
124
+ }
125
+ validate(e = null) {
126
+ return e ? this.validateField(e) : this.validateForm();
127
+ }
128
+ validateField(e) {
129
+ this.validation.errors.unset(e);
130
+ let t = this.validation.rules.get(e);
131
+ if (t && t.length > 0) {
132
+ let n = t.map((t) => {
133
+ let n = t.split(":"), r = n[0], i = n.length === 2 ? n[1].split(",") : [];
134
+ return r in s ? s[r](this[e], i).catch((t) => {
135
+ let n = this.validation.messages.get(e);
136
+ return n && r in n && this.validation.errors.push(e, n[r]), Promise.reject(t);
137
+ }) : Promise.reject(/* @__PURE__ */ Error(`There is no validation rule called "${r}"`));
138
+ });
139
+ return Promise.all(n).then(() => {});
140
+ }
141
+ return Promise.resolve();
142
+ }
143
+ validateForm() {
144
+ let e = Object.keys(this.originalValues).map((e) => this.validateField(e));
145
+ return Promise.all(e).then(() => Promise.resolve(this)).catch(() => Promise.reject(this));
146
+ }
147
+ values(e) {
148
+ let t = {};
149
+ return Object.keys(this.originalValues).forEach((n) => {
150
+ (!e || e.includes(n)) && (t[n] = this[n]);
151
+ }), t;
152
+ }
153
+ valuesAsFormData(e) {
154
+ return l(this.values(e));
155
+ }
156
+ }, d = (e) => new u(e), f = u;
157
+ //#endregion
158
+ 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(){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"}})});
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 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}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});
@@ -22,6 +22,6 @@ export declare class Form {
22
22
  validate(field?: string | null): Promise<this | void>;
23
23
  validateField(field: string): Promise<void>;
24
24
  validateForm(): Promise<this>;
25
- values(): Values;
26
- valuesAsFormData(): FormData;
25
+ values(only?: string[]): Values;
26
+ valuesAsFormData(only?: string[]): FormData;
27
27
  }
@@ -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.13",
3
+ "version": "1.0.2",
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
File without changes