@keysdown/form-wrapper 0.0.4 → 0.0.6

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
@@ -17,7 +17,6 @@ npm install @keysdown/form-wrapper --save
17
17
  Basic example using Vue.
18
18
 
19
19
  ```vue
20
-
21
20
  <template>
22
21
  <form @submit.prevent="submit">
23
22
  <input type="text" v-model="form.first_name"/>
@@ -30,9 +29,9 @@ Basic example using Vue.
30
29
  <script setup lang="ts">
31
30
  import axios from 'axios'
32
31
  import {ref} from 'vue'
33
- import Form from '@keysdown/form-wrapper'
32
+ import {createForm} from '@keysdown/form-wrapper'
34
33
 
35
- const form = ref(Form({
34
+ const form = ref(createForm({
36
35
  first_name: null,
37
36
  last_name: null,
38
37
  username: null
@@ -73,9 +72,9 @@ Basic example with validation using Vue.
73
72
  <script setup lang="ts">
74
73
  import axios from 'axios'
75
74
  import {ref} from 'vue'
76
- import Form from '@keysdown/form-wrapper'
75
+ import {createForm} from '@keysdown/form-wrapper'
77
76
 
78
- const form = ref(Form({
77
+ const form = ref(createForm({
79
78
  first_name: {
80
79
  value: null,
81
80
  rules: ['required'],
@@ -179,7 +178,7 @@ form.addFields({
179
178
  Method used when you want to fill in several form fields at once.
180
179
 
181
180
  ```js
