All files / src utils.js

52.5% Statements 21/40
50% Branches 12/24
58.33% Functions 7/12
51.35% Lines 19/37
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              5x 5x   45x   5x     5x           4x 5x 2x                           2x 2x 2x   1x 1x     1x   1x                           1x 1x   1x               2x                    
import { parse, format as formatDate } from 'date-fns'
 
/**
 * javscript data type judgment
 * @param {*} obj 
 */
function classOf (obj) {
  const class2type = {}
  'Boolean Number String Function Array Date RegExp Object tips'.split(' ')
  .forEach((e, i) => {
    class2type['[object ' + e + ']'] = e.toLowerCase()
  })
  Iif (obj == null) {
    return String(obj)
  }
  return typeof obj === 'object' || typeof obj === 'function'
    ? class2type[ class2type.toString.call(obj) ] || 'object'
    : typeof obj
}
 
function filterRegParams (reg) {
  if (reg.indexOf(':') === -1) return [reg]
  const _reg = reg.split(':').map(item => item.trim())
  return [_reg.shift(), _reg]
}
 
function splitRegs (regs) {
  if (!regs) {
    throw new function () {
      return `the directive v-verify value is undefined`
    }()
    return
  }
  return regs.split('|')
}
 
function verifyValue (reg, value, params) {
  const _regType = classOf(reg)
  let _fn = null
  switch (_regType) {
    case 'regexp':
      _fn = (value) => {
        Iif (!reg.test(value)) {
          return false
        }
        return true
      }
      break
    case 'array':
      _fn = (value) => {
        let _bool = true
        for (let i = 0; i < reg.length; i++) {
          if (_typeof(reg[i]) === 'regexp') {
            _bool = reg[i].test(value)
          }
          if (!_bool) break
        }
        return _bool
      }
      break
    case 'function':
      _fn = (value) => {
        return params ? reg(value, params) : reg(value)
      }
      break
    default:
      _fn = (value) => {
        throw new function () {
          return 'type wrong in the config file'
        }()
      }
  }
  return _fn(value)
}
 
export {
  classOf,
  filterRegParams,
  splitRegs,
  parse,
  verifyValue,
  formatDate
}