js方法

127 阅读1分钟

/**

  • @description 匹配关键词高亮
  • @param {string} text
  • @param {Array} words 关键词数组
  • @returns {string} tag 匹配出的关键词标签 / export const highLightKeywords = (text, words, tag = 'span') => { if (!isDef(text)) { return '' } const len = words.length for (let i = 0; i < len; i++) { // 正则匹配所有的文本 const re = new RegExp((words[i]) + '\b', 'g') if (re.test(text)) { // let num = -1 // const rHtml = new RegExp('<.?>', 'ig') // const aHtml = text.match(rHtml) // 存放html元素的数组 // text = text.replace(rHtml, '{}') // 替换html标签 text = text.replace(re, '<' + tag + ' class="highlight">$&</ // text = text.replace(/{}/g, function() { // num++ // return aHtml[num] // }) } } return text }

/**

  • @description 截取字符串的长度
  • @param {string} str
  • @param {number} len 获取的长度
  • @returns {Boolean} b 是否区分中文(一个中文等于两个字节长度) / export const getStrByByte = (str, len, b, e) => { if (!isDef(str)) { return '' } let _str = '' let bytesCount = 0 for (let i = 0; i < str.length; i++) { if (str.charCodeAt(i) < 27 || (str.charCodeAt(i) > 126 && b)) bytesCount += 2 } else { bytesCount += 1 } if (bytesCount > len) { if (e) { _str += '...' } break } else { _str += str.charAt(i) } } return _str } /*
  • @description 把url参数转换为json
  • @param {string} url
  • @returns {Object} */ export const param2Obj = (url) => { const search = url.split('?')[1] || url.split('?')[0] if (!search) { return {} } return JSON.parse( '{"' + decodeURIComponent(search) .replace(/"/g, '\"') .replace(/&/g, '","') .replace(/=/g, '":"') .replace(/+/g, ' ') + '"}' ) }

/**

  • @description split number 用于千分位分割
  • @param {(string|number)} source
  • @param {string} length
  • @returns {string} */ export const numberComma = (source, length = 3) => { if (!isDef(source)) { return 0 } source = String(source).split('.') source[0] = source[0].replace(new RegExp('(\d)(?=(\d{' + lengt return source.join('.') }

/**

  • @description 取两个数之间的随机整数
  • @param {(number)} min
  • @param {number} max
  • @returns {number} */ export const numberRandom = (min, max) => { return Math.floor(Math.random() * (max - min + 1) + min) }

/**

  • @description 判断是否为null 或者 undefined
  • @param {any} value
  • @returns {boolean} */ export const isDef = (value) => { return value !== undefined && value !== null }

/**

  • @description 判断是否为obj
  • @param {any} x
  • @returns {boolean} */ export const isObj = (x) => { const type = typeof x return x !== null && (type === 'object' || type === 'function') }

/**

  • @description 判断是否为数字
  • @param {any} value
  • @returns {boolean} */ export const isNumber = (value) => { return /^\d+(.\d+)?$/.test(value) }

export const range = (num, min, max) => { return Math.min(Math.max(num, min), max) }

/**

  • @description 生成唯一uuid
  • @returns {string} */ export const uuid = () => { return ('xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, const r = Math.random() * 16 | 0 const v = c === 'x' ? r : (r & 0x3 | 0x8) return v.toString(16) })) }

/**

  • @description 根据子级查找父级
  • @param {array} data
  • @param {string|number} target
  • @param {array} children
  • @param {string|number} value
  • @returns {array} */ export const getParentNode = (data, target, children = 'children', const get = (child, target, record = []) => ( child.reduce((result, { [value]: id, [children]: innerChildren if (id === target) { return [...record, target] } if (innerChildren) { return [...result, ...get(innerChildren, target, [...recor } return result }, []))

return get(data, target) }

/**

  • @description 处理树形结构数组,把为空数组的child设置为null
  • @param {array} data
  • @returns {array} */ export const getTreeList = (tree = [], children = 'children') => { return tree.map(item => { if (item[children] && item[children].length > 0) { item[children] = getTreeList(item[children]) } else { item[children] = null } return item }) }

/**

  • 获取设备信息
  • @returns {*} */ export const device = () => { const deviceReg = /(iphone|ipad|android|mac|win|linux)/i const appReg = /aihotelbrowser/(Android_|Ios_|[\d.]{5,})([\d. const userAgent = window.navigator.userAgent let device = userAgent.match(deviceReg) device = device && device[0].toLowerCase() const app = userAgent.match(appReg) const version = app && app[2] return { isWin: device === 'win', isLinux: device === 'linux', isMac: device === 'mac', isIphone: device === 'iphone', isIpad: device === 'ipad', isIos: device === 'iphone' || device === 'ipad', isAndroid: device === 'android', isPC: !device, isApp: !!app, appVersion: version ? app[2] : '', isWeixin: Boolean(userAgent.match(/micromessenger/i)) } }