AyrA icon

bignum fuckup fix in mongoose-shortid (tiny)

AyrA | PRO | 01/05/16 10:57:17 AM UTC | 0 ⭐ | 410 👁️ | Never ⏰ | []
JavaScript |

960 B

|

None

|

0 👍

/

0 👎

module.exports = exports = function() {
    var defaultAlphabets = {
        10: "0123456789",
        16: "0123456789ABCDEF",
        32: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
        36: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
        62: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
        64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
    };
    return function(options, cb) {
        var len = options.len || 7;
        var base = options.alphabet ? options.alphabet.length : (options.base || 64);
        var alphabet = options.alphabet || defaultAlphabets[base];
 
        if (!alphabet) {
            var err = new Error("Only base " + Object.keys(alphabets).join(", ") + " supported if an alphabet is not provided.");
            cb(err, null);
            return;
        }
        var id="";
        while(id.length<len)
        {
            id+=alphabet[(Math.random()*alphabet.length|0)].toString();
        }
        cb(null, id);
    };
}();

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