GUID / UUID type in typescript

前端 未结 2 1907
花落未央
花落未央 2020-12-17 15:08

I have this function:

function getProduct(id: string){    
    //return some product 
}

where id is actually GUID. Typescript doesn\'t have

2条回答
  •  暗喜
    暗喜 (楼主)
    2020-12-17 15:45

    I think one should extend a bit on the answer by David Sherret.
    Like this:

    // export 
    class InvalidUuidError extends Error {
        constructor(m?: string) {
            super(m || "Error: invalid UUID !");
    
            // Set the prototype explicitly.
            Object.setPrototypeOf(this, InvalidUuidError.prototype);
        }
    
    }
    
    
    // export 
    class UUID 
    {
        protected m_str: string;
    
        constructor(str?: string) {
            this.m_str = str || UUID.newUuid().toString();
    
            let reg:RegExp = new RegExp("[A-F0-9]{8}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{4}-[A-F0-9]{12}", "i")
            if(!reg.test(this.m_str))
                throw new InvalidUuidError();
        }
    
        toString() {
            return this.m_str;
        }
    
        public static newUuid(version?:number) :UUID
        {
            version = version || 4;
    
    
            // your favourite guid generation function could go here
            // ex: http://stackoverflow.com/a/8809472/188246
            let d = new Date().getTime();
            if (window.performance && typeof window.performance.now === "function") {
                d += performance.now(); //use high-precision timer if available
            }
            let uuid:string = ('xxxxxxxx-xxxx-' + version.toString().substr(0,1) + 'xxx-yxxx-xxxxxxxxxxxx').replace(/[xy]/g, (c) => {
                let r = (d + Math.random() * 16) % 16 | 0;
                d = Math.floor(d/16);
                return (c=='x' ? r : (r & 0x3 | 0x8)).toString(16);
            });
    
            return new UUID(uuid);
        }
    }
    
    
    function getProduct(id: UUID) {    
        alert(id); // alerts "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx"
    }
    
    
    const guid2 = new UUID();
    console.log(guid2.toString()); // some guid string
    
    
    const guid = new UUID("xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx");
    getProduct(guid); // ok
    getProduct("notGuidbutJustString"); // errors, good
    

提交回复
热议问题