import request from 'superagent'

/**
 * @param  {Object} options
 * @return {Promise}
 */
function ajax(options) {
    const defaults = {
        type: 'post',
        url: null,
        data: {},
        'Content-Type': 'application/json'
    }
    let promise

    options = Object.assign({}, defaults, options)
    promise = request[options.type](options.url).withCredentials()
    Object.keys(options).forEach(key => {
        if (!key.match(/type|url|data/)) {
            promise.set(key, options[key])
        }
    })

    return new Promise(resolve => {
        promise.send(options.data).then(res => {
            resolve(res.body)
        }).catch(err => {
            console.log(err)
        })
    })
}

/**
 * @param  {number} timestamp
 * @param  {String} format
 * @return {String}
 */
function formatDate(timestamp, format) {
    const date = new Date(timestamp),
        years = date.getFullYear().toString(),
        month = date.getMonth().toString(),
        day = date.getDate().toString(),
        hours = date.getMonth().toString(),
        minutes = date.getMinutes().toString(),
        seconds = date.getSeconds().toString()

    return format.replace(/y+|M+|d+|h+|m+|s+/g, match => {
        const length = match.length

        switch (match.charAt(0)) {
            case 'y':
                return years.substr(-length)
            case 'M':
                return (month.length === 1 ? `0${month}` : month).substr(-length)
            case 'd':
                return (day.length === 1 ? `0${day}` : day).substr(-length)
            case 'h':
                return (hours.length === 1 ? `0${hours}` : hours).substr(-length)
            case 'm':
                return (minutes.length === 1 ? `0${minutes}` : minutes).substr(-length)
            case 's':
                return (seconds.length === 1 ? `0${seconds}` : seconds).substr(-length)
        }
    })
}

/**
 * @param  {String} str
 * @return {Date}
 */
function parseDate(str) {
    let results

    if (results = str.match(/^\s*(\d{4})-(\d{1,2})-(\d{1,2})\s*$/)) {
        return new Date(+results[1], +results[2] - 1, +results[3])
    } else if (results = str.match(/^\s*(\d{4})-(\d{1,2})-(\d{1,2})\s+(\d{1,2}):(\d{1,2}):(\d{1,2})\s*$/)) {
        return new Date(+results[1], +results[2] - 1, +results[3], +results[4], +results[5], +results[6])
    }
}

/**
 * @return {Object}
 */
function getUrlParams() {
    const search = location.search.slice(1),
        rParam = /([^&]*)=([^&]*)/g
    let ret = {},
        param

    while (param = rParam.exec(search)) {
        ret[param[1]] = param[2]
    }

    return ret
}

export default {
    ajax,
    formatDate,
    parseDate,
    getUrlParams
}

@@ -888,24 +888,27 @@
     years = 
+%60$%7B
 date.getFull
@@ -913,27 +913,18 @@
 llYear()
-.toString()
+%7D%60
 ,%0A      
@@ -933,16 +933,19 @@
 month = 
+%60$%7B
 date.get
@@ -947,35 +947,26 @@
 e.getMonth()
-.toString()
+%7D%60
 ,%0A        da
@@ -969,16 +969,19 @@
   day = 
+%60$%7B
 date.get
@@ -986,27 +986,18 @@
 etDate()
-.toString()
+%7D%60
 ,%0A      
@@ -1002,24 +1002,27 @@
     hours = 
+%60$%7B
 date.getMont
@@ -1024,27 +1024,18 @@
 tMonth()
-.toString()
+%7D%60
 ,%0A      
@@ -1042,24 +1042,27 @@
   minutes = 
+%60$%7B
 date.getMinu
@@ -1066,27 +1066,18 @@
 inutes()
-.toString()
+%7D%60
 ,%0A      
@@ -1088,16 +1088,19 @@
 conds = 
+%60$%7B
 date.get
@@ -1112,19 +1112,10 @@
 ds()
-.toString()
+%7D%60
 %0A%0A  

Optimize formatDate util using es6