Skip to content

Commit dcf3b7a

Browse files
committed
feat: 添加新功能
- 新增 isInRange() - 新增 isEmptyString()
1 parent 8e111e2 commit dcf3b7a

File tree

4 files changed

+72
-17
lines changed

4 files changed

+72
-17
lines changed

CHANGELOG.md

+13-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
# 变更日志
22

3+
## 0.7.0 / 2018-9-29
4+
5+
- 新增 isInRange()
6+
- 新增 isEmptyString()
7+
38
## 0.6.0 / 2018-9-28
49

510
- isInt -> isInteger
@@ -24,11 +29,11 @@
2429

2530
## 0.1.0 / 2018-3-17
2631

27-
- 实现 isNumber
28-
- 实现 isBoolean
29-
- 实现 isString
30-
- 实现 isNull
31-
- 实现 isUndefined
32-
- 实现 isObject
33-
- 实现 isFunction
34-
- 实现 isArray
32+
- 新增 isNumber()
33+
- 新增 isBoolean()
34+
- 新增 isString()
35+
- 新增 isNull()
36+
- 新增 isUndefined()
37+
- 新增 isObject()
38+
- 新增 isFunction()
39+
- 新增 isArray()

doc/api.md

+21
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,22 @@
11
# 文档
22
一组类型判断函数,解决80%类型判断问题
33

4+
## isInRange
5+
判断指定的数字,是否在指定的范围内
6+
7+
- @param {number} x 要判断的参数,如果不为数字,将返回false
8+
- @param {number} [min] 最小值,包含min,缺省则不会校验
9+
- @param {number} [max] 最大值,包含max,缺省则不会校验
10+
- @return {boolean} 是否包含
11+
12+
举个例子:
13+
14+
```js
15+
isInRange(1, 0, 2) // true
16+
isInRange(1, 1, 2) // true
17+
isInRange(1, 2, 3) // false
18+
```
19+
420
## isNumber
521
判断是否为数字,参数和返回值如下:
622

@@ -29,6 +45,11 @@
2945
## isString
3046
判断是否为字符串类型,参数和返回值同上
3147

48+
## isEmptyString
49+
判断指定参数是否是空字符串,会去掉首尾的空白字符(空格、tab、回车、换行等)
50+
51+
参数和返回值同上
52+
3253
## isNull
3354
判断是否为null类型,参数和返回值同上
3455

src/index.js

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,23 @@
11
import { type } from '@jsmini/type';
22

3-
export function isNumber(x, min, max) {
3+
export function isInRange(x, min, max) {
4+
x = +x;
45
min = +min;
56
max = +max;
67

7-
return type(x) === 'number'
8-
&& (!isNaN(min) ? x >= min : true)
9-
&& (!isNaN(max) ? x <= max : true);
8+
// x 不是数字,则返回false
9+
if (isNaN(x)) return false;
10+
11+
// min 或 max 不传,则认为不设置下限或上限
12+
return (!isNaN(min) ? x >= min : true) && (!isNaN(max) ? x <= max : true);
13+
}
14+
15+
export function isNumber(x, min, max) {
16+
return type(x) === 'number' && isInRange(x, min, max);
1017
}
1118

1219
export function isInteger(x, min, max) {
13-
min = +min;
14-
max = +max;
15-
return parseInt(x, 10) === x
16-
&& (!isNaN(min) ? x >= min : true)
17-
&& (!isNaN(max) ? x <= max : true);
20+
return parseInt(x, 10) === x && isInRange(x, min, max);
1821
}
1922

2023
export function isInt(x) {
@@ -30,6 +33,12 @@ export function isString(x) {
3033
return type(x) === 'string';
3134
}
3235

36+
export function isEmptyString(x) {
37+
if (!isString(x)) return false;
38+
39+
return x.replace(/(^\s*|\s*$)/g, '') === '';
40+
}
41+
3342
export function isNull(x) {
3443
return type(x) === 'null';
3544
}

test/test.js

+20
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ describe('单元测试', function() {
1616
expect(is.isFunction(function () {})).to.equal(true);
1717
});
1818

19+
it('isInRange', function() {
20+
expect(is.isInRange(1)).to.equal(true);
21+
expect(is.isInRange('abc')).to.equal(false);
22+
23+
expect(is.isInRange(1, 1)).to.equal(true);
24+
expect(is.isInRange(1, 2)).to.equal(false);
25+
26+
expect(is.isInRange(1, undefined, 1)).to.equal(true);
27+
expect(is.isInRange(1, 0, 2)).to.equal(true);
28+
expect(is.isInRange(1, 0, 0)).to.equal(false);
29+
});
30+
1931
it('isNumber', function() {
2032
expect(is.isNumber(1)).to.equal(true);
2133
expect(is.isNumber(1.1)).to.equal(true);
@@ -41,5 +53,13 @@ describe('单元测试', function() {
4153
expect(is.isInt(2147483647)).to.equal(true);
4254
expect(is.isInt(2147483648)).to.equal(false);
4355
});
56+
57+
it('isEmptyString', function() {
58+
expect(is.isEmptyString(123)).to.equal(false);
59+
expect(is.isEmptyString('')).to.equal(true);
60+
expect(is.isEmptyString(' ')).to.equal(true);
61+
expect(is.isEmptyString(' ')).to.equal(true);
62+
expect(is.isEmptyString(' a ')).to.equal(false);
63+
});
4464
});
4565
});

0 commit comments

Comments
 (0)