Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- };
- }();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement