亚洲see少妇裸体pics,欧美日产欧美日产免费一区,亚洲综合av一区二区三区不卡,一区二区中文字幕无码成人片,一区二区三区四区高清无码

函數開(kāi)發(fā)-純函數

函數開(kāi)發(fā) - 純函數

數據處理函數

1. 數據轉換函數

// 數據轉換函數
export const transformData = (rawData) => {
    if (!rawData || !Array.isArray(rawData)) return [];
    
    return rawData.map(item => ({
        ...item,
        formattedDate: dateFormater(item.createTime),
        statusText: getStatusText(item.status),
        amountFormatted: amountFormater(item.amount)
    }));
};

// 狀態(tài)文本轉換
export const getStatusText = (status) => {
    const statusMap = {
        'Y': '已申報',
        'N': '未申報',
        'B': '部分申報',
        'S': '已提交',
        'A': '已審核',
        'R': '已拒絕'
    };
    return statusMap[status] || status;
};

// 數據過(guò)濾函數
export const filterDataByStatus = (data, status) => {
    if (!data || !Array.isArray(data)) return [];
    
    return data.filter(item => item.status === status);
};

// 數據排序函數
export const sortDataByField = (data, field, order = 'asc') => {
    if (!data || !Array.isArray(data)) return [];
    
    return [...data].sort((a, b) => {
        const aValue = a[field];
        const bValue = b[field];
        
        if (order === 'asc') {
            return aValue < bValue ? -1 : aValue > bValue ? 1 : 0;
        } else {
            return aValue > bValue ? -1 : aValue < bValue ? 1 : 0;
        }
    });
};

2. 工具函數

// 防抖函數
export const debounce = (func, wait, immediate = false) => {
    let timeout;
    
    return function executedFunction(...args) {
        const context = this;
        
        const later = () => {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        
        const callNow = immediate && !timeout;
        
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        
        if (callNow) func.apply(context, args);
    };
};

// 節流函數
export const throttle = (func, limit) => {
    let inThrottle;
    
    return function(...args) {
        const context = this;
        
        if (!inThrottle) {
            func.apply(context, args);
            inThrottle = true;
            setTimeout(() => inThrottle = false, limit);
        }
    };
};

// 深拷貝函數
export const 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 (obj instanceof Object) {
        const clonedObj = {};
        Object.keys(obj).forEach(key => {
            clonedObj[key] = deepClone(obj[key]);
        });
        return clonedObj;
    }
};

// 對象合并函數
export const deepMerge = (target, source) => {
    const output = Object.assign({}, target);
    
    if (isObject(target) && isObject(source)) {
        Object.keys(source).forEach(key => {
            if (isObject(source[key])) {
                if (!(key in target)) {
                    Object.assign(output, { [key]: source[key] });
                } else {
                    output[key] = deepMerge(target[key], source[key]);
                }
            } else {
                Object.assign(output, { [key]: source[key] });
            }
        });
    }
    
    return output;
};

// 判斷是否為對象
const isObject = (item) => {
    return item && typeof item === 'object' && !Array.isArray(item);
};

日期時(shí)間函數

1. 日期格式化函數

// 日期格式化函數
export const formatDate = (date, format = 'YYYY-MM-DD') => {
    if (!date) return '';
    
    const d = new Date(date);
    const year = d.getFullYear();
    const month = String(d.getMonth() + 1).padStart(2, '0');
    const day = String(d.getDate()).padStart(2, '0');
    
    return format
        .replace('YYYY', year)
        .replace('MM', month)
        .replace('DD', day);
};

// 時(shí)間格式化函數
export const formatTime = (date, format = 'HH:mm:ss') => {
    if (!date) return '';
    
    const d = new Date(date);
    const hours = String(d.getHours()).padStart(2, '0');
    const minutes = String(d.getMinutes()).padStart(2, '0');
    const seconds = String(d.getSeconds()).padStart(2, '0');
    
    return format
        .replace('HH', hours)
        .replace('mm', minutes)
        .replace('ss', seconds);
};

// 日期時(shí)間格式化函數
export const formatDateTime = (date, format = 'YYYY-MM-DD HH:mm:ss') => {
    const dateStr = formatDate(date, 'YYYY-MM-DD');
    const timeStr = formatTime(date, 'HH:mm:ss');
    
    return format
        .replace('YYYY-MM-DD', dateStr)
        .replace('HH:mm:ss', timeStr);
};

// 計算日期差
export const dateDiff = (date1, date2, unit = 'days') => {
    const d1 = new Date(date1);
    const d2 = new Date(date2);
    const diff = Math.abs(d1 - d2);
    
    switch (unit) {
        case 'days': return Math.floor(diff / (1000 * 60 * 60 * 24));
        case 'hours': return Math.floor(diff / (1000 * 60 * 60));
        case 'minutes': return Math.floor(diff / (1000 * 60));
        case 'seconds': return Math.floor(diff / 1000);
        default: return diff;
    }
};

2. 字符串處理函數

// 字符串截取函數
export const truncate = (str, length, suffix = '...') => {
    if (!str || str.length <= length) return str;
    
    return str.substring(0, length) + suffix;
};

// 首字母大寫(xiě)函數
export const capitalize = (str) => {
    if (!str) return '';
    
    return str.charAt(0).toUpperCase() + str.slice(1).toLowerCase();
};

// 駝峰命名轉換函數
export const toCamelCase = (str) => {
    if (!str) return '';
    
    return str.replace(/[-_\s]+(.)?/g, (_, c) => c ? c.toUpperCase() : '');
};

// 下劃線(xiàn)命名轉換函數
export const toSnakeCase = (str) => {
    if (!str) return '';
    
    return str.replace(/([A-Z])/g, '_$1').toLowerCase();
};

// HTML轉義函數
export const escapeHtml = (str) => {
    if (!str) return '';
    
    const escapeMap = {
        '&': '&',
        '<': '<',
        '>': '>',
        '"': '"',
        "'": '&#x27;',
        '/': '&#x2F;'
    };
    
    return str.replace(/[&<>"'\/]/g, char => escapeMap[char]);
};

這些純函數為應用提供了無(wú)副作用的工具函數,支持函數式編程模式。

文章目錄

    亚洲see少妇裸体pics,欧美日产欧美日产免费一区,亚洲综合av一区二区三区不卡,一区二区中文字幕无码成人片,一区二区三区四区高清无码