import { getCookie, setCookie, deleteCookie } from "./utility/cookie"; import { domLoad, init, isTag, r, lang } from "./utility/lgres"; import { get, post, upload } from "./utility/request"; import { nullOrEmpty, contains, endsWith, padStart, formatUrl, escapeHtml, escapeEmoji } from "./utility/strings"; const g = typeof window !== 'undefined' ? window : self; /** * @param {any} n * @returns {n is number} */ function isPositive(n) { const l = typeof n !== 'number' ? Number(n) : n; return !isNaN(l) && l > 0; } /** * @returns {boolean} */ function isMobile() { return /mobile/i.test(navigator.userAgent); } /** * @template {any[]} TArgs * @template [TContext=typeof globalThis] * @param {((this: TContext, ...args: TArgs) => void) & { tiid?: number; tdate?: number }} method * @param {number} [delay=100] * @param {TContext} [context] * @param {TArgs} args * @returns {void} */ function throttle(method, delay = 100, context, ...args) { if (method == null) { return; } const ctx = typeof context === 'undefined' ? /** @type {TContext} */ (/** @type {unknown} */ (g)) : context; method.tiid && clearTimeout(method.tiid); const current = Date.now(); if (method.tdate == null || current - method.tdate > delay) { method.apply(ctx, args); method.tdate = current; } else { method.tiid = setTimeout(() => method.apply(ctx, args), delay); } } /** * @template {any[]} TArgs * @template [TContext=typeof globalThis] * @param {((this: TContext, ...args: TArgs) => void) & { tiid?: number }} method * @param {number} [delay=100] * @param {TContext} [context] * @param {TArgs} args * @returns {void} */ function debounce(method, delay = 100, context, ...args) { if (method == null) { return; } const ctx = typeof context === 'undefined' ? /** @type {TContext} */ (/** @type {unknown} */ (g)) : context; method.tiid && clearTimeout(method.tiid); method.tiid = setTimeout(() => method.apply(ctx, args), delay); } /** * @param {number} v * @returns {number} */ function truncate(v) { return (v > 0 ? Math.floor : Math.ceil)(v); } /** * @param {string} text * @returns {boolean} */ function isEmail(text) { return /^\w[-\w.+]*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/.test(text); } /** * @param {string} text * @returns {boolean} */ function isPhone(text) { if (/^[1-9]\d{9,}$/.test(text)) { return true; } if (/^\+?[1-9][\d-]{9,}\d$/.test(text) && /^[1-9]\d{9,}$/.test(text.replace(/\+/g, '').replace(/-/g, ''))) { return true; } return false; } /** * `[0-9]`, `[a-z]`, `[A-Z]`, `[^0-9a-zA-Z]` * each additional type adds one layer of strength. * @param {string} [password] * @returns {number} */ function getPasswordStrength(password) { if (password == null || typeof password !== 'string') { return 0; } if (password.length < 8) { return 0; } let secure = 0; if (/[0-9]/.test(password)) { secure++ } if (/[a-z]/.test(password)) { secure++ } if (/[A-Z]/.test(password)) { secure++ } if (/[^0-9a-zA-Z]/.test(password)) { secure++ } return secure; } /** * @param {string} [password] * @param {number} [min=3] * @returns {boolean} */ function verifyPassword(password, min) { min ??= 3; const secure = getPasswordStrength(password); return secure >= min; } export { // cookie getCookie, setCookie, deleteCookie, // lgres domLoad, init, isTag, r, lang, // request get, post, upload, // strings nullOrEmpty, contains, endsWith, padStart, formatUrl, escapeHtml, escapeEmoji, // variables g as global, isPositive, isMobile, // functions throttle, debounce, truncate, isEmail, isPhone, getPasswordStrength, verifyPassword }