js工具函数

63 阅读1分钟
// 提取src
export function httpString(s) {
  return s.split(' ').filter(_ => ~_.indexOf('src="'))
    .map(_ => _.replace(/src="|"/gi, ''));
}

// 数组元素置顶 top在数组中1是置顶的标识
export const top = (list, topId, optType) => {
  // optType场景
  // 1 - 置顶 - 置顶会话前
  // -1 - 取消置顶
  // 0 - 置顶 - 置顶会话之后
  const srcList = list.slice(0);
  const targetIdx = list.findIndex(el => el.id === topId);
  if (targetIdx < 0) {
    return srcList;
  }
  const optId = list.findIndex(el => el.top !== 1); // 操作位id
  const targetSession = srcList.splice(targetIdx, 1)[0];
  if (optType === 1) {
    srcList.unshift({
      ...targetSession,
      top: 1,
    });
  }

  if (optType === 0) {
    // 已置顶的会话当有新消息来的时候不该改变该会话于列表中的顺序
    srcList.splice(targetSession.top !== 1 ? (optId || 0) : targetIdx, 0, targetSession);
  }

  if (optType === -1) {
    srcList.push({
      ...targetSession,
      top: 0,
    });
  }
  return srcList;
};