Skip to content

Commit

Permalink
Make weights unit depend on number [#151243411]
Browse files Browse the repository at this point in the history
  • Loading branch information
cannawen committed Sep 21, 2017
1 parent 51406ab commit 5ec8169
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 6 deletions.
19 changes: 16 additions & 3 deletions src/conversion_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ function distanceMap(m) {
}
}

function weightMap(g) {
const kg = g/1000;
if (g < 1000) {
return createMap(g, " g");

} else if (kg < 1000) {
return createMap(kg, " kg");

} else {
return createMap(kg/1000, " metric tons")
}
}

const metricDistances = ["mm", "cm", "km", "light-?years?",
/(?:milli|centi|deca|kilo)?met(?:re|er)s?/];

Expand Down Expand Up @@ -116,7 +129,7 @@ const unitLookupList = [
"standardInputUnit" : " inches",
"isInvalidInput" : isZeroOrNegative,
"isWeaklyValidInput" : isHyperbole,
"conversionFunction" : (i) => distanceMap(i * 2.54 / 100),
"conversionFunction" : (i) => distanceMap(i * 0.0254),
"ignoredKeywords" : ["monitor", "monitors", "screen", "tv", "tvs",
"ipad", "iphone", "phone", "tablet", "tablets",
"apple", "windows", "linux", "android", "ios",
Expand All @@ -129,7 +142,7 @@ const unitLookupList = [
"standardInputUnit" : " lb",
"isInvalidInput" : isZeroOrNegative,
"isWeaklyValidInput" : isHyperbole,
"conversionFunction" : (i) => createMap(i * 0.453592, " kg"),
"conversionFunction" : (i) => weightMap(i * 453.592),
"ignoredKeywords" : ["kgs?", "grams?", "kilograms?",

"football", "soccer", "fifa"]
Expand All @@ -139,7 +152,7 @@ const unitLookupList = [
"standardInputUnit" : " miles",
"isInvalidInput" : isZeroOrNegative,
"isWeaklyValidInput" : (i) => isHyperbole(i) || i === 8,
"conversionFunction" : (i) => distanceMap(i * 1.609344 * 1000),
"conversionFunction" : (i) => distanceMap(i * 1609.344),
"ignoredKeywords" : ["churn", "credit card", "visa", "mastercard", "awardtravel",
"air miles", "aeroplan", "points",
"britain", "british", "england", "scotland", "wales", "uk",
Expand Down
20 changes: 17 additions & 3 deletions test/conversion_helper-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -538,8 +538,22 @@ describe('conversion_helper', () => {

describe('#calculateMetric()', () => {
context('lbs', () => {
it('should convert', () => {
verifyConversion(1, " lb", 0.453592, " kg");
context('under 1 kg', () => {
it('should convert to g', () => {
verifyConversion(1, " lb", 453.592, " g");
});
});

context('under 1,000 kg', () => {
it('should convert to kg', () => {
verifyConversion(3, " lb", 1.3607759999999998, " kg");
});
});

context('over 1,000 kg', () => {
it('should convert to g', () => {
verifyConversion(2222, " lb", 1.007881424, " metric tons");
});
});
});

Expand All @@ -557,7 +571,7 @@ describe('conversion_helper', () => {

context('less than 1 cm', () => {
it('should convert in mm', () => {
verifyConversion(0.38, " inches", 9.652000000000001, " mm");
verifyConversion(0.38, " inches", 9.652, " mm");
});
});
});
Expand Down

0 comments on commit 5ec8169

Please sign in to comment.