/**
 * Created by PanJiaChen on 16/11/18.
 */

/**
 * @param {string} path
 * @returns {Boolean}
 */
export function isExternal(path) {
    return /^(https?:|mailto:|tel:)/.test(path)
}

/**
 * @param {string} str
 * @returns {Boolean}
 */
export function validUsername(str) {
    const valid_map = ['admin', 'editor', 'zhangsan', 'lisi']
    return valid_map.indexOf(str.trim()) >= 0
}
/**
 * 手机号码验证，含必填校验
 * @param {*} rule
 * @param {*} value
 * @param {*} callback
 */
export const validPhone = (rule, value, callback) => {
    const reg = /^1([38][0-9]|4[014-9]|[59][0-35-9]|6[2567]|7[0-8])\d{8}$/
    if (!value) {
        callback(new Error('请输入电话号码'))
    } else if (!reg.test(value)) {
        callback(new Error('请输入正确的11位手机号码'))
    } else {
        callback()
    }
}
/**
 * @param {string} email
 * @returns {Boolean}
 */
export function validEmail(email) {
    const reg = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    return reg.test(email)
}
/**
 *
 * @param {string|number} phone
 * @returns {Boolean}
 */
export function isvalidPhone(phone) {
    const reg = /^1([38][0-9]|4[014-9]|[59][0-35-9]|6[2567]|7[0-8])\d{8}$/
    return reg.test(phone)
}

/**
 * @param {string} number
 * @returns {Boolean}
 */
 export function isPosNumber(number) {
    const reg = /^[0-9]+(.[0-9]+)?$/
    return reg.test(number)
}
/**
 * @param {string} number
 * @returns {Boolean}
 */
 export function isNegNumber(number) {
    const reg = /^\-[0-9]+(.[0-9]+)?$/
    return reg.test(number)
}

/**
 * @param {string} number
 * @returns {Boolean}
 */
 export function isNumber(number) {
    const reg = /^\-?[0-9]+(.[0-9]+)?$/
    return reg.test(number)
}

/**
 * @param {string} number
 * @returns {Boolean}
 */
 export function isPosInteger(number) {
    const reg = /^[0-9]+$/
    return reg.test(number)
}
/**
 * @param {string} number
 * @returns {Boolean}
 */
 export function isNegInteger(number) {
    const reg = /^\-[0-9]+$/
    return reg.test(number)
}

/**
 * @param {string} number
 * @returns {Boolean}
 */
 export function isInteger(number) {
    const reg = /^\-?[0-9]+$/
    return reg.test(number)
}

export function isTableName(str) {
    const reg = /^[a-z0-9_]+$/
    return reg.test(str)
}

export function isFieldName(str) {
    const reg = /^\w+$/
    return reg.test(str)
}

export default { isExternal, validUsername, validPhone, validEmail, isvalidPhone, isPosNumber, isNegNumber, isNumber, isPosInteger, isNegInteger, isInteger, isTableName, isFieldName }