All files / src mtfunit.js

36.17% Statements 34/94
36.47% Branches 31/85
25% Functions 5/20
32.81% Lines 21/64

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185                    9x 9x 9x                     10x 10x 10x                                                                                                                                                                                                                                                                       21x 16x 16x 16x 14x 14x 14x 42x 14x                       15x 11x 11x 33x 11x     1x
/**
 * 获取指定的 querystring 中指定 name 的 value
 * @param {String} name
 * @param {String} querystring
 * @return {String|undefined}
 *
 * query('hello', '?hello=js') 结果是 js
 *
 */
function query (name, querystring) {
  if (name && name.match(/(?:http[s]?:|^)\/{2}.*?(?:\.)/)) name = encodeURIComponent(name) // 仅编码Url
  const r = (querystring + '').match(new RegExp('(?:^|&)\\s*?' + name + '\\s*=\\s*(.*?)\\s*(?:&|$)')) 
  return r === null ? undefined : decodeURIComponent(r[1])
}
 
/**
 * 序列化对象,就是把对象转成 url 字符串
 * @param {Obj} data
 * @return {String}
 *
 * serialize({hello: 'js', hi: 'test'}) 结果是 ''
 */
function serialize (data) {
  let queryString = []
  Object.keys(data || {}).forEach(key => typeof key === 'string' && queryString.push(encodeURIComponent(key) + '=' + encodeURIComponent(typeof data[key] === 'object' ? JSON.stringify(data[key]) : data[key])))
  return queryString.join('&')
}
 
/**
 * 根据选择器查找 DOM
 * 就是模拟 $() ,当然,这里返回元素的 DOM 对象即可
 * @param {String} selector
 * @return {DOM|Null|Array} 单selector,始终返回第一个DOM。多个逗号分隔selector,始终返回Array
 */
function $ (selector) {
  if (!selector) return null
  let selectorStr = (selector + '').replace(/,$/, '') , i = -1
  try {
    const nComma = selectorStr.split(',').length, r = document.querySelectorAll(selectorStr), res = []
    if (nComma > 1) {
      while (++i < nComma && r[i]) res[i] = r[i]
      return res
    }
    return r[0] || null
  } catch (e) {
    return null
  }
}
 
/**
 * 删除 DOM 节点
 * @param {DOM|Array<Dom>} node / nodesList
 * @return {DOM|Array<Dom>} node / nodesList
 */
 
function removeNode (node) {
  return (node && node.length ? Array.from(node) : [node]).map(node => node?.parentNode?.removeChild(node))
}
 
/**
 * 在 target 节点之后插入 node 节点
 * 类似 $().insertAfter()
 * @param {DOM|Array<Dom>} node / nodesList
 * @param {DOM|Null} target
 */
function insertAfter (node, target) {
  if (!node || node.length === 0) return null
  let fragment = document.createDocumentFragment()
  if (node && node.length) for (let i = 0; i < node.length; i++) fragment.appendChild(node[i])
  else fragment = node
  return (target?.parentNode || document.body).insertBefore(fragment, target?.nextSibling || null), node
}
 
/**
 * 添加类名
 * @param {DOM|Array<Dom>} node / nodesList
 * @param {String|Array} className
 */
function addClass (node, className) {
  return (node && node.length ? Array.from(node) : [node]).map(node => node?.classList.add(className))
}
 
 
/**
 * 移除类名
 * @param {DOM|Array<Dom>} node / nodesList
 * @param {String|Array} className
 */
function removeClass (node, className) {
  return (node && node.length ? Array.from(node) : [node]).map(node => node?.classList.remove(className))
}
 
/**
 * 获取绝对路径
 * @param {String} url
 * @return {String} // 返回值与浏览器表现相同
 *
 * getAbsoluteUrl('/jerojiang') => 'http://imweb.io/jerojiang'
 * 在当前页面获取绝对路径,这里要创建 A 元素,测试用例看你们的了
 */
function getAbsoluteUrl (url) {
  if (!url) return location.href
  url = (url || '') + ''
  const { origin, pathname } = location
  const stack = pathname.replace(/^\/|\/$/g, '').split('/')
  for (let i = 0, w = ''; i <= url.length; i++) {
    const s = url[i]
    if (s === '/' || s === void 0) {
      if (w.substring(0, 2) === '..') {
        stack.pop()
      } else if (w !== '.') {
        i === 0 ? stack.length = 0 : stack.push(w)
      }
      w = ''
    } else w += s
  } 
  return origin + '/' + stack.join('/')
}
 
/**
 * 防抖动
 * 防抖动函数了啦,有做个这个习题,不清楚回去复习
 */
function debounce (callback, time) {
  let timer = null
  return function (...args) {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      timer = null
      callback.apply(this, args)
    }, time | 0 || 1000 / 60)
  }
}
 
/**
 * 节流
 */
function throttle (callback, time) {
  let timer = null
  return function (...args) {
    if (timer) return
    timer = setTimeout(() => {
      timer = null
      callback.apply(this, args)
    }, time | 0 || 1000 / 60)
  }
}
 
/**
 *  根据索引移出数组的某一项
 * @param {Number} index 与splice的index参数表现相同
 * @param {Array} arr
 * @return {Array}
 *
 * removeItemByIndex(1, [1,2,3]) => [1, 3]
 */
function removeItemByIndex (index, arr) {
  if (!arr || !arr.length || typeof index === 'bigint') return arr
  const n = arr.length
  index = index | 0
  if (index >= n) return arr
  if (index < 0) index = index <= -n ? 0 : index % n + n 
  const r = Array(n - 1)
  for (let i = 0, j = 0; i < n; i++)
    if (i !== index) r[j++] = arr[i]
  return r
}
 
/**
 *  根据值移出数组的某一项
 * @param {Number} index
 * @param {Array} arr
 * @return {Array}
 *
 * removeItemByValue(1, [1,2,3]) => [2, 3]
 */
function removeItemByValue (value, arr) {
  if (!arr || !arr.length) return arr
  const n = arr.length, r = []
  for (let i = 0, j = 0; i < n; i++)
    if (arr[i] !== value) r[j++] = arr[i]
  return r
}
 
module.exports = { query, serialize, $, removeNode, insertAfter, addClass, removeClass, getAbsoluteUrl, debounce, throttle, removeItemByIndex, removeItemByValue }