Sha256: 8f8317c6517081062acbbded96fbbfd4dc33ae7fc09e16d2cd3b53751ce91564

Contents?: true

Size: 976 Bytes

Versions: 28

Compression:

Stored size: 976 Bytes

Contents

import crypto from 'crypto'
import { urlAlphabet } from '../url-alphabet/index.js'
let random = bytes =>
  new Promise((resolve, reject) => {
    crypto.randomFill(Buffer.allocUnsafe(bytes), (err, buf) => {
      if (err) {
        reject(err)
      } else {
        resolve(buf)
      }
    })
  })
let customAlphabet = (alphabet, defaultSize = 21) => {
  let mask = (2 << (31 - Math.clz32((alphabet.length - 1) | 1))) - 1
  let step = Math.ceil((1.6 * mask * defaultSize) / alphabet.length)
  let tick = (id, size = defaultSize) =>
    random(step).then(bytes => {
      let i = step
      while (i--) {
        id += alphabet[bytes[i] & mask] || ''
        if (id.length === size) return id
      }
      return tick(id, size)
    })
  return size => tick('', size)
}
let nanoid = (size = 21) =>
  random(size).then(bytes => {
    let id = ''
    while (size--) {
      id += urlAlphabet[bytes[size] & 63]
    }
    return id
  })
export { nanoid, customAlphabet, random }

Version data entries

28 entries across 28 versions & 3 rubygems

Version Path
clapton-0.0.26 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.25 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.24 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.23 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.22 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.21 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.20 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.19 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.18 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.17 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.16 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.15 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.14 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.13 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.12 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.11 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.10 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.9 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.8 lib/clapton/javascripts/node_modules/nanoid/async/index.js
clapton-0.0.7 lib/clapton/javascripts/node_modules/nanoid/async/index.js