-
Notifications
You must be signed in to change notification settings - Fork 6
/
nid.js
125 lines (125 loc) · 3.52 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
"use strict";
/* Copyright (c) 2013-2022 Richard Rodger, MIT License */
Object.defineProperty(exports, "__esModule", { value: true });
const dc = (y) => y.map((x) => x.map((n) => String.fromCharCode(n)).join(''));
const cl = [
[0x66, 0x75, 0x63, 0x6b],
[0x73, 0x68, 0x69, 0x74],
[0x63, 0x75, 0x6e, 0x74],
[0x6e, 0x69, 0x67, 0x67],
[0x63, 0x6f, 0x63, 0x6b],
[0x73, 0x75, 0x63, 0x6b],
[0x62, 0x69, 0x74, 0x63, 0x68],
[0x61, 0x73, 0x73],
[0x68, 0x6f, 0x6c, 0x65],
[0x77, 0x68, 0x6f, 0x72, 0x65],
[0x77, 0x61, 0x6e, 0x6b],
[0x73, 0x6c, 0x75, 0x74],
[0x70, 0x75, 0x73, 0x73],
[0x65, 0x72, 0x72, 0x6F, 0x72],
];
const defaults = {
len: 6,
alphabet: '0123456789abcdefghijklmnopqrstuvwxyz',
};
let default_cursed;
let default_cursed_list;
function cursing(curses) {
if ('string' == typeof curses) {
curses = curses.split(/\s*,\s*/);
}
if (Array.isArray(curses)) {
return function (code) {
const codelower = code.toLowerCase();
for (let i = 0; i < curses.length; i++) {
if (-1 != codelower.indexOf(curses[i])) {
return true;
}
}
};
}
else if ('function' == typeof curses) {
return curses;
}
else if (curses instanceof RegExp) {
return (code) => !!code.match(curses);
}
else
return () => false;
}
function generate(opts) {
let len = defaults.len || 0;
let alphabet = defaults.alphabet;
if (null == default_cursed) {
default_cursed_list = dc(cl);
default_cursed = cursing(default_cursed_list);
// Uncomment to review curses.
// console.log(default_cursed_list)
}
let cursed = default_cursed;
if (opts) {
len = opts.length || opts.len || len || 0;
alphabet = opts.alphabet || alphabet;
cursed = opts.curses ? cursing(opts.curses) : cursed;
}
alphabet = null == alphabet ? '' : alphabet;
let code = null;
const numchars = alphabet.length || 0;
do {
const time = new Date().getTime();
const sb = [];
for (let i = 0; i < len; i++) {
const c = Math.floor((time * Math.random()) % numchars);
sb.push(alphabet[c]);
}
code = sb.join('');
} while (cursed(code));
return code;
}
function make(opts) {
opts.len = opts.len || opts.length;
['len', '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;
let nid = function nid() {
return generate(opts);
};
const curses = opts.curses;
nid.curses = () => curses || default_cursed;
nid.len = opts.len;
nid.alphabet = opts.alphabet;
return nid;
}
function Nid(spec) {
if (spec) {
if ('number' === typeof spec) {
return generate({
len: spec,
});
}
else if ('object' === typeof spec) {
return make(spec);
}
else
return generate();
}
return generate();
}
// ensure default_cursed_list is generated
Nid();
Nid.curses = () => default_cursed_list;
Nid.len = defaults.len;
Nid.alphabet = defaults.alphabet;
exports.default = Nid;
module.exports = Nid;
//# sourceMappingURL=nid.js.map