-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
52 lines (41 loc) · 1.55 KB
/
index.js
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// --------------------------------------------------------------------------------------------------------------------
'use strict'
// npm
const mathRandom = require('math-random')
// --------------------------------------------------------------------------------------------------------------------
// setup
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'.split('')
const alphabetLength = alphabet.length
const different = 'ABCDEFGHJKLMNPQRTUVWXYacdefghijkmnopqrstuvwxyz0123456789'.split('')
const differentLength = different.length
const defaults = {
lookalikes: true,
}
// --------------------------------------------------------------------------------------------------------------------
function id(len, opts) {
const o = Object.assign({}, defaults, opts || {})
let set = o.lookalikes ? alphabet : different
let setLength = set.length
if ( !len ) {
throw new Error('len must be provided')
}
if ( typeof len !== 'number' ) {
throw new Error('len must be a number')
}
if ( len === Infinity ) {
throw new Error('len must be less than Infinity')
}
if ( len < 0 ) {
throw new Error('len must be greater than 0')
}
let str = ''
while(len > 0) {
var i = Math.floor(mathRandom() * setLength)
str += set[i]
len--
}
return str
}
// --------------------------------------------------------------------------------------------------------------------
module.exports = id
// --------------------------------------------------------------------------------------------------------------------