-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathisUUID.js
36 lines (32 loc) · 1.13 KB
/
isUUID.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import isString from './isString'
/**
* 检测测试数据是否为合法的 UUID 字符串
* ========================================================================
* @method isUUID
* @since 2.1.0
* @cagetory String
* @see https://stackoverflow.com/questions/7905929/how-to-test-valid-uuid-guid
* @param {String} str - 要检测的数据
* @returns {Boolean} 'str' 为合法的 UUID 字符串,返回 true,否则返回 false
* @example
*
* isUUID('749d0000-0194-1005-2e05-08d61613bf2f') // -> true
* isUUID('d3aa88e2-c754-41e0-8ba6-4198a34aa0a2') // -> true
* isUUID('00000000-0000-0000-0000-000000000000') // -> true
* isUUID('{0e40c5ab-1d9b-ee11-983e-e0be0335d021}') // -> true
* isUUID('08dbe0f11c8641cf8afe6b2824e8f8f5') // -> true
*
* isUUID('') // -> false
* isUUID('xxxA987FBC9-4BED-3078-CF07-9141BA07C9F3') // -> false
* isUUID('934859') // -> false
* isUUID('A987FBC94BED3078CF079141BA07C9F') // -> false
*/
const isUUID = (str) => {
const pattern =
/^{?([0-9a-fA-F]{8})-?(([0-9a-fA-F]{4}-?){3})([0-9a-fA-F]{12})}?$/i
if (!isString(str)) {
return false
}
return pattern.test(str)
}
export default isUUID