Last active
March 16, 2019 19:54
-
-
Save suissa/ea66e7c8d24f8ec892fd8458bbfed27c to your computer and use it in GitHub Desktop.
Source Code from ThermoCLI - refatorado by Suissinha
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
'use strict'; | |
const program = require('commander'); | |
program | |
.version('0.1.5') | |
.usage('[options] <number>') | |
.option('--c2f', 'convert Celsius to Fahrenheit') | |
.option('--c2k', 'convert Celsius to Kelvin') | |
.option('--f2c', 'convert Fahrenheit to Celsius') | |
.option('--f2k', 'convert Fahrenheit to Kelvin') | |
.option('--k2c', 'convert Kelvin to Celsius') | |
.option('--k2f', 'convert Kelvin to Fahrenheit') | |
program.on('--help', () => | |
console.log(` Example: | |
$ thermo --c2f 32 | |
Converting 32 Celsius to Fahrenheit is equals to 89.6 Fahrenheit' | |
`) | |
) | |
program.parse(process.argv); | |
const logResponse = ( num, start, end, converted ) => | |
console.log(` | |
Converting ${num} ${start} to ${end} is equals to ${converted} ${end} | |
`) | |
const OPTIONS = [ 'c2f', 'c2k', 'f2c', 'f2k', 'k2c', 'k2f' ] | |
const ACTIONS = { | |
c2f: ( num ) => logResponse( num, 'Celsius', 'Fahrenheit', num * 1.8 + 32 ), | |
c2k: ( num ) => logResponse( num, 'Celsius', 'Kelvin', num + 273.15 ), | |
f2c: ( num ) => logResponse( num, 'Fahrenheit', 'Celsius', (num - 32) / 1.8 ), | |
f2k: ( num ) => logResponse( num, 'Fahrenheit', 'Kelvin', (num - 32) * 5 / 9 + 273.15 ), | |
k2c: ( num ) => logResponse( num, 'Kelvin', 'Celsius', num - 273.15), | |
k2f: ( num ) => logResponse( num, 'Kelvin', 'Fahrenheit', num * 1.8 - 459.67), | |
} | |
const keys = Object.keys( program ) | |
const option = keys[ keys.length - 2 ] | |
ACTIONS[ option ]( parseFloat( program.args[0] ) ) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment