3 var alphabet = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_'.split('')
11 * Return a string representing the specified number.
13 * @param {Number} num The number to convert.
14 * @returns {String} The string representation of the number.
17 function encode(num) {
21 encoded = alphabet[num % length] + encoded;
22 num = Math.floor(num / length);
29 * Return the integer value specified by the given string.
31 * @param {String} str The string to convert.
32 * @returns {Number} The integer value represented by the string.
35 function decode(str) {
38 for (i = 0; i < str.length; i++) {
39 decoded = decoded * length + map[str.charAt(i)];
46 * Yeast: A tiny growing id generator.
48 * @returns {String} A unique id.
52 var now = encode(+new Date());
54 if (now !== prev) return seed = 0, prev = now;
55 return now +'.'+ encode(seed++);
59 // Map each character to its index.
61 for (; i < length; i++) map[alphabet[i]] = i;
64 // Expose the `yeast`, `encode` and `decode` functions.
66 yeast.encode = encode;
67 yeast.decode = decode;
68 module.exports = yeast;