JS操作COOKIE

作者: 万年对朝夕 | 来源:发表于2019-01-02 14:14 被阅读3次
Cookies = {
  _cookies: {},
  keyTest (key) {
    if (!key) {
      console.error('key不能为空')
      return true
    }
    if (/(?:[`~!@#%^&*()-=+])/.test(key)) {
      console.error('key不能有`~!@#%^&*()-=+字符')
      return true
    }
    if (/^(?:path|domain|secure|expires|max-age)$/.test(key)) {
      console.error('key不能为path|domain|secure|expires|maxAge')
      return true
    }
    return false
  },
  valTest (val) {
    if (/(?:[;])/.test(val)) {
      console.error('val不能有;字符')
      return true
    }
    return false
  },
  endTest (end) {
    if (/^(0|-?[1-9]\d*)$/.test(end)) {
      return 'number'
    } else if (/^(?:[\d]+[Y|M|D|h|m|s])$/.test(end)) {
      return 'scheme'
    }
    console.error('end格式错识,Number默认秒|String默认秒,例:100Y=100年,Y:1年/365天,M:1月/30天,D:天,H:小时,m:分钟,s:秒')
    return false
  },
  typeTest (val, type) {
    if (!/^string$/.test(typeof type)) {
      console.error('type必须为string')
      return true
    }
    if (!/^boolean|null|undefined|number|string|symbol|object$/.test(type)) {
      console.error('只能判断boolean|null|undefined|number|string|symbol|object类型')
      return true
    }
    // eslint-disable-next-line valid-typeof
    if (typeof val === type) {
      return false
    }
    return true
  },
  set (
    key = '',
    val = '',
    end = '100Y',
    path = '',
    domain = '',
    secure = false
  ) {
    if (this.typeTest(key, 'string')) {
      console.error('key必须为string')
      return false
    }
    if (this.typeTest(val, 'string') && this.typeTest(val, 'number')) {
      console.error('val必须为string|number')
      return false
    }
    if (this.typeTest(end, 'string') && this.typeTest(end, 'number')) {
      console.error('end必须为string|number')
      return false
    }
    if (this.typeTest(path, 'string')) {
      console.error('path必须为string')
      return false
    }
    if (this.typeTest(domain, 'string')) {
      console.error('domain必须为string')
      return false
    }
    if (this.typeTest(secure, 'boolean')) {
      console.error('secure必须为boolean')
      return false
    }

    key = this.trim(key)
    val = this.trim(val.toString())
    end = this.trim(end.toString())
    path = this.trim(path)
    domain = this.trim(domain)

    if (this.keyTest(key)) return false
    if (this.valTest(val)) return false
    let NorS = this.endTest(end)
    if (!NorS) return false

    let _end = ''

    if (NorS === 'number') {
      _end = ';max-age=' + end
    }
    if (NorS === 'scheme') {
      _end = ';expires=' + this.getUTCString(end)
    }

    let _path = path && ';domain=' + path
    let _domain = domain && ';domain=' + domain
    let _secure = secure ? ';secure' : ''

    let _ck = `${key}=${val}${_end}${_path}${_domain}${_secure}`
    document.cookie = _ck
  },
  get (key) {
    if (this.keyTest(key) || !this.hasKey(key)) return false
    return (document.cookie.replace(new RegExp(`(?:^|;\\s)(?:${key}=)([^;]*)(?:$|;\\s)`), '$1') || false)
  },
  clear (key) {
    if (this.keyTest(key) || !this.hasKey(key)) return false
    this.set(key, '', -1)
  },
  getUTCString (time) {
    const Times = {
      Y: 1000 * 60 * 60 * 24 * 365,
      M: 1000 * 60 * 60 * 24 * 30,
      D: 1000 * 60 * 60 * 24,
      h: 1000 * 60 * 60,
      m: 1000 * 60,
      s: 1000
    }
    // (^0$)|(^-[1-9]\d*$)|(^[1-9]\d*$)
    const TimesKey = time.match(/Y|M|D|h|m|s/)[0]
    return new Date(new Date().getTime() + time.replace(TimesKey, '') * Times[TimesKey]).toUTCString()
  },
  keys () {
    return this.trim(document.cookie.replace(/(?:^|;\s)([^=]*)(?:=[^;]*)/g, '$1 ')).split(' ')
  },
  hasKey (key) {
    return !!this.keys().filter(item => item === key).length
  },
  trim (val) {
    return val.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '')
  }
}

相关文章

网友评论

    本文标题:JS操作COOKIE

    本文链接:https://www.haomeiwen.com/subject/upjzlqtx.html