{"size": 1695, "ext": "js", "max_stars_repo_path": "web/src/common/commonUtil.js", "max_stars_repo_name": "andrew/anyproxy", "max_stars_repo_stars_event_min_datetime": "2021-04-23T04:46:07.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-23T04:46:07.000Z", "max_issues_repo_path": "web/src/common/commonUtil.js", "max_issues_repo_name": "andrew/anyproxy", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "web/src/common/commonUtil.js", "max_forks_repo_name": "andrew/anyproxy", "max_forks_repo_forks_event_min_datetime": "2020-04-24T07:03:51.000Z", "max_forks_repo_forks_event_max_datetime": "2020-04-24T07:03:51.000Z"}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 25.2985074627, "max_line_length": 86, "alphanum_fraction": 0.5575221239}
/*
 * 存放常用工具类
 */

/*
 * 格式化日期
 * @param date Date or timestamp
 * @param formatter yyyyMMddHHmmss
 */
export function formatDate(date, formatter) {
    if (typeof date !== 'object') {
        date = new Date(date);
    }
    const transform = function(value) {
        return value < 10 ? '0' + value : value;
    };
    return formatter.replace(/^YYYY|MM|DD|hh|mm|ss/g, function(match) {
        switch (match) {
            case 'YYYY':
                return transform(date.getFullYear());
            case 'MM':
                return transform(date.getMonth() + 1);
            case 'mm':
                return transform(date.getMinutes());
            case 'DD':
                return transform(date.getDate());
            case 'hh':
                return transform(date.getHours());
            case 'ss':
                return transform(date.getSeconds());
        }
    });
}

export function selectText(element) {
    let range, selection;

    if (window.getSelection) {
        selection = window.getSelection();
        range = document.createRange();
        range.selectNodeContents(element);
        selection.removeAllRanges();
        selection.addRange(range);
    } else if (document.body.createTextRange) {
        range = document.body.createTextRange();
        range.moveToElementText(element);
        range.select();
    }
}

export function getQueryParameter (name) {
    var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
    if (results == null) {
        return '';
    } else {
        return results[1] || '';
    }
}

const CommonUtil = {
    formatDate,
    selectText,
    getQueryParameter
};

export default CommonUtil;
