add jsdoc comments

This commit is contained in:
lchen committed 2026-11-18 10:14:21 +08:00
1 parent 6de354ad47
commit 8c1205bc13
24 files changed
+1228 -425

No files matched your search

+68 -12
View File
@@ -1,58 +1,108 @@
import { getCookie, setCookie, deleteCookie } from "./utility/cookie";
import { domLoad, init, r, lang } from "./utility/lgres";
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";
let g = typeof globalThis !== 'undefined' ? globalThis : self;
const g = typeof window !== 'undefined' ? window : self;
/**
* @param {any} n
* @returns {n is number}
*/
function isPositive(n) {
return !isNaN(n) && n > 0;
const l = typeof n !== 'number' ? Number(n) : n;
return !isNaN(l) && l > 0;
}
/**
* @returns {boolean}
*/
function isMobile() {
return /mobile/i.test(navigator.userAgent);
}
function throttle(method, delay = 100, context = g, ...args) {
/**
* @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 = new Date();
const current = Date.now();
if (method.tdate == null || current - method.tdate > delay) {
method.apply(context, args);
method.apply(ctx, args);
method.tdate = current;
} else {
method.tiid = setTimeout(() => method.apply(context, args), delay);
method.tiid = setTimeout(() => method.apply(ctx, args), delay);
}
}
function debounce(method, delay = 100, context = g, ...args) {
/**
* @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(context, args), delay);
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('+', '').replace(new RegExp('-', 'g'), ''))) {
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;
@@ -68,6 +118,11 @@ function getPasswordStrength(password) {
return secure;
}
/**
* @param {string} [password]
* @param {number} [min=3]
* @returns {boolean}
*/
function verifyPassword(password, min) {
min ??= 3;
const secure = getPasswordStrength(password);
@@ -80,7 +135,9 @@ export {
setCookie,
deleteCookie,
// lgres
domLoad,
init,
isTag,
r,
lang,
// request
@@ -106,6 +163,5 @@ export {
isEmail,
isPhone,
getPasswordStrength,
verifyPassword,
domLoad
verifyPassword
}