commit 222077f6c263ef7d28e3ac8a4d88fc1564f43724 Author: Victor Batischev Date: Wed Oct 11 15:13:40 2023 +0300 init commit diff --git a/fieldChecks.ts b/fieldChecks.ts new file mode 100644 index 0000000..fe7744a --- /dev/null +++ b/fieldChecks.ts @@ -0,0 +1,68 @@ +export class FieldCheck { + error = ""; + + constructor(error: string) { + this.error = error; + } + + check(val: string) { + throw new Error("check() not implemented"); + return ""; + } +} + +class FieldCheckRegex extends FieldCheck { + regex = /none/; + + constructor(regex: RegExp, error: string) { + super(error); + this.regex = regex; + } + + check(val: string) { + if (!this.regex.test(val)) return this.error; + return ""; + } +} + +class FieldCheckMaxLength extends FieldCheck { + maxLength = 0; + + constructor(maxLength: number) { + super(`Максимальная длина равна ${maxLength}.`); + this.maxLength = maxLength; + } + + check(val: string) { + if (val.length > this.maxLength) return this.error; + return ""; + } +} + +class FieldCheckMinLength extends FieldCheck { + minLength = 0; + + constructor(minLength: number) { + super(`Минимальная длина равна ${minLength}.`); + this.minLength = minLength; + } + + check(val: string) { + if (val.length < this.minLength) return this.error; + return ""; + } +} + +export class FieldChecks { + static nonEmpty = new FieldCheckRegex(/\S+/, "Поле не может быть пустым."); + static onlyLatinLettersWhitespaces = new FieldCheckRegex(/^[A-Za-z\s]+$/, "Разрешены только Латинские буквы и пробелы."); + static onlyLettersWhitespaces = new FieldCheckRegex(/^[A-Za-zА-Яа-я\s]+$/, "Разрешены только буквы и пробелы."); + static onlyLatinLettersNumbersUnderscores = new FieldCheckRegex( + /^[A-Za-z0-9_\s]+$/, + "Разрешены только Латинские буквы, цифры и нижнее подчёркивание." + ); + static email = new FieldCheckRegex(/^[^\s@]+@[^\s@]+\.[^\s@]+$/, "Проверьте почтовый адрес."); + static noSpecialChars = new FieldCheckRegex(/^[A-Za-z0-9\s.,()!%]+$/, "Специальные символы не разрешены."); + static maxLength = (maxLength: number) => new FieldCheckMaxLength(maxLength); + static minLength = (minLength: number) => new FieldCheckMinLength(minLength); +} diff --git a/formFields.ts b/formFields.ts new file mode 100644 index 0000000..2764186 --- /dev/null +++ b/formFields.ts @@ -0,0 +1,45 @@ +import { FieldCheck } from "./fieldChecks"; + +export class FieldsContainer { + fields = new Map(); + + /** + * Add field to container. + * @param {string} name - The field name. + * @param {string} value - The field value. + * @param {Array} checks - Array of necessary FieldCheck objects. + */ + addField(name: string, value: string, checks: FieldCheck[]) { + this.fields.set(name, new Field(value, checks)); + } +} + +export class Field { + value: string; + checks: FieldCheck[]; + + constructor(value: string, checks: FieldCheck[]) { + this.value = value; + this.checks = checks; + } + + tryGetFirstError() { + let result = ""; + + this.checks.every((fieldCheck) => { + result = fieldCheck.check(this.value); + return result === ""; //break or continue every() + }); + + return result; + } + + tryGetAllErrors() { + const result = this.checks.flatMap((fieldCheck) => { + const err = fieldCheck.check(this.value); + return err === "" ? [] : [err]; + }); + + return result.join(", "); + } +} diff --git a/index.d.ts b/index.d.ts new file mode 100644 index 0000000..db2a797 --- /dev/null +++ b/index.d.ts @@ -0,0 +1,4 @@ +import { FieldChecks } from "./fieldChecks"; +import { Field, FieldsContainer } from "./formFields"; + +export { FieldChecks, Field, FieldsContainer }; diff --git a/index.ts b/index.ts new file mode 100644 index 0000000..1f63156 --- /dev/null +++ b/index.ts @@ -0,0 +1,2 @@ +export { FieldChecks } from "./fieldChecks"; +export { Field, FieldsContainer } from "./formFields";