dragonroll/backend/node_modules/nanoid/index.js

35 lines
927 B
JavaScript
Raw Normal View History

2024-09-21 17:08:36 +00:00
var random = require('./random')
var url = require('./url')
/**
* Generate secure URL-friendly unique ID.
*
* By default, ID will have 21 symbols to have a collision probability similar
* to UUID v4.
*
* @param {number} [size=21] The number of symbols in ID.
*
* @return {string} Random string.
*
* @example
* const nanoid = require('nanoid')
* model.id = nanoid() //=> "Uakgb_J5m9g-0JDMbcJqL"
*
* @name nanoid
* @function
*/
module.exports = function (size) {
size = size || 21
var bytes = random(size)
var id = ''
// Compact alternative for `for (var i = 0; i < size; i++)`
while (size--) {
// We cant use bytes bigger than the alphabet. 63 is 00111111 bitmask.
// This mask reduces random byte 0-255 to 0-63 values.
// There is no need in `|| ''` and `* 1.6` hacks in here,
// because bitmask trim bytes exact to alphabet size.
id += url[bytes[size] & 63]
}
return id
}