Skip to content

Commit

Permalink
Refactor area into a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
cannawen committed Oct 24, 2017
1 parent 001713d commit fc61dbf
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 26 deletions.
39 changes: 39 additions & 0 deletions src/conversion/area/_area.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
const shared = require('../shared');

function areaMap(imperialInputs, metricTransform) {
const metresSquared = imperialInputs.map(metricTransform);

const unitDecider = Math.max(...metresSquared);

let unitTransform = undefined;
let unit = undefined;

if (unitDecider >= 1000000) {
unitTransform = i => i / 1000000;
unit = " km^2";

} else if (unitDecider >= 10000) {
unitTransform = i => i / 10000;
unit = " hectares";

} else {
unitTransform = i => i;
unit = " m^2";
}

return shared.createMap(metresSquared, unitTransform, unit);
}

const metricAreaUnits = [
/square kilometers?/,
/sq\.? km/,
/sq\.? kilometers?/,
/km[^]2/,
/hectares/,
/m^2/
];

module.exports = {
"toMap" : areaMap,
"metricUnits" : metricAreaUnits
}
11 changes: 11 additions & 0 deletions src/conversion/area/acres.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const shared = require('../shared');
const area = require('./_area');

module.exports = {
"imperialUnits" : [/acres?/],
"standardInputUnit" : " acres",
"isInvalidInput" : shared.isZeroOrNegative,
"isWeaklyInvalidInput" : shared.isHyperbole,
"conversionFunction" : (i) => area.toMap(i, (j) => j * 4046.8564),
"ignoredUnits" : area.metricUnits
}
26 changes: 0 additions & 26 deletions src/conversion_helper.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,6 @@
const rh = require('./regex_helper');
const fsh = require('./file_system_helper');

function areaMap(m2) {
const unitDecider = Math.max(...m2);
if (unitDecider >= 1000000) {
return createMap(m2.map((i) => i / 1000000), " km^2");

} else if (unitDecider >= 10000) {
return createMap(m2.map((i) => i / 10000), " hectares");

} else {
return createMap(m2, " m^2");
}
}

function pressureMap(pa) {
const unitDecider = Math.max(...pa);
if (unitDecider < 1000) {
Expand Down Expand Up @@ -185,19 +172,6 @@ let unitLookupList = [
}
},
"ignoredUnits" : [/° ?C/, "degrees? c", "celsius", "kelvin"]
},
{
"imperialUnits" : [/acres?/],
"standardInputUnit" : " acres",
"isInvalidInput" : isZeroOrNegative,
"isWeaklyInvalidInput" : isHyperbole,
"conversionFunction" : (i) => areaMap(i.map((j) => j * 4046.8564)),
"ignoredUnits" : [
/square kilometers?/,
/sq.? km/,
/sq.? kilometers?/,
/km[^]2/
]
}
];

Expand Down

0 comments on commit fc61dbf

Please sign in to comment.