@keysdown/form-wrapper 0.0.3 → 0.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.
@@ -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 @@
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,4 @@
1
- import { Form } from './core/Form.ts';
1
+ import { Form } from './core/Form';
2
2
  declare const createForm: (data: any) => Form;
3
3
  export { createForm };
4
4
  export default Form;
@@ -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,17 +1,18 @@
1
1
  {
2
2
  "name": "@keysdown/form-wrapper",
3
- "version": "0.0.3",
4
- "description": "JavaScript wrapper for forms",
3
+ "version": "0.0.5",
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",
8
+ "main": "./dist/form-wrapper.umd.cjs",
9
+ "module": "./dist/form-wrapper.js",
10
+ "types": "./dist/main.d.ts",
11
11
  "exports": {
12
12
  ".": {
13
- "import": "./dist/form-wrapper.es.js",
14
- "require": "./dist/form-wrapper.umd.js"
13
+ "import": "./dist/form-wrapper.js",
14
+ "require": "./dist/form-wrapper.umd.cjs",
15
+ "types": "./dist/main.d.ts"
15
16
  }
16
17
  },
17
18
  "scripts": {
@@ -20,6 +21,7 @@
20
21
  "preview": "vite preview"
21
22
  },
22
23
  "devDependencies": {
24
+ "@types/node": "^22.7.4",
23
25
  "typescript": "^5.5.3",
24
26
  "vite": "^5.4.1",
25
27
  "vite-plugin-dts": "^4.2.3"
File without changes