Skip to content

Commit

Permalink
Add minification to JS, remove ES6 from JS
Browse files Browse the repository at this point in the history
  • Loading branch information
zack committed Oct 14, 2018
1 parent 6d145c6 commit 86b6387
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 45 deletions.
3 changes: 3 additions & 0 deletions Gruntfile.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ module.exports = function(grunt) {
pkg: grunt.file.readJSON('package.json'), pkg: grunt.file.readJSON('package.json'),
assets_inline: { assets_inline: {
all: { all: {
options: {
minify: true
},
files: { files: {
"./dist/index.html": "./src/index.html" "./dist/index.html": "./src/index.html"
} }
Expand Down
7 changes: 2 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
"author": "zack@youngren.io", "author": "zack@youngren.io",
"license": "MIT", "license": "MIT",
"homepage": "https://rpe.youngren.io/", "homepage": "https://rpe.youngren.io/",
"dependencies": {}, "dependencies": {
"uglify-js": "^3.4.9"
},
"devDependencies": { "devDependencies": {
"grunt": "^1.0.3", "grunt": "^1.0.3",
"grunt-assets-inline": "^1.1.2" "grunt-assets-inline": "^1.1.2"
Expand Down
77 changes: 38 additions & 39 deletions src/js/script.js
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
(function() { (function() {
const RPEs = { var RPEs = {
RPE: { RPE: {
"10": { "10": {
REPS: { REPS: {
Expand Down Expand Up @@ -85,16 +85,16 @@
}; };


function validateWeight(inputRow, errors) { function validateWeight(inputRow, errors) {
const input = inputRow.querySelector("input"); var input = inputRow.querySelector("input");
const inputValue = input.value; var inputValue = input.value;
if (inputValue.length > 0 && inputValue <= 0) { if (inputValue.length > 0 && inputValue <= 0) {
errors.push([input.getAttribute("id"), "Weight must be above 0"]); errors.push([input.getAttribute("id"), "Weight must be above 0"]);
} }
} }


function validateRPE(inputRow, errors) { function validateRPE(inputRow, errors) {
const input = inputRow.querySelector("input"); var input = inputRow.querySelector("input");
const inputValue = input.value; var inputValue = input.value;
if (inputValue.length > 0 && inputValue < 6) { if (inputValue.length > 0 && inputValue < 6) {
errors.push([input.getAttribute("id"), "RPE must be 6 or above"]); errors.push([input.getAttribute("id"), "RPE must be 6 or above"]);
} else if (inputValue.length > 0 && inputValue > 10) { } else if (inputValue.length > 0 && inputValue > 10) {
Expand All @@ -103,8 +103,8 @@
} }


function validateReps(inputRow, errors) { function validateReps(inputRow, errors) {
const input = inputRow.querySelector("input"); var input = inputRow.querySelector("input");
const inputValue = input.value; var inputValue = input.value;
if (inputValue.length > 0 && inputValue <= 0) { if (inputValue.length > 0 && inputValue <= 0) {
errors.push([input.getAttribute("id"), "Reps must be above 0"]); errors.push([input.getAttribute("id"), "Reps must be above 0"]);
} else if (inputValue.length > 0 && inputValue > 12) { } else if (inputValue.length > 0 && inputValue > 12) {
Expand All @@ -116,9 +116,8 @@
} }


function sanitizeAndGetInputErrors() { function sanitizeAndGetInputErrors() {
const errors = []; var errors = [];
const inputRows = document.querySelectorAll(".input-row"); var inputRowsArr = [].slice.call(document.querySelectorAll(".input-row"));
const inputRowsArr = [...inputRows];
inputRowsArr.forEach(function(inputRow) { inputRowsArr.forEach(function(inputRow) {
if (inputRow.className.indexOf("weight") > 0) { if (inputRow.className.indexOf("weight") > 0) {
validateWeight(inputRow, errors); validateWeight(inputRow, errors);
Expand All @@ -133,19 +132,20 @@
} }


function clearAllErrors() { function clearAllErrors() {
const inputRows = document.querySelectorAll(".input-row"); var inputRowsArr = [].slice.call(document.querySelectorAll(".input-row"));
const inputRowArr = [...inputRows]; inputRowsArr.forEach(function(inputRow) {
inputRowArr.forEach(function(inputRow) {
inputRow.classList.remove("error"); inputRow.classList.remove("error");
inputRow.querySelector(".error").innerHTML = ""; inputRow.querySelector(".error").innerHTML = "";
}); });
} }


function displayErrors(errors) { function displayErrors(errors) {
errors.forEach(function([divName, errorText]) { errors.forEach(function(arr) {
const inputRow = `.input-row.${divName}`; var divName = arr[0];
var errorText = arr[1];
var inputRow = ".input-row." + divName;
document.querySelector(inputRow).classList.add("error"); document.querySelector(inputRow).classList.add("error");
document.querySelector(`${inputRow} .error`).innerHTML = errorText; document.querySelector(inputRow + " .error").innerHTML = errorText;
}); });
} }


Expand All @@ -161,37 +161,37 @@
} }


function inputsEventHandler() { function inputsEventHandler() {
const desiredRPE = document.querySelector("input#desired-rpe").value; var desiredRPE = document.querySelector("input#desired-rpe").value;
const desiredReps = document.querySelector("input#desired-reps").value; var desiredReps = document.querySelector("input#desired-reps").value;
const givenRPE = document.querySelector("input#given-rpe").value; var givenRPE = document.querySelector("input#given-rpe").value;
const givenReps = document.querySelector("input#given-reps").value; var givenReps = document.querySelector("input#given-reps").value;
const givenWeight = document.querySelector("input#given-weight").value; var givenWeight = document.querySelector("input#given-weight").value;


clearAllErrors(); clearAllErrors();
const errors = sanitizeAndGetInputErrors(); var errors = sanitizeAndGetInputErrors();


if (errors.length > 0) { if (errors.length > 0) {
displayErrors(errors); displayErrors(errors);
return false; return false;
} }


const haveAllGivens = givenWeight && givenRPE && givenReps; var haveAllGivens = givenWeight && givenRPE && givenReps;


document.querySelector("input#desired-reps").disabled = !haveAllGivens; document.querySelector("input#desired-reps").disabled = !haveAllGivens;
document.querySelector("input#desired-rpe").disabled = !haveAllGivens; document.querySelector("input#desired-rpe").disabled = !haveAllGivens;
const desiredWeightEl = document.querySelector("#solved-weight"); var desiredWeightEl = document.querySelector("#solved-weight");
const e1RMEl = document.querySelector("#e1RM"); var e1RMEl = document.querySelector("#e1RM");
const ninetyFivePEl = document.querySelector("#ninetyFiveP"); var ninetyFivePEl = document.querySelector("#ninetyFiveP");
const eightyFivePEl = document.querySelector("#eightyFiveP"); var eightyFivePEl = document.querySelector("#eightyFiveP");
const eightyPEl = document.querySelector("#eightyP"); var eightyPEl = document.querySelector("#eightyP");
const seventyFivePEl = document.querySelector("#seventyFiveP"); var seventyFivePEl = document.querySelector("#seventyFiveP");
const sixtyFivePEl = document.querySelector("#sixtyFiveP"); var sixtyFivePEl = document.querySelector("#sixtyFiveP");


if (haveAllGivens) { if (haveAllGivens) {
const givenRPEDecimal = RPEs["RPE"][givenRPE]["REPS"][givenReps]; var givenRPEDecimal = RPEs["RPE"][givenRPE]["REPS"][givenReps];
const estimated1RM = givenWeight / givenRPEDecimal; var estimated1RM = givenWeight / givenRPEDecimal;


const roundingValue = Number.parseFloat(document.querySelector("select#rounding").value); var roundingValue = Number.parseFloat(document.querySelector("select#rounding").value);


e1RMEl.innerHTML = roundToFloat(estimated1RM, roundingValue); e1RMEl.innerHTML = roundToFloat(estimated1RM, roundingValue);
ninetyFivePEl.innerHTML = roundToFloat(estimated1RM * 0.95, roundingValue); ninetyFivePEl.innerHTML = roundToFloat(estimated1RM * 0.95, roundingValue);
Expand All @@ -201,7 +201,7 @@
sixtyFivePEl.innerHTML = roundToFloat(estimated1RM * 0.65, roundingValue); sixtyFivePEl.innerHTML = roundToFloat(estimated1RM * 0.65, roundingValue);


if (desiredRPE && desiredReps) { if (desiredRPE && desiredReps) {
const desiredRPEDecimal = RPEs["RPE"][desiredRPE]["REPS"][desiredReps]; var desiredRPEDecimal = RPEs["RPE"][desiredRPE]["REPS"][desiredReps];
desiredWeightEl.innerHTML = roundToFloat( desiredWeightEl.innerHTML = roundToFloat(
parseInt(estimated1RM * desiredRPEDecimal), roundingValue parseInt(estimated1RM * desiredRPEDecimal), roundingValue
); );
Expand All @@ -217,11 +217,10 @@
} }
} }


const inputs = document.querySelectorAll("input"); var inputsArr = [].slice.call(document.querySelectorAll("input"));
const inputsArr = [...inputs];
inputsArr.forEach(function(input) { inputsArr.forEach(function(input) {
input.addEventListener("input", e => inputsEventHandler(), false); input.addEventListener("input", inputsEventHandler, false);
}); });
const roundingSelect = document.querySelector("select#rounding"); var roundingSelect = document.querySelector("select#rounding");
roundingSelect.addEventListener("change", e => inputsEventHandler(), false); roundingSelect.addEventListener("change", inputsEventHandler, false);
})(); })();

0 comments on commit 86b6387

Please sign in to comment.