Skip to content
Snippets Groups Projects
Select Git revision
  • 9264c12d85a48e395363e80ef0c4632665d25414
  • master default protected
  • rednatco-v2
  • rednatco
  • test
  • ntc-tube-uniform-color
  • ntc-tube-missing-atoms
  • restore-vertex-array-per-program
  • watlas2
  • dnatco_new
  • cleanup-old-nodejs
  • webmmb
  • fix_auth_seq_id
  • update_deps
  • ext_dev
  • ntc_balls
  • nci-2
  • plugin
  • bugfix-0.4.5
  • nci
  • servers
  • v0.5.0-dev.1
  • v0.4.5
  • v0.4.4
  • v0.4.3
  • v0.4.2
  • v0.4.1
  • v0.4.0
  • v0.3.12
  • v0.3.11
  • v0.3.10
  • v0.3.9
  • v0.3.8
  • v0.3.7
  • v0.3.6
  • v0.3.5
  • v0.3.4
  • v0.3.3
  • v0.3.2
  • v0.3.1
  • v0.3.0
41 results

index.ts

Blame
  • uuid.ts 1.04 KiB
    /**
     * Copyright (c) 2017 mol* contributors, licensed under MIT, See LICENSE file for more info.
     *
     * @author David Sehnal <david.sehnal@gmail.com>
     */
    
    import { now } from 'mol-util/now';
    
    type UUID = string & { '@type': 'uuid' }
    
    namespace UUID {
        const chars: string[] = [];
        /** Creates 22 characted "base64" UUID */
        export function create22(): UUID {
            let d = (+new Date()) + now();
            for (let i = 0; i < 16; i++) {
                chars[i] = String.fromCharCode((d + Math.random()*0xff)%0xff | 0);
                d = Math.floor(d/0xff);
            }
            return btoa(chars.join('')).replace(/\+/g, '-').replace(/\//g, '_').substr(0, 22) as UUID;
        }
    
        export function createv4(): UUID {
            let d = (+new Date()) + now();
            const uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
                const r = (d + Math.random()*16)%16 | 0;
                d = Math.floor(d/16);
                return (c==='x' ? r : (r&0x3|0x8)).toString(16);
            });
            return uuid as any;
        }
    }
    
    export default UUID;