Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

增加 ipv4 校验 #22

Merged
merged 1 commit into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ parserOptions:
# 2: error 开启规则, 使用错误级别的错误, 会导致程序退出
rules:
# 强制一行的最大长度
max-len:
- 2
- 120
# max-len:
# - 2
# - 120

# 禁止自身比较
no-self-compare:
Expand Down
2 changes: 1 addition & 1 deletion README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ validator

> [中文](https://github.com/sTdobTs/validator/blob/master/README.zh-CN.md) | [English](https://github.com/sTdobTs/validator)

基于 ECMAScript5.1 实现的验证工具包,提供了丰富的校验和工具方法,在 Node.js 和现代浏览器中可以使用。
基于 ECMAScript 5.1 实现的验证工具包,提供了丰富的校验和工具方法,在 Node.js 和现代浏览器中可以使用。

## 安装

Expand Down
7 changes: 4 additions & 3 deletions docs/en-US/APIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
API 文档
===========================

假设,您已经阅读,并且知道如何使用,可以直接跳过该步骤。若是还没有看过的同学,请看这里:
假设,您已经阅读,并且知道如何了使用,可以直接跳过该步骤。若是还没有看过的同学,请看这里:

```javascript
// 引入 `jxm-validator`
const jxmValidator = require('jxm-validator');

jxmValidator.isNull(null);
```

接下来,一起来看API吧。若是想了解更信息的信息,请点击相应的API
接下来,一起来看 API 吧。若是, 您想了解更多的 API 信息,请点击相应的 API 即可

* Null/Undefined
- [isUndefined(val)](./APIs/isUndefined.md)
Expand Down Expand Up @@ -69,3 +68,5 @@ API 文档

* Other
- [isEmail(val)](./APIs/isEmail.md)
- [isIpv4(val)](./APIs/isIpv4.md)

33 changes: 33 additions & 0 deletions docs/en-US/APIs/isIpv4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

## isIpv4(val)

#### 描述:

校验 `val` 是否为 ipv4

#### 参数:

val,任意类型

#### 说明:

若参数 `val` 是 ipv4, 则返回 true, 否则返回 false

#### 示例:

```javascript
jxmValidator.ipv4('192.168.1.1'); // => true
jxmValidator.ipv4('0.0.0.0'); // => true
jxmValidator.ipv4('0.0.0.1'); // => true
jxmValidator.ipv4('01.1.1.1'); // => true
jxmValidator.ipv4('255.255.255.255'); // => true

jxmValidator.ipv4('127.0.0.1'); // => true
jxmValidator.ipv4('127.0.0'); // => false
jxmValidator.ipv4('127.a.0.1'); // => false
jxmValidator.ipv4('127..0.1'); // => false

jxmValidator.ipv4('111.111.111.256'); // => false

jxmValidator.ipv4(192); // => false
```
7 changes: 4 additions & 3 deletions docs/zh-CN/APIs.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@
API 文档
===========================

假设,您已经阅读,并且知道如何使用,可以直接跳过该步骤。若是还没有看过的同学,请看这里:
假设,您已经阅读,并且知道如何了使用,可以直接跳过该步骤。若是还没有看过的同学,请看这里:

```javascript
// 引入 `jxm-validator`
const jxmValidator = require('jxm-validator');

jxmValidator.isNull(null);
```

接下来,一起来看API吧。若是想了解更信息的信息,请点击相应的API
接下来,一起来看 API 吧。若是, 您想了解更多的 API 信息,请点击相应的 API 即可

* Null/Undefined
- [isUndefined(val)](./APIs/isUndefined.md)
Expand Down Expand Up @@ -69,3 +68,5 @@ API 文档

* Other
- [isEmail(val)](./APIs/isEmail.md)
- [isIpv4(val)](./APIs/isIpv4.md)

33 changes: 33 additions & 0 deletions docs/zh-CN/APIs/isIpv4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

## isIpv4(val)

#### 描述:

校验 `val` 是否为 ipv4

#### 参数:

val,任意类型

#### 说明:

若参数 `val` 是 ipv4, 则返回 true, 否则返回 false

#### 示例:

```javascript
jxmValidator.ipv4('192.168.1.1'); // => true
jxmValidator.ipv4('0.0.0.0'); // => true
jxmValidator.ipv4('0.0.0.1'); // => true
jxmValidator.ipv4('01.1.1.1'); // => true
jxmValidator.ipv4('255.255.255.255'); // => true

jxmValidator.ipv4('127.0.0.1'); // => true
jxmValidator.ipv4('127.0.0'); // => false
jxmValidator.ipv4('127.a.0.1'); // => false
jxmValidator.ipv4('127..0.1'); // => false

jxmValidator.ipv4('111.111.111.256'); // => false

jxmValidator.ipv4(192); // => false
```
6 changes: 5 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ var _isPhone = require('./isPhone');

var _isEmail = require('./isEmail');

var _isIpv4 = require('./isIpv4');

module.exports = {
isUndefined: _isUndefined,
isNull: _isNull,
Expand Down Expand Up @@ -92,5 +94,7 @@ module.exports = {
isTelephone: _isTelephone,
isPhone: _isPhone,

isEmail: _isEmail
isEmail: _isEmail,

isIpv4: _isIpv4
};
5 changes: 4 additions & 1 deletion src/internal/enum/regexEnum.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ var REGEX_ENUM = {
},
MOBILE_REX: { // 手机号码|移动电话
'zh-CN': /^(\+?0?86-?)?1\d{10}$/
}
},

// ipv4
IPV4_REX: /\b((?!\d\d\d)\d+|1\d\d|2[0-4]\d|25[0-5])\.((?!\d\d\d)\d+|1\d\d|2[0-4]\d|25[0-5])\.((?!\d\d\d)\d+|1\d\d|2[0-4]\d|25[0-5])\.((?!\d\d\d)\d+|1\d\d|2[0-4]\d|25[0-5])\b/
};

