diff --git a/climbgrades/climbgrades.js b/climbgrades/climbgrades.js index f60b0af..bed2b2c 100644 --- a/climbgrades/climbgrades.js +++ b/climbgrades/climbgrades.js @@ -1,4 +1,4 @@ - +// Copyright 2014, Michael Stanton. var ClimbGrades = (function() { // index UIAA YDS French @@ -148,54 +148,6 @@ var ClimbGrades = (function() { return module; }()); +// Uncomment this if you are embedding in a web page. exports = module.exports = ClimbGrades; -/* -function ParseSportGradeFromUIAA(uiaa) { - var trimmed_uiaa = uiaa.trim(); - var index = ClimbGrades.ToIndex(trimmed_uiaa, ClimbGrades.GRADE.UIAA); - return index; -} - - -function UIAAFromSportGrade(grade) { - var uiaa_string = ClimbGrades.FromIndex(grade, ClimbGrades.GRADE.UIAA); - return uiaa_string; -} - -function print(str) { - console.log(str); -} - - -function Main() { - var arguments = process.argv.slice(2); - if (arguments.length == 0) { - console.log("Usage: " + process.argv[1] + - " "); - return 1; - } - - accum = 0.0; - climb_count = 0; - for (var i = 0; i < arguments.length; i++) { - var arg = arguments[i]; - sport_grade = ParseSportGradeFromUIAA(arg); - if (sport_grade < 0) { - print("Expression " + arg + " could not be parsed"); - } else { - print(arg + " -> " + sport_grade); - climb_count = climb_count + 1; - accum = accum + sport_grade; - } - } - - average = Math.round(accum / climb_count); - str_average_rating = UIAAFromSportGrade(average); - print("UIAA average rating -> " + str_average_rating); - print("Average sport grade -> " + average); - print("Total effort -> " + accum); -} - -Main(); -*/ diff --git a/climbgrades/grades.js b/climbgrades/grades.js new file mode 100644 index 0000000..f775f68 --- /dev/null +++ b/climbgrades/grades.js @@ -0,0 +1,78 @@ +// Copyright 2014, Michael Stanton. +var ClimbGrades = require("./climbgrades"); + +function ParseSportGradeFromUIAA(uiaa) { + var trimmed_uiaa = uiaa.trim(); + var index = ClimbGrades.ToIndex(trimmed_uiaa, ClimbGrades.GRADE.UIAA); + return index; +} + + +function UIAAFromSportGrade(grade) { + var uiaa_string = ClimbGrades.FromIndex(grade, ClimbGrades.GRADE.UIAA); + return uiaa_string; +} + +function print(str) { + console.log(str); +} + + +function GetGrade(str) { + str = str.trim().toLowerCase(); + if (str === "uiaa") return ClimbGrades.GRADE.UIAA; + else if (str === "yds") return ClimbGrades.GRADE.YDS; + else if (str === "french") return ClimbGrades.GRADE.FRENCH; + throw new TypeError(str + " is not a valid grade."); +} + + +function Main() { + var arguments = process.argv.slice(2); + if (arguments.length < 3) { + print("Usage: " + process.argv[1] + + " "); + print(" and are UIAA, YDS or French"); + return 1; + } + + accum = 0.0; + climb_count = 0; + var from = GetGrade(arguments[0]); + var to = GetGrade(arguments[1]); + var output = []; + for (var i = 2; i < arguments.length; i++) { + var arg = arguments[i].trim(); + try { + var index = ClimbGrades.ToIndex(arg, from); + if (index < 0) { + print("skipping " + arg + ". Couldn't be parsed."); + } else { + var result = ClimbGrades.FromIndex(index, to); + output.push(result); + climb_count = climb_count + 1; + accum = accum + index; + } + } catch(e) { + print("error occurred: " + e.message); + } + } + + // print the results + var outputStr = ""; + for (var i = 0; i < output.length; i++) { + outputStr += output[i].toString(); + outputStr += " "; + } + print(outputStr); + + if (output.length > 1) { + average = Math.round(accum / climb_count); + average_from = ClimbGrades.FromIndex(average, from); + average_to = ClimbGrades.FromIndex(average, to); + print("average rating: " + average_from + " (" + from.name + ") " + + average_to + " (" + to.name + ")"); + } +} + +Main(); diff --git a/climbgrades/grades.sh b/climbgrades/grades.sh new file mode 100755 index 0000000..bb1841f --- /dev/null +++ b/climbgrades/grades.sh @@ -0,0 +1,5 @@ +#!/bin/bash +# A shell script to wrap the climbgrades utility. +DIRECT=`dirname $0`/climbgrades +node $DIRECT/grades.js $@ +