@pisell/pisellos
Version:
一个可扩展的前端模块化SDK框架,支持插件系统
280 lines (278 loc) • 8.86 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/solution/RegisterAndLogin/utils.ts
var utils_exports = {};
__export(utils_exports, {
debounce: () => debounce,
deepClone: () => deepClone,
formatEmailDisplay: () => formatEmailDisplay,
formatPhoneDisplay: () => formatPhoneDisplay,
formatTimestamp: () => formatTimestamp,
generateRandomString: () => generateRandomString,
getDeviceType: () => getDeviceType,
getOAuthProviderDisplayName: () => getOAuthProviderDisplayName,
getOAuthProviderIconUrl: () => getOAuthProviderIconUrl,
getTimeDifference: () => getTimeDifference,
isAndroidDevice: () => isAndroidDevice,
isIOSDevice: () => isIOSDevice,
isMobileDevice: () => isMobileDevice,
isTimestampExpired: () => isTimestampExpired,
isWebAuthnSupported: () => isWebAuthnSupported,
safeJsonParse: () => safeJsonParse,
safeJsonStringify: () => safeJsonStringify,
throttle: () => throttle,
validateEmail: () => validateEmail,
validatePassword: () => validatePassword,
validatePhone: () => validatePhone,
validateVerificationCode: () => validateVerificationCode
});
module.exports = __toCommonJS(utils_exports);
var import_types = require("./types");
function validateEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
function validatePhone(phone) {
const cleanPhone = phone.replace(/[^\d+]/g, "");
const phoneRegex = /^(\+\d{1,3}[- ]?)?\d{10,14}$/;
return phoneRegex.test(cleanPhone);
}
function validatePassword(password) {
const errors = [];
if (password.length < 8) {
errors.push("密码长度至少为8位");
}
if (password.length > 128) {
errors.push("密码长度不能超过128位");
}
if (!/[a-z]/.test(password)) {
errors.push("密码必须包含至少一个小写字母");
}
if (!/[A-Z]/.test(password)) {
errors.push("密码必须包含至少一个大写字母");
}
if (!/\d/.test(password)) {
errors.push("密码必须包含至少一个数字");
}
if (!/[!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/?]/.test(password)) {
errors.push("密码必须包含至少一个特殊字符");
}
return {
isValid: errors.length === 0,
errors
};
}
function validateVerificationCode(code, type) {
const cleanCode = code.replace(/\s/g, "");
switch (type) {
case import_types.VerificationCodeType.EMAIL:
case import_types.VerificationCodeType.SMS:
return /^\d{4,8}$/.test(cleanCode);
default:
return false;
}
}
function formatPhoneDisplay(phone) {
const cleanPhone = phone.replace(/[^\d+]/g, "");
if (/^\d{11}$/.test(cleanPhone)) {
return cleanPhone.replace(/(\d{3})(\d{4})(\d{4})/, "$1 $2 $3");
}
if (cleanPhone.startsWith("+")) {
return cleanPhone;
}
return phone;
}
function formatEmailDisplay(email, maskLength = 3) {
const [localPart, domain] = email.split("@");
if (!localPart || !domain) {
return email;
}
if (localPart.length <= maskLength * 2) {
return email;
}
const visibleStart = localPart.substring(0, maskLength);
const visibleEnd = localPart.substring(localPart.length - maskLength);
const maskedPart = "*".repeat(localPart.length - maskLength * 2);
return `${visibleStart}${maskedPart}${visibleEnd}@${domain}`;
}
function generateRandomString(length = 32) {
const chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
let result = "";
for (let i = 0; i < length; i++) {
result += chars.charAt(Math.floor(Math.random() * chars.length));
}
return result;
}
function getOAuthProviderDisplayName(provider) {
const displayNames = {
[import_types.OAuthProvider.FACEBOOK]: "Facebook",
[import_types.OAuthProvider.APPLE]: "Apple",
[import_types.OAuthProvider.GOOGLE]: "Google",
[import_types.OAuthProvider.WECHAT]: "微信",
[import_types.OAuthProvider.GITHUB]: "GitHub"
};
return displayNames[provider] || provider;
}
function getOAuthProviderIconUrl(provider) {
const iconUrls = {
[import_types.OAuthProvider.FACEBOOK]: "https://cdn.jsdelivr.net/npm/simple-icons@v9/icons/facebook.svg",
[import_types.OAuthProvider.APPLE]: "https://cdn.jsdelivr.net/npm/simple-icons@v9/icons/apple.svg",
[import_types.OAuthProvider.GOOGLE]: "https://cdn.jsdelivr.net/npm/simple-icons@v9/icons/google.svg",
[import_types.OAuthProvider.WECHAT]: "https://cdn.jsdelivr.net/npm/simple-icons@v9/icons/wechat.svg",
[import_types.OAuthProvider.GITHUB]: "https://cdn.jsdelivr.net/npm/simple-icons@v9/icons/github.svg"
};
return iconUrls[provider] || "";
}
function isMobileDevice() {
if (typeof window === "undefined") {
return false;
}
return /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
navigator.userAgent
);
}
function isWebAuthnSupported() {
if (typeof window === "undefined") {
return false;
}
return !!(navigator.credentials && navigator.credentials.create);
}
function isIOSDevice() {
if (typeof window === "undefined") {
return false;
}
return /iPad|iPhone|iPod/.test(navigator.userAgent);
}
function isAndroidDevice() {
if (typeof window === "undefined") {
return false;
}
return /Android/.test(navigator.userAgent);
}
function getDeviceType() {
if (typeof window === "undefined") {
return "desktop";
}
const userAgent = navigator.userAgent;
if (/iPad/.test(userAgent)) {
return "tablet";
}
if (/Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(userAgent)) {
return "mobile";
}
return "desktop";
}
function debounce(func, wait) {
let timeout = null;
return (...args) => {
if (timeout) {
clearTimeout(timeout);
}
timeout = setTimeout(() => {
func(...args);
}, wait);
};
}
function throttle(func, wait) {
let lastTime = 0;
return (...args) => {
const now = Date.now();
if (now - lastTime >= wait) {
lastTime = now;
func(...args);
}
};
}
function deepClone(obj) {
if (obj === null || typeof obj !== "object") {
return obj;
}
if (obj instanceof Date) {
return new Date(obj.getTime());
}
if (obj instanceof Array) {
return obj.map((item) => deepClone(item));
}
if (typeof obj === "object") {
const cloned = {};
for (const key in obj) {
if (obj.hasOwnProperty(key)) {
cloned[key] = deepClone(obj[key]);
}
}
return cloned;
}
return obj;
}
function safeJsonParse(jsonString, defaultValue) {
try {
return JSON.parse(jsonString);
} catch (error) {
console.warn("JSON 解析失败:", error);
return defaultValue;
}
}
function safeJsonStringify(obj, defaultValue = "{}") {
try {
return JSON.stringify(obj);
} catch (error) {
console.warn("JSON 字符串化失败:", error);
return defaultValue;
}
}
function formatTimestamp(timestamp, format = "YYYY-MM-DD HH:mm:ss") {
const date = new Date(timestamp);
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
const hours = String(date.getHours()).padStart(2, "0");
const minutes = String(date.getMinutes()).padStart(2, "0");
const seconds = String(date.getSeconds()).padStart(2, "0");
return format.replace("YYYY", year.toString()).replace("MM", month).replace("DD", day).replace("HH", hours).replace("mm", minutes).replace("ss", seconds);
}
function getTimeDifference(timestamp1, timestamp2) {
return Math.abs(timestamp1 - timestamp2) / 1e3;
}
function isTimestampExpired(timestamp, expirationTime = 5 * 60 * 1e3) {
return Date.now() - timestamp > expirationTime;
}
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
debounce,
deepClone,
formatEmailDisplay,
formatPhoneDisplay,
formatTimestamp,
generateRandomString,
getDeviceType,
getOAuthProviderDisplayName,
getOAuthProviderIconUrl,
getTimeDifference,
isAndroidDevice,
isIOSDevice,
isMobileDevice,
isTimestampExpired,
isWebAuthnSupported,
safeJsonParse,
safeJsonStringify,
throttle,
validateEmail,
validatePassword,
validatePhone,
validateVerificationCode
});