run-pace
Version:
Calculate running pace
69 lines (58 loc) • 2.19 kB
JavaScript
;
const parseArgv = require("../lib/parse-argv.js");
const runPace = require("../lib/run-pace.js");
const flags = [
{ short: "l", long: "length" },
{ short: "p", long: "pace" },
{ short: "t", long: "time" },
{ short: "i", long: "imperial" },
{ short: "m", long: "metric" },
{ short: "s", long: "speed" },
];
const speedError = "\"speed\" is only valid when (only) pace is given or calculated.";
function cli(argv, nodeProcess) {
try {
const options = parseArgv(argv, flags);
let result;
if (options.imperial && options.metric) {
throw new Error("Conflicting parameters: \"metric\" and \"imperial\" cannot be provided at the same time");
}
if (options.time && options.length && options.pace) {
throw new Error("Too many arguments. Only two of \"time\", \"length\" and \"pace\" may be provided at any time");
} else if (options.time && options.pace) {
if (options.speed) {
throw new Error(speedError);
}
result = runPace.calculateLength(options);
} else if (options.time && options.length) {
result = runPace.calculatePace(options);
} else if (options.pace && options.length) {
if (options.speed) {
throw new Error(speedError);
}
result = runPace.calculateTime(options);
} else if (options.pace && options.speed) {
result = runPace.paceToSpeed(options);
} else {
throw new Error(`Two of "time", "length" and "pace" must be provided.\n${speedError}`);
}
nodeProcess.stdout.write(`${result}\n`);
} catch (e) {
nodeProcess.stderr.write(`${e.message}\n`);
nodeProcess.stdout.write("\nParameters: \n"
+ " -l, --length, <value><unit> (10km, 10mi, 10000m, hm, ma)\n"
+ " -p, --pace, <value>/<unit> (4:30/km, 4m30s/mi)\n"
+ " -t, --time, <value> (11:23, 11min23sec, 11m23s)\n"
+ " -i, --imperial, force imperial output\n"
+ " -m, --metric, force metric output\n"
+ " -s, --speed, output speed instead of pace\n"
);
}
}
// Expose to tests
module.exports = cli;
// Execute, if run as script
if (require.main === module) {
cli(process.argv, process);
}