Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🎪🙅 merges glitch branch into master #1

Merged
merged 1 commit into from
Jan 10, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[core]
excludesfile = /etc/.gitignore-global
5 changes: 5 additions & 0 deletions .hyperdev-assets
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{"name":"drag-in-files.svg","date":"2016-10-22T16:17:49.954Z","url":"https://cdn.hyperdev.com/drag-in-files.svg","type":"image/svg","size":7646,"imageWidth":276,"imageHeight":276,"thumbnail":"https://cdn.hyperdev.com/drag-in-files.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(102, 153, 205)","uuid":"adSBq97hhhpFNUna"}
{"name":"click-me.svg","date":"2016-10-23T16:17:49.954Z","url":"https://cdn.hyperdev.com/click-me.svg","type":"image/svg","size":7116,"imageWidth":276,"imageHeight":276,"thumbnail":"https://cdn.hyperdev.com/click-me.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(243, 185, 186)","uuid":"adSBq97hhhpFNUnb"}
{"name":"paste-me.svg","date":"2016-10-24T16:17:49.954Z","url":"https://cdn.hyperdev.com/paste-me.svg","type":"image/svg","size":7242,"imageWidth":276,"imageHeight":276,"thumbnail":"https://cdn.hyperdev.com/paste-me.svg","thumbnailWidth":276,"thumbnailHeight":276,"dominantColor":"rgb(42, 179, 185)","uuid":"adSBq97hhhpFNUnc"}
{"name":"Screen Shot 2016-12-16 at 1.35.56 AM.png","date":"2016-12-16T06:36:32.934Z","url":"https://cdn.gomix.com/d7932c52-287f-4dae-b175-631fef453000%2FScreen%20Shot%202016-12-16%20at%201.35.56%20AM.png","type":"image/png","size":18223,"imageWidth":499,"imageHeight":80,"thumbnail":"https://cdn.gomix.com/d7932c52-287f-4dae-b175-631fef453000%2Fthumbnails%2FScreen%20Shot%202016-12-16%20at%201.35.56%20AM.png","thumbnailWidth":330,"thumbnailHeight":53,"dominantColor":"rgb(236,236,236)","uuid":"IHJDL7lzupnoDep4"}
{"uuid":"IHJDL7lzupnoDep4","deleted":true}
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
A metric <-> imperial converter for a freeCodeCamp assignment: https://learn.freecodecamp.org/information-security-and-quality-assurance/information-security-and-quality-assurance-projects/metric-imperial-converter
**FreeCodeCamp**- Information Security and Quality Assurance
------

1) SET NODE_ENV to `test` without quotes
2) Most logic will need done in `controllers/convertHandler.js` but do complete `routes/api.js`
3) You will add any security features to `server.js`
4) You will create all of the functional/unit tests in `tests/2_functional-tests.js` and `tests/1_unit-tests.js`


131 changes: 131 additions & 0 deletions assertion-analyser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/*
*
*
*
*
*
*
*
*
*
*
*
* DO NOT EDIT THIS FILE
* For FCC testing purposes!
*
*
*
*
*
*
*
*
*
*
*
*/

function objParser(str, init) {
// finds objects, arrays, strings, and function arguments
// between parens, because they may contain ','
var openSym = ['[', '{', '"', "'", '('];
var closeSym = [']', '}', '"', "'", ')'];
var type;
for(var i = (init || 0); i < str.length; i++ ) {
type = openSym.indexOf(str[i]);
if( type !== -1) break;
}
if (type === -1) return null;
var open = openSym[type];
var close = closeSym[type];
var count = 1;
for(var k = i+1; k < str.length; k++) {
if(open === '"' || open === "'") {
if(str[k] === close) count--;
if(str[k] === '\\') k++;
} else {
if(str[k] === open) count++;
if(str[k] === close) count--;
}
if(count === 0) break;
}
if(count !== 0) return null;
var obj = str.slice(i, k+1);
return {
start : i,
end: k,
obj: obj
};
}

function replacer(str) {
// replace objects with a symbol ( __#n)
var obj;
var cnt = 0;
var data = [];
while(obj = objParser(str)) {
data[cnt] = obj.obj;
str = str.substring(0, obj.start) + '__#' + cnt++ + str.substring(obj.end+1)
}
return {
str : str,
dictionary : data
}
}

function splitter(str) {
// split on commas, then restore the objects
var strObj = replacer(str);
var args = strObj.str.split(',');
args = args.map(function(a){
var m = a.match(/__#(\d+)/);
while (m) {
a = a.replace(/__#(\d+)/, strObj.dictionary[m[1]]);
m = a.match(/__#(\d+)/);
}
return a.trim();
})
return args;
}

function assertionAnalyser(body) {

// already filtered in the test runner
// // remove comments
// body = body.replace(/\/\/.*\n|\/\*.*\*\//g, '');
// // get test function body
// body = body.match(/\{\s*([\s\S]*)\}\s*$/)[1];

if(!body) return "invalid assertion";
// replace assertions bodies, so that they cannot
// contain the word 'assertion'

var body = body.match(/(?:browser\s*\.\s*)?assert\s*\.\s*\w*\([\s\S]*\)/)[0];
var s = replacer(body);
// split on 'assertion'
var splittedAssertions = s.str.split('assert');
var assertions = splittedAssertions.slice(1);
// match the METHODS

var assertionBodies = [];
var methods = assertions.map(function(a, i){
var m = a.match(/^\s*\.\s*(\w+)__#(\d+)/);
assertionBodies.push(parseInt(m[2]));
var pre = splittedAssertions[i].match(/browser\s*\.\s*/) ? 'browser.' : '';
return pre + m[1];
});
if(methods.some(function(m){ return !m })) return "invalid assertion";
// remove parens from the assertions bodies
var bodies = assertionBodies.map(function(b){
return s.dictionary[b].slice(1,-1).trim();
});
assertions = methods.map(function(m, i) {
return {
method: m,
args: splitter(bodies[i]) //replace objects, split on ',' ,then restore objects
}
})
return assertions;
}

module.exports = assertionAnalyser;
162 changes: 162 additions & 0 deletions controllers/convertHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
/*
*
*
* Complete the handler logic below
*
*
*/

function ConvertHandler() {

this.getNum = function(input) {
// const numberRegex = /([0-9\/\.]*)(lbs?|kgs?|gals?|Ls?|mis?|kms?)*$/
// const numberRegex = /([0-9\/\.]*)(.*)$/
// const numberRegex = /([0-9\/\.]*)([A-Za-z]*)$/
const numberRegex = /^([0-9\.]+\/?[0-9\.]+|[0-9\.]*)([A-Za-z]*)$/

const numberMatch = input.match(numberRegex)

// console.log('this is numberMatch: ', numberMatch)

if (input.match(/^(lbs?|kgs?|gals?|Ls?|mis?|kms?)*$/)) {
return 1
}

if (!numberMatch) {
return undefined
}

return eval(numberMatch[1]);
};

this.getUnit = function(input) {
const unitRegex = /(lbs?|kgs?|gals?|Ls?|mis?|kms?)*$/i
const unitMatch = input.match(unitRegex)

// console.log('this is unitMatch: ', unitMatch)

if (!unitMatch || !unitMatch[0]) {
return undefined
}

return unitMatch[0];
};

this.stripPlural = function(initUnit) {
if (initUnit.substr(initUnit.length - 1, 1) === 's') {
return initUnit.substring(0, initUnit.length - 1)
}

return initUnit
}

this.getReturnUnit = function(initUnit) {
initUnit = this.stripPlural(initUnit).toLowerCase()

switch(initUnit) {
case "lb":
return "kg";
break;

case "kg":
return "lbs";
break;

case "gal":
return "l";
break;

case "l":
return "gal";
break;

case "mi":
return "km";
break;

case "km":
return "mi";
break;

default:
return undefined
}
};

this.spellOutUnit = function(unit) {
unit = this.stripPlural(unit).toLowerCase()

switch(unit) {
case "lb":
return "pound(s)";
break;

case "kg":
return "kilogram(s)";
break;

case "gal":
return "gallon(s)";
break;

case "l":
return "liter(s)";
break;

case "mi":
return "mile(s)";
break;

case "km":
return "kilometer(s)";
break;

default:
return undefined
}
};

this.convert = function(initNum, initUnit) {
const galToL = 3.78541; // 1 gal is 3.78 L
const lbsToKg = 0.453592; // 1 lb is .45 kilos
const miToKm = 1.60934; // 1 mi is 1.6 km

initUnit = this.stripPlural(initUnit)

switch (initUnit) {
case "lb":
return parseFloat((initNum * lbsToKg).toFixed(5));
break;

case "kg":
return parseFloat((initNum / lbsToKg).toFixed(5));
break;

case "gal":
return parseFloat((initNum * galToL).toFixed(5));
break;

case "L":
return parseFloat((initNum / galToL).toFixed(5));
break;

case "mi":
return parseFloat((initNum * miToKm).toFixed(5));
break;

case "km":
return parseFloat((initNum / miToKm).toFixed(5));
break;

default:
return undefined
}
};

this.getString = function(initNum, initUnit, returnNum, returnUnit) {
return initNum + ' ' + this.spellOutUnit(initUnit) + ' converts to ' + returnNum + ' ' + this.spellOutUnit(returnUnit)
};

}

module.exports = ConvertHandler;
36 changes: 36 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"//1": "describes your app and its dependencies",
"//2": "https://docs.npmjs.com/files/package.json",
"//3": "updating this file will download and update your packages",
"name": "my-hyperdev-app",
"version": "0.0.1",
"description": "What am I about?",
"main": "server.js",
"scripts": {
"start": "node server.js"
},
"dependencies": {
"express": "^4.14.0",
"cors": "^2.8.1",
"body-parser": "^1.15.2",
"chai": "^3.5.0",
"mongodb": "^2.2.16",
"chai-http": "^3.0.0",
"mocha": "^3.2.0",
"zombie": "^5.0.5",
"helmet": "^3.1.0"
},
"engines": {
"node": "4.4.3"
},
"repository": {
"type": "git",
"url": "https://hyperdev.com/#!/project/welcome-project"
},
"keywords": [
"node",
"hyperdev",
"express"
],
"license": "MIT"
}
Empty file added public/style.css
Empty file.
42 changes: 42 additions & 0 deletions routes/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
*
*
* Complete the API routing below
*
*
*/

'use strict';

var expect = require('chai').expect;
var ConvertHandler = require('../controllers/convertHandler.js');

module.exports = function (app) {

var convertHandler = new ConvertHandler();

app.route('/api/convert')
.get(function (req, res){
var input = req.query.input;
var initNum = convertHandler.getNum(input);
var initUnit = convertHandler.getUnit(input);

// console.log('initNum: ', initNum)
// console.log('initUnit: ', initUnit)

if (!initNum && !initUnit) {
res.send('invalid number and unit')
} else if (!initNum) {
res.send('invalid number.')
} else if (!initUnit) {
res.send('invalid unit.')
} else {
var returnNum = convertHandler.convert(initNum, initUnit);
var returnUnit = convertHandler.getReturnUnit(initUnit);
var toString = convertHandler.getString(initNum, initUnit, returnNum, returnUnit);

res.json({ input, initNum, initUnit, returnNum, returnUnit, string: toString })
}
});

};
Loading