forked from rjrodger/nid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nid.js
146 lines (116 loc) · 3.91 KB
/
nid.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* Copyright (c) 2013 Richard Rodger, MIT License */
"use strict";
/* Module API:
* var nid = require('nid')
* nid() // returns new random id as string
* nid({options}) // returns new nid function that generates ids using options
*
* options are:
* length: number of chars in id
* alphabet: alphabet to take chars from randomly
* hex, HEX: alphabet is hexadecimal, lower or upper case respectively (overrides alphabet)
* exclude,curses: (exlude overrides)
* array of strings - excluded if contained in id
* string: comma-separated list of curses
* regexp: excluded if matches
* function: return true if exluded, first arg is id
*
*/
var defaults = {
length: 6,
alphabet: '0123456789abcdefghijklmnopqrstuvwxyz',
// function hidecurse(s) {o='';for(i=0; i<s.length; i++){o+='\\x'+(s.charCodeAt(i).toString(16).toUpperCase())} return o; }
curses: [
'\x66\x75\x63\x6B',
'\x73\x68\x69\x74',
'\x70\x69\x73\x73',
'\x63\x75\x6E\x74',
'\x74\x69\x74\x73',
'\x6E\x69\x67\x67\x65\x72',
'\x63\x6F\x63\x6B\x73\x75\x63\x6B\x65\x72',
'\x6D\x6F\x74\x68\x65\x72\x66\x75\x63\x6B\x65\x72',
'\\x66\\x72\\x61\\x63\\x6B',
'\\x66\\x72\\x61\\x6B'
],
}
var default_cursed = cursing(defaults.curses)
function cursing(curses) {
var typestr = Object.prototype.toString.call(curses).substring(8)
if ('String]' == typestr) {
curses = curses.split(/\s*,\s*/)
}
if (Array.isArray(curses)) {
return function (code) {
var codelower = code.toLowerCase()
for (var i = 0; i < curses.length; i++) {
if (-1 != codelower.indexOf(curses[i])) {
return true
}
}
}
} else {
if ('Function]' == typestr) {
return curses
} else if ('RegExp]' == typestr) {
return function (code) {
return !!code.match(curses)
}
} else return function () {
return false
}
}
}
function generate(opts) {
var length = defaults.length
var alphabet = defaults.alphabet
var cursed = default_cursed
if (opts) {
length = opts.length || length
alphabet = opts.alphabet || alphabet
cursed = opts.curses ? cursing(opts.curses) : cursed
}
var code = null,
numchars = alphabet.length
do {
var time = new Date().getTime()
var sb = []
for (var i = 0; i < length; i++) {
var c = Math.floor((time * Math.random()) % numchars)
sb.push(alphabet[c])
}
code = sb.join('')
}
while (cursed(code))
return code
}
function make(opts) {
['length', 'alphabet', 'curses'].forEach(function (setting) {
opts[setting] = (void 0 === opts[setting]) ? defaults[setting] : opts[setting]
})
if (opts.hex) {
opts.alphabet = '0123456789abcdef'
} else if (opts.HEX) {
opts.alphabet = '0123456789ABCDEF'
}
// exclude overrides curses
opts.curses = opts.exclude || opts.curses
return function () {
return generate(opts)
}
}
function nid() {
var arg0 = arguments[0]
if (arg0) {
var typestr = Object.prototype.toString.call(arg0).substring(8)
if ("Number]" === typestr) {
return generate({
length: arg0
})
} else if ("Object]" === typestr) {
return make(arg0)
} else return generate();
}
return generate()
}
nid.__defaults = defaults
module.exports = nid