JS 构建参数过滤器

buildFilterParams.js
function buildFilterParams(params) {
    let filterParams = {}
    for (let key in params) {
        if (params[key]) {
            filterParams[key] = params[key]
        }
    }
    return filterParams
}





/**
 * 构建过滤参数
 * @param {Object} filters - 过滤条件对象,键为字段名,值为过滤值
 * @returns {Object} - 返回构建好的过滤参数
 */
function buildFilterParams(filters) {
    const filterParams = {};

    // 遍历过滤条件对象
    for (const [key, value] of Object.entries(filters)) {
        if (value !== undefined && value !== null) {
            if (Array.isArray(value)) {
                // 如果过滤值是数组,通常意味着这是一个范围或多项过滤
                filterParams[key] = value;
            } else if (typeof value === 'object' && value !== null) {
                // 如果过滤值是对象,假设是一个范围查询,处理 min 和 max
                if (value.min !== undefined) filterParams[`${key}_min`] = value.min;
                if (value.max !== undefined) filterParams[`${key}_max`] = value.max;
            } else {
                // 否则直接设置值
                filterParams[key] = value;
            }
        }
    }
    return filterParams;
}

// 示例用法
const filters = {
    name: 'John',              // 字段 'name' 为 'John'
    age: { min: 18, max: 30 },  // 'age' 字段为范围查询 18 到 30
    status: ['active', 'pending'], // 'status' 字段为多个值的数组
    country: null              // 'country' 为 null, 将不会被加入过滤参数
};

const filterParams = buildFilterParams(filters);
console.log(filterParams);






/**
 * 构建过滤参数对象
 * @param {Object} filters - 包含所有可能过滤条件的对象
 * @param {Object} options - 配置选项
 * @param {Array} [options.excludeKeys=[]] - 需要排除的键名
 * @param {Object} [options.keyMap={}] - 键名映射,将前端键名映射为后端键名
 * @param {boolean} [options.removeEmpty=true] - 是否移除空值(null, undefined, '')
 * @param {boolean} [options.removeEmptyArray=true] - 是否移除空数组
 * @param {boolean} [options.removeEmptyString=false] - 是否移除空字符串(当removeEmpty为false时有效)
 * @param {Function} [options.valueTransform=null] - 值转换函数
 * @returns {Object} 处理后的过滤参数对象
 */
function buildFilterParams(filters = {}, options = {}) {
    const {
        excludeKeys = [],
        keyMap = {},
        removeEmpty = true,
        removeEmptyArray = true,
        removeEmptyString = false,
        valueTransform = null
    } = options;
  
    const params = {};
  
    Object.entries(filters).forEach(([key, value]) => {
        // 跳过排除的键
        if (excludeKeys.includes(key)) {
            return;
        }
  
        // 处理空值
        if (removeEmpty && (value === null || value === undefined || value === '')) {
            return;
        }
      
        // 单独处理空字符串
        if (!removeEmpty && removeEmptyString && value === '') {
            return;
        }
  
        // 处理空数组
        if (removeEmptyArray && Array.isArray(value) && value.length === 0) {
            return;
        }
  
        // 应用键名映射
        const mappedKey = keyMap[key] || key;
  
        // 应用值转换
        const finalValue = valueTransform ? valueTransform(key, value) : value;
  
        params[mappedKey] = finalValue;
    });
  
    return params;
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容