1 |
|
2 |
|
3 |
|
4 | var TABLE = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
|
5 | TABLE_LEN = TABLE.length;
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 | function generate(template) {
|
19 | var match, i, len, result;
|
20 |
|
21 | if (typeof template !== 'string') {
|
22 | throw new TypeError('template must be a String: ' + template);
|
23 | }
|
24 |
|
25 | match = template.match(/(X+)[^X]*$/);
|
26 |
|
27 |
|
28 | if (match === null) {
|
29 | return template;
|
30 | }
|
31 |
|
32 |
|
33 | for (result = '', i = 0, len = match[1].length; i < len; ++i) {
|
34 | result += TABLE[Math.floor(Math.random() * TABLE_LEN)];
|
35 | }
|
36 |
|
37 |
|
38 | return template.slice(0, match.index) + result +
|
39 | template.slice(match.index + result.length);
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 | module.exports = {
|
47 | generate: generate
|
48 | };
|