-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
66 lines (58 loc) · 1.91 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
const args = process.argv.slice(2)
const checkAge = async (arg) => {
if (Number.isNaN(parseInt(arg))) return console.log('Second argument needs to be a number')
const responseString = arg < 18 ? 'Is not over 18' : 'Is over 18'
console.log(responseString)
}
const startClock = async (arg) => {
if (arg && Number.isNaN(parseInt(arg))) return console.log('Second argument needs to be a number')
let time = arg ? parseInt(arg) : 0
const logTime = () => {
console.log(++time)
setTimeout(logTime, 1000)
}
logTime()
}
const drawTriangle = async (arg) => {
if (Number.isNaN(parseInt(arg))) return console.log('Second argument needs to be a number')
const height = parseInt(arg)
let tree = ''
for (let index = 0; index <= height; index++) {
for (let index2 = 0; index2 < index; index2++) {
if (index2 === 0 || index2 === index - 1 || index === height) {
tree += '*'
} else {
tree += ' '
}
}
tree += "\n"
}
console.log(tree)
}
const FEATURES = [
{
inputs: ['-a', '--adult'],
function: checkAge
},
{
inputs: ['-c', '--clock'],
function: startClock
},
{
inputs: ['-t', '--triangle'],
function: drawTriangle
}
]
const possible_inputs = FEATURES.map(f => f.inputs).reduce((acc, cur) => [...acc, ...cur], [])
if (args.length === 0 || !possible_inputs.includes(args[0])) {
console.log(`
-h, \t--help \tto show this message
-a [age], \t--adult [age] \tto check if you're older than 18
-c [time],\t--clock [time] \tto start a clock
-t [size],\t--triangle [size]\tto draw a triangle, takes second argument as the size
`)
process.exit(0)
}
FEATURES.find(f => f.inputs.includes(args[0])).function(args[1])
process.on('SIGTERM', () => process.exit(0))
process.on('SIGINT', () => process.exit(0))