diff --git a/.npmignore b/.npmignore index 9133e45f..c1ac288d 100644 --- a/.npmignore +++ b/.npmignore @@ -14,5 +14,4 @@ conf.json test/ src/ docs/ -example/ -generate.js \ No newline at end of file +example/ \ No newline at end of file diff --git a/package.json b/package.json index b0a4c637..8686c2ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "util-helpers", - "version": "3.3.1", + "version": "3.4.0", "description": "一个基于业务场景的工具方法库", "main": "lib/index.js", "scripts": { @@ -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" + ] } ] ], @@ -76,4 +74,4 @@ ] ] } -} +} \ No newline at end of file diff --git a/src/formatMoney.js b/src/formatMoney.js index 5184d92a..f419acde 100644 --- a/src/formatMoney.js +++ b/src/formatMoney.js @@ -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; } diff --git a/src/isIPv6.js b/src/isIPv6.js index d6eb0c0e..2fae1077 100644 --- a/src/isIPv6.js +++ b/src/isIPv6.js @@ -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 diff --git a/src/isPassword.js b/src/isPassword.js index e907c3a6..feeda42c 100644 --- a/src/isPassword.js +++ b/src/isPassword.js @@ -32,9 +32,9 @@ * // 仅支持 数字、字母、特殊字符,其他字符如中文字符是校验不通过的 * isPassword('_Aa一二三45678', {level: 3, ignoreCase: true}); * // => false + * * isPassword(' _Aa12345678', {level: 3, ignoreCase: true}); * // => false - * */ function isPassword(value, { level = 2, diff --git a/src/isUrl.js b/src/isUrl.js new file mode 100644 index 00000000..52f59f1e --- /dev/null +++ b/src/isUrl.js @@ -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; \ No newline at end of file diff --git a/src/numberToChinese.js b/src/numberToChinese.js index 846e0f78..3df7acda 100644 --- a/src/numberToChinese.js +++ b/src/numberToChinese.js @@ -176,7 +176,7 @@ function numberToChinese(num, { } = {}) { // 非数字 或 NaN 不处理 if (typeof num !== 'number' || isNaN(num)) { - console.error(`参数错误 ${num},请传入数字`); + console.warn(`参数错误 ${num},请传入数字`); return ''; } diff --git a/test/validator/isUrl.test.js b/test/validator/isUrl.test.js new file mode 100644 index 00000000..1b256813 --- /dev/null +++ b/test/validator/isUrl.test.js @@ -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); + }); +}); \ No newline at end of file