/** * 判断是否为空 * @param val 待判断值 * @returns 是否合法 */ export const isRequired = (val: string): boolean => { return val !== '' } // /** * 判断是否为整数 * @param value * @returns 是否合法 */ export const isInteger = (value: any) => { return Number.isInteger(Number(value)) } /** * 判断是否为ip * @param ip 待判断 * @returns 是否合法 */ export const isIp = (ip: string): boolean => { var rep = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/ return rep.test(ip) } /** * 判断端口是否合法 * @param port 待判断端口 * @returns 是否合法 */ export const isPort = (port: number | string): boolean => { if (!isInteger(port) || port > 65535 || port < 1) { return false } return true } // /** * 判断整数范围 * @param value 待判断数值 * @param max 最大值 * @param min 最小值 * @returns 是否符合数值范围 */ export const isInRange = (value: number, max = 20, min = -10): boolean => { if (!isInteger(value) || value > max || min < -10) { return false } return true }