通用js方法封装处理

通用js方法封装处理


/**
 * 通用js方法封装处理
 */


/**
* 参数处理
* @param {*} params  参数
*/
export function tansParams(params) {
  let result = ''
  for (const propName of Object.keys(params)) {
    const value = params[propName];
    var part = encodeURIComponent(propName) + "=";
    if (value !== null && value !== "" && typeof (value) !== "undefined") {
      if (typeof value === 'object') {
        for (const key of Object.keys(value)) {
          if (value[key] !== null && value[key] !== "" && typeof (value[key]) !== 'undefined') {
            let params = propName + '[' + key + ']';
            var subPart = encodeURIComponent(params) + "=";
            result += subPart + encodeURIComponent(value[key]) + "&";
          }
        }
      } else {
        result += part + encodeURIComponent(value) + "&";
      }
    }
  }
  return result
}

// 验证是否为blob格式
export function blobValidate(data) {
  return data.type !== 'application/json'
}

//轮播展示的数据分组
/**
* 使用方法
* <el-carousel-item v-for="(group, index) in dividedArr" :key="index">
      <div v-for="item in group" :key="item.id">
        <h3 class="small">{{ item.name }}</h3>
      </div>
    </el-carousel-item>
  </el-carousel>
  import { generateCarouselItems } from "~/utils/tyler";
  computed: {
    dividedArr() {
      return generateCarouselItems(arr, 2);数组  个数
    },
  },
*/
export const generateCarouselItems = (arr, itemsPerGroup) => {
  const totalGroups = Math.ceil(arr.length / itemsPerGroup);
  const result = [];
  for (let i = 0; i < totalGroups; i++) {
    const startIndex = i * itemsPerGroup;
    const endIndex = startIndex + itemsPerGroup;
    result.push(arr.slice(startIndex, endIndex));
  }
  return result;
};
export function extractContent(htmlString) {
  let withoutImg = htmlString.replace(/<img[^>]*>/g, '');
  var removeImgTag = withoutImg.replace(/<br>/g, "");
  removeImgTag = removeImgTag.replace(/\s+/g, ' ').trim();
  const regex = /<p>(.*?)<\/p>/g;
  const matches = [];
  let match;
  while ((match = regex.exec(removeImgTag)) !== null) {
    matches.push(match[1]);
  }
  for (let i = 0; i < matches.length; i++) {
    if (matches[i]) {
      return matches[i];
    }

  }

  return ''
};
//各种时间格式
export function formatDate(timestamp, format = 'y-m-d') {
  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 formats = {
    'y': year,
    'm': month,
    'd': day,
    'y-m': `${year}/${month}`,
    'm-d': `${month}-${day}`,
    'y.m': `${year}.${month}`,
    'default': `${year}-${month}-${day}`
  };

  return formats[format] || formats['default'];
};
//字典回显
export function getDictLabelFromVal(dictionaryArray, dictValue) {
  const dictItem = dictionaryArray.find(item => item.dictValue === dictValue);
  return dictItem ? dictItem.dictLabel : null;
}
// 获取指定数组对象数据(arr,[{age:20,sex:'male'}])
export function filterByFields(dataArray, filtersArray) {
  return dataArray.filter(function (item) {
    for (const filterObj of filtersArray) {
      let match = true;
      for (const key in filterObj) {
        if (item.hasOwnProperty(key) && item[key] !== filterObj[key]) {
          match = false;
          break;
        }
      }
      if (match) {
        return true;
      }
    }
    return false;
  });

  export function removeTagsAndSpaces(input) {
        // 使用正则表达式替换
        var output = input.replace(/<\/?[^>]+(>|$)/g, ''); // 去除所有的<>标签
        output = output.replace(/[\t\n\r]/g, ''); // 去除制表符、换行符、回车符
        output = output.replace(/&nbsp;/g, ''); // 去除&nbsp;
        output = output.replace(/\s+/g, ''); // 去除空白字符

        return output;
    },
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值