module.exports = REGEX_ENUM;
23 changes: 23 additions & 0 deletions src/isIpv4.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@

var _isString = require('./isString');
var REGEX_ENUM = require('./internal/enum/regexEnum');

/**
* 校验参数 `val` 是否为 ipv4
*
* 若参数 `val` 是 ipv4, 则返回 true, 否则返回 false
*
* @param {*} val 待校验的参数
* @return {Boolean} 返回校验结果
* @version 0.0.6
* @since 0.0.6
*/
function _isIpv4(val) {
if (!_isString(val)) {
return false;
}

return REGEX_ENUM.IPV4_REX.test(val);
}

module.exports = _isIpv4;
2 changes: 1 addition & 1 deletion src/isNegativeNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var REGEX_ENUM = require('./internal/enum/regexEnum');
*
* 该接口存在两种模式, 即严格模式、非严格模式;
*
* 默认采用`非严格模式`
* 默认采用 `非严格模式`
*
* 若参数 `val` 为 number 类型, 并且是负数 则返回 true, 否则返回 false
* 若参数 `val` 为 string 类型, 并且通过验证为负数字符串, 则返回 true, 否则返回 false
Expand Down
2 changes: 1 addition & 1 deletion src/isPositiveNumber.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var REGEX_ENUM = require('./internal/enum/regexEnum');
*
* 该接口存在两种模式, 即严格模式、非严格模式
*
* 默认采用`非严格模式`
* 默认采用 `非严格模式`
*
* 若参数 `val` 为 number 类型, 并且是正数 则返回 true, 否则返回 false
* 若参数 `val` 为 string 类型, 并且通过验证为正数字符串, 则返回 true, 否则返回 false
Expand Down
65 changes: 65 additions & 0 deletions test/isIpv4.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@

const isIpv4 = require('../src/isIpv4');
const chai = require('chai');
const should = chai.should;

chai.use(require('chai-things'));
should();

// Test
describe('isIpv4 test', function() {
// '192.168.1.1'
it(`isIpv4('192.168.1.1') === true`, function() {
isIpv4('192.168.1.1').should.equal(true);
});

// '0.0.0.0'
it(`isIpv4('0.0.0.0') === true`, function() {
isIpv4('0.0.0.0').should.equal(true);
});

// '0.0.0.1'
it(`isIpv4('0.0.0.1') === true`, function() {
isIpv4('0.0.0.1').should.equal(true);
});

// '01.1.1.1'
it(`isIpv4('01.1.1.1') === true`, function() {
isIpv4('01.1.1.1').should.equal(true);
});

// '255.255.255.255'
it(`isIpv4('255.255.255.255') === true`, function() {
isIpv4('255.255.255.255').should.equal(true);
});

// '127.0.0.1'
it(`isIpv4('127.0.0.1') === true`, function() {
isIpv4('127.0.0.1').should.equal(true);
});

// '127.0.0'
it(`isIpv4('127.0.0') === false`, function() {
isIpv4('127.0.0').should.equal(false);
});

// '127.a.0.1'
it(`isIpv4('127.a.0.1') === false`, function() {
isIpv4('127.a.0.1').should.equal(false);
});

// '127..0.1'
it(`isIpv4('127..0.1') === false`, function() {
isIpv4('127..0.1').should.equal(false);
});

// '111.111.111.256'
it(`isIpv4('111.111.111.256') === false`, function() {
isIpv4('111.111.111.256').should.equal(false);
});

// 192
it(`isIpv4(192) === false`, function() {
isIpv4(192).should.equal(false);
});
});
Loading