{"size": 1213, "ext": "js", "max_stars_repo_path": "src/utils/cookie/set.js", "max_stars_repo_name": "wenyejie/stormjs", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "src/utils/cookie/set.js", "max_issues_repo_name": "wenyejie/stormjs", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "src/utils/cookie/set.js", "max_forks_repo_name": "wenyejie/stormjs", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 23.7843137255, "max_line_length": 83, "alphanum_fraction": 0.5737840066}
/**
 *
 *
 * @author: Storm
 * @date: 2018/02/24
 */

import encodeURI from '../encodeURI'

import { domain, path, secure } from './options'

export default (key, value, opts) => {
  opts = Object.assign({
    path,
    domain,
    secure,

    // 有效期一天
    expires: new Date().setTime(Date.now() + 864e5)
  }, opts)

  // key为空, 或者key为关键字, 退出方法
  if (!key || /^(?:expires|path|domain|secure)$/i.test(key)) {
    return false
  }

  // 有效期 1year = 31536e6, 1month = 2592e6, 1days = 864e5, 1hour = 36e5, 1min = 6e4;
  if (typeof opts.expires === 'number') {
    if (opts.expires === Infinity) {
      opts.expires = '9999/12/31 23:59:59:999'
    } else if (opts.expires / 1e12 < 1) {
      opts.expires = Date.now() + opts.expires
    }
  }

  // 如果日期错误
  if (/invalid date/i.test(opts.expires.toString())) {
    opts.expires = new Date().setTime(Date.now() + 864e5)
  }
  opts.expires = new Date(+opts.expires + 288e5)

  document.cookie = ([
    encodeURI(key) + '=' + encodeURI(value),
    opts.expires ? '; expires=' + opts.expires.toUTCString() : '',
    opts.path ? '; path=' + opts.path : '',
    opts.domain ? '; domain=' + opts.domain : '',
    opts.secure ? '; secure' : ''
  ].join(''))
  return true
}
