Skip to content
Myr

format.js

This file

Path

format.js

48 lines · 1.2 KiB · JavaScript

Last changed by

Fix JSDocAndrey Sitnikbefore adoption8e101a3

History of this file

Take it

Add format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf

line 1why, one step back
1/**

Fix JSDocAndrey Sitnikbefore adoption29e48f0

line 2why, one step back
2 * Secure random string generator with custom alphabet.

Add format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf

line 3why, one step back
3 *

Fix JSDocAndrey Sitnikbefore adoption29e48f0

line 4why, one step back
4 * Alphabet must contain 256 symbols or less. Otherwise, the generator

Add format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf

lines 5–6why, one step back
5 * will not be secure.
6 *

Fix nanoid.format JSDocAndrey Sitnikbefore adoptioneb3d04f

line 7why, one step back
7 * @param {generator} random The random bytes generator.

Add format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf

lines 8–22why, one step back
8 * @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 back
23 *
24 * @name format

Fix JSDocAndrey Sitnikbefore adoption8e101a3

line 25why, one step back
25 * @function

Add format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf

lines 26–27why, one step back
26 */
27module.exports = function (random, alphabet, size) {

Calculate mask based on alphabet length (#29)Anton Khlynovskiybefore adoption3e641fe

line 28why, one step back
28 var mask = (2 << Math.log(alphabet.length - 1) / Math.LN2) - 1

Speed up ID generation by binary maskAndrey Sitnikbefore adoptiond6ad341

line 29why, one step back
29 var step = Math.ceil(1.6 * mask * size / alphabet.length)

3x speed up ID generationAndrey Sitnikbefore adoption17b274e

line 30why, one step back
30

Add format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf

line 31why, one step back
31 var id = ''

Reduce library sizeAndrey Sitnikbefore adoptionfdb2dd3

line 32why, one step back
32 while (true) {

Move bytes inside cycle, remove length lookupmokhovbefore adoption40d0e42

lines 33–34why, one step back
33 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 back
35 var byte = bytes[i] & mask
36 if (alphabet[byte]) {37 id += alphabet[byte]

Reduce library sizeAndrey Sitnikbefore adoptionfdb2dd3

line 38why, one step back
38 if (id.length === size) return id

Add format function to work with any RNGAndrey Sitnikbefore adoptione1bd3bf

lines 39–48why, one step back
39 }
40 }41 }42}4344/**45 * @callback generator46 * @param {number} bytes The number of bytes to generate.47 * @return {number[]} Random bytes.48 */