已构建jQuery扩展,但不允许最大返回值

Built jQuery extension, but not allowing max return values

本文关键字:不允许 返回值 构建 jQuery 扩展      更新时间:2024-04-26

我在这里整理了一个问题的jsfiddle:https://jsfiddle.net/sd1x3am3/5/

基本上,我构建了一个名为$.generateSerial的扩展,它包含3个参数。第一个参数是公式,第二个是允许的字符,第三个是不包含在返回值中的序列。基本上,该扩展允许您生成一个唯一的随机序列,该序列不是第三个参数中的任何序列,也不是每次在for循环中调用$.generateSerial时在output变量中返回的任何序列。

由于某种原因,当i = 146时,$.generateSerial在循环中返回"",但应该有更多的值供其返回,因此不应该返回""

公式为XXXX-XXXX-XXXX-XXXX,允许的字符为:ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789,这意味着这些字符中的任何一个都可以替换formula中的任何X字符。但返回的结果应该超过145/146个,不包括currSerials数组中的结果。应该有更多的序列可以生成。

但我不明白为什么它没有生成所有可能的连续剧并停止。。。

$.extend({
    generateSerial: function(formula, chrs, checks) {
        var formula = formula && formula != "" ? formula : 'XXXXX-XXXXX-XXXXX-XXXXX-XXXXX', // Default Formula to use, should change to what's most commonly used!
            chrs = chrs && chrs != "" ? chrs : "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", // Default characters to randomize, if not defined!
            len = (formula.match(/X/ig) || []).length,
            oStatic = formula.match(/[^X]+/ig) || [],
            indices = [],
            lenChecks = checks.length || 0,
            rand;
        // Let's first find out if it's possible to create another random serial from params
        // Should put the formula below through a stress test here to be sure it works!
        var possible = (len * chrs.length) - lenChecks;
        // return empty string if not possible!
        if (possible <= 0)
            return '';
        var currIndex = 0;
        // Getting all indices here of all strings that are not X or x
        for (var o = 0; o < oStatic.length; o++) {
            currIndex = formula.indexOf(oStatic[o], currIndex);
            indices.push(currIndex);
            currIndex = currIndex + oStatic[o].length; // set the position to start at for next loop! incrementing...
        }
        do {
            rand = Array(len).join().split(',').map(function() {
                return chrs.charAt(Math.floor(Math.random() * chrs.length));
            }).join('');
            // Rebuild with indices, if exists!
            if (indices && indices.length > 0) {
                for (var x = 0; x < indices.length; x++)
                    rand = rand.insert(indices[x], oStatic[x]);
            }
        } while (checks && $.inArray(rand, checks) !== -1);
        return rand;
    }
});

有人能帮我吗?扩展名中的possible变量可能有问题吗?这不正确吗?

甚至在没有currSerials数组的情况下尝试过,最初它是空的,在它说不能再生成之前只生成576个序列。你可以在这里看到:https://jsfiddle.net/sd1x3am3/8/

var currSerials = [];
for (var i = 0; i < 2000; i++) {
    var output = $.generateSerial('XXXX-XXXX-XXXX-XXXX', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789', currSerials);
    $(".serials").append($("<p />").text((i+1) + '. ' + output));
    if (output != "")
        currSerials.push(output);
    else
    {
        alert('Can not generate anymore serials, stopped at ' + i + '.');
        break;
    }
}

从讨论中,我发现你不喜欢排列,所以你应该使用下面的方法来找到所有可能的项目,

var possible=Math.pow(chrs.length,len) - lenChecks