182
- const form = Form({
181
+ const form = createForm({
183
182
  username: null
184
183
  })
185
184
 
@@ -197,7 +196,7 @@ console.log(form.values()) // {username: 'keysdown'}
197
196
  Method used when you want to remove a field from the form.
198
197
 
199
198
  ```js
200
- const form = Form({
199
+ const form = createForm({
201
200
  username: null
202
201
  })
203
202
 
@@ -213,7 +212,7 @@ console.log(form.values()) // {}
213
212
  Method used to remove multiple fields to the form.
214
213
 
215
214
  ```js
216
- const form = Form({
215
+ const form = createForm({
217
216
  username: null
218
217
  })
219
218
 
@@ -229,7 +228,7 @@ console.log(form.values()) // {}
229
228
  Method used to reset all form values to original state.
230
229
 
231
230
  ```js
232
- const form = Form({
231
+ const form = createForm({
233
232
  username: null
234
233
  })
235
234
 
@@ -247,7 +246,7 @@ console.log(form.values()) // {username: null}
247
246
  Method used to change the form loading state.
248
247
 
249
248
  ```js
250
- const form = Form({
249
+ const form = createForm({
251
250
  username: null
252
251
  })
253
252
 
@@ -267,7 +266,7 @@ console.log(form.awaiting) // false
267
266
  Method used to validate the entire form or a specific field.
268
267
 
269
268
  ```js
270
- const form = Form({
269
+ const form = createForm({
271
270
  username: {
272
271
  value: null,
273
272
  rules: ['required'],
@@ -301,7 +300,7 @@ form.validate()
301
300
  Method used to validate a specific field.
302
301
 
303
302
  ```js
304
- const form = Form({
303
+ const form = createForm({
305
304
  username: {
306
305
  value: null,
307
306
  rules: ['required'],
@@ -335,7 +334,7 @@ form.validate('username')
335
334
  Method used to validate the entire form.
336
335
 
337
336
  ```js
338
- const form = Form({
337
+ const form = createForm({
339
338
  username: {
340
339
  value: null,
341
340
  rules: ['required'],
@@ -371,7 +370,7 @@ form.validate()
371
370
  Method used to access all form values in json format.
372
371
 
373
372
  ```js
374
- const form = Form({
373
+ const form = createForm({
375
374
  username: null
376
375
  })
377
376
 
@@ -385,7 +384,7 @@ axios.post('some-api', form.values())
385
384
  Method used to access all form values as form data.
386
385
 
387
386
  ```js
388
- const form = Form({
387
+ const form = createForm({
389
388
  username: null
390
389
  })
391
390
 
@@ -1,6 +1,6 @@
1
- import { Field, Fields, RawFields } from '../types/fields.ts';
2
- import { Values } from '../types/values.ts';
3
- import { Validation } from './Validation.ts';
1
+ import { Field, Fields, RawFields } from '../types/fields';
2
+ import { Values } from '../types/values';
3
+ import { Validation } from './Validation';
4
4
  export declare class Form {
5
5
  [key: string]: any;
6
6
  awaiting: boolean;
@@ -9,15 +9,15 @@ export declare class Form {
9
9
  constructor(fields: Fields | RawFields);
10
10
  addField(field: string, value: Field): this;
11
11
  addFields(fields: Fields | RawFields): this;
12
- get errors(): import('./Errors.ts').Errors;
12
+ get errors(): import('./Errors').Errors;
13
13
  fill(data: {
14
14
  [key: string]: any;
15
15
  }, updateOriginalValues?: boolean): this;
16
- get messages(): import('./Messages.ts').Messages;
16
+ get messages(): import('./Messages').Messages;
17
17
  removeField(field: string): this;
18
18
  removeFields(fields: string[]): this;
19
19
  reset(): this;
20
- get rules(): import('./Rules.ts').Rules;
20
+ get rules(): import('./Rules').Rules;
21
21
  setAwaiting(awaiting?: boolean): this;
22
22
  validate(field?: string | null): Promise<this | void>;
23
23
  validateField(field: string): Promise<void>;
@@ -1,4 +1,4 @@
1
1
  import { Collection } from '../utils/collections';
2
- import { ErrorMessage } from '../types/messages.ts';
2
+ import { ErrorMessage } from '../types/messages';
3
3
  export declare class Messages extends Collection<ErrorMessage> {
4
4
  }
@@ -1,6 +1,6 @@
1
- import { Messages } from './Messages.ts';
2
- import { Rules } from './Rules.ts';
3
- import { Errors } from './Errors.ts';
1
+ import { Messages } from './Messages';
2
+ import { Rules } from './Rules';
3
+ import { Errors } from './Errors';
4
4
  export declare class Validation {
5
5
  errors: Errors;
6
6
  messages: Messages;
@@ -0,0 +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;
@@ -0,0 +1,6 @@
1
+ export * from './main'
2
+ export {}
3
+ import FormWrapper from './main'
4
+ export default FormWrapper
5
+ export * from './main'
6
+ export {}
@@ -0,0 +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}({});
package/dist/main.d.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { Form } from './core/Form.ts';
2
- declare const createForm: (data: any) => Form;
3
- export { createForm };
4
- export default Form;
1
+ import { Form as FormWrapper } from './core/Form';
2
+ export declare const createForm: (data: any) => FormWrapper;
3
+ export default FormWrapper;
@@ -1,3 +1,3 @@
1
- import { Rules } from '../types/rules.ts';
1
+ import { Rules } from '../types/rules';
2
2
  export declare const required: (value: string) => Promise<any>;
3
3
  export declare const Rule: Rules;
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@keysdown/form-wrapper",
3
- "version": "0.0.4",
4
- "description": "JavaScript wrapper for forms",
3
+ "version": "0.0.6",
5
4
  "type": "module",
6
5
  "files": [
7
- "dist"
6
+ "dist/*"
8
7
  ],
9
- "main": "./dist/form-wrapper.umd.js",
10
- "module": "./dist/form-wrapper.es.js",
11
- "types": "./dist/types/index.d.ts",
8
+ "main": "./dist/form-wrapper.umd.cjs",
9
+ "module": "./dist/form-wrapper.js",
10
+ "types": "./dist/form-wrapper.d.ts",
12
11
  "exports": {
13
12
  ".": {
14
- "import": "./dist/form-wrapper.es.js",
15
- "require": "./dist/form-wrapper.umd.js"
13
+ "import": "./dist/form-wrapper.js",
14
+ "require": "./dist/form-wrapper.umd.cjs",
15
+ "types": "./dist/form-wrapper.d.ts"
16
16
  }
17
17
  },
18
18
  "scripts": {
@@ -21,6 +21,7 @@
21
21
  "preview": "vite preview"
22
22
  },
23
23
  "devDependencies": {
24
+ "@types/node": "^22.7.4",
24
25
  "typescript": "^5.5.3",
25
26
  "vite": "^5.4.1",
26
27
  "vite-plugin-dts": "^4.2.3"
@@ -1,6 +0,0 @@
1
- export * from '../main'
2
- export {}
3
- import FormWrapper from '../main'
4
- export default FormWrapper
5
- export * from '../main'
6
- export {}
File without changes