-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcli.js
73 lines (62 loc) · 1.44 KB
/
cli.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
#!/usr/bin/env node
const stringMutilator = require('./lib');
const [,, fnString, ...args] = process.argv;
const usageString = `
Functions for mutilating strings.
Usage
$ string-mutilator <function> <string> [argument]...
Functions
charCase.invert
charCase.snakeCase
charCase.camelCase
charCase.kebabCase
charCase.dotCase
charCase.pascalCase
charCase.capitalize
compressor.pack
compressor.unpack
compressor.signature
flipBits
gobbledygook
jumble
reverse
reverseBits
rockdotize
rot13
scramble
shift
shiftBits
toMANS
unicode.fixSurrogates
unicode.unfixSurrogates
Examples
$ string-mutilator shift "Hello World!" 6
$ string-mutilator charCase.invert "Hello World!"
$ string-mutilator toMANS "Hello World!" 4
`;
const errorString = '\n✖ Invalid input. Please check the help below:\n';
const resolveFn = (object, dotString) => dotString
.split('.')
.reduce((o, c) => o[c], object);
try
{
if (/^(-h|--help)$/.test(fnString))
{
console.info(usageString);
process.exit(0);
}
if (!fnString || args.length === 0)
{
console.error(errorString, usageString);
process.exit(1);
}
const fn = resolveFn(stringMutilator, fnString)
const fnArgs = args.map((v, i) => (i === 0) ? v : parseInt(v));
console.log(fn.apply(this, fnArgs));
}
catch (e)
{
console.error(errorString, usageString);
process.exit(1);
}
process.exit(0);