All files uuid.ts

100% Statements 8/8
100% Branches 4/4
100% Functions 2/2
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20                  2x 2x 2x 2x 62x 62x 62x   2x    
/**
 * 这是来自某网友的代码, 由于时间太久了,我也忘了是在哪找的了 所以没办法标注出处
 */
/**
 * 生成并返回一个uuid
 * @param {boolean} bar 是否带“ - ” 默认false
 * @return {string} uuid
 */
export default function (bar?: boolean): string {
    let d: number = new Date().getTime()
    let tpl: string = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
    !bar && (tpl = tpl.replace(/-/g, ''))
    const uuid: string = tpl.replace(/[xy]/g, function (c) {
        var r: number = (d + Math.random() * 16) % 16 | 0
        d = Math.floor(d / 16)
        return (c == 'x' ? r : (r & 0x3) | 0x8).toString(16)
    })
    return uuid
}