@tdb/web
Version:
Common condiguration for serving a web-site and testing web-based UI components.
30 lines (28 loc) • 612 B
JavaScript
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 id = ''
var bytes = random(size)
while (0 < size--) {
id += url[bytes[size] & 63]
}
return id
}