Skip to content

Commit 7a8e7ac

Browse files
committed
test: add unit tests
1 parent cce7e88 commit 7a8e7ac

File tree

13 files changed

+415
-4
lines changed

13 files changed

+415
-4
lines changed

.autod.conf.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
module.exports = {
4+
write: true,
5+
prefix: '^',
6+
test: [
7+
'test',
8+
],
9+
devdep: [
10+
'egg-ci',
11+
'egg-bin',
12+
'autod',
13+
'eslint',
14+
'eslint-config-egg'
15+
],
16+
exclude: [
17+
'./test/fixtures',
18+
'./docs',
19+
'./coverage',
20+
],
21+
};

.eslintignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
coverage

.eslintrc

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "eslint-config-egg"
3+
}

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!--
2+
Thank you for your pull request. Please review below requirements.
3+
Bug fixes and new features should include tests and possibly benchmarks.
4+
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
5+
6+
感谢您贡献代码。请确认下列 checklist 的完成情况。
7+
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
8+
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
9+
-->
10+
11+
##### Checklist
12+
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->
13+
14+
- [ ] `npm test` passes
15+
- [ ] tests and/or benchmarks are included
16+
- [ ] documentation is changed or added
17+
- [ ] commit message follows commit guidelines
18+
19+
##### Affected core subsystem(s)
20+
<!-- Provide affected core subsystem(s). -->
21+
22+
23+
##### Description of change
24+
<!-- Provide a description of the change below this comment. -->

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
logs/
2+
npm-debug.log
3+
node_modules/
4+
coverage/
5+
.idea/
6+
run/
7+
.DS_Store
8+
*.swp
9+

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- '8'
5+
- '10'
6+
install:
7+
- npm i npminstall && npminstall
8+
script:
9+
- npm run ci
10+
after_script:
11+
- npminstall codecov && codecov

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018-present node-modules and other contributors.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# egg-china-mobile-phone-regexp
1+
# china-mobile-phone-regexp
22

33
[![NPM version][npm-image]][npm-url]
44
[![build status][travis-image]][travis-url]
@@ -31,12 +31,19 @@ $ npm i china-mobile-phone-regexp --save
3131
## Usage
3232

3333
```js
34+
const phone = require('china-mobile-phone-regexp');
35+
// Is phone number?
36+
console.log(phone.is('13800138000'));
3437

35-
```
38+
// Is CHINA_MOBILE phone number?
39+
console.log(phone.is('13800138000', phone.CHINA_MOBILE));
3640

37-
## Example
41+
// Is CHINA_UNICOM phone number?
42+
console.log(phone.is('13800138000', phone.CHINA_UNICOM));
3843

39-
<!-- example here -->
44+
// Is CHINA_TELECOM phone number?
45+
console.log(phone.is('13800138000', phone.CHINA_TELECOM));
46+
```
4047

4148
## Questions & Suggestions
4249

appveyor.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
environment:
2+
matrix:
3+
- nodejs_version: '8'
4+
- nodejs_version: '10'
5+
6+
install:
7+
- ps: Install-Product node $env:nodejs_version
8+
- npm i npminstall && node_modules\.bin\npminstall
9+
10+
test_script:
11+
- node --version
12+
- npm --version
13+
- npm run test
14+
15+
build: off

index.js

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
'use strict';
2+
3+
// Match all
4+
// 全部
5+
const ALL = /^1(?:3\d|4[04-9]|41(?=[1-9])|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/;
6+
7+
// MVNO: 167, 170/1
8+
// 虚拟运营商
9+
const MVNO_PREFIXS = [
10+
'167',
11+
'170/1',
12+
];
13+
const MVNO = /^1(?:67|7[01])\d{8}$/;
14+
const EXCLUDE_MVNO = /^1(?:3\d|4[4-9]|5[0-35-9]|66|7[3-8]|8\d|9\d)\d{8}$/;
15+
16+
// SATELLITE: 1349, 17400/1/2/3/4/5, 1749
17+
// 卫星电话
18+
const SATELLITE_PREFIXS = [
19+
'1349',
20+
'17400/1/2/3/4/5',
21+
'1749',
22+
];
23+
const SATELLITE = /^1(?:349|749|740(?=[0-5]))\d{7}$/;
24+
const EXCLUDE_SATELLITE = /^1(?:3(?!49)\d|4[4-9]|5[0-35-9]|6[67]|7[0135-8]|8\d|9\d)\d{8}$/;
25+
26+
// DATA: 144/5/6/7/8/9
27+
// 数据卡
28+
const DATA_PREFIXS = [
29+
'144/5/6/7/8/9',
30+
];
31+
const DATA = /^1(?:4[4-9])\d{8}$/;
32+
const EXCLUDE_DATA = /^1(?:3\d|5[0-35-9]|6[67]|7[013-8]|8\d|9\d)\d{8}$/;
33+
34+
const CHINA_MOBILE_PREFIXS = [
35+
'1340/1/2/3/4/5/6/7/8',
36+
'135/6/7/8/9',
37+
// 14410~14499 + 1440 => 144
38+
'144/7/8',
39+
'150/1/2/7/8/9',
40+
'1703/5/6',
41+
'178',
42+
'182/3/4/7/8',
43+
'198',
44+
];
45+
const CHINA_MOBILE = /^1(?:34(?=[0-8])|3[5-9]|4[478]|5[012789]|70(?=[356])|78|8[23478]|98)\d{8}$/;
46+
47+
const CHINA_UNICOM_PREFIXS = [
48+
'130/1/2',
49+
// 14000~14099 => 140
50+
'140',
51+
'145/6',
52+
'155/6',
53+
'167',
54+
'1704/7/8/9',
55+
'171/5/6',
56+
'185/6',
57+
];
58+
const CHINA_UNICOM = /^1(?:3[012]|4[056]|5[56]|67|70(?=[4789])|7[156]|8[56])\d{8}$/;
59+
60+
const CHINA_TELECOM_PREFIXS = [
61+
'133',
62+
'1349',
63+
// 14110~14199 => 1411/2/3/4/5/6/7/8/9
64+
'1411/2/3/4/5/6/7/8/9',
65+
'149',
66+
'153',
67+
'1700/1/2',
68+
'173/7',
69+
'17400/1/2/3/4/5',
70+
'180/1/9',
71+
'191/9',
72+
];
73+
const CHINA_TELECOM = /^1(?:33|34(?=9)|41(?=[1-9])|49|53|70(?=[012])|7[37]|74(?=0[0-5])|8[019]|9[19])\d{8}$/;
74+
75+
// +86
76+
// 086
77+
// 0086
78+
// +0086
79+
// +086
80+
const COUNTRY_CODE = /^(\+?0{0,2}86)/;
81+
82+
module.exports = {
83+
ALL,
84+
CHINA_MOBILE,
85+
CHINA_UNICOM,
86+
CHINA_TELECOM,
87+
MVNO,
88+
EXCLUDE_MVNO,
89+
SATELLITE,
90+
EXCLUDE_SATELLITE,
91+
DATA,
92+
EXCLUDE_DATA,
93+
COUNTRY_CODE,
94+
95+
MVNO_PREFIXS,
96+
SATELLITE_PREFIXS,
97+
DATA_PREFIXS,
98+
CHINA_MOBILE_PREFIXS,
99+
CHINA_UNICOM_PREFIXS,
100+
CHINA_TELECOM_PREFIXS,
101+
102+
is(number, regexp) {
103+
regexp = regexp || ALL;
104+
if (number.length > 11) {
105+
const m = COUNTRY_CODE.exec(number);
106+
if (m) {
107+
number = number.substring(m[1].length);
108+
}
109+
}
110+
111+
return number.length === 11 && regexp.test(number);
112+
},
113+
};

0 commit comments

Comments
 (0)