-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
蔡金锋
committed
Mar 30, 2020
1 parent
0bfad85
commit 466c92a
Showing
8 changed files
with
115 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,4 @@ conf.json | |
test/ | ||
src/ | ||
docs/ | ||
example/ | ||
generate.js | ||
example/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); |