format.js
This file
Path
format.js
48 lines · 1.2 KiB · JavaScript
Last changed by
Fix JSDocAndrey Sitnikbefore adoption8e101a3
History of this fileTake it
Add format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf
line 1why, one step back1/**Fix JSDocAndrey Sitnikbefore adoption29e48f0
line 2why, one step back2 * Secure random string generator with custom alphabet.Add format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf
line 3why, one step back3 *Fix JSDocAndrey Sitnikbefore adoption29e48f0
line 4why, one step back4 * Alphabet must contain 256 symbols or less. Otherwise, the generatorAdd format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf
lines 5–6why, one step back5 * will not be secure.6 *Fix nanoid.format JSDocAndrey Sitnikbefore adoptioneb3d04f
line 7why, one step back7 * @param {generator} random The random bytes generator.Add format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf
lines 8–22why, one step back8 * @param {string} alphabet Symbols to be used in new random string.9 * @param {size} size The number of symbols in new random string.10 *11 * @return {string} Random string.12 *13 * @example14 * var format = require('nanoid/format')15 *16 * function random (size) {17 * var result = []18 * for (var i = 0; i < size; i++) result.push(randomByte())19 * return result20 * }21 *22 * format(random, "abcdef", 5) //=> "fbaef"Fix JSDocAndrey Sitnikbefore adoption486009f
lines 23–24why, one step back23 *24 * @name formatFix JSDocAndrey Sitnikbefore adoption8e101a3
line 25why, one step back25 * @functionAdd format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf
lines 26–27why, one step back26 */27module.exports = function (random, alphabet, size) {Calculate mask based on alphabet length (#29)Anton Khlynovskiybefore adoption3e641fe
line 28why, one step back28 var mask = (2 << Math.log(alphabet.length - 1) / Math.LN2) - 1Speed up ID generation by binary maskAndrey Sitnikbefore adoptiond6ad341
line 29why, one step back29 var step = Math.ceil(1.6 * mask * size / alphabet.length)3x speed up ID generationAndrey Sitnikbefore adoption17b274e
line 30why, one step back30Add format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf
line 31why, one step back31 var id = ''Reduce library sizeAndrey Sitnikbefore adoptionfdb2dd3
line 32why, one step back32 while (true) {Move bytes inside cycle, remove length lookupmokhovbefore adoption40d0e42
lines 33–34why, one step back33 var bytes = random(step)34 for (var i = 0; i < step; i++) {Speed up ID generation by binary maskAndrey Sitnikbefore adoptiond6ad341
lines 35–37why, one step back35 var byte = bytes[i] & mask36 if (alphabet[byte]) {37 id += alphabet[byte]Reduce library sizeAndrey Sitnikbefore adoptionfdb2dd3
line 38why, one step back38 if (id.length === size) return idAdd format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf
lines 39–48why, one step back39 }40 }41 }42}4344/**45 * @callback generator46 * @param {number} bytes The number of bytes to generate.47 * @return {number[]} Random bytes.48 */