forked from ai/nanoid
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
29 lines (27 loc) · 648 Bytes
/
Copy pathindex.js
File metadata and controls
29 lines (27 loc) · 648 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
var random = require('./random')
var url = '_~0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
/**
* 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
* var nanoid = require('nanoid')
* model.id = nanoid() //=> "Uakgb_J5m9g~0JDMbcJqL"
*
* @name nanoid
*/
module.exports = function (size) {
size = size || 21
var id = ''
var bytes = random(size)
while (0 < size--) {
id += url[bytes[size] & 63]
}
return id
}