adaptform 1.1.5 → 1.1.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.
@@ -20,4 +20,5 @@ export interface DateTimeFieldOptions extends FormFieldOptions {
20
20
  min?: Date | string | number;
21
21
  max?: Date | string | number;
22
22
  example?: string;
23
+ format?: string;
23
24
  }
@@ -6,8 +6,10 @@ export declare class DateTimeField extends FormField<Date, string> {
6
6
  min: Date | null;
7
7
  max: Date | null;
8
8
  example: string;
9
+ format: string;
9
10
  constructor(defaultValue?: DateValue, options?: DateTimeFieldOptions);
10
11
  protected normalizeValue(value: any): Date | null;
12
+ protected parseByFormat(value: string, format: string): Date | null;
11
13
  get valueClear(): string | null;
12
14
  private typeValidator;
13
15
  private minValidator;
package/dist/index.mjs CHANGED
@@ -1,4 +1,4 @@
1
- class r {
1
+ class l {
2
2
  _value;
3
3
  defaultValue;
4
4
  _error = null;
@@ -54,7 +54,7 @@ class r {
54
54
  return this._error = null, this.isValid;
55
55
  }
56
56
  }
57
- class m extends r {
57
+ class x extends l {
58
58
  minLength;
59
59
  maxLength;
60
60
  regex;
@@ -87,9 +87,9 @@ class m extends r {
87
87
  super.value = "";
88
88
  return;
89
89
  }
90
- let s = 0;
91
- const l = this.mask.replace(/_/g, () => i[s++] ?? "_"), a = l.indexOf("_");
92
- super.value = a === -1 ? l : l.slice(0, a);
90
+ let r = 0;
91
+ const s = this.mask.replace(/_/g, () => i[r++] ?? "_"), n = s.indexOf("_");
92
+ super.value = n === -1 ? s : s.slice(0, n);
93
93
  }
94
94
  minLengthValidator(t) {
95
95
  return this.minLength === null || !t ? null : t.length < this.minLength ? `Minimum ${this.minLength} characters` : null;
@@ -101,7 +101,7 @@ class m extends r {
101
101
  return t ? this.regex.test(t) ? null : this.example ? `Example: ${this.example}` : "Invalid format" : null;
102
102
  }
103
103
  }
104
- class d extends r {
104
+ class p extends l {
105
105
  min;
106
106
  max;
107
107
  example;
@@ -132,28 +132,64 @@ class d extends r {
132
132
  return t === null || this.max === null ? null : t > this.max ? `Maximum value: ${this.max}` : null;
133
133
  }
134
134
  }
135
- class o extends r {
135
+ class _ extends l {
136
136
  mode;
137
137
  min;
138
138
  max;
139
139
  example;
140
+ format;
140
141
  constructor(t = null, e = {}) {
141
142
  const i = t instanceof Date ? t : t ? new Date(t) : null;
142
- super(i, e), this.mode = e.mode ?? "datetime", this.min = e.min ? new Date(e.min) : null, this.max = e.max ? new Date(e.max) : null, this.example = e.example ?? (this.mode === "date" ? "YYYY-MM-DD" : "YYYY-MM-DD HH:mm:ss"), this.validators.push(this.typeValidator.bind(this)), this.validators.push(this.minValidator.bind(this)), this.validators.push(this.maxValidator.bind(this));
143
+ super(i, e), this.mode = e.mode ?? "datetime", this.min = e.min ? new Date(e.min) : null, this.max = e.max ? new Date(e.max) : null, this.example = e.example ?? (this.mode === "date" ? "YYYY-MM-DD" : "YYYY-MM-DD HH:mm"), this.format = e.format ?? (this.mode === "date" ? "YYYY-MM-DD" : "YYYY-MM-DD HH:mm"), this.validators.push(this.typeValidator.bind(this)), this.validators.push(this.minValidator.bind(this)), this.validators.push(this.maxValidator.bind(this));
143
144
  }
144
145
  normalizeValue(t) {
145
146
  if (this.fieldIsEmpty(t)) return null;
146
147
  if (t instanceof Date) return isNaN(t.getTime()) ? null : t;
148
+ if (typeof t == "string" && this.format)
149
+ return this.parseByFormat(t, this.format);
147
150
  if (typeof t == "string" || typeof t == "number") {
148
151
  const e = new Date(t);
149
152
  return isNaN(e.getTime()) ? null : e;
150
153
  }
151
154
  return null;
152
155
  }
156
+ parseByFormat(t, e) {
157
+ const i = e.match(/[^A-Z]/gi);
158
+ if (!i) return null;
159
+ const r = t.split(new RegExp(`[${i.join("")}]`)), s = e.split(new RegExp("[^A-Z]+"));
160
+ if (r.length !== s.length) return null;
161
+ let n = 0, u = 0, m = 0, d = 0, c = 0, f = 0;
162
+ for (let o = 0; o < s.length; o++) {
163
+ const a = parseInt(r[o], 10);
164
+ if (isNaN(a)) return null;
165
+ switch (s[o]) {
166
+ case "YYYY":
167
+ n = a;
168
+ break;
169
+ case "MM":
170
+ u = a - 1;
171
+ break;
172
+ case "DD":
173
+ m = a;
174
+ break;
175
+ case "HH":
176
+ d = a;
177
+ break;
178
+ case "mm":
179
+ c = a;
180
+ break;
181
+ case "ss":
182
+ f = a;
183
+ break;
184
+ }
185
+ }
186
+ const g = new Date(n, u, m, d, c, f);
187
+ return isNaN(g.getTime()) ? null : g;
188
+ }
153
189
  get valueClear() {
154
190
  if (!this._value) return null;
155
- const t = (l) => String(l).padStart(2, "0"), e = this._value.getFullYear(), i = t(this._value.getMonth() + 1), s = t(this._value.getDate());
156
- return this.mode === "date" ? `${e}-${i}-${s}` : this._value.toISOString();
191
+ const t = (s) => String(s).padStart(2, "0"), e = this._value.getFullYear(), i = t(this._value.getMonth() + 1), r = t(this._value.getDate());
192
+ return this.mode === "date" ? `${e}-${i}-${r}` : this._value.toISOString();
157
193
  }
158
194
  typeValidator(t) {
159
195
  return t === null ? this.isRequired ? `Example: ${this.example}` : null : !(t instanceof Date) || isNaN(t.getTime()) ? this.example ? `Example: ${this.example}` : "Invalid date format" : null;
@@ -166,22 +202,22 @@ class o extends r {
166
202
  return !t || !this.max ? null : t > this.max ? `Date must be before ${this.formatDate(this.max)}` : null;
167
203
  }
168
204
  formatDate(t) {
169
- const e = (u) => String(u).padStart(2, "0"), i = t.getFullYear(), s = e(t.getMonth() + 1), l = e(t.getDate()), a = e(t.getHours()), h = e(t.getMinutes());
170
- return this.mode === "date" ? `${i}-${s}-${l}` : `${i}-${s}-${l} ${a}:${h}`;
205
+ const e = (m) => String(m).padStart(2, "0"), i = t.getFullYear(), r = e(t.getMonth() + 1), s = e(t.getDate()), n = e(t.getHours()), u = e(t.getMinutes());
206
+ return this.mode === "date" ? `${i}-${r}-${s}` : `${i}-${r}-${s} ${n}:${u}`;
171
207
  }
172
208
  }
173
- class c {
209
+ class V {
174
210
  _errors = {};
175
211
  get declaredFields() {
176
212
  return Object.keys(this).filter(
177
- (t) => this[t] instanceof r
213
+ (t) => this[t] instanceof l
178
214
  );
179
215
  }
180
216
  checkValid() {
181
217
  let t = !0;
182
218
  for (const e of this.declaredFields) {
183
219
  const i = this[e];
184
- i instanceof r && (i.checkValid(), (i.isValid === !1 || i.isValid === null && i.isRequired) && (t = !1));
220
+ i instanceof l && (i.checkValid(), (i.isValid === !1 || i.isValid === null && i.isRequired) && (t = !1));
185
221
  }
186
222
  return t;
187
223
  }
@@ -189,14 +225,14 @@ class c {
189
225
  const t = {};
190
226
  for (const e of this.declaredFields) {
191
227
  const i = this[e];
192
- t[e] = i instanceof r ? i.valueClear : i;
228
+ t[e] = i instanceof l ? i.valueClear : i;
193
229
  }
194
230
  return t;
195
231
  }
196
232
  get valueArray() {
197
233
  return this.declaredFields.map((t) => {
198
234
  const e = this[t];
199
- return e instanceof r ? e.valueClear : e;
235
+ return e instanceof l ? e.valueClear : e;
200
236
  });
201
237
  }
202
238
  get errors() {
@@ -205,7 +241,7 @@ class c {
205
241
  set errors(t) {
206
242
  for (const e in t) {
207
243
  const i = this[e];
208
- i instanceof r ? i.error = t[e] : this._errors[e] = t[e];
244
+ i instanceof l ? i.error = t[e] : this._errors[e] = t[e];
209
245
  }
210
246
  }
211
247
  set fields(t) {
@@ -214,31 +250,31 @@ class c {
214
250
  throw new Error(
215
251
  `Field "${e}" is not declared in form "${this.constructor.name}"`
216
252
  );
217
- const i = this[e], s = t[e];
218
- i instanceof r ? i.value = s : this[e] = s;
253
+ const i = this[e], r = t[e];
254
+ i instanceof l ? i.value = r : this[e] = r;
219
255
  }
220
256
  }
221
257
  setFieldValue(t, e) {
222
258
  const i = this[t];
223
- i instanceof r ? i.value = e : this[t] = e;
259
+ i instanceof l ? i.value = e : this[t] = e;
224
260
  }
225
261
  getFieldValue(t) {
226
262
  const e = this[t];
227
- return e instanceof r ? e.valueClear : e;
263
+ return e instanceof l ? e.valueClear : e;
228
264
  }
229
265
  reset() {
230
266
  for (const t of this.declaredFields) {
231
267
  const e = this[t];
232
- e instanceof r && e.reset();
268
+ e instanceof l && e.reset();
233
269
  }
234
270
  for (const t of Object.keys(this._errors)) delete this._errors[t];
235
271
  return this;
236
272
  }
237
273
  }
238
274
  export {
239
- o as DateTimeField,
240
- c as Form,
241
- r as FormField,
242
- d as NumberField,
243
- m as StringField
275
+ _ as DateTimeField,
276
+ V as Form,
277
+ l as FormField,
278
+ p as NumberField,
279
+ x as StringField
244
280
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "adaptform",
3
- "version": "1.1.5",
3
+ "version": "1.1.6",
4
4
  "type": "module",
5
5
  "main": "dist/index.mjs",
6
6
  "module": "dist/index.mjs",