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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃彧馃彜 Updated with Glitch #1

Merged
merged 1 commit into from Dec 15, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitconfig
@@ -0,0 +1,2 @@
[core]
excludesfile = /etc/.gitignore-global
Empty file added .glitch-assets
Empty file.
5 changes: 5 additions & 0 deletions .hyperdev-assets
@@ -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}
11 changes: 9 additions & 2 deletions README.md
@@ -1,2 +1,9 @@
# fcc-metric-imperial-unitConverter
FCC: Information Security & Quality Assurance Project # 1.
**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
@@ -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;
104 changes: 104 additions & 0 deletions controllers/convertHandler.js
@@ -0,0 +1,104 @@
/*
*
*
* Complete the handler logic below
*
*
*/

function ConvertHandler() {

this.getNum = function (input) {
let numRegex = /^(\d+\.?\d*)?(\/?)(\d+)?(\w)+$/;

// return numRegex.test(input);

if (!numRegex.test(input)) {
return null;
}

let arr = input.match(numRegex);

console.log(arr);

let [, num1, backslash, num2, unit] = arr;

if (backslash && num2) {
return Number(num1) / Number(num2);
} else if (num1) {
return Number(num1);
} else {
return 1;
}
};

// getNum('3.2.2mi');
// getNum('3.1mi');
// getNum('8.4/4mi');
// getNum('2.3.4.5mi');
// getNum('8/4mi')
// getNum('3/mi'); // just returns 3 / 1, should give an error but ignores it for now
// getNum('8.4/4.2mi') // not working, returns null;

this.getUnit = function(input) {
let arr = input.split('');
let index = arr.findIndex(w => w.match(/[a-zA-Z]/));
let unit = arr.slice(index).join('');
let units = ['gal', 'lbs', 'mi', 'L', 'kg', 'km'];
if (units.includes(unit)) {
return unit;
} else {
return null;
}
};


this.getReturnUnit = function(initUnit) {
let units = {
lbs: 'kg',
kg: 'lbs',
mi: 'km',
km: 'mi',
gal: 'L',
l: 'gal'
};
return units[initUnit.toLowerCase()];
};


this.spellOutUnit = function(unit) {
let units = {
mi: 'mile',
km: 'kilometer',
kg: 'kilogram',
lbs: 'pound',
gal: 'gallon',
l: 'litre'
};
return units[unit.toLowerCase()];
};


this.convert = function(initNum, initUnit) {
const galToL = 3.78541;
const lbsToKg = 0.453592;
const miToKm = 1.60934;
let convertTable = {
km: 1 / miToKm,
mi: miToKm,
gal: galToL,
l: 1 / galToL,
lbs: lbsToKg,
kg: 1 / lbsToKg
}
return Number((convertTable[initUnit.toLowerCase()] * initNum).toFixed(5));
};


this.getString = function(initNum, initUnit, returnNum, returnUnit) {
return `${initNum} ${this.spellOutUnit(initUnit)}${initNum == 1 ? ' ' : 's '}convert${initNum == 1? 's': ''} to ${returnNum} ${this.spellOutUnit(returnUnit)}${returnNum == 1 ? '': 's'}.`;
};

}

module.exports = ConvertHandler;
36 changes: 36 additions & 0 deletions package.json
@@ -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.
36 changes: 36 additions & 0 deletions routes/api.js
@@ -0,0 +1,36 @@
/*
*
*
* 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);
var returnNum = convertHandler.convert(initNum, initUnit);
var returnUnit = convertHandler.getReturnUnit(initUnit);
var toString = convertHandler.getString(initNum, initUnit, returnNum, returnUnit);

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

};