File tree 2 files changed +42
-3
lines changed
2 files changed +42
-3
lines changed Original file line number Diff line number Diff line change 1
1
export * from './is' ;
2
2
export * from './rect' ;
3
3
4
+ /** 空方法 */
5
+ export const noop = ( ) => { } ;
4
6
export const now = ( ) => Date . now ( ) ;
5
7
export const timestamp = ( ) => + Date . now ( ) ;
Original file line number Diff line number Diff line change @@ -16,9 +16,46 @@ export const isString = (val: unknown): val is string => typeof val === 'string'
16
16
export const isWindow = ( val : any ) : val is Window =>
17
17
typeof window !== 'undefined' && toString . call ( val ) === '[object Window]' ;
18
18
19
- /** 空方法 */
20
- export const noop = ( ) => { } ;
21
-
22
19
/** 判断是否是IOS系统 */
23
20
export const isIOS =
24
21
isClient && window ?. navigator ?. userAgent && / i P ( a d | h o n e | o d ) / . test ( window . navigator . userAgent ) ;
22
+
23
+ const protocolAndDomainRE = / ^ (?: \w + : ) ? \/ \/ ( \S + ) $ / ;
24
+
25
+ const localhostDomainRE = / ^ l o c a l h o s t [ \: ? \d ] * (?: [ ^ \: ? \d ] \S * ) ? $ / ;
26
+ const nonLocalhostDomainRE = / ^ [ ^ \s \. ] + \. \S { 2 , } $ / ;
27
+
28
+ /**
29
+ * 检查字符串是否是 url
30
+ *
31
+ * @param value 要检查的值
32
+ * @returns `value` 是 Url 返回 `true`,否则返回 `false`
33
+ * @example
34
+ * ```ts
35
+ * isUrl('https://www.baidu.com') // => true
36
+ * ```
37
+ */
38
+ export default function isUrl ( value : string ) {
39
+ if ( ! isString ( value ) ) {
40
+ return false ;
41
+ }
42
+
43
+ const match = value . match ( protocolAndDomainRE ) ;
44
+ if ( ! match ) {
45
+ return false ;
46
+ }
47
+
48
+ const everythingAfterProtocol = match [ 1 ] ;
49
+ if ( ! everythingAfterProtocol ) {
50
+ return false ;
51
+ }
52
+
53
+ if (
54
+ localhostDomainRE . test ( everythingAfterProtocol ) ||
55
+ nonLocalhostDomainRE . test ( everythingAfterProtocol )
56
+ ) {
57
+ return true ;
58
+ }
59
+
60
+ return false ;
61
+ }
You can’t perform that action at this time.
0 commit comments