Advertisement
AyrA

bignum fuckup fix in mongoose-shortid (tiny)

Jan 5th, 2016
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module.exports = exports = function() {
  2.     var defaultAlphabets = {
  3.         10: "0123456789",
  4.         16: "0123456789ABCDEF",
  5.         32: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
  6.         36: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
  7.         62: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
  8.         64: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"
  9.     };
  10.     return function(options, cb) {
  11.         var len = options.len || 7;
  12.         var base = options.alphabet ? options.alphabet.length : (options.base || 64);
  13.         var alphabet = options.alphabet || defaultAlphabets[base];
  14.  
  15.         if (!alphabet) {
  16.             var err = new Error("Only base " + Object.keys(alphabets).join(", ") + " supported if an alphabet is not provided.");
  17.             cb(err, null);
  18.             return;
  19.         }
  20.         var id="";
  21.         while(id.length<len)
  22.         {
  23.             id+=alphabet[(Math.random()*alphabet.length|0)].toString();
  24.         }
  25.         cb(null, id);
  26.     };
  27. }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement