@keysdown/form-wrapper 0.0.2 → 0.0.4

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.
@@ -0,0 +1,5 @@
1
+ import { Collection } from '../utils/collections';
2
+ export declare class Errors extends Collection<string[]> {
3
+ push(item: string, data: any): this;
4
+ get(key: string): string[] | null;
5
+ }
@@ -0,0 +1,27 @@
1
+ import { Field, Fields, RawFields } from '../types/fields.ts';
2
+ import { Values } from '../types/values.ts';
3
+ import { Validation } from './Validation.ts';
4
+ export declare class Form {
5
+ [key: string]: any;
6
+ awaiting: boolean;
7
+ originalValues: Values;
8
+ validation: Validation;
9
+ constructor(fields: Fields | RawFields);
10
+ addField(field: string, value: Field): this;
11
+ addFields(fields: Fields | RawFields): this;
12
+ get errors(): import('./Errors.ts').Errors;
13
+ fill(data: {
14
+ [key: string]: any;
15
+ }, updateOriginalValues?: boolean): this;
16
+ get messages(): import('./Messages.ts').Messages;
17
+ removeField(field: string): this;
18
+ removeFields(fields: string[]): this;
19
+ reset(): this;
20
+ get rules(): import('./Rules.ts').Rules;
21
+ setAwaiting(awaiting?: boolean): this;
22
+ validate(field?: string | null): Promise<this | void>;
23
+ validateField(field: string): Promise<void>;
24
+ validateForm(): Promise<this>;
25
+ values(): Values;
26
+ valuesAsFormData(): FormData;
27
+ }
@@ -0,0 +1,4 @@
1
+ import { Collection } from '../utils/collections';
2
+ import { ErrorMessage } from '../types/messages.ts';
3
+ export declare class Messages extends Collection<ErrorMessage> {
4
+ }
@@ -0,0 +1,3 @@
1
+ import { Collection } from '../utils/collections';
2
+ export declare class Rules extends Collection<string[]> {
3
+ }
@@ -0,0 +1,8 @@
1
+ import { Messages } from './Messages.ts';
2
+ import { Rules } from './Rules.ts';
3
+ import { Errors } from './Errors.ts';
4
+ export declare class Validation {
5
+ errors: Errors;
6
+ messages: Messages;
7
+ rules: Rules;
8
+ }
@@ -0,0 +1,182 @@
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
+ return s ? Array.isArray(s) ? s[0] : s : null;
24
+ }
25
+ any() {
26
+ return Object.keys(this.collection).length > 0;
27
+ }
28
+ fill(t) {
29
+ return this.collection = t, this;
30
+ }
31
+ push(t, s) {
32
+ return this.collection = {
33
+ ...this.collection,
34
+ [t]: s
35
+ }, this;
36
+ }
37
+ has(t) {
38
+ return this.collection.hasOwnProperty(t);
39
+ }
40
+ get(t, s = null) {
41
+ return this.has(t) ? this.collection[t] : s;
42
+ }
43
+ unset(t) {
44
+ return this.has(t) && delete this.collection[t], this;
45
+ }
46
+ clear() {
47
+ return this.collection = {}, this;
48
+ }
49
+ }
50
+ class j extends h {
51
+ }
52
+ class b extends h {
53
+ }
54
+ class w extends h {
55
+ push(t, s) {
56
+ const r = this.get(t) || [];
57
+ return this.collection = {
58
+ ...this.collection,
59
+ [t]: [...r, s]
60
+ }, this;
61
+ }
62
+ get(t) {
63
+ return super.get(t, []);
64
+ }
65
+ }
66
+ class y {
67
+ constructor() {
68
+ a(this, "errors", new w());
69
+ a(this, "messages", new j());
70
+ a(this, "rules", new b());
71
+ }
72
+ }
73
+ const f = (e) => new Promise((t, s) => {
74
+ e == null && s(), String(e).replace(/\s/g, "").length > 0 ? t(e) : s();
75
+ }), c = {
76
+ required: f
77
+ }, V = (e) => e !== null && typeof e == "object" && !Array.isArray(e), u = (e, t, s = null) => {
78
+ const r = t || new FormData();
79
+ return Object.keys(e).forEach((i) => {
80
+ const n = e[i];
81
+ if (i = s ? `${s}[${i}]` : i, !([void 0, null].indexOf(n) > -1)) {
82
+ if (V(n) && !(n instanceof File) || Array.isArray(n)) {
83
+ u(n, r, i);
84
+ return;
85
+ }
86
+ r.append(i, n);
87
+ }
88
+ }), r;
89
+ };
90
+ class O {
91
+ constructor(t) {
92
+ a(this, "awaiting", !1);
93
+ a(this, "originalValues", {});
94
+ a(this, "validation", new y());
95
+ this.addFields(t);
96
+ }
97
+ addField(t, s) {
98
+ if (typeof s == "object" && "value" in s) {
99
+ const r = p(s);
100
+ this[t] = r.value, this.originalValues[t] = r.value, this.validation.messages.push(t, r.validation.messages), this.validation.rules.push(t, r.validation.rules);
101
+ } else
102
+ this[t] = s, this.originalValues[t] = s;
103
+ return this;
104
+ }
105
+ addFields(t) {
106
+ return Object.keys(t).forEach((s) => {
107
+ this.addField(s, t[s]);
108
+ }), this;
109
+ }
110
+ get errors() {
111
+ return this.validation.errors;
112
+ }
113
+ fill(t, s = !1) {
114
+ return Object.keys(t).forEach((r) => {
115
+ let i = t[r];
116
+ s && (this.originalValues[r] = i), this[r] = i;
117
+ }), this;
118
+ }
119
+ get messages() {
120
+ return this.validation.messages;
121
+ }
122
+ removeField(t) {
123
+ return delete this[t], delete this.originalValues[t], this.validation.messages.unset(t), this.validation.rules.unset(t), this;
124
+ }
125
+ removeFields(t) {
126
+ return t.forEach((s) => {
127
+ this.removeField(s);
128
+ }), this;
129
+ }
130
+ reset() {
131
+ return this.validation.errors.clear(), Object.keys(this.originalValues).forEach((t) => {
132
+ this[t] = this.originalValues[t];
133
+ }), this;
134
+ }
135
+ get rules() {
136
+ return this.validation.rules;
137
+ }
138
+ setAwaiting(t = !0) {
139
+ return this.awaiting = t, this;
140
+ }
141
+ validate(t = null) {
142
+ return t ? this.validateField(t) : this.validateForm();
143
+ }
144
+ validateField(t) {
145
+ this.validation.errors.unset(t);
146
+ const s = this.validation.rules.get(t);
147
+ if (s && s.length > 0) {
148
+ const r = s.map(
149
+ (i) => {
150
+ const n = i.split(":"), l = n[0], d = n.length === 2 ? n[1].split(",") : [];
151
+ return l in c ? c[l](this[t], d).catch((g) => {
152
+ const o = this.validation.messages.get(t);
153
+ return o && l in o && this.validation.errors.push(t, o[l]), Promise.reject(g);
154
+ }) : Promise.reject(new Error(`There is no validation rule called "${i}"`));
155
+ }
156
+ );
157
+ return Promise.all(r).then(() => {
158
+ });
159
+ }
160
+ return Promise.resolve();
161
+ }
162
+ validateForm() {
163
+ const t = Object.keys(this.originalValues).map(
164
+ (s) => this.validateField(s)
165
+ );
166
+ return Promise.all(t).then(() => Promise.resolve(this)).catch(() => Promise.reject(this));
167
+ }
168
+ values() {
169
+ const t = {};
170
+ return Object.keys(this.originalValues).forEach((s) => {
171
+ t[s] = this[s];
172
+ }), t;
173
+ }
174
+ valuesAsFormData() {
175
+ return u(this.values());
176
+ }
177
+ }
178
+ const A = (e) => new O(e);
179
+ export {
180
+ A as createForm,
181
+ O as default
182
+ };
@@ -0,0 +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 c{constructor(){l(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 p extends c{}class f extends c{}class F extends c{push(t,e){const i=this.get(t)||[];return this.collection={...this.collection,[t]:[...i,e]},this}get(t){return super.get(t,[])}}class b{constructor(){l(this,"errors",new F);l(this,"messages",new p);l(this,"rules",new f)}}const g={required:s=>new Promise((t,e)=>{s==null&&e(),String(s).replace(/\s/g,"").length>0?t(s):e()})},j=s=>s!==null&&typeof s=="object"&&!Array.isArray(s),v=(s,t,e=null)=>{const i=t||new FormData;return Object.keys(s).forEach(a=>{const o=s[a];if(a=e?`${e}[${a}]`:a,!([void 0,null].indexOf(o)>-1)){if(j(o)&&!(o instanceof File)||Array.isArray(o)){v(o,i,a);return}i.append(a,o)}}),i};class m{constructor(t){l(this,"awaiting",!1);l(this,"originalValues",{});l(this,"validation",new b);this.addFields(t)}addField(t,e){if(typeof e=="object"&&"value"in e){const i=u(e);this[t]=i.value,this.originalValues[t]=i.value,this.validation.messages.push(t,i.validation.messages),this.validation.rules.push(t,i.validation.rules)}else this[t]=e,this.originalValues[t]=e;return this}addFields(t){return Object.keys(t).forEach(e=>{this.addField(e,t[e])}),this}get errors(){return this.validation.errors}fill(t,e=!1){return Object.keys(t).forEach(i=>{let a=t[i];e&&(this.originalValues[i]=a),this[i]=a}),this}get messages(){return this.validation.messages}removeField(t){return delete this[t],delete this.originalValues[t],this.validation.messages.unset(t),this.validation.rules.unset(t),this}removeFields(t){return t.forEach(e=>{this.removeField(e)}),this}reset(){return this.validation.errors.clear(),Object.keys(this.originalValues).forEach(t=>{this[t]=this.originalValues[t]}),this}get rules(){return this.validation.rules}setAwaiting(t=!0){return this.awaiting=t,this}validate(t=null){return t?this.validateField(t):this.validateForm()}validateField(t){this.validation.errors.unset(t);const e=this.validation.rules.get(t);if(e&&e.length>0){const i=e.map(a=>{const o=a.split(":"),h=o[0],w=o.length===2?o[1].split(","):[];return h in g?g[h](this[t],w).catch(O=>{const d=this.validation.messages.get(t);return d&&h in d&&this.validation.errors.push(t,d[h]),Promise.reject(O)}):Promise.reject(new Error(`There is no validation rule called "${a}"`))});return Promise.all(i).then(()=>{})}return Promise.resolve()}validateForm(){const t=Object.keys(this.originalValues).map(e=>this.validateField(e));return Promise.all(t).then(()=>Promise.resolve(this)).catch(()=>Promise.reject(this))}values(){const t={};return Object.keys(this.originalValues).forEach(e=>{t[e]=this[e]}),t}valuesAsFormData(){return v(this.values())}}const y=s=>new m(s);r.createForm=y,r.default=m,Object.defineProperties(r,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}})});
package/dist/main.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ import { Form } from './core/Form.ts';
2
+ declare const createForm: (data: any) => Form;
3
+ export { createForm };
4
+ export default Form;
@@ -1,3 +1,3 @@
1
1
  export interface Items<T> {
2
- [key: string]: T
3
- }
2
+ [key: string]: T;
3
+ }
@@ -0,0 +1,15 @@
1
+ import { Validation } from './validations';
2
+ export interface Field {
3
+ validation?: object;
4
+ value: any;
5
+ }
6
+ export interface FieldDeclaration {
7
+ validation: Validation;
8
+ value: any;
9
+ }
10
+ export interface Fields {
11
+ [key: string]: Field;
12
+ }
13
+ export interface RawFields {
14
+ [key: string]: any;
15
+ }
@@ -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,3 @@
1
+ export type ErrorMessage = {
2
+ [key: string]: string;
3
+ };
@@ -1,3 +1,3 @@
1
1
  export interface Rules {
2
- [key: string]: (value: any, attributes: string[]) => Promise<any>
3
- }
2
+ [key: string]: (value: any, attributes: string[]) => Promise<any>;
3
+ }
@@ -0,0 +1,4 @@
1
+ export interface Validation {
2
+ rules: string[];
3
+ messages: object;
4
+ }
@@ -1,3 +1,3 @@
1
1
  export interface Values {
2
- [key: string]: any
3
- }
2
+ [key: string]: any;
3
+ }
@@ -0,0 +1,13 @@
1
+ import { Items } from '../types/collections';
2
+ export declare class Collection<T> {
3
+ collection: Items<T>;
4
+ all(): Items<T>;
5
+ first(key: string): T | null;
6
+ any(): boolean;
7
+ fill(items: Items<T>): this;
8
+ push(key: string, data: any): this;
9
+ has(key: string): boolean;
10
+ get(item: string, defaultValue?: T | null): T | null;
11
+ unset(key: string): this;
12
+ clear(): this;
13
+ }
@@ -0,0 +1,2 @@
1
+ import { FieldDeclaration } from '../types/fields';
2
+ export declare const generateFieldDeclaration: (value: any) => FieldDeclaration;
@@ -0,0 +1,2 @@
1
+ export declare const isObject: (value: any) => boolean;
2
+ export declare const objectToFormData: (values: Record<string, any>, context?: FormData, namespace?: string | null) => FormData;
@@ -0,0 +1,3 @@
1
+ import { Rules } from '../types/rules.ts';
2
+ export declare const required: (value: string) => Promise<any>;
3
+ export declare const Rule: Rules;
package/package.json CHANGED
@@ -1,10 +1,14 @@
1
1
  {
2
2
  "name": "@keysdown/form-wrapper",
3
- "version": "0.0.2",
3
+ "version": "0.0.4",
4
4
  "description": "JavaScript wrapper for forms",
5
5
  "type": "module",
6
+ "files": [
7
+ "dist"
8
+ ],
6
9
  "main": "./dist/form-wrapper.umd.js",
7
10
  "module": "./dist/form-wrapper.es.js",
11
+ "types": "./dist/types/index.d.ts",
8
12
  "exports": {
9
13
  ".": {
10
14
  "import": "./dist/form-wrapper.es.js",
@@ -18,6 +22,7 @@
18
22
  },
19
23
  "devDependencies": {
20
24
  "typescript": "^5.5.3",
21
- "vite": "^5.4.1"
25
+ "vite": "^5.4.1",
26
+ "vite-plugin-dts": "^4.2.3"
22
27
  }
23
28
  }
@@ -1,21 +0,0 @@
1
- import {Collection} from '../utils/collections'
2
-
3
- export class Errors extends Collection<string[]> {
4
- public push(
5
- item: string,
6
- data: any,
7
- ): this {
8
- const currentItem = this.get(item) || []
9
-
10
- this.collection = {
11
- ...this.collection,
12
- [item]: [...currentItem, data]
13
- }
14
-
15
- return this
16
- }
17
-
18
- public get(key: string): string[] | null {
19
- return super.get(key, [])
20
- }
21
- }
package/src/core/Form.ts DELETED
@@ -1,197 +0,0 @@
1
- import {Field, FieldDeclaration, Fields, RawFields} from '../types/fields.ts'
2
- import {generateFieldDeclaration} from '../utils/fields.ts'
3
- import {Values} from '../types/values.ts'
4
- import {Validation} from './Validation.ts'
5
- import {Rule} from '../utils/validations.ts'
6
- import {objectToFormData} from '../utils/helpers.ts'
7
- import {ErrorMessage} from '../types/messages.ts'
8
-
9
- export class Form {
10
- [key: string]: any
11
-
12
- public awaiting: boolean = false
13
-
14
- public originalValues: Values = {}
15
-
16
- public validation: Validation = new Validation()
17
-
18
- public constructor(
19
- fields: Fields | RawFields
20
- ) {
21
- this.addFields(fields)
22
- }
23
-
24
- public addField(
25
- field: string,
26
- value: Field
27
- ): this {
28
- if (typeof value === 'object' && 'value' in value) {
29
- const fieldDeclaration: FieldDeclaration = generateFieldDeclaration(value)
30
-
31
- this[field] = fieldDeclaration.value
32
-
33
- this.originalValues[field] = fieldDeclaration.value
34
-
35
- this.validation.messages.push(field, fieldDeclaration.validation.messages)
36
-
37
- this.validation.rules.push(field, fieldDeclaration.validation.rules)
38
- } else {
39
- this[field] = value
40
-
41
- this.originalValues[field] = value
42
- }
43
-
44
- return this
45
- }
46
-
47
- public addFields(
48
- fields: Fields | RawFields
49
- ): this {
50
- Object.keys(fields).forEach((field: string) => {
51
- this.addField(field, fields[field])
52
- })
53
-
54
- return this
55
- }
56
-
57
- public get errors() {
58
- return this.validation.errors
59
- }
60
-
61
- public fill(
62
- data: { [key: string]: any },
63
- updateOriginalValues: boolean = false
64
- ): this {
65
- Object.keys(data).forEach((field: string) => {
66
- let value = data[field]
67
-
68
- if (updateOriginalValues) {
69
- this.originalValues[field] = value
70
- }
71
-
72
- this[field] = value
73
- })
74
-
75
- return this
76
- }
77
-
78
- public get messages() {
79
- return this.validation.messages
80
- }
81
-
82
- public removeField(
83
- field: string
84
- ): this {
85
- delete this[field]
86
-
87
- delete this.originalValues[field]
88
-
89
- this.validation.messages.unset(field)
90
-
91
- this.validation.rules.unset(field)
92
-
93
- return this
94
- }
95
-
96
- public removeFields(
97
- fields: string[]
98
- ): this {
99
- fields.forEach((field: string) => {
100
- this.removeField(field)
101
- })
102
-
103
- return this
104
- }
105
-
106
- public reset(): this {
107
- this.validation.errors.clear()
108
-
109
- Object.keys(this.originalValues).forEach((field: string) => {
110
- this[field] = this.originalValues[field]
111
- })
112
-
113
- return this
114
- }
115
-
116
- public get rules() {
117
- return this.validation.rules
118
- }
119
-
120
- public setAwaiting(
121
- awaiting: boolean = true
122
- ): this {
123
- this.awaiting = awaiting
124
-
125
- return this
126
- }
127
-
128
- public validate(
129
- field: string | null = null
130
- ): Promise<this | void> {
131
- return field ? this.validateField(field) : this.validateForm()
132
- }
133
-
134
- public validateField(
135
- field: string
136
- ): Promise<void> {
137
- this.validation.errors.unset(field)
138
-
139
- const rules = this.validation.rules.get(field)
140
-
141
- if (rules && rules.length > 0) {
142
- const validations = rules.map((
143
- rule: string
144
- ) => {
145
- const ruleParts = rule.split(':')
146
-
147
- const ruleName: string = ruleParts[0]
148
- const ruleAttributes: string[] = ruleParts.length === 2 ? ruleParts[1].split(',') : []
149
-
150
- if (ruleName in Rule) {
151
- return Rule[ruleName](this[field], ruleAttributes)
152
- .catch((error?: string) => {
153
- const errorMessage: ErrorMessage | null = this.validation.messages.get(field)
154
-
155
- if (errorMessage && ruleName in errorMessage) {
156
- this.validation.errors.push(field, errorMessage[ruleName])
157
- }
158
-
159
- return Promise.reject(error)
160
- })
161
- }
162
-
163
- return Promise.reject(new Error(`There is no validation rule called "${rule}"`))
164
- }
165
- )
166
-
167
- return Promise.all(validations).then(() => {
168
- })
169
- }
170
-
171
- return Promise.resolve()
172
- }
173
-
174
- public validateForm(): Promise<this> {
175
- const validations = Object.keys(this.originalValues).map(
176
- (field: string) => this.validateField(field)
177
- )
178
-
179
- return Promise.all(validations)
180
- .then(() => Promise.resolve(this))
181
- .catch(() => Promise.reject(this))
182
- }
183
-
184
- public values(): Values {
185
- const values: Values = {}
186
-
187
- Object.keys(this.originalValues).forEach((field: string): void => {
188
- values[field] = this[field]
189
- })
190
-
191
- return values
192
- }
193
-
194
- public valuesAsFormData() {
195
- return objectToFormData(this.values())
196
- }
197
- }
@@ -1,5 +0,0 @@
1
- import { Collection } from '../utils/collections'
2
- import {ErrorMessage} from '../types/messages.ts'
3
-
4
- export class Messages extends Collection<ErrorMessage> {
5
- }
package/src/core/Rules.ts DELETED
@@ -1,4 +0,0 @@
1
- import { Collection } from '../utils/collections'
2
-
3
- export class Rules extends Collection<string[]> {
4
- }
@@ -1,11 +0,0 @@
1
- import {Messages} from './Messages.ts'
2
- import {Rules} from './Rules.ts'
3
- import {Errors} from './Errors.ts'
4
-
5
- export class Validation {
6
- public errors: Errors = new Errors()
7
-
8
- public messages: Messages = new Messages()
9
-
10
- public rules: Rules = new Rules()
11
- }
package/src/main.ts DELETED
@@ -1,9 +0,0 @@
1
- import {Form} from './core/Form.ts'
2
-
3
- const createForm = (data: any) => new Form(data)
4
-
5
- export {
6
- createForm
7
- }
8
-
9
- export default Form
@@ -1,19 +0,0 @@
1
- import { Validation } from './validations'
2
-
3
- export interface Field {
4
- validation?: object,
5
- value: any
6
- }
7
-
8
- export interface FieldDeclaration {
9
- validation: Validation,
10
- value: any
11
- }
12
-
13
- export interface Fields {
14
- [key: string]: Field
15
- }
16
-
17
- export interface RawFields {
18
- [key: string]: any
19
- }
@@ -1,3 +0,0 @@
1
- export type ErrorMessage = {
2
- [key: string]: string
3
- }
@@ -1,4 +0,0 @@
1
- export interface Validation {
2
- rules: string[],
3
- messages: object
4
- }
@@ -1,78 +0,0 @@
1
- import {Items} from '../types/collections'
2
-
3
- export class Collection<T> {
4
- public collection: Items<T> = {}
5
-
6
- public all(): Items<T> {
7
- return this.collection
8
- }
9
-
10
- public first(
11
- key: string
12
- ): T | null {
13
- const data = this.get(key)
14
-
15
- if (data) {
16
- return Array.isArray(data) ? data[0] : data
17
- }
18
-
19
- return null
20
- }
21
-
22
- public any(): boolean {
23
- return Object.keys(this.collection).length > 0
24
- }
25
-
26
- public fill(
27
- items: Items<T>
28
- ): this {
29
- this.collection = items
30
-
31
- return this
32
- }
33
-
34
- public push(
35
- key: string,
36
- data: any
37
- ): this {
38
- this.collection = {
39
- ...this.collection,
40
- [key]: data
41
- }
42
-
43
- return this
44
- }
45
-
46
- public has(
47
- key: string
48
- ): boolean {
49
- return this.collection.hasOwnProperty(key)
50
- }
51
-
52
- public get(
53
- item: string,
54
- defaultValue: T | null = null
55
- ): T | null {
56
- if (!this.has(item)) {
57
- return defaultValue
58
- }
59
-
60
- return this.collection[item]
61
- }
62
-
63
- public unset(
64
- key: string
65
- ): this {
66
- if (this.has(key)) {
67
- delete this.collection[key]
68
- }
69
-
70
- return this
71
- }
72
-
73
- public clear(): this {
74
- this.collection = {}
75
-
76
- return this
77
- }
78
- }
@@ -1,23 +0,0 @@
1
- import {FieldDeclaration} from '../types/fields'
2
-
3
- const generateValidationRules = (
4
- rules: string | string[]
5
- ): string[] => {
6
- return typeof rules === 'string' ?
7
- rules.split('|') :
8
- rules
9
- }
10
-
11
- export const generateFieldDeclaration = (
12
- value: any
13
- ): FieldDeclaration => {
14
- return {
15
- validation: {
16
- rules: value.validation?.rules ?
17
- generateValidationRules(value.validation.rules) :
18
- [],
19
- messages: value.validation?.messages || {}
20
- },
21
- value: value.value || null
22
- }
23
- }
@@ -1,31 +0,0 @@
1
- export const isObject = (value: any): boolean => {
2
- return value !== null && typeof value === 'object' && !Array.isArray(value)
3
- }
4
-
5
- export const objectToFormData = (
6
- values: Record<string, any>,
7
- context?: FormData,
8
- namespace: string | null = null
9
- ): FormData => {
10
- const formData: FormData = context || new FormData()
11
-
12
- Object.keys(values).forEach((key: string): void => {
13
- const value = values[key]
14
-
15
- key = namespace ? `${namespace}[${key}]` : key
16
-
17
- if ([undefined, null].indexOf(value) > -1) {
18
- return
19
- }
20
-
21
- if ((isObject(value) && !(value instanceof File)) || Array.isArray(value)) {
22
- objectToFormData(value, formData, key)
23
-
24
- return
25
- }
26
-
27
- formData.append(key, value)
28
- })
29
-
30
- return formData
31
- }
@@ -1,17 +0,0 @@
1
- import {Rules} from '../types/rules.ts'
2
-
3
- export const required = (
4
- value: string
5
- ): Promise<any> => new Promise((resolve, reject) => {
6
- if (value === undefined || value === null) {
7
- reject()
8
- }
9
-
10
- let str = String(value).replace(/\s/g, "");
11
-
12
- str.length > 0 ? resolve(value) : reject()
13
- })
14
-
15
- export const Rule: Rules = {
16
- required
17
- }
package/tsconfig.json DELETED
@@ -1,23 +0,0 @@
1
- {
2
- "compilerOptions": {
3
- "target": "ESNext",
4
- "useDefineForClassFields": true,
5
- "module": "ESNext",
6
- "lib": ["ESNext", "DOM", "DOM.Iterable"],
7
- "skipLibCheck": true,
8
-
9
- /* Bundler mode */
10
- "moduleResolution": "bundler",
11
- "allowImportingTsExtensions": true,
12
- "isolatedModules": true,
13
- "moduleDetection": "force",
14
- "noEmit": true,
15
-
16
- /* Linting */
17
- "strict": true,
18
- "noUnusedLocals": true,
19
- "noUnusedParameters": true,
20
- "noFallthroughCasesInSwitch": true
21
- },
22
- "include": ["src"]
23
- }
package/vite.config.js DELETED
@@ -1,17 +0,0 @@
1
- import {resolve} from 'path'
2
- import {defineConfig} from 'vite'
3
-
4
- export default defineConfig({
5
- build: {
6
- lib: {
7
- entry: resolve(__dirname, 'src/main.ts'),
8
- name: 'FormWrapper',
9
- fileName: (format) => `form-wrapper.${format}.js`
10
- },
11
- rollupOptions: {
12
- output: {
13
- exports: 'named'
14
- },
15
- },
16
- },
17
- })