Skip to content

Commit

Permalink
Utilities around grades.
Browse files Browse the repository at this point in the history
  • Loading branch information
ripsawridge committed Dec 7, 2014
1 parent b7e29e5 commit dbc4a4f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 50 deletions.
52 changes: 2 additions & 50 deletions climbgrades/climbgrades.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@

// Copyright 2014, Michael Stanton.
var ClimbGrades = (function() {

// index UIAA YDS French
Expand Down Expand Up @@ -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] +
" <space separated UIAA graded climbs>");
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();
*/
78 changes: 78 additions & 0 deletions climbgrades/grades.js
Original file line number Diff line number Diff line change
@@ -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] +
"<from> <to> <space separated graded climbs>");
print("<from> and <to> 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();
5 changes: 5 additions & 0 deletions climbgrades/grades.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
# A shell script to wrap the climbgrades utility.
DIRECT=`dirname $0`/climbgrades
node $DIRECT/grades.js $@

0 comments on commit dbc4a4f

Please sign in to comment.