all files / common/core/ UUID.ts

100% Statements 13/13
66.67% Branches 4/6
100% Functions 4/4
100% Lines 12/12
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                              186× 186×   186×              
/*
 *  @license
 *  Copyright Hôpitaux Universitaires de Genève. All Rights Reserved.
 *
 *  Use of this source code is governed by an Apache-2.0 license that can be
 *  found in the LICENSE file at https://github.com/DSI-HUG/dejajs-components/blob/master/LICENSE
 */
 
/** Classe de génération d'un guid. */
export class UUID {
    private uuid: string;
 
    constructor() {
        let d = new Date().getTime();
        Eif (window.performance !== undefined) {
            Eif (typeof window.performance.now === 'function') {
                d += performance.now();
            }
        }
        this.uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, (c) => {
            // tslint:disable-next-line:no-bitwise
            const r = (d + Math.random() * 16) % 16 | 0;
            d = Math.floor(d / 16);
            // tslint:disable-next-line:no-bitwise
            return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16);
        });
    }
 
    /** Le renoive le guid sous format string. */
    public toString() {
        return this.uuid;
    }
}