diff --git a/src/pages/config/__test__/useConfigCheck.test.ts b/src/pages/config/__test__/useConfigCheck.test.ts new file mode 100644 index 00000000..91bae0c3 --- /dev/null +++ b/src/pages/config/__test__/useConfigCheck.test.ts @@ -0,0 +1,26 @@ +import { checkHost, checkIp, checkPort } from '../useConfigCheck'; + +describe('check methods', () => { + it('check ip', () => { + expect(checkIp('127.0.0.1')).toBe(true); + expect(checkIp('127.0.1')).toBe(false); + expect(checkIp('')).toBe(false); + }); + + it('check port', () => { + expect(checkPort(80)).toBe(true); + expect(checkPort(-1)).toBe(false); + expect(checkPort(70000)).toBe(false); + expect(checkPort()).toBe(false); + }); + + it('check host', () => { + expect(checkHost('127.0.0.1:2523')).toContainEqual(true); + expect(checkHost('127.0.0.1')).toContainEqual(false); + expect(checkHost('127.0.1:')).toContainEqual(false); + expect(checkHost(':2523')).toContainEqual(false); + expect(checkHost('127.0.0.1:70000')).toContainEqual(false); + expect(checkHost('dfajlskdfa0')).toContainEqual(false); + expect(checkHost('')).toContainEqual(false); + }); +}); diff --git a/src/pages/config/useConfigCheck.ts b/src/pages/config/useConfigCheck.ts index cdc18d2f..8635f4d9 100644 --- a/src/pages/config/useConfigCheck.ts +++ b/src/pages/config/useConfigCheck.ts @@ -1,39 +1,40 @@ import { form } from './form-data'; -export function useConfigCheck() { - const checkIp = (ip: string) => { - const check = - /^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$/; +export function checkIp(ip: string) { + const check = + /^((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.){3}(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])(?::(?:[0-9]|[1-9][0-9]{1,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5]))?$/; - return check.test(ip); - }; + return check.test(ip); +} - /** 有效端口检测 */ - const checkPort = (port: number) => { - if (port >= 0 && port <= 65535) return true; - else return false; - }; +/** 有效端口检测 */ +export function checkPort(port?: number) { + if (!port) return false; + if (port >= 0 && port <= 65535) return true; + else return false; +} - /** host 格式检测 */ - const checkHost = (host: string): [boolean, string] => { - if (host === '') return [false, '请输入host']; - if (!/:/.test(host)) { - return [false, "缺少 ':'"]; - } else { - const [ip, port] = host.split(':'); +/** host 格式检测 */ +export function checkHost(host: string): [boolean, string] { + if (host === '') return [false, '请输入host']; + if (!/:/.test(host)) { + return [false, "缺少 ':'"]; + } else { + const [ip, port] = host.split(':'); - if (!checkIp(ip)) { - return [false, '请输入有效ip!']; - } - - if (!checkPort(Number(port))) { - return [false, '请输入有效端口!']; - } + if (!checkIp(ip)) { + return [false, '请输入有效ip!']; } - return [true, '']; - }; + if (!checkPort(Number(port))) { + return [false, '请输入有效端口!']; + } + } + return [true, '']; +} + +export function useConfigCheck() { /** 端口验证 */ function validtePort(rule: any, value: any, callback: any) { if (value === '') return callback(new Error('请输入端口号'));