-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
94 lines (86 loc) · 2.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
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
/**
* Created by Wisdom Kwan on 17/3/17.
*/
'use strict';
const program = require('commander');
const randomize = require('randomatic');
const version = require('./package').version;
const paramsDefault = {
pattern: '0Aa',
length: 8,
count: 1,
options: {
chars: undefined
},
};
/**
* use pattern param set random string length.
* @param {String} pattern
* @return {boolean}
*/
const lengthByPattern = function (pattern) {
return false;
};
/**
* generate random string with command args.
* @param {Object} params
* @return {String}
*/
const doCmd = function (params) {
if (params.pattern === '?') {
return randomize('?', params.length, params.options);
}
else if (lengthByPattern(params.pattern)) {
return randomize(params.pattern);
}
return randomize(params.pattern, params.length);
};
/**
* # randomatic cmd:
* randomatic -p aA0 -l 16
*
* ## pattern
* a: lowercase alpha characters ('abcdefghijklmnopqrstuvwxyz').
* A: uppercase alpha characters ('ABCDEFGHIJKLMNOPQRSTUVWXYZ').
* 0: numeric characters ('0123456789').
* !: special characters ('~!@#$%^&()_+-={}[];\',.').
* *: all characters (all of the above combined).
* ?: custom characters (pass a string of custom characters to the options).
*
* ## length
* the length of the string to generate.
* examples:
* randomize('A', 5) will generate 5-character, uppercase string.
* randomize('AAAA') will generate 4-characters, uppercase string.
*
* ## chars
* define a custom string to be randomized.
* example:
* randomize('?', 20, {chars: 'qwertyuio'})
*/
program
.version(`randomatic-cli ${version}`)
.option('-p, --pattern [pattern]', 'the pattern to use for randomizing, [a], [A], [0], [!], [*], [?], default: [0Aa]')
.option('-l, --length [length]', 'the length of the string to generate.', Number.parseInt)
.option('-c, --count [count]', 'randomize count.', Number.parseInt)
.option('-C, --chars [chars]', 'define a custom string to be randomized.')
.parse(process.argv);
let params = paramsDefault;
if (program.pattern) {
params = Object.assign(params, { pattern: program.pattern });
}
if (program.length) {
params = Object.assign(params, { length: program.length});
}
if (program.count) {
params = Object.assign(params, { count: program.count });
}
if (program.chars) {
params = Object.assign(params, { options: { chars: program.chars } });
}
// exec randomize function
let result = new Array(params.count);
for (let i=0; i<params.count; i++) {
result[i] = doCmd(params);
}
console.log(JSON.stringify({ randomatic: result }, null, ' '));