Skip to content

Commit

Permalink
新增isUrl方法;部分error提示改为warn提示;
Browse files Browse the repository at this point in the history
  • Loading branch information
蔡金锋 committed Mar 30, 2020
1 parent 0bfad85 commit 466c92a
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 32 deletions.
3 changes: 1 addition & 2 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,4 @@ conf.json
test/
src/
docs/
example/
generate.js
example/
38 changes: 18 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "util-helpers",
"version": "3.3.1",
"version": "3.4.0",
"description": "一个基于业务场景的工具方法库",
"main": "lib/index.js",
"scripts": {
Expand Down Expand Up @@ -34,33 +34,31 @@
},
"homepage": "https://doly-dev.github.io/util-helpers/index.html",
"devDependencies": {
"@babel/cli": "^7.7.7",
"@babel/core": "^7.7.7",
"@babel/plugin-transform-modules-umd": "^7.7.4",
"@babel/preset-env": "^7.7.7",
"@babel/register": "^7.7.7",
"babel-loader": "^8.0.6",
"@babel/cli": "^7.8.4",
"@babel/core": "^7.9.0",
"@babel/plugin-transform-modules-umd": "^7.9.0",
"@babel/preset-env": "^7.9.0",
"@babel/register": "^7.9.0",
"babel-loader": "^8.1.0",
"chai": "^4.2.0",
"docdash": "^1.1.1",
"docdash": "^1.2.0",
"jsdoc": "^3.6.3",
"minimist": "^1.2.0",
"minimist": "^1.2.5",
"mocha": "^6.2.2",
"webpack": "^4.41.5",
"webpack-cli": "^3.3.10"
"webpack": "^4.42.0",
"webpack-cli": "^3.3.11"
},
"babel": {
"presets": [
[
"@babel/env",
{
"targets": {
"browsers": [
"> 1%",
"last 4 versions",
"Firefox ESR",
"not ie < 9"
]
}
"targets": [
"> 1%",
"last 4 versions",
"Firefox ESR",
"not ie < 9"
]
}
]
],
Expand All @@ -76,4 +74,4 @@
]
]
}
}
}
2 changes: 1 addition & 1 deletion src/formatMoney.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function checkNumber(num) {
(typeof num !== 'number' && typeof num !== 'string') ||
num === ''
) {
console.error(`${num} invalid parameter.`);
console.warn(`${num} invalid parameter.`);
return false;
}

Expand Down
14 changes: 7 additions & 7 deletions src/isIPv6.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ const reg = /^((([0-9A-F]{1,4}:){7}([0-9A-F]{1,4}|:))|(([0-9A-F]{1,4}:){6}(:[0-9
*
* // 0位压缩表示法
* isIPv6('FF01::1101');
*
* isIPv6('0:0:0:0:0:0:0:1');
* // => true
*
* // 0位压缩表示法
*
* isIPv6('::1');
*
* isIPv6('0:0:0:0:0:0:0:0');
* // => true
*
* // 0位压缩表示法
* isIPv6('::');
* // => true
*
* isIPv6('0:0:0:0:0:0:0:1');
* // => true
*
* isIPv6('0:0:0:0:0:0:0:0');
* // => true
*
* // 内嵌IPv4地址表示法
* isIPv6('::192.168.1.1');
* // => true
Expand Down
2 changes: 1 addition & 1 deletion src/isPassword.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
* // 仅支持 数字、字母、特殊字符,其他字符如中文字符是校验不通过的
* isPassword('_Aa一二三45678', {level: 3, ignoreCase: true});
* // => false
*
* isPassword(' _Aa12345678', {level: 3, ignoreCase: true});
* // => false
*
*/
function isPassword(value, {
level = 2,
Expand Down
36 changes: 36 additions & 0 deletions src/isUrl.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// url正则
const reg = /(((^https?:(?:\/\/)?)(?:[-;:&=\+\$,\w]+@)?[A-Za-z0-9.-]+(?::\d+)?|(?:www.|[-;:&=\+\$,\w]+@)[A-Za-z0-9.-]+)((?:\/[\+~%\/.\w-_]*)?\??(?:[-\+=&;%@.\w_]*)#?(?:[\w]*))?)$/;

/**
* 检测值是否为url
*
* @static
* @alias module:Validator.isUrl
* @since 3.4.0
* @param {String} value 要检测的值
* @returns {Boolean} 值是否为url
* @example
*
* isUrl('');
* // => false
*
* isUrl('8.8.8.8');
* // => false
*
* isUrl('http://example.com');
* // => true
*
* isUrl('https://example.com:8080');
* // => true
*
* isUrl('http://www.example.com/test/123');
* // => true
*
* isUrl('http://www.example.com/test/123?foo=bar');
* // => true
*/
function isUrl(value) {
return reg.test(value);
}

export default isUrl;
2 changes: 1 addition & 1 deletion src/numberToChinese.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ function numberToChinese(num, {
} = {}) {
// 非数字 或 NaN 不处理
if (typeof num !== 'number' || isNaN(num)) {
console.error(`参数错误 ${num},请传入数字`);
console.warn(`参数错误 ${num},请传入数字`);
return '';
}

Expand Down
50 changes: 50 additions & 0 deletions test/validator/isUrl.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
expect
} from 'chai';

import isUrl from '../../src/isUrl'

describe('isUrl', () => {
it('非字符串 => false', () => {
expect(isUrl([])).to.be.equal(false);
expect(isUrl({})).to.be.equal(false);
expect(isUrl(false)).to.be.equal(false);
expect(isUrl(true)).to.be.equal(false);
expect(isUrl(NaN)).to.be.equal(false);
expect(isUrl(null)).to.be.equal(false);
expect(isUrl(undefined)).to.be.equal(false);
expect(isUrl(123)).to.be.equal(false);
expect(isUrl()).to.be.equal(false);
expect(isUrl('')).to.be.equal(false);
});
it('无效url => false', () => {
expect(isUrl('foo')).to.be.equal(false);
expect(isUrl('bar')).to.be.equal(false);
expect(isUrl('/bar/test')).to.be.equal(false);
expect(isUrl('http:/example.com')).to.be.equal(false);
expect(isUrl('ttp://example.com')).to.be.equal(false);
expect(isUrl('8.8.8.8')).to.be.equal(false);
expect(isUrl('ftp://192.17.11.22:22/测试.tar')).to.be.equal(false);
expect(isUrl('http://www.百度.中国/')).to.be.equal(false);
expect(isUrl('http://www.baidu.com/?a=1&b=网络')).to.be.equal(false);
});

it('正常url => true', () => {
expect(isUrl('http://example.com')).to.be.equal(true);
expect(isUrl('http://example.com/')).to.be.equal(true);
expect(isUrl('https://example.com/')).to.be.equal(true);
expect(isUrl('http://example.com/test/123')).to.be.equal(true);
expect(isUrl('https://example.com/test/123')).to.be.equal(true);
expect(isUrl('http://example.com/test/123?foo=bar')).to.be.equal(true);
expect(isUrl('https://example.com/test/123?foo=bar')).to.be.equal(true);
expect(isUrl('http://www.example.com')).to.be.equal(true);
expect(isUrl('http://www.example.com/')).to.be.equal(true);
expect(isUrl('https://www.example.com/')).to.be.equal(true);
expect(isUrl('http://www.example.com/test/123')).to.be.equal(true);
expect(isUrl('https://www.example.com/test/123')).to.be.equal(true);
expect(isUrl('http://www.example.com/test/123?foo=bar')).to.be.equal(true);
expect(isUrl('https://www.example.com/test/123?foo=bar')).to.be.equal(true);
expect(isUrl('https://example.com:8080')).to.be.equal(true);
expect(isUrl('https://example.com:8080/')).to.be.equal(true);
});
});

0 comments on commit 466c92a

Please sign in to comment.