guid.util.ts

export class GuidUtil {
    public str: string;

    constructor(str?: string) {
        this.str = str || GuidUtil.getNewGUIDString();
    }


      public static getNewGUIDString() {
        // 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
        }
        return 'xxxxxxxx-xxxx-4xxx-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);
        });
    }

    toString() {
      return this.str;
   }
}
最后修改:2023 年 04 月 27 日
如果觉得我的文章对你有用,请随意赞赏