-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathexamples.js
98 lines (83 loc) · 2.51 KB
/
examples.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
'use strict';
var rand = require('./');
// Example replacement patterns.
var replacements = [{
pattern: /:random\(([^)]*)\)/,
replacement: function (match, args) {
if(args.match(/,/)) {
args = parse(args.split(','));
// console.log(args)
return rand.apply(rand, args);
} else {
return rand(args);
}
}
}];
function randomize(patterns, arr) {
var keys = Object.keys(patterns);
return keys.reduce(function(res, str) {
var desc = patterns[str];
var result = arr.reduce(function(acc, o) {
return acc.replace(o.pattern, o.replacement);
}, str);
res[desc] = {};
res[desc].result = result;
res[desc].length = result.length;
return res;
}, {});
}
var patterns = {
// Pattern // Should result in...
':random(A, 10)' : 'alpha, (10 digits)',
':random(A, 5)' : 'alpha, (5 digits)',
':random(7, {chars: "foo\'s"})' : 'special chars, (7 digits)',
':random(AA, 10)' : 'alpha, (10 digits)',
':random(Aa, 12)' : 'alpha, (12 digits)',
':random(A, 3)' : 'alpha, (3 digits)',
':random(AAa)' : 'alpha, (3 digits)',
':random(AA, 3)' : 'alpha, (3 digits)',
':random(A0, 5)' : 'alpha-numeric, (5 digits)',
':random(AA00, 5)' : 'alpha-numeric, (5 digits)',
':random(A0A0, 5)' : 'alpha-numeric, (5 digits)',
':random(AaAa0000)' : 'alpha-numeric, (8 digits)',
':random(0, 1)' : 'numeric, (1 digit)',
':random(0, 8)' : 'numeric, (8 digits)',
':random(00000000)' : 'numeric, (8 digits)',
':random(A0!, 7)' : 'special chars, (7 digits)',
':random(A0!a0A0)' : 'special chars, (7 digits)',
':random(Aa0, 1)' : 'alpha, (1 digit)',
':random(*, 16)' : 'all characters, (16 digits)',
':random(?, 16, jonathan)' : 'custom chars, (16 digit)'
};
console.log(randomize(patterns, replacements));
function parse(args) {
return args.map(function(arg) {
if (/\{/.test(arg)) {
var start = /(['"])/;
var i = arg.search(start);
var ch = arg[i];
var match;
var chars = '';
while ((match = arg.charAt(++i)) !== ch) {
chars += match;
}
console.log(chars);
return {chars: chars};
}
return arg;
});
}
console.log();
console.log(rand('*', 20, { exclude: '0oOiIlL1' }));
console.log();
// function parse(args) {
// return args.map(function(arg) {
// if (/\{/.test(arg)) {
// var match = /['"]([^"']+)["']/.exec(arg);
// if (match) {
// return {chars: match[1]};
// }
// }
// return arg;
// });
// }