()
| 2 | // Licensed under the MIT License. |
| 3 | |
| 4 | export function generateUUID(): string { |
| 5 | // 检查是否支持 crypto.randomUUID() |
| 6 | if (typeof crypto !== 'undefined' && crypto.randomUUID) { |
| 7 | return crypto.randomUUID(); |
| 8 | } |
| 9 | |
| 10 | // 降级方案:手动实现 UUID v4 |
| 11 | return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
| 12 | const r = Math.random() * 16 | 0; |
| 13 | const v = c === 'x' ? r : (r & 0x3 | 0x8); |
| 14 | return v.toString(16); |
| 15 | }); |
| 16 | } |
no outgoing calls
no test coverage detected