peppa/bench/tasks/04-new-field/repo/product/patients.mjs
2026-11-17 02:09:17 +05:00

27 lines
794 B
JavaScript

// Пациенты: проверка полей и хранение.
import { collection } from "./lib/store.mjs";
const patients = collection("patients");
export function normalizePhone(raw) {
const d = String(raw ?? "").replace(/[\s()-]/g, "");
return /^\+7\d{10}$/.test(d) ? d : null;
}
export function validatePatient(b) {
const errors = {};
const name = String(b.name ?? "").trim();
if (name.length < 2) errors.name = "Укажите имя и фамилию.";
const phone = normalizePhone(b.phone);
if (!phone) errors.phone = "Укажите телефон в виде +7 и 10 цифр.";
return { errors, value: { name, phone } };
}
export function addPatient(fields) {
return patients.add(fields);
}
export function listPatients() {
return patients.all();
}