From 6ac746f4fdeb7692c03cf4b0a51aaecb6685d9cf Mon Sep 17 00:00:00 2001 From: xjq Date: Sat, 19 Nov 2022 22:22:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=20IP=20=E5=9C=B0?= =?UTF-8?q?=E5=9D=80=E5=90=88=E6=B3=95=E6=80=A7=E9=AA=8C=E8=AF=81=E9=A2=98?= =?UTF-8?q?=E7=9B=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- question/FrontEnd/ipAddress/answer.md | 11 +++++ question/FrontEnd/ipAddress/answer.mjs | 5 +++ question/FrontEnd/ipAddress/index.md | 59 ++++++++++++++++++++++++++ question/FrontEnd/ipAddress/index.mjs | 12 ++++++ question/FrontEnd/ipAddress/test.mjs | 31 ++++++++++++++ question/FrontEnd/template/index.md | 6 +++ 6 files changed, 124 insertions(+) create mode 100644 question/FrontEnd/ipAddress/answer.md create mode 100644 question/FrontEnd/ipAddress/answer.mjs create mode 100644 question/FrontEnd/ipAddress/index.md create mode 100644 question/FrontEnd/ipAddress/index.mjs create mode 100644 question/FrontEnd/ipAddress/test.mjs diff --git a/question/FrontEnd/ipAddress/answer.md b/question/FrontEnd/ipAddress/answer.md new file mode 100644 index 0000000..fc6e500 --- /dev/null +++ b/question/FrontEnd/ipAddress/answer.md @@ -0,0 +1,11 @@ +### 方法一 + +正则表达式 + +```js +export default function isIp(ip) { + return /^((2(5[0-5]|[0-4]\d)|1\d{2}|\d{1,2})\.){3}(2(5[0-5]|[0-4]\d)|1\d{2}|\d{1,2})$/.test( + ip + ); +} +``` diff --git a/question/FrontEnd/ipAddress/answer.mjs b/question/FrontEnd/ipAddress/answer.mjs new file mode 100644 index 0000000..0b3f27e --- /dev/null +++ b/question/FrontEnd/ipAddress/answer.mjs @@ -0,0 +1,5 @@ +export default function f(ip) { + return /((2(5[0-4]|[0-4]\d)|1\d{2}|\d{1,2})\.){3}(2(5[0-4]|[0-4]\d)|1\d{2}|\d{1,2})/.test( + ip + ); +} diff --git a/question/FrontEnd/ipAddress/index.md b/question/FrontEnd/ipAddress/index.md new file mode 100644 index 0000000..3383b6c --- /dev/null +++ b/question/FrontEnd/ipAddress/index.md @@ -0,0 +1,59 @@ +验证 IP 地址合法性 + +一个合法的 IP 地址 在 0.0.0.0 ~ 255.255.255.255 的范围 + +具体的 IP 地址 分类 + +- A 类 IP 地址 范围 0.0.0.0 ~ 127.255.255.255 +- B 类 IP 地址 范围 128.0.0.0 ~ 191.255.255.255 +- C 类 IP 地址 范围 192.0.0.0 ~ 223.255.255.255 +- D 类 Ip 地址 范围 224.0.0.0 ~ 239.255.255.255 +- E 类 IP 地址 范围 240.0.0.0 ~ 255.255.255.255 + +用例 1: + +```js +const ip = '192.168.1.1'; + +isIp(ip); // true +``` + +用例 2: + +```js +const ip = '127.0.0.1'; + +isIp(ip); // true +``` + +用例 3: + +```js +const ip = '254.254.254.256'; + +isIp(ip); // false +``` + +用例 4: + +```js +const ip = '255.255.255.255'; + +isIp(ip); // true +``` + +用例 5: + +```js +const ip = '0.0.0.0'; + +isIp(ip); // true +``` + +用例 6: + +```js +const ip = '192.168.1.-1'; + +isIp(ip); // false +``` diff --git a/question/FrontEnd/ipAddress/index.mjs b/question/FrontEnd/ipAddress/index.mjs new file mode 100644 index 0000000..4837a51 --- /dev/null +++ b/question/FrontEnd/ipAddress/index.mjs @@ -0,0 +1,12 @@ +/** + * 判断 IP 地址是否合法 + * + * @export + * @param {string} ip + * @return {boolean} + */ +export default function isIp(ip) { + return /^((2(5[0-5]|[0-4]\d)|1\d{2}|\d{1,2})\.){3}(2(5[0-5]|[0-4]\d)|1\d{2}|\d{1,2})$/.test( + ip + ); +} diff --git a/question/FrontEnd/ipAddress/test.mjs b/question/FrontEnd/ipAddress/test.mjs new file mode 100644 index 0000000..ac9c81d --- /dev/null +++ b/question/FrontEnd/ipAddress/test.mjs @@ -0,0 +1,31 @@ +import f from './index.mjs'; +import { it } from 'mocha'; +import { assert } from 'chai'; + +it('用例 1: 输入 127.0.0.1', () => { + assert.deepEqual(f('127.0.0.1'), true); +}); + +it('用例 2: 输入 192.168.1.1', () => { + assert.deepEqual(f('192.168.1.1'), true); +}); + +it('用例 3: 输入 0.0.0.0', () => { + assert.deepEqual(f('0.0.0.0'), true); +}); + +it('用例 4: 输入 254.254.254.256', () => { + assert.deepEqual(f('254.254.254.256'), false); +}); + +it('用例 5: 输入 255.255.255.255', () => { + assert.deepEqual(f('255.255.255.255'), true); +}); + +it('用例 6: 输入 192.168.1.-1', () => { + assert.deepEqual(f('192.168.1.-1'), false); +}); + +it('用例 7: 输入 192.168.1.260', () => { + assert.deepEqual(f('192.168.1.260'), false); +}); diff --git a/question/FrontEnd/template/index.md b/question/FrontEnd/template/index.md index c03849c..8013e55 100644 --- a/question/FrontEnd/template/index.md +++ b/question/FrontEnd/template/index.md @@ -11,3 +11,9 @@ ```js ``` + +用例 3: + +```js + +```