前端生成 guid 的方法

参考:
javascript - How to create a GUID / UUID - Stack Overflow
https://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid

/**
     * 生成一个 uuid/guid
     */
    $.uuid = $.uuidv4 = function () {
        return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
    }

    /**
     * 生成一个 uuid/guid,es6的版本
     */
    $.uuides6 = $.uuidv4es6 = function () {
        return ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
            (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
        );
    }

构建 guid 对象

newGuid 1 2 3 分别有现有的常见的三个生成方法

var GuidBuilder = function () {
    /**
     * 表示全局唯一标识符 (GUID)对象
     * @param {*} guidString guid 字符串
     */
    var Guid = function (guidString) {
        //存放guid 32位数值的数组
        this.list = [];
        this.isGuid = true;
        //如果构造函数的参数为字符串
        if (typeof (guidString) == "string") {
            this.initByString(guidString);
        } else {
            this.initByOther();
        }
    };
    /**
     * 返回一个值,该值指示 Guid 的两个实例是否表示同一个值。
     * @param {Guid} otherGuid 
     */
    Guid.prototype.equals = function (otherGuid) {
        if (otherGuid === null ||
            otherGuid === undefined ||
            !otherGuid.isGuid) {
            return false;
        }
        return this.toString() === otherGuid.toString();
    };

    /**
     * 由字符串加载
     * @param {*} guidString 
     */
    Guid.prototype.initByString = function (guidString) {
        guidString = guidString.replace(/\{|\(|\)|\}|-/g, "");
        guidString = guidString.toLowerCase();

        //如果不是guid字符串,生成全零 guid
        if (guidString.length != 32 ||
            guidString.search(/[^0-9,a-f]/i) != -1
        ) {
            this.initByOther(this.list);
        } else {
            this.list = guidString.split('');
        }
    };
    /**
     * 由其他类型加载
     */
    Guid.prototype.initByOther = function () {
        var i = 32;
        while (i-- > 0) {
            this.list.push("0");
        }
    };

    /**
     * 根据所提供的格式说明符,返回此 Guid 实例值的 String 表示形式。
     * @param {String} format 
     * 
     * N  32 位: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
     * 
     * D  由连字符分隔的 32 位数字 xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
     * 
     * B  括在大括号中、由连字符分隔的 32 位数字:{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}
     * 
     * P  括在圆括号中、由连字符分隔的 32 位数字:(xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)
     * 
     * @returns 
     */
    Guid.prototype.toString = function (format) {
        if (format === null || format === undefined) {
            format = "D";
        }
        format = format.toUpperCase();
        var formatArr = ["D", "N", "B", "P"];
        var isMatch = false;
        for (var i = 0; i < formatArr.length; i++) {
            var format1 = formatArr[i];
            if (format1 === format) {
                isMatch = true;
                break;
            }
        }

        if (!isMatch) {
            format = "D";
        }

        var listText = this.list.join("");

        switch (format) {
            case "N":
                return listText.replace(/,/g, "");
            case "D":
                var str = listText.slice(0, 8) + "-" +
                    listText.slice(8, 12) + "-" +
                    listText.slice(12, 16) + "-" +
                    listText.slice(16, 20) + "-" +
                    listText.slice(20, 32);
                str = str.replace(/,/g, "");
                return str;
            case "B":
                var str = "{" + listText + "}";
                return str;
            case "P":
                var str = "(" + listText + ")";
                return str;
            default:
                return listText;
        }
    };

    //初始化 Guid 类的一个新实例。
    Guid.newGuid = function () {
        return newGuid1();
    }

    /**
     * 算法1
     * @returns 
     */
    Guid.newGuid1 = function () {
        var guidString = "";
        var i = 32;
        while (i-- > 0) {
            guidString += Math.floor(Math.random() * 16.0).toString(16);
        }
        return new Guid(guidString);
    }

    /**
     * 算法2
     * @returns 
     */
    Guid.newGuid2 = function () {
        var guidString = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
            var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
            return v.toString(16);
        });
        return new Guid(guidString);
    }

    /**
     * 算法3
     * @returns 
     */
    Guid.newGuid3 = function () {
        var guidString = ([1e7] + -1e3 + -4e3 + -8e3 + -1e11).replace(/[018]/g, c =>
            (c ^ crypto.getRandomValues(new Uint8Array(1))[0] & 15 >> c / 4).toString(16)
        );
        return new Guid(guidString);
    }

    //Guid 类的默认实例,其值保证均为零。
    Guid.empty = new Guid();

    return Guid;
};

var Guid = GuidBuilder();

Guid.newGuid().toString();
posted @ 2021-11-03 15:22  DHclly  阅读(944)  评论(0编辑  收藏  举报