index.js 974 Bytes
'use strict';

const url = require('url');

/**
 *   refer限制
 *  @param referer string
 *  @param blacklist [array|function] refer黑名单  TODO: 未实现
 *      1. array: ['/login', '/signin'] 如果referer 在array中,将返回 /home
 *      2. function: 如果返回true, 返回/home
 *  @return referer
 */
exports.refererLimit = (referer, blacklist) => {  // eslint-disable-line
    let result;

    try {
        result = decodeURIComponent(referer || '/home');
    } catch (e) {
        result = referer || '/home';
    }

    let urlObj = url.parse(result, false, true);


    if (urlObj.hostname && !/(?:yohobuy\.com$)|(?:yoho\.cn$)/.test(urlObj.hostname)) {
        result = '/home';
    }

    // TODO: blacklist;

    return result;
};

/**
 * 对象字段排序
 */
exports.mapSort = obj => {
    if (!obj) {
        return {};
    }
    let data = {};

    Object.keys(obj).sort().forEach(k => {
        data[k] = obj[k];
    });

    return data;
};