UNPKG

operator-checker

Version:

Iranian mobile operator detection package for Vue 3 and TypeScript

125 lines (124 loc) 3.22 kB
import { ref as u, computed as h, watch as H } from "vue"; var g = /* @__PURE__ */ ((e) => (e[e.IRANCELL = 0] = "IRANCELL", e[e.HAMRAH_E_AVAL = 1] = "HAMRAH_E_AVAL", e[e.RIGHTEL = 2] = "RIGHTEL", e))(g || {}); const C = { 0: { code: 0, name: "Irancell", nameFa: "ایرانسل", prefixes: ["0930", "0933", "0935", "0936", "0937", "0938", "0939"] }, 1: { code: 1, name: "Hamrah-e-Aval", nameFa: "همراه اول", prefixes: [ "0910", "0911", "0912", "0913", "0914", "0915", "0916", "0917", "0918", "0919", "0990", "0991", "0992", "0993", "0994" ] }, 2: { code: 2, name: "RighTel", nameFa: "رایتل", prefixes: ["0920", "0921", "0922"] } }; function L(e) { if (!e || typeof e != "string") return null; const l = e.replace(/\D/g, ""); let a = l.startsWith("98") ? l.slice(2) : l; a.startsWith("9") && (a = "0" + a); const r = a.slice(0, 4); for (const n of Object.values(C)) if (n.prefixes.includes(r)) return n.code; return null; } function p(e) { const l = L(e); return l !== null ? C[l] : null; } function N(e) { return p(e)?.nameFa || null; } function W(e) { return p(e)?.name || null; } function A(e) { if (!e || typeof e != "string") return !1; const l = e.replace(/\D/g, ""); let a = l.startsWith("98") ? l.slice(2) : l; return a.startsWith("9") && (a = "0" + a), a.length === 11 && a.startsWith("09"); } function y(e = {}) { const { autoValidate: l = !0, showDetails: a = !1 } = e, r = u(""), n = u(null), o = u(null), s = u(null), i = u(null), c = u(!1), v = u(!1), f = u(null), R = h(() => n.value), I = h(() => ({ code: n.value, name: s.value, nameFa: i.value, info: o.value })), m = (t) => { v.value = !0, f.value = null; try { if (r.value = t, l && !A(t)) { c.value = !1, n.value = null, o.value = null, s.value = null, i.value = null, f.value = "شماره موبایل معتبر نیست"; return; } const d = L(t); n.value = d, d !== null ? (c.value = !0, a && (o.value = p(t)), s.value = W(t), i.value = N(t)) : (c.value = !1, o.value = null, s.value = null, i.value = null, f.value = "اپراتور شناسایی نشد"); } catch { f.value = "خطا در پردازش شماره", c.value = !1; } finally { v.value = !1; } }, E = () => { r.value = "", n.value = null, o.value = null, s.value = null, i.value = null, c.value = !1, v.value = !1, f.value = null; }, F = (t) => A(t); return r.value && H(r, (t) => { t && m(t); }), { // State phoneNumber: r, operator: n, operatorInfo: o, operatorName: s, operatorNameFa: i, isValid: c, isLoading: v, error: f, // Computed operatorCode: R, operatorDetails: I, // Methods checkOperator: m, reset: E, validateNumber: F, // Constants Operator: g }; } export { C as OPERATORS, g as Operator, L as default, L as detectOperator, p as getOperatorInfo, W as getOperatorName, N as getOperatorNameFa, A as isValidIranianMobileNumber, y as useOperatorChecker